Skip to content

Commit f0b89d3

Browse files
ereshetovaingomolnar
authored andcommitted
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 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> 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-6-git-send-email-elena.reshetova@intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ec1d281 commit f0b89d3

5 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎include/linux/init_task.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/securebits.h>
1414
#include <linux/seqlock.h>
1515
#include <linux/rbtree.h>
16+
#include <linux/refcount.h>
1617
#include <linux/sched/autogroup.h>
1718
#include <net/net_namespace.h>
1819
#include <linux/sched/rt.h>

‎include/linux/sched.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ struct task_struct {
11941194
#endif
11951195
#ifdef CONFIG_THREAD_INFO_IN_TASK
11961196
/* A live task holds one reference: */
1197-
atomic_t stack_refcount;
1197+
refcount_t stack_refcount;
11981198
#endif
11991199
#ifdef CONFIG_LIVEPATCH
12001200
int patch_state;

‎include/linux/sched/task_stack.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static inline unsigned long *end_of_stack(struct task_struct *p)
6161
#ifdef CONFIG_THREAD_INFO_IN_TASK
6262
static inline void *try_get_task_stack(struct task_struct *tsk)
6363
{
64-
return atomic_inc_not_zero(&tsk->stack_refcount) ?
64+
return refcount_inc_not_zero(&tsk->stack_refcount) ?
6565
task_stack_page(tsk) : NULL;
6666
}
6767

‎init/init_task.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct task_struct init_task
6161
= {
6262
#ifdef CONFIG_THREAD_INFO_IN_TASK
6363
.thread_info = INIT_THREAD_INFO(init_task),
64-
.stack_refcount = ATOMIC_INIT(1),
64+
.stack_refcount = REFCOUNT_INIT(1),
6565
#endif
6666
.state = 0,
6767
.stack = init_stack,

‎kernel/fork.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ static void release_task_stack(struct task_struct *tsk)
429429
#ifdef CONFIG_THREAD_INFO_IN_TASK
430430
void put_task_stack(struct task_struct *tsk)
431431
{
432-
if (atomic_dec_and_test(&tsk->stack_refcount))
432+
if (refcount_dec_and_test(&tsk->stack_refcount))
433433
release_task_stack(tsk);
434434
}
435435
#endif
@@ -447,7 +447,7 @@ void free_task(struct task_struct *tsk)
447447
* If the task had a separate stack allocation, it should be gone
448448
* by now.
449449
*/
450-
WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0);
450+
WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0);
451451
#endif
452452
rt_mutex_debug_task_free(tsk);
453453
ftrace_graph_exit_task(tsk);
@@ -867,7 +867,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
867867
tsk->stack_vm_area = stack_vm_area;
868868
#endif
869869
#ifdef CONFIG_THREAD_INFO_IN_TASK
870-
atomic_set(&tsk->stack_refcount, 1);
870+
refcount_set(&tsk->stack_refcount, 1);
871871
#endif
872872

873873
if (err)

0 commit comments

Comments
 (0)