History

sched/core: Convert task_struct.stack_refcount 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 task_struct.stack_refcount 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 task_struct.stack_refcount it might make a difference in following places: - try_get_task_stack(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart - put_task_stack(): 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 f0b89d31 parent ec1d2815 files · +7 −6Browse files

Unified · Split

5 files
  1. include/linux/init_task.h +1 −0
  2. include/linux/sched.h +1 −1
  3. include/linux/sched/task_stack.h +1 −1
  4. init/init_task.c +1 −1
  5. kernel/fork.c +3 −3
include/linux/init_task.h+1−0
@@ -13,6 +13,7 @@ · 12 unmodified lines
13#include <linux/securebits.h>13#include <linux/securebits.h>
14#include <linux/seqlock.h>14#include <linux/seqlock.h>
15#include <linux/rbtree.h>15#include <linux/rbtree.h>
16#include <linux/refcount.h>
16#include <linux/sched/autogroup.h>17#include <linux/sched/autogroup.h>
17#include <net/net_namespace.h>18#include <net/net_namespace.h>
18#include <linux/sched/rt.h>19#include <linux/sched/rt.h>
include/linux/sched.h+1−1
@@ -1194,7 +1194,7 @@ struct task_struct { · 1193 unmodified lines
1194#endif1194#endif
1195#ifdef CONFIG_THREAD_INFO_IN_TASK1195#ifdef CONFIG_THREAD_INFO_IN_TASK
1196 /* A live task holds one reference: */1196 /* A live task holds one reference: */
1197 atomic_t stack_refcount;1197 refcount_t stack_refcount;
1198#endif1198#endif
1199#ifdef CONFIG_LIVEPATCH1199#ifdef CONFIG_LIVEPATCH
1200 int patch_state;1200 int patch_state;
include/linux/sched/task_stack.h+1−1
@@ -61,7 +61,7 @@ static inline unsigned long *end_of_stack(struct task_struct *p) · 60 unmodified lines
61#ifdef CONFIG_THREAD_INFO_IN_TASK61#ifdef CONFIG_THREAD_INFO_IN_TASK
62static inline void *try_get_task_stack(struct task_struct *tsk)62static inline void *try_get_task_stack(struct task_struct *tsk)
63{63{
64 return atomic_inc_not_zero(&tsk->stack_refcount) ?64 return refcount_inc_not_zero(&tsk->stack_refcount) ?
65 task_stack_page(tsk) : NULL;65 task_stack_page(tsk) : NULL;
66}66}
67 67
init/init_task.c+1−1
@@ -61,7 +61,7 @@ struct task_struct init_task · 60 unmodified lines
61= {61= {
62#ifdef CONFIG_THREAD_INFO_IN_TASK62#ifdef CONFIG_THREAD_INFO_IN_TASK
63 .thread_info = INIT_THREAD_INFO(init_task),63 .thread_info = INIT_THREAD_INFO(init_task),
64 .stack_refcount = ATOMIC_INIT(1),64 .stack_refcount = REFCOUNT_INIT(1),
65#endif65#endif
66 .state = 0,66 .state = 0,
67 .stack = init_stack,67 .stack = init_stack,
kernel/fork.c+3−3
@@ -429,7 +429,7 @@ static void release_task_stack(struct task_struct *tsk) · 428 unmodified lines
429#ifdef CONFIG_THREAD_INFO_IN_TASK429#ifdef CONFIG_THREAD_INFO_IN_TASK
430void put_task_stack(struct task_struct *tsk)430void put_task_stack(struct task_struct *tsk)
431{431{
432 if (atomic_dec_and_test(&tsk->stack_refcount))432 if (refcount_dec_and_test(&tsk->stack_refcount))
433 release_task_stack(tsk);433 release_task_stack(tsk);
434}434}
435#endif435#endif
@@ -447,7 +447,7 @@ void free_task(struct task_struct *tsk) · 11 unmodified lines
447 * If the task had a separate stack allocation, it should be gone447 * If the task had a separate stack allocation, it should be gone
448 * by now.448 * by now.
449 */449 */
450 WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0);450 WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0);
451#endif451#endif
452 rt_mutex_debug_task_free(tsk);452 rt_mutex_debug_task_free(tsk);
453 ftrace_graph_exit_task(tsk);453 ftrace_graph_exit_task(tsk);
@@ -867,7 +867,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) · 413 unmodified lines
867 tsk->stack_vm_area = stack_vm_area;867 tsk->stack_vm_area = stack_vm_area;
868#endif868#endif
869#ifdef CONFIG_THREAD_INFO_IN_TASK869#ifdef CONFIG_THREAD_INFO_IN_TASK
870 atomic_set(&tsk->stack_refcount, 1);870 refcount_set(&tsk->stack_refcount, 1);
871#endif871#endif
872 872
873 if (err)873 if (err)

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