Skip to contentSkip to conversation

copy_mount_string: Limit string length to PATH_MAX

PATCH Applied Chandan Rajendra 3 participants 3 messages Jan 22, 2019 – Feb 1, 2019 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 copy_mount_string: Limit string length to PATH_MAX

On ppc64le, When a string with PAGE_SIZE - 1 (i.e. 64k-1) length is passed as a "filesystem type" argument to the mount(2) syscall, copy_mount_string() ends up allocating 64k (the PAGE_SIZE on ppc64le) worth of space for holding the string in kernel's address space.

Later, in set_precision() (invoked by get_fs_type() -> __request_module() -> vsnprintf()), we end up assigning strlen(fs-type-string) i.e. 65535 as the
value to 'struct printf_spec'->precision member. This field has a width of 16 bits and it is a signed data type. Hence an invalid value ends up getting assigned. This causes the "WARN_ONCE(spec->precision != prec, "precision %d too large", prec)" statement inside set_precision() to be executed.

This commit fixes the bug by limiting the length of the string passed by copy_mount_string() to strndup_user() to PATH_MAX.

---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -1
index 09d2aec0f88c..103513f07483 100644
@@ -2831,7 +2831,7 @@ void *copy_mount_options(const void __user * data)
 
 char *copy_mount_string(const void __user *data)
 {
-	return data ? strndup_user(data, PAGE_SIZE) : NULL;
+	return data ? strndup_user(data, PATH_MAX) : NULL;
 }
 
 /*
Signature · 1 line
2.19.1
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Chandan Rajendra

On ppc64le, When a string with PAGE_SIZE - 1 (i.e. 64k-1) length is passed as a "filesystem type" argument to the mount(2) syscall, copy_mount_string() ends up allocating 64k (the PAGE_SIZE on ppc64le) worth of space for holding the string in kernel's address space.

Later, in set_precision() (invoked by get_fs_type() -> __request_module() -> vsnprintf()), we end up assigning strlen(fs-type-string) i.e. 65535 as the
value to 'struct printf_spec'->precision member. This field has a width of 16 bits and it is a signed data type. Hence an invalid value ends up getting assigned. This causes the "WARN_ONCE(spec->precision != prec, "precision %d too large", prec)" statement inside set_precision() to be executed.

Quoted 6 lines

This commit fixes the bug by limiting the length of the string passed by copy_mount_string() to strndup_user() to PATH_MAX.

Quoted 20 lines
---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -2
index 09d2aec0f88c..103513f07483 100644
@@ -2831,7 +2831,7 @@ void *copy_mount_options(const void __user * data)
 
 char *copy_mount_string(const void __user *data)
 {
-       return data ? strndup_user(data, PAGE_SIZE) : NULL;
+       return data ? strndup_user(data, PATH_MAX) : NULL;
 }
 
 /*
--

2.19.1

maintainer Applied
#2 lore raw
In reply to Chandan Rajendra
Quoted 14 lines · Chandan Rajendra

On ppc64le, When a string with PAGE_SIZE - 1 (i.e. 64k-1) length is passed as a "filesystem type" argument to the mount(2) syscall, copy_mount_string() ends up allocating 64k (the PAGE_SIZE on ppc64le) worth of space for holding the string in kernel's address space.

Later, in set_precision() (invoked by get_fs_type() -> __request_module() -> vsnprintf()), we end up assigning strlen(fs-type-string) i.e. 65535 as the
value to 'struct printf_spec'->precision member. This field has a width of 16 bits and it is a signed data type. Hence an invalid value ends up getting assigned. This causes the "WARN_ONCE(spec->precision != prec, "precision %d too large", prec)" statement inside set_precision() to be executed.

Quoted 6 lines

This commit fixes the bug by limiting the length of the string passed by copy_mount_string() to strndup_user() to PATH_MAX.

Applied