diff mbox series

[U-Boot,v3,1/2] fs: fat: cannot write to subdirectories

Message ID 20180702004124.13042-2-xypron.glpk@gmx.de
State Accepted
Delegated to: Alexander Graf
Headers show
Series efi_loader: allow writing to FAT | expand

Commit Message

Heinrich Schuchardt July 2, 2018, 12:41 a.m. UTC
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>
---
 fs/fat/fat_write.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Comments

Simon Glass July 9, 2018, 2:35 a.m. UTC | #1
Hi Heinrich,

On 1 July 2018 at 17:41, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> 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>
> ---
>  fs/fat/fat_write.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)

It would be great if we have filesystem tests. We do have the
fs-test.sh script but it is not using pytest, so best not to build on
it.

Regards,
Simon
Heinrich Schuchardt July 9, 2018, 6:07 a.m. UTC | #2
On 07/09/2018 04:35 AM, Simon Glass wrote:
> Hi Heinrich,
> 
> On 1 July 2018 at 17:41, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>> 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>
>> ---
>>  fs/fat/fat_write.c | 16 +++++++++++++++-
>>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> It would be great if we have filesystem tests. We do have the
> fs-test.sh script but it is not using pytest, so best not to build on
> it.

There is a write test for FAT in
lib/efi_selftest/efi_selftest_block_device.c.
See patch "efi_selftest: test writing to file".

I agree having tests independent of the EFI subsystem would be a good idea.

Best regards

Heinrich
diff mbox series

Patch

diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
index 5ca8fcda73..c5aee519b7 100644
--- a/fs/fat/fat_write.c
+++ b/fs/fat/fat_write.c
@@ -908,9 +908,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;
@@ -970,6 +972,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;