Skip to contentSkip to conversation

vfs: fix statfs64() does not handle errors

PATCH Superseded Li Wang 2 participants 3 messages Nov 7, 2016 – Nov 14, 2016 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 vfs: fix statfs64() does not handle errors

statfs64() does NOT return -1 and setting errno to EOVERFLOW when some variables(like: f_bsize) overflowed in the returned struct.

reproducer:
step1. mount hugetlbfs with two different pagesize on ppc64 arch.

$ hugeadm --pool-pages-max 16M:0
$ hugeadm --create-mount
$ mount | grep -i hugetlbfs
none on /var/lib/hugetlbfs/pagesize-16MB type hugetlbfs (rw,relatime,seclabel,pagesize=16777216)
none on /var/lib/hugetlbfs/pagesize-16GB type hugetlbfs (rw,relatime,seclabel,pagesize=17179869184)

step2. compile & run this C program.

$ cat statfs64_test.c

#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <sys/statfs.h>

int main()

 {
	struct statfs64 sb;
	int err;

	err = statfs64("/var/lib/hugetlbfs/pagesize-16GB", &sb);
	if (err)
		return -1;

	printf("sizeof f_bsize = %d, f_bsize=%ld\n", sizeof(sb.f_bsize), sb.f_bsize);

	return 0;

}

$ gcc -m32 statfs64_test.c
$ ./a.out
sizeof f_bsize = 4, f_bsize=0

---

Notes:
This is my first patch to kernel fs part, I'm not sure if this one useful, but just want someone have a look.

thanks~

 fs/statfs.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
fs/statfs.c +17 -0
index 083dc0a..849dde95 100644
@@ -151,6 +151,23 @@ static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
 	if (sizeof(buf) == sizeof(*st))
 		memcpy(&buf, st, sizeof(*st));
 	else {
+		if (sizeof buf.f_bsize == 4) {
+			if ((st->f_blocks | st->f_bfree | st->f_bavail |
+			     st->f_bsize | st->f_frsize) &
+			    0xffffffff00000000ULL)
+				return -EOVERFLOW;
+			/*
+			 * f_files and f_ffree may be -1; it's okay to stuff
+			 * that into 32 bits
+			 */
+			if (st->f_files != -1 &&
+			    (st->f_files & 0xffffffff00000000ULL))
+				return -EOVERFLOW;
+			if (st->f_ffree != -1 &&
+			    (st->f_ffree & 0xffffffff00000000ULL))
+				return -EOVERFLOW;
+		}
+
 		buf.f_type = st->f_type;
 		buf.f_bsize = st->f_bsize;
 		buf.f_blocks = st->f_blocks;
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 56 lines · Li Wang

statfs64() does NOT return -1 and setting errno to EOVERFLOW when some variables(like: f_bsize) overflowed in the returned struct.

reproducer:
step1. mount hugetlbfs with two different pagesize on ppc64 arch.

$ hugeadm --pool-pages-max 16M:0
$ hugeadm --create-mount
$ mount | grep -i hugetlbfs
none on /var/lib/hugetlbfs/pagesize-16MB type hugetlbfs (rw,relatime,seclabel,pagesize=16777216)
none on /var/lib/hugetlbfs/pagesize-16GB type hugetlbfs (rw,relatime,seclabel,pagesize=17179869184)

step2. compile & run this C program.

$ cat statfs64_test.c

#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <sys/statfs.h>

int main()

{
	struct statfs64 sb;
	int err;

	err = statfs64("/var/lib/hugetlbfs/pagesize-16GB", &sb);
	if (err)
		return -1;

	printf("sizeof f_bsize = %d, f_bsize=%ld\n", sizeof(sb.f_bsize), sb.f_bsize);

	return 0;
}
$ gcc -m32 statfs64_test.c
$ ./a.out
sizeof f_bsize = 4, f_bsize=0

---

Notes:
This is my first patch to kernel fs part, I'm not sure if this one useful, but just want someone have a look.

thanks~

fs/statfs.c | 17 +++++++++++++++++

1 file changed, 17 insertions(+)
fs/statfs.c +0 -0
index 083dc0a..849dde95 100644

fs/statfs.c static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) · line 154

