Skip to contentSkip to conversation

f2fs: fixes for FS_IOC_{GET,SET}FSLABEL

PATCH In review Eric Biggers 4 participants 16 messages Aug 16, 2019 – Aug 19, 2019 F2FS file system

lore
Share & tools
lore
#0 lore raw

cover f2fs: fixes for FS_IOC_{GET,SET}FSLABEL

Fix some bugs in the f2fs implementation of the FS_IOC_GETFSLABEL and FS_IOC_SETFSLABEL ioctls.

Eric Biggers (3):
f2fs: fix buffer overruns in FS_IOC_{GET,SET}FSLABEL
f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL
f2fs: add missing authorization check in FS_IOC_SETFSLABEL

 fs/f2fs/file.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)
Signature · 8 lines
2.22.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH 1/3 f2fs: fix buffer overruns in FS_IOC_{GET, SET}FSLABEL

From: Eric Biggers <[email protected]>

utf16s_to_utf8s() and utf8s_to_utf16s() take the number of characters, not the number of bytes.

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")
---
 fs/f2fs/file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/file.c +2 -2
index eb1aa9b75eda..d521a582d94d 100644
@@ -3094,7 +3094,7 @@ static int f2fs_get_volume_name(struct file *filp, unsigned long arg)
 
 	down_read(&sbi->sb_lock);
 	count = utf16s_to_utf8s(sbi->raw_super->volume_name,
-			sizeof(sbi->raw_super->volume_name),
+			ARRAY_SIZE(sbi->raw_super->volume_name),
 			UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
 	up_read(&sbi->sb_lock);
 
@@ -3139,7 +3139,7 @@ static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
 			sizeof(sbi->raw_super->volume_name));
 	utf8s_to_utf16s(vbuf, MAX_VOLUME_NAME, UTF16_LITTLE_ENDIAN,
 			sbi->raw_super->volume_name,
-			sizeof(sbi->raw_super->volume_name));
+			ARRAY_SIZE(sbi->raw_super->volume_name));
 
 	err = f2fs_commit_super(sbi, false);
 
Signature · 8 lines
2.22.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by on v1 1/3
#2 lore raw

Re: [f2fs-dev] [PATCH 1/3] f2fs: fix buffer overruns in FS_IOC_{GET, SET}FSLABEL

Quoted 7 lines · Eric Biggers

From: Eric Biggers <[email protected]>

utf16s_to_utf8s() and utf8s_to_utf16s() take the number of characters, not the number of bytes.

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw

PATCH 2/3 f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")
---
 fs/f2fs/file.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)
fs/f2fs/file.c +5 -17
index d521a582d94d..315127251bc1 100644
@@ -3111,23 +3111,11 @@ static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
 	struct inode *inode = file_inode(filp);
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	char *vbuf;
-	int len;
 	int err = 0;
 
-	vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
-	if (!vbuf)
-		return -ENOMEM;
-
-	if (copy_from_user(vbuf, (char __user *)arg, FSLABEL_MAX)) {
-		err = -EFAULT;
-		goto out;
-	}
-
-	len = strnlen(vbuf, FSLABEL_MAX);
-	if (len > FSLABEL_MAX - 1) {
-		err = -EINVAL;
-		goto out;
-	}
+	vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
+	if (IS_ERR(vbuf))
+		return PTR_ERR(vbuf);
 
 	err = mnt_want_write_file(filp);
 	if (err)
@@ -3137,7 +3125,7 @@ static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
 
 	memset(sbi->raw_super->volume_name, 0,
 			sizeof(sbi->raw_super->volume_name));
-	utf8s_to_utf16s(vbuf, MAX_VOLUME_NAME, UTF16_LITTLE_ENDIAN,
+	utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
 			sbi->raw_super->volume_name,
 			ARRAY_SIZE(sbi->raw_super->volume_name));
 
@@ -3147,7 +3135,7 @@ static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
 
 	mnt_drop_write_file(filp);
 out:
-	kvfree(vbuf);
+	kfree(vbuf);
 	return err;
 }
 
Signature · 8 lines
2.22.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by on v1 2/3
#4 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

Quoted 5 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.>

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

It may only copy redundant zero bytes, and will not hit security issue, it doesn't look like a bug fix?

Quoted 1 line

Anyway, it makes sense to me.

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#5 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

Quoted 7 lines · Chao Yu
Quoted 5 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.>

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")
Quoted 9 lines

It may only copy redundant zero bytes, and will not hit security issue, it doesn't look like a bug fix?

Quoted 1 line

Anyway, it makes sense to me.

It's not clear that userspace is guaranteed to provide a full FSLABEL_MAX bytes in the buffer. E.g. it could provide "foo\0" followed by an unmapped page.

