0

I am trying to create a FUSE filesystem that encrypts files as they go in and then decrypts them as they come out. I started with a template that just called the appropriate system calls for most file operations then changed the read() and write() methods to call my encryption and decryption methods. When I write a file (using echo "readme" > /debug), I get the following output:

Encrypted Buffer: ??????}x?
Being written to: /debug
Locally Decrypted to: readme

The "locally decrypted" value is just to prove that the decryption method works appropriately. Next, I use cat /debug to try to read the file and it returns nonsense. Here are the debug statements I get:

Just read: ??????}
From: /debug
Decrypted to: ;?N?"Cs?
So you should see: ;?N?"Cs?

It looks to me like there is something going on when I either write my encrypted buffer to the file or when I try to read the encrypted buffer back out of the file. Could it have something to do with EOFs or the size parameter that each method takes in? Here are the read and write methods from my FUSE file:

    static int pv_read(const char *path, char *buf, size_t size, off_t offset,  
                struct fuse_file_info *fi)  
    {  
      int fd;  
      int res;
      (void) fi;
      fd = open(dir_path(path), O_RDONLY);
      if (fd == -1)
            return -errno;
      char *decrypt_buf = (char *) malloc(size);
      char *raw_buf = (char *) malloc(size);
      res = pread(fd, raw_buf, size, offset);

      FILE *f = fopen("debug_read.txt", "w");

      fprintf(f, "Just read: %s\nFrom: %s\n\n", raw_buf, path);
      file_decrypt((byte *)raw_buf, size, (byte *)decrypt_buf);

      fprintf(f, "Decrypted to: %s\n",decrypt_buf);

      memcpy(buf, decrypt_buf, size);

      fprintf(f, "So you should see: %s\n", buf);
      fclose(f);

      if (res == -1)
            res = -errno;
      close(fd);
      return res;
    }
    static int pv_write(const char *path, const char *buf, size_t size,
                 off_t offset, struct fuse_file_info *fi)
    {
      int fd;
      int res;
      (void) fi;
      fd = open(dir_path(path), O_WRONLY);
      if (fd == -1)
            return -errno;
      unsigned char *encrypt_buf = (unsigned char *) malloc(size);
      unsigned char *d_buf = (unsigned char *) malloc(size);
      file_encrypt((byte *)buf, size, (byte *)encrypt_buf);

      file_decrypt((byte *) encrypt_buf, size, (byte *) d_buf);
      FILE *f = fopen("debug_write.txt", "w");

      fprintf(f, "Encrypted Buffer: %s\nBeing written to: %s\nLocally Decrypted to: %s\n",   encrypt_buf, path,d_buf);
      fclose(f);


      res = pwrite(fd, encrypt_buf, size, offset);

      if (res == -1)
          res = -errno;
      close(fd);
      return res;
    }

Thanks a lot!

GTwreck
  • 31
  • 2
  • 2
    1. You need to pay attention to return values. `pread` actually returns a value you need to use. `pwrite` returns something you might be interested in knowing. 2. You can't just print an arbitrary binary-data buffer as a string. Instead, do something like `printf("data: "); for(i=0;i – indiv Apr 08 '14 at 21:37
  • 1
    @indiv - Your comment has useful information and looks like an answer. You should post it as one. :) – ryyker Apr 08 '14 at 21:54
  • @indiv - Thanks! I guess I have some thinking to do. – GTwreck Apr 08 '14 at 22:31

0 Answers0