/help please

//can someone help me solve this question, i don't understand the code and what is is needed from me? I have a test coming up and id like to understand, Thank you

Q) Identify where the vulnerabilities are in the given code below and try to

correct as much as you can. Write a complete report explaining what you did

and why.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <time.h>

char greeting[] = "Hello there\n1. Receive wisdom\n2. Add wisdom\nSelection >";

char prompt[] = "Enter some wisdom\n";

char pat[] = "Achievement unlocked!\n";

char secret[] = "secret key";

int infd = 0; /* stdin */

int outfd = 1; /* stdout */

#define DATA_SIZE 128

typedef struct _WisdomList {

struct _WisdomList *next;

char data[DATA_SIZE];

} WisdomList;

struct _WisdomList *head = NULL;

typedef void (*fptr)(void);

void write_secret(void) {

write(outfd, secret, sizeof(secret));

return;

}

void pat_on_back(void) {

write(outfd, pat, sizeof(pat));

return;

}

void get_wisdom(void) {

char buf[] = "no wisdom\n";

if(head == NULL) {

write(outfd, buf, sizeof(buf)-sizeof(char));

} else {

WisdomList *l = head;

while(l != NULL) {

write(outfd, l->data, strlen(l->data));

write(outfd, "\n", 1);

l = l->next;

}

}

return;

}

void put_wisdom(void) {

char wis[DATA_SIZE] = {0};

int r;

r = write(outfd, prompt, sizeof(prompt)-sizeof(char));

if(r < 0) {

return;

}

r = (int)gets(wis);

if (r == 0)

return;

WisdomList *l = malloc(sizeof(WisdomList));

if(l != NULL) {

memset(l, 0, sizeof(WisdomList));

strcpy(l->data, wis);

if(head == NULL) {

head = l;

} else {

WisdomList *v = head;

while(v->next != NULL) {

v = v->next;

}

v->next = l;

}

}

return;

}

fptr ptrs[3] = { NULL, get_wisdom, put_wisdom };

int main(int argc, char *argv[]) {

while(1) {

char buf[1024] = {0};

int r;

fptr p = pat_on_back;

r = write(outfd, greeting, sizeof(greeting)-sizeof(char));

if(r < 0) {

break;

}

r = read(infd, buf, sizeof(buf)-sizeof(char));

if(r > 0) {

buf[r] = '\0';

int s = atoi(buf);

fptr tmp = ptrs[s];

tmp();

} else {

break;

}

}

return 0;

}
Q) Identify where the vulnerabilities are in the given code below and try to correct as much as you can. Write a complete report explaining what you did and why.

i don't understand the code and what is is needed from me?


Identify vulnerabilities in code
In other words, you have to understand code and know what operations are not safe.

correct as much as you can
Know the unsafe and more safe alternatives for it.

Write a report
Explain what was not safe, why it wasn't safe, and how your changes are supposed to be safer.


As an example, the program shows:
Hello there
1. Receive wisdom
2. Add wisdom
Selection >

That is so hard selection that I will write 42.

Is that a problem?
Topic archived. No new replies allowed.