Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File opened successfully but reads result in "Bad file descriptor" error and stat shows filesize zero

Tags:

c

linux

I'm working on an assignment where we're supposed to crypt-analyze a PDF that had been encrypted with a poor encryption algorithm.

The code supplied by the prof creates the encrypted file with fd=open(filename, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR).

In my code to attempt decryption I open that file with fd_in=open(file, O_RDONLY).

The problem is that when I try to read in from the file I'm getting a "Bad file descriptor" error. I used stat to attempt to get more information about what the file descriptor "thought" about the file that had been opened and it shows that the file is of length 0 when it is actually a few hundred KB.

The debug code I'm using is:

if (0 > (len = read(fd_in, (char*)&read_buff, BITE))) {    // BITE is defined as 8
  printf("Error occured grabbing first bite of %s.\n", file);
  printf("%s.\n", strerror(errno));

  struct stat fileStat;
  int stat = fstat(fd_in, &fileStat);

  printf("fstat returned: %d.\n", stat);      // Consistently printing 0
  printf("Information for %s\n",file);
  printf("---------------------------\n");
  printf("File Size: \t\t%d bytes\n",fileStat.st_size);
  printf("Number of Links: \t%d\n",fileStat.st_nlink);
  printf("File inode: \t\t%d\n",fileStat.st_ino);

  printf("File Permissions: \t");
  printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
  printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
  printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
  printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
  printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
  printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
  printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
  printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
  printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
  printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
  printf("\n\n");

  return 1;

}

The result I'm getting is:

Error occured grabbing first bite of enc.pdf.
Bad file descriptor.
Information for enc.pdf
---------------------------
File Size:      0 bytes
Number of Links:    1
File inode:         16441996
File Permissions:   -rw-------

ls reports the file as

-rw-------  1 matt  matt   157887 Oct 29 03:01 enc.pdf

The code related to opening the file:

int fd_in=open(file, O_RDONLY);
if(fd_in<0) {
   printf("Failed to open the input file %s.\n", file);
   return 1;
} else {
    printf("File open, descriptor is: %d.\n", fd_in);
}

This has been consistently printing out the value 3 for the filed descriptor.

There were some questions about read_buff. The encryption/decryption process involves XORing the values that are read. Because of this the buffer is declared as a unsigned long long and in order to read into it I take the address and cast it to (char*). This tactic is straight out of the prof's code for creating the encrypted file.

I even added an else with a printf to verify the file descriptor was coming out valid. At the moment it seems to be conistently 3 which is definitely not -1

like image 917
Matt Avatar asked Nov 03 '22 12:11

Matt


1 Answers

You might like to check whether the stack gets corrupted in between the calls to open()and read(), so that the value of the file descriptor fd_in will be changed.

like image 81
alk Avatar answered Nov 09 '22 09:11

alk