History

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

Elena Reshetova committed 7 years ago

Commit d036bda1 parent 15917dc4 files · +9 −8Browse files

Unified · Split

4 files
  1. fs/exec.c +2 −2
  2. fs/proc/task_nommu.c +1 −1
  3. include/linux/sched/signal.h +2 −1
  4. kernel/fork.c +4 −4
fs/exec.c+2−2
@@ -1189,7 +1189,7 @@ no_thread_group: · 1188 unmodified lines
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 @@ no_thread_group: · 3 unmodified lines
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+1−1
@@ -64,7 +64,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm) · 63 unmodified lines
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+2−1
@@ -8,13 +8,14 @@ · 7 unmodified lines
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+4−4
@@ -1463,7 +1463,7 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) · 1462 unmodified lines
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);
@@ -1471,7 +1471,7 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) · 1 unmodified line
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) · 2 unmodified lines
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) · 952 unmodified lines
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) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t