Skip to content

Commit 5657c11

Browse files
Waiman-LongIngo Molnar
authored andcommitted
sched/core: Fix NULL pointer access fault in sched_setaffinity() with non-SMP configs
The kernel commit 9a5418b ("sched/core: Use kfree_rcu() in do_set_cpus_allowed()") introduces a bug for kernels built with non-SMP configs. Calling sched_setaffinity() on such a uniprocessor kernel will cause cpumask_copy() to be called with a NULL pointer leading to general protection fault. This is not really a problem in real use cases as there aren't that many uniprocessor kernel configs in use and calling sched_setaffinity() on such a uniprocessor system doesn't make sense. Fix this problem by making sure cpumask_copy() will not be called in such a case. Fixes: 9a5418b ("sched/core: Use kfree_rcu() in do_set_cpus_allowed()") Reported-by: kernel test robot <yujie.liu@intel.com> Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Acked-by: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20230115193122.563036-1-longman@redhat.com
1 parent da07d2f commit 5657c11

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

kernel/sched/core.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8290,12 +8290,18 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
82908290
if (retval)
82918291
goto out_put_task;
82928292

8293+
/*
8294+
* With non-SMP configs, user_cpus_ptr/user_mask isn't used and
8295+
* alloc_user_cpus_ptr() returns NULL.
8296+
*/
82938297
user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
8294-
if (IS_ENABLED(CONFIG_SMP) && !user_mask) {
8298+
if (user_mask) {
8299+
cpumask_copy(user_mask, in_mask);
8300+
} else if (IS_ENABLED(CONFIG_SMP)) {
82958301
retval = -ENOMEM;
82968302
goto out_put_task;
82978303
}
8298-
cpumask_copy(user_mask, in_mask);
8304+
82998305
ac = (struct affinity_context){
83008306
.new_mask = in_mask,
83018307
.user_mask = user_mask,

0 commit comments

Comments
 (0)