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
1189 flush_itimer_signals();1189 flush_itimer_signals();
1190#endif1190#endif
1191 1191
1192 if (atomic_read(&oldsighand->count) != 1) {1192 if (refcount_read(&oldsighand->count) != 1) {
1193 struct sighand_struct *newsighand;1193 struct sighand_struct *newsighand;
1194 /*1194 /*
1195 * This ->sighand is shared with the CLONE_SIGHAND1195 * This ->sighand is shared with the CLONE_SIGHAND
@@ -1199,7 +1199,7 @@ no_thread_group: · 3 unmodified lines
1199 if (!newsighand)1199 if (!newsighand)
1200 return -ENOMEM;1200 return -ENOMEM;
1201 1201
1202 atomic_set(&newsighand->count, 1);1202 refcount_set(&newsighand->count, 1);
1203 memcpy(newsighand->action, oldsighand->action,1203 memcpy(newsighand->action, oldsighand->action,
1204 sizeof(newsighand->action));1204 sizeof(newsighand->action));
1205 1205
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
64 else64 else
65 bytes += kobjsize(current->files);65 bytes += kobjsize(current->files);
66 66
67 if (current->sighand && atomic_read(&current->sighand->count) > 1)67 if (current->sighand && refcount_read(&current->sighand->count) > 1)
68 sbytes += kobjsize(current->sighand);68 sbytes += kobjsize(current->sighand);
69 else69 else
70 bytes += kobjsize(current->sighand);70 bytes += kobjsize(current->sighand);
include/linux/sched/signal.h+2−1
@@ -8,13 +8,14 @@ · 7 unmodified lines
8#include <linux/sched/jobctl.h>8#include <linux/sched/jobctl.h>
9#include <linux/sched/task.h>9#include <linux/sched/task.h>
10#include <linux/cred.h>10#include <linux/cred.h>
11#include <linux/refcount.h>
11 12
12/*13/*
13 * Types defining task->signal and task->sighand and APIs using them:14 * Types defining task->signal and task->sighand and APIs using them:
14 */15 */
15 16
16struct sighand_struct {17struct sighand_struct {
17 atomic_t count;18 refcount_t count;
18 struct k_sigaction action[_NSIG];19 struct k_sigaction action[_NSIG];
19 spinlock_t siglock;20 spinlock_t siglock;
20 wait_queue_head_t signalfd_wqh;21 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
1463 struct sighand_struct *sig;1463 struct sighand_struct *sig;
1464 1464
1465 if (clone_flags & CLONE_SIGHAND) {1465 if (clone_flags & CLONE_SIGHAND) {
1466 atomic_inc(&current->sighand->count);1466 refcount_inc(&current->sighand->count);
1467 return 0;1467 return 0;
1468 }1468 }
1469 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);1469 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
1471 if (!sig)1471 if (!sig)
1472 return -ENOMEM;1472 return -ENOMEM;
1473 1473
1474 atomic_set(&sig->count, 1);1474 refcount_set(&sig->count, 1);
1475 spin_lock_irq(&current->sighand->siglock);1475 spin_lock_irq(&current->sighand->siglock);
1476 memcpy(sig->action, current->sighand->action, sizeof(sig->action));1476 memcpy(sig->action, current->sighand->action, sizeof(sig->action));
1477 spin_unlock_irq(&current->sighand->siglock);1477 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
1480 1480
1481void __cleanup_sighand(struct sighand_struct *sighand)1481void __cleanup_sighand(struct sighand_struct *sighand)
1482{1482{
1483 if (atomic_dec_and_test(&sighand->count)) {1483 if (refcount_dec_and_test(&sighand->count)) {
1484 signalfd_cleanup(sighand);1484 signalfd_cleanup(sighand);
1485 /*1485 /*
1486 * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it1486 * 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
2439 return -EINVAL;2439 return -EINVAL;
2440 }2440 }
2441 if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {2441 if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) {
2442 if (atomic_read(&current->sighand->count) > 1)2442 if (refcount_read(&current->sighand->count) > 1)
2443 return -EINVAL;2443 return -EINVAL;
2444 }2444 }
2445 if (unshare_flags & CLONE_VM) {2445 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