Quoted 4 lines
fs/statfs.c +1 -0
@@ -151,6 +151,23 @@ static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
 	if (sizeof(buf) == sizeof(*st))
 		memcpy(&buf, st, sizeof(*st));
 	else {
+		if (sizeof buf.f_bsize == 4) {

Linux CodingStyle says this should be used like sizeof(buf.f_bsize).

fs/statfs.c static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) · line 158

Quoted 4 lines
fs/statfs.c +4 -0
+			if ((st->f_blocks | st->f_bfree | st->f_bavail |
+			     st->f_bsize | st->f_frsize) &
+			    0xffffffff00000000ULL)
+				return -EOVERFLOW;

I'm not sure I agree with this check. Sure, if sizeof(buf.f_bsize) == 4 then the large st->f_bsize will overflow this field, and that is valid.

However, that doesn't mean that large values for f_blocks, f_bfree, f_bavail should return an error. I assume you are concerned that the product of the large f_bsize and one of those values would overflow a 64-bit bytes value, but that is for userspace to worry about, since the values in the individual fields themselves are OK.

We're already over 100PiB Lustre filesystems (2^57 bytes) today, and I don't want statfs() failing prematurely because userspace feels the need to multiply out the blocks and blocksize into bytes, instead of shifting the values to KB (which would allow filesystems up to 2^74-1024 bytes to be handled correctly in userspace).

fs/statfs.c static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) · line 168

Quoted 11 lines
fs/statfs.c +10 -0
+			/*
+			 * f_files and f_ffree may be -1; it's okay to stuff
+			 * that into 32 bits
+			 */
+			if (st->f_files != -1 &&
+			    (st->f_files & 0xffffffff00000000ULL))
+				return -EOVERFLOW;
 
+			if (st->f_ffree != -1 &&
+			    (st->f_ffree & 0xffffffff00000000ULL))
+				return -EOVERFLOW;

Why does sizeof(f_bsize) have anything to do with the number of free files? These two checks are just plain wrong, since f_files and f_ffree are 64-bit fields in struct statfs64.

Cheers, Andreas

Quoted 5 lines
fs/statfs.c +2 -0
+		}
+
 		buf.f_type = st->f_type;
 		buf.f_bsize = st->f_bsize;
		buf.f_blocks = st->f_blocks;
Quoted 7 lines

--
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

author
#2 lore raw
Quoted 52 lines · Andreas Dilger
Quoted 51 lines · Li Wang

statfs64() does NOT return -1 and setting errno to EOVERFLOW when some variables(like: f_bsize) overflowed in the returned struct.

reproducer:
step1. mount hugetlbfs with two different pagesize on ppc64 arch.

$ hugeadm --pool-pages-max 16M:0
$ hugeadm --create-mount
$ mount | grep -i hugetlbfs
none on /var/lib/hugetlbfs/pagesize-16MB type hugetlbfs (rw,relatime,seclabel,pagesize=16777216)
none on /var/lib/hugetlbfs/pagesize-16GB type hugetlbfs (rw,relatime,seclabel,pagesize=17179869184)

step2. compile & run this C program.

$ cat statfs64_test.c

#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <sys/statfs.h>

int main()

{
	struct statfs64 sb;
	int err;

	err = statfs64("/var/lib/hugetlbfs/pagesize-16GB", &sb);
	if (err)
		return -1;

	printf("sizeof f_bsize = %d, f_bsize=%ld\n", sizeof(sb.f_bsize), sb.f_bsize);

	return 0;
}
$ gcc -m32 statfs64_test.c
$ ./a.out
sizeof f_bsize = 4, f_bsize=0

---

Notes:
This is my first patch to kernel fs part, I'm not sure if this one useful, but just want someone have a look.

thanks~

fs/statfs.c | 17 +++++++++++++++++

1 file changed, 17 insertions(+)
Quoted 11 lines

