fs: fat: cannot write to subdirectories
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Mon, 2 Jul 2018 00:41:23 +0000 (02:41 +0200)
committerAlexander Graf <agraf@suse.de>
Wed, 25 Jul 2018 12:59:44 +0000 (14:59 +0200)
fs_fat_write() is not able to write to subdirectories.

Currently if a filepath with a leading slash is passed, the slash is
treated as part of the filename to be created in the root directory.

Strip leading (back-)slashes.

Check that the remaining filename does not contain any illegal characters
(<>:"/\|?*). This way we will throw an error when trying to write to a
subdirectory.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>
fs/fat/fat_write.c

index 3b77557b3ede57951c4e35e0d5d753fda845e903..27e0ff66966c54a2eaf3843a7d6e46be996f24ae 100644 (file)
@@ -909,9 +909,11 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size,
        volume_info volinfo;
        fsdata datablock;
        fsdata *mydata = &datablock;
-       int cursect;
+       int cursect, i;
        int ret = -1, name_len;
        char l_filename[VFAT_MAXLEN_BYTES];
+       char bad[2] = " ";
+       const char illegal[] = "<>:\"/\\|?*";
 
        *actwrite = size;
        dir_curclust = 0;
@@ -971,6 +973,18 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size,
        }
        dentptr = (dir_entry *) do_fat_read_at_block;
 
+       /* Strip leading (back-)slashes */
+       while ISDIRDELIM(*filename)
+               ++filename;
+       /* Check that the filename is valid */
+       for (i = 0; i < strlen(illegal); ++i) {
+               *bad = illegal[i];
+               if (strstr(filename, bad)) {
+                       printf("FAT: illegal filename (%s)\n", filename);
+                       return -1;
+               }
+       }
+
        name_len = strlen(filename);
        if (name_len >= VFAT_MAXLEN_BYTES)
                name_len = VFAT_MAXLEN_BYTES - 1;