Skip to content

Commit d036bda

Browse files
ereshetovaingomolnar
authored andcommitted
sched/core: Convert sighand_struct.count to refcount_t
atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable sighand_struct.count is used as pure reference counter. Convert it to refcount_t and fix up the operations. ** Important note for maintainers: Some functions from refcount_t API defined in lib/refcount.c have different memory ordering guarantees than their atomic counterparts. The full comparison can be seen in https://lkml.org/lkml/2017/11/15/57 and it is hopefully soon in state to be merged to the documentation tree. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the sighand_struct.count it might make a difference in following places: - __cleanup_sighand: decrement in refcount_dec_and_test() only provides RELEASE ordering and control dependency on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook <keescook@chromium.org> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Reviewed-by: Andrea Parri <andrea.parri@amarulasolutions.com> Reviewed-by: Oleg Nesterov <oleg@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk Link: https://lkml.kernel.org/r/1547814450-18902-2-git-send-email-elena.reshetova@intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 15917dc commit d036bda

4 files changed

Lines changed: 9 additions & 8 deletions

File tree

‎fs/exec.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ static int de_thread(struct task_struct *tsk)
11891189
flush_itimer_signals();
11901190
#endif
11911191

1192-
if (atomic_read(&oldsighand->count) != 1) {
1192+
if (refcount_read(&oldsighand->count) != 1) {
11931193
struct sighand_struct *newsighand;
11941194
/*
11951195
* This ->sighand is shared with the CLONE_SIGHAND
@@ -1199,7 +1199,7 @@ static int de_thread(struct task_struct *tsk)
11991199
if (!newsighand)
12001200
return -ENOMEM;
12011201

1202-
atomic_set(&newsighand->count, 1);
1202+
refcount_set(&newsighand->count, 1);
12031203
memcpy(newsighand->action, oldsighand->action,
12041204
sizeof(newsighand->action));
12051205

‎fs/proc/task_nommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
6464
else
6565
bytes += kobjsize(current->files);
6666

67-
if (current->sighand && atomic_read(&current->sighand->count) > 1)
67+
if (current->sighand && refcount_read(&current->sighand->count) > 1)
6868
sbytes += kobjsize(current->sighand);
6969
else
7070
bytes += kobjsize(current->sighand);

‎include/linux/sched/signal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
#include <linux/sched/jobctl.h>
99
#include <linux/sched/task.h>
1010
#include <linux/cred.h>
11+
#include <linux/refcount.h>
1112

1213
/*
1314
* Types defining task->signal and task->sighand and APIs using them:
1415
*/
1516

1617
struct sighand_struct {
17-
atomic_t count;
18+
refcount_t count;
1819
struct k_sigaction action[_NSIG];
1920
spinlock_t siglock;
2021
wait_queue_head_t signalfd_wqh;

‎kernel/fork.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,15 +1463,15 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
14631463
struct sighand_struct *sig;
14641464

14651465
if (clone_flags & CLONE_SIGHAND) {
1466-
atomic_inc(&current->sighand->count);
1466+
refcount_inc(&current->sighand->count);
14671467
return 0;
14681468
}
14691469
sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
14701470
rcu_assign_pointer(tsk->sighand, sig);
14711471
if (!sig)
14721472
return -ENOMEM;
14731473

1474-
atomic_set(&sig->count, 1);
1474+
refcount_set(&sig->count, 1);
14751475
spin_lock_irq(&current->sighand->siglock);
14761476
memcpy(sig->action, current->sighand->action, sizeof(sig->action));
14771477
spin_unlock_irq(&current->sighand->siglock);
@@ -1480,7 +1480,7 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
14801480

14811481
void __cleanup_sighand(struct sighand_struct *sighand)
14821482
{
1483-
if (atomic_dec_and_test(&sighand->count)) {
1483+
if (refcount_dec_and_test(&sighand->count)) {
14841484
signalfd_cleanup(sighand);
14851485
/*
14861486
* sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it
@@ -2439,7 +2439,7 @@ static int check_unshare_flags(unsigned long unshare_flags)
24392439
return -EINVAL;
24402440
}
24412441
if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
2442-
if (atomic_read(&current->sighand->count) > 1)
2442+
if (refcount_read(&current->sighand->count) > 1)
24432443
return -EINVAL;
24442444
}
24452445
if (unshare_flags & CLONE_VM) {

0 commit comments

Comments
 (0)