fs/statfs.c static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) · line 154

Quoted 9 lines
fs/statfs.c +1 -0
index 083dc0a..849dde95 100644
@@ -151,6 +151,23 @@ static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
 	if (sizeof(buf) == sizeof(*st))
 		memcpy(&buf, st, sizeof(*st));
 	else {
+		if (sizeof buf.f_bsize == 4) {

Linux CodingStyle says this should be used like sizeof(buf.f_bsize).

agree.

Quoted 8 lines

fs/statfs.c static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) · line 158

Quoted 5 lines
fs/statfs.c +4 -0
+			if ((st->f_blocks | st->f_bfree | st->f_bavail |
+			     st->f_bsize | st->f_frsize) &
+			    0xffffffff00000000ULL)
+				return -EOVERFLOW;

I'm not sure I agree with this check. Sure, if sizeof(buf.f_bsize) == 4 then the large st->f_bsize will overflow this field, and that is valid.

After thinking over, I feel that my fix in this patch is not right.

The reproducer.c running on ppc64 arch was build in 32bit, but it does not call SYS_statfs64 in kernel. It calls compat_sys_statfs64 indeed.

# cat reproducer.c

#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <sys/statfs.h>
#include <sys/syscall.h>

int main()

{
	struct statfs64 sb;
	int err;

	err = syscall(SYS_statfs64, "/var/lib/hugetlbfs/pagesize-16GB", sizeof(sb), &sb);
	if (err)
		return -1;

	printf("sizeof f_bsize = %d, f_bsize=%ld\n", sizeof(sb.f_bsize), sb.f_bsize);
	return 0;
}
# gcc reproducer.c -m32
# stap -e 'probe kernel.function("compat_sys_statfs64") {printf ("%s",
$$parms);}' -vvv &
# ./a.out 
sizeof f_bsize = 4, f_bsize=0
# pathname=0x100006c4 sz=0x58 buf=0xff8a20b0

Guess the fix should be like:

fs/compat.c +1 -1
index bd064a2..3d923fd 100644
@@ -253,7 +253,7 @@ static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *
 
 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf,
 struct kstatfs *kbuf)
 {
-       if (sizeof ubuf->f_blocks == 4) {
+       if (sizeof ubuf->f_bsize == 4) {
                if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
                     kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
                        return -EOVERFLOW;

I will test it and send a new patch.

Regards,
Li Wang

Quoted 21 lines

However, that doesn't mean that large values for f_blocks, f_bfree, f_bavail should return an error. I assume you are concerned that the product of the large f_bsize and one of those values would overflow a 64-bit bytes value, but that is for userspace to worry about, since the values in the individual fields themselves are OK.

We're already over 100PiB Lustre filesystems (2^57 bytes) today, and I don't want statfs() failing prematurely because userspace feels the need to multiply out the blocks and blocksize into bytes, instead of shifting the values to KB (which would allow filesystems up to 2^74-1024 bytes to be handled correctly in userspace).

Quoted 7 lines
fs/statfs.c +7 -0
+			/*
+			 * f_files and f_ffree may be -1; it's okay to stuff
+			 * that into 32 bits
+			 */
+			if (st->f_files != -1 &&
+			    (st->f_files & 0xffffffff00000000ULL))
+				return -EOVERFLOW;
Quoted 7 lines

fs/statfs.c static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) · line 168

Quoted 3 lines
fs/statfs.c +3 -0
+			if (st->f_ffree != -1 &&
+			    (st->f_ffree & 0xffffffff00000000ULL))
+				return -EOVERFLOW;

Why does sizeof(f_bsize) have anything to do with the number of free files? These two checks are just plain wrong, since f_files and f_ffree are 64-bit fields in struct statfs64.

right.

Quoted 23 lines

Cheers, Andreas

Quoted 8 lines
fs/statfs.c +2 -1
+		}
+
 		buf.f_type = st->f_type;
 		buf.f_bsize = st->f_bsize;
 		buf.f_blocks = st->f_blocks;
--

1.8.3.1

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Cheers, Andreas