- Eric

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#6 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

Quoted 13 lines · Eric Biggers
Quoted 12 lines · Chao Yu
Quoted 5 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.>

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

It may only copy redundant zero bytes, and will not hit security issue, it doesn't look like a bug fix?

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Anyway, it makes sense to me.

It's not clear that userspace is guaranteed to provide a full FSLABEL_MAX bytes in the buffer. E.g. it could provide "foo\0" followed by an unmapped page.

You're right, thanks for your explanation.

Thanks,

Quoted 4 lines

- Eric
.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#7 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

Quoted 16 lines · Chao Yu
Quoted 15 lines · Eric Biggers
Quoted 14 lines · Chao Yu
Quoted 5 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.>

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

It may only copy redundant zero bytes, and will not hit security issue, it doesn't look like a bug fix?

Quoted 1 line

Anyway, it makes sense to me.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

It's not clear that userspace is guaranteed to provide a full FSLABEL_MAX bytes in the buffer. E.g. it could provide "foo\0" followed by an unmapped page.

You're right, thanks for your explanation.

One more question, there is no validation check on length of user passed buffer,

So in most ioctl interfaces, user can pass a buffer which has less size than we defined intentionally/unintentionally.

E.g.

user space:

struct f2fs_defragment_user {
	unsigned long long start;

// unsigned long long len;
};

main()

{
	struct f2fs_defragment_user *df;

	df = malloc();
	
	ioctl(fd, F2FS_IOC_DEFRAGMENT, df);
}

kernel:

f2fs_ioc_defragment()
{
...

	if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
							sizeof(range)))
		return -EFAULT;
}

Is that a common issue?

Thanks,

Quoted 9 lines

Thanks,

Quoted 4 lines

- Eric
.

Quoted 6 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#8 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

Quoted 53 lines · Chao Yu
Quoted 16 lines · Chao Yu
Quoted 15 lines · Eric Biggers
Quoted 14 lines · Chao Yu
Quoted 5 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.>

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

It may only copy redundant zero bytes, and will not hit security issue, it doesn't look like a bug fix?

Quoted 1 line

Anyway, it makes sense to me.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

It's not clear that userspace is guaranteed to provide a full FSLABEL_MAX bytes in the buffer. E.g. it could provide "foo\0" followed by an unmapped page.

You're right, thanks for your explanation.

One more question, there is no validation check on length of user passed buffer,

So in most ioctl interfaces, user can pass a buffer which has less size than we defined intentionally/unintentionally.

E.g.

user space:

struct f2fs_defragment_user {
	unsigned long long start;

// unsigned long long len;
};

main()

{
	struct f2fs_defragment_user *df;

	df = malloc();
	
	ioctl(fd, F2FS_IOC_DEFRAGMENT, df);
}

kernel:

f2fs_ioc_defragment()
{
...

Quoted 7 lines
	if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
							sizeof(range)))
		return -EFAULT;
}

Is that a common issue?

No, but that's different because that only involves a fixed-length struct.

My concern was that since FS_IOC_SETFSLABEL takes in a string, users might do:

	ioctl(fd, FS_IOC_SETFSLABEL, "foo");

Rather than:

	char label[FSLABEL_MAX] = "foo";

	ioctl(fd, FS_IOC_SETFSLABEL, label);

At least that's how I understand the ioctl; AFAICS it does not have a man page, so I'm not sure what was intended. Assuming the buffer is always FSLABEL_MAX bytes seems like a really bad idea though, since if users pass a conventional string (as is the natural thing to do; open() doesn't require a buffer of length PATH_MAX, for example...) it will succeed/fail at random depending on whether the following page is mapped or not.

- Eric

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#9 lore raw

Re: [f2fs-dev] [PATCH 2/3] f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL

Quoted 59 lines · Eric Biggers
Quoted 58 lines · Chao Yu
Quoted 16 lines · Chao Yu
Quoted 15 lines · Eric Biggers
Quoted 14 lines · Chao Yu
Quoted 5 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Userspace provides a null-terminated string, so don't assume that the full FSLABEL_MAX bytes can always be copied.>

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

It may only copy redundant zero bytes, and will not hit security issue, it doesn't look like a bug fix?

Quoted 1 line

Anyway, it makes sense to me.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

It's not clear that userspace is guaranteed to provide a full FSLABEL_MAX bytes in the buffer. E.g. it could provide "foo\0" followed by an unmapped page.

You're right, thanks for your explanation.

One more question, there is no validation check on length of user passed buffer,

So in most ioctl interfaces, user can pass a buffer which has less size than we defined intentionally/unintentionally.

E.g.

user space:

struct f2fs_defragment_user {
	unsigned long long start;

// unsigned long long len;
};

main()

{
	struct f2fs_defragment_user *df;

	df = malloc();
	
	ioctl(fd, F2FS_IOC_DEFRAGMENT, df);
}

kernel:

f2fs_ioc_defragment()
{
...

	if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
							sizeof(range)))
		return -EFAULT;
}
Quoted 8 lines
Quoted 2 lines

