Skip to contentSkip to conversation

ext4: Don't leak old mountpoint samples

PATCH In review Richard Weinberger 4 participants 5 messages Dec 1, 2020 – Dec 17, 2020 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: Don't leak old mountpoint samples

As soon the first file is opened, ext4 samples the mountpoint of the filesystem in 64 bytes of the super block.
It does so using strlcpy(), this means that the remaining bytes in the super block string buffer are untouched.
If the mount point before had a longer path than the current one, it can be reconstructed.

Consider the case where the fs was mounted to "/media/johnjdeveloper" and later to "/".
The the super block buffer then contains "/\x00edia/johnjdeveloper".

This case was seen in the wild and caused confusion how the name of a developer ands up on the super block of a filesystem used in production...

Fix this by clearing the string buffer before writing to it,

---
 fs/ext4/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/ext4/file.c +1 -0
index 3ed8c048fb12..dba521250d01 100644
@@ -809,6 +809,7 @@ static int ext4_sample_last_mounted(struct super_block *sb,
 	err = ext4_journal_get_write_access(handle, sbi->s_sbh);
 	if (err)
 		goto out_journal;
+	memset(sbi->s_es->s_last_mounted, 0x00, sizeof(sbi->s_es->s_last_mounted));
 	strlcpy(sbi->s_es->s_last_mounted, cp,
 		sizeof(sbi->s_es->s_last_mounted));
 	ext4_handle_dirty_super(handle, sb);
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw

Thanks for the patch Richard, it looks good to me.

Quoted 38 lines · Richard Weinberger

As soon the first file is opened, ext4 samples the mountpoint of the filesystem in 64 bytes of the super block.
It does so using strlcpy(), this means that the remaining bytes in the super block string buffer are untouched.
If the mount point before had a longer path than the current one, it can be reconstructed.

Consider the case where the fs was mounted to "/media/johnjdeveloper" and later to "/".
The the super block buffer then contains "/\x00edia/johnjdeveloper".

This case was seen in the wild and caused confusion how the name of a developer ands up on the super block of a filesystem used in production...

Fix this by clearing the string buffer before writing to it,

---
 fs/ext4/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/ext4/file.c +1 -1
index 3ed8c048fb12..dba521250d01 100644
@@ -809,6 +809,7 @@ static int ext4_sample_last_mounted(struct super_block *sb,
        err = ext4_journal_get_write_access(handle, sbi->s_sbh);
        if (err)
                goto out_journal;
+       memset(sbi->s_es->s_last_mounted, 0x00, sizeof(sbi->s_es->s_last_mounted));
        strlcpy(sbi->s_es->s_last_mounted, cp,
                sizeof(sbi->s_es->s_last_mounted));
        ext4_handle_dirty_super(handle, sb);
--

2.26.2

maintainer
#2 lore raw
In reply to Richard Weinberger
Quoted 11 lines · Richard Weinberger

As soon the first file is opened, ext4 samples the mountpoint of the filesystem in 64 bytes of the super block.
It does so using strlcpy(), this means that the remaining bytes in the super block string buffer are untouched.
If the mount point before had a longer path than the current one, it can be reconstructed.

Consider the case where the fs was mounted to "/media/johnjdeveloper" and later to "/".
The the super block buffer then contains "/\x00edia/johnjdeveloper".

Quoted 7 lines

This case was seen in the wild and caused confusion how the name of a developer ands up on the super block of a filesystem used in production...

Fix this by clearing the string buffer before writing to it,

Thank for reporting this issue. In fact, the better fix is to use strncpy(). See my revised patch for an explanation of why....

commit cdc9ad7d3f201a77749432878fb4caa490862de6

Author: Theodore Ts'o <[email protected]>
Date:   Thu Dec 17 13:24:15 2020 -0500

ext4: don't leak old mountpoint samples

When the first file is opened, ext4 samples the mountpoint of the filesystem in 64 bytes of the super block. It does so using strlcpy(), this means that the remaining bytes in the super block string buffer are untouched. If the mount point before had a longer path than the current one, it can be reconstructed.

Consider the case where the fs was mounted to "/media/johnjdeveloper" and later to "/". The super block buffer then contains "/\x00edia/johnjdeveloper".

This case was seen in the wild and caused confusion how the name of a developer ands up on the super block of a filesystem used in production...

Fix this by using strncpy() instead of strlcpy(). The superblock field is defined to be a fixed-size char array, and it is already marked using __nonstring in fs/ext4/ext4.h. The consumer of the field in e2fsprogs already assumes that in the case of a 64+ byte mount path, that s_last_mounted will not be NUL terminated.

fs/ext4/file.c +1 -1
index 1cd3d26e3217..349b27f0dda0 100644
@@ -810,7 +810,7 @@ static int ext4_sample_last_mounted(struct super_block *sb,
 	if (err)
 		goto out_journal;
 	lock_buffer(sbi->s_sbh);
-	strlcpy(sbi->s_es->s_last_mounted, cp,
+	strncpy(sbi->s_es->s_last_mounted, cp,
 		sizeof(sbi->s_es->s_last_mounted));
 	ext4_superblock_csum_set(sb);
 	unlock_buffer(sbi->s_sbh);
#3 lore raw
Quoted 44 lines · Theodore Y. Ts'o
Quoted 11 lines · Richard Weinberger

As soon the first file is opened, ext4 samples the mountpoint of the filesystem in 64 bytes of the super block.
It does so using strlcpy(), this means that the remaining bytes in the super block string buffer are untouched.
If the mount point before had a longer path than the current one, it can be reconstructed.

Consider the case where the fs was mounted to "/media/johnjdeveloper" and later to "/".
The the super block buffer then contains "/\x00edia/johnjdeveloper".

Quoted 7 lines

This case was seen in the wild and caused confusion how the name of a developer ands up on the super block of a filesystem used in production...

Fix this by clearing the string buffer before writing to it,

Thank for reporting this issue. In fact, the better fix is to use strncpy(). See my revised patch for an explanation of why....

commit cdc9ad7d3f201a77749432878fb4caa490862de6

Author: Theodore Ts'o <[email protected]>
Date:   Thu Dec 17 13:24:15 2020 -0500

ext4: don't leak old mountpoint samples

When the first file is opened, ext4 samples the mountpoint of the filesystem in 64 bytes of the super block. It does so using strlcpy(), this means that the remaining bytes in the super block string buffer are untouched. If the mount point before had a longer path than the current one, it can be reconstructed.

Consider the case where the fs was mounted to "/media/johnjdeveloper" and later to "/". The super block buffer then contains "/\x00edia/johnjdeveloper".

This case was seen in the wild and caused confusion how the name of a developer ands up on the super block of a filesystem used in production...

Quoted 8 lines

Fix this by using strncpy() instead of strlcpy(). The superblock field is defined to be a fixed-size char array, and it is already marked using __nonstring in fs/ext4/ext4.h. The consumer of the field in e2fsprogs already assumes that in the case of a 64+ byte mount path, that s_last_mounted will not be NUL terminated.

Color me confused, but I don't see how this change makes any difference? If "cp" is only "/" then it will *still* not overwrite "edia/johnjdeveloper" at the end of the s_last_mounted array. To my mind, the only difference between using strlcpy() and strncpy() would be whether the last byte in the array can be used or not, but doesn't affect the remaining bytes.

Quoted 6 lines
fs/ext4/file.c +0 -0
index 1cd3d26e3217..349b27f0dda0 100644

fs/ext4/file.c static int ext4_sample_last_mounted(struct super_block *sb, · line 816

Quoted 8 lines
fs/ext4/file.c +1 -1
@@ -810,7 +810,7 @@ static int ext4_sample_last_mounted(struct super_block *sb,
 	if (err)
 		goto out_journal;
 	lock_buffer(sbi->s_sbh);
-	strlcpy(sbi->s_es->s_last_mounted, cp,
+	strncpy(sbi->s_es->s_last_mounted, cp,
 		sizeof(sbi->s_es->s_last_mounted));
 	ext4_superblock_csum_set(sb);
 	unlock_buffer(sbi->s_sbh);

Cheers, Andreas

#4 lore raw

----- Ursprüngliche Mail -----

Quoted 6 lines
Quoted 6 lines

Fix this by using strncpy() instead of strlcpy(). The superblock field is defined to be a fixed-size char array, and it is already marked using __nonstring in fs/ext4/ext4.h. The consumer of the field in e2fsprogs already assumes that in the case of a 64+ byte mount path, that s_last_mounted will not be NUL terminated.

Quoted 8 lines
Quoted 2 lines

Color me confused, but I don't see how this change makes any difference? If "cp" is only "/" then it will *still* not overwrite "edia/johnjdeveloper" at the end of the s_last_mounted array. To my mind, the only difference between using strlcpy() and strncpy() would be whether the last byte in the array can be used or not, but doesn't affect the remaining bytes.

strncpy() zeros all remaining bytes.

Thanks,
//richard