mips: sched: Fix CPUMASK_OFFSTACK memory corruption
PATCH v2 1/1 mips: sched: Fix CPUMASK_OFFSTACK memory corruption
This patch addresses a critical memory management flaw. When CONFIG_CPUMASK_OFFSTACK is enabled, cpumask_var_t is a pointer. Consequently, sizeof(new_mask) evaluates to the pointer size, causing copy_from_user() to clobber the mask pointer. Furthermore, the old logic performed copy_from_user() before allocating the mask.
Fix this by allocating new_mask first. To handle variable-sized user masks correctly, use cpumask_size() to truncate overly large user masks or pad undersized masks with zeros before copying the data directly into the allocated buffer.
---
Changes since v1:
- Fixed the !p error path to set retval = -ESRCH and jump to out_free_new_mask so new_mask is properly freed on failure (Sasha Levin)
- Link to v1: https://lore.kernel.org/stable/[email protected]/
--- arch/mips/kernel/mips-mt-fpaff.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-)
arch/mips/kernel/mips-mt-fpaff.c +15 -13
@@ -70,11 +70,16 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,struct task_struct *p;int retval; -if (len <sizeof (new_mask)) -return -EINVAL ; - -if (copy_from_user (&new_mask, user_mask_ptr,sizeof (new_mask))) -return -EFAULT ; +if (!alloc_cpumask_var (&new_mask,GFP_KERNEL )) +return -ENOMEM ; +if (len <cpumask_size ()) +cpumask_clear (new_mask); +else if (len >cpumask_size ()) + len =cpumask_size (); +if (copy_from_user (new_mask, user_mask_ptr, len)) { + retval = -EFAULT ; + goto out_free_new_mask; + }get_online_cpus ();rcu_read_lock ();
@@ -83,7 +88,8 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len,if (!p) {rcu_read_unlock ();put_online_cpus (); -return -ESRCH ; + retval = -ESRCH ; + goto out_free_new_mask; }/* Prevent p going away */
@@ -94,13 +100,9 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len, retval = -ENOMEM ; goto out_put_task; } -if (!alloc_cpumask_var (&new_mask,GFP_KERNEL )) { - retval = -ENOMEM ; - goto out_free_cpus_allowed; - }if (!alloc_cpumask_var (&effective_mask,GFP_KERNEL )) { retval = -ENOMEM ; - goto out_free_new_mask; + goto out_free_cpus_allowed; }if (!check_same_owner (p) && !capable (CAP_SYS_NICE )) { retval = -EPERM ;
@@ -141,13 +143,13 @@ asmlinkage long mipsmt_sys_sched_setaffinity(pid_t pid, unsigned int len, } out_unlock:free_cpumask_var (effective_mask); -out_free_new_mask: -free_cpumask_var (new_mask); out_free_cpus_allowed:free_cpumask_var (cpus_allowed); out_put_task:put_task_struct (p);put_online_cpus (); +out_free_new_mask: +free_cpumask_var (new_mask);return retval; }
Signature · 1 line
2.55.0
Quoted 4 lines
Changes since v1:
- Fixed the !p error path to set retval =
-ESRCHand jump toout_free_new_masksonew_maskis properly freed on failure (Sasha Levin)
Queued for 5.10, thanks.
Signature · 2 lines
Thanks, Sasha