Is that a common issue?

No, but that's different because that only involves a fixed-length struct.

My concern was that since FS_IOC_SETFSLABEL takes in a string, users might do:

	ioctl(fd, FS_IOC_SETFSLABEL, "foo");

Yes, I can understand your concern, since in this case, user's behavior is normal.

But what I'm trying to say is, from the result aspect, when user pass a buffer which has less size intentionally/unintentionally (even kernel defines a fix-sized struture, but there is no rules that users can not reconstruct it or change its size as their wish), kernel may access unmapped page follows to user's buffer potentially. It needs to be fixed if that's a real problem.

Thanks,

Quoted 14 lines

Rather than:

	char label[FSLABEL_MAX] = "foo";

	ioctl(fd, FS_IOC_SETFSLABEL, label);

At least that's how I understand the ioctl; AFAICS it does not have a man page, so I'm not sure what was intended. Assuming the buffer is always FSLABEL_MAX bytes seems like a really bad idea though, since if users pass a conventional string (as is the natural thing to do; open() doesn't require a buffer of length PATH_MAX, for example...) it will succeed/fail at random depending on whether the following page is mapped or not.

Quoted 3 lines

- Eric
.

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#10 lore raw

PATCH 3/3 f2fs: add missing authorization check in FS_IOC_SETFSLABEL

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

FS_IOC_SETFSLABEL modifies the filesystem superblock, so it shouldn't be allowed to regular users. Require CAP_SYS_ADMIN, like xfs and btrfs do.

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")
---
 fs/f2fs/file.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/file.c +3 -0
index 315127251bc1..6939cddc542a 100644
@@ -3113,6 +3113,9 @@ static int f2fs_set_volume_name(struct file *filp, unsigned long arg)
 	char *vbuf;
 	int err = 0;
 
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
 	vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
 	if (IS_ERR(vbuf))
 		return PTR_ERR(vbuf);
Signature · 8 lines
2.22.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by on v1 3/3
#11 lore raw

Re: [f2fs-dev] [PATCH 3/3] f2fs: add missing authorization check in FS_IOC_SETFSLABEL

Quoted 7 lines · Eric Biggers

From: Eric Biggers <[email protected]>

FS_IOC_SETFSLABEL modifies the filesystem superblock, so it shouldn't be allowed to regular users. Require CAP_SYS_ADMIN, like xfs and btrfs do.

Fixes 61a3da4d5ef8 ("f2fs: support FS_IOC_{GET,SET}FSLABEL")

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#12 lore raw
In reply to Eric Biggers

Eric, thanks for all the fixes.

Thanks,

Quoted 11 lines · Eric Biggers

Fix some bugs in the f2fs implementation of the FS_IOC_GETFSLABEL and FS_IOC_SETFSLABEL ioctls.

Eric Biggers (3):
f2fs: fix buffer overruns in FS_IOC_{GET,SET}FSLABEL
f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL
f2fs: add missing authorization check in FS_IOC_SETFSLABEL

 fs/f2fs/file.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

maintainer
#13 lore raw

Hi Eric,

Do you mind folding these fixes into original patch as it's still in dev branch?

BTW, any comment or suggestion is welcome in my original patch.... :)

Thanks,

Quoted 18 lines · Chao Yu

Eric, thanks for all the fixes.

Thanks,

Quoted 11 lines · Eric Biggers

Fix some bugs in the f2fs implementation of the FS_IOC_GETFSLABEL and FS_IOC_SETFSLABEL ioctls.

Eric Biggers (3):
f2fs: fix buffer overruns in FS_IOC_{GET,SET}FSLABEL
f2fs: fix copying too many bytes in FS_IOC_SETFSLABEL
f2fs: add missing authorization check in FS_IOC_SETFSLABEL

 fs/f2fs/file.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)
Quoted 5 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

maintainer
#15 lore raw
Quoted 11 lines · Eric Biggers
Quoted 8 lines · Chao Yu

Hi Eric,

Do you mind folding these fixes into original patch as it's still in dev branch?

BTW, any comment or suggestion is welcome in my original patch.... :)

Thanks,

I'm fine with the patches being folded in.

Okay, let me fold them.

Quoted 8 lines

- Eric

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel