Received: by 10.223.164.202 with SMTP id h10csp678589wrb; Wed, 15 Nov 2017 06:17:21 -0800 (PST) X-Google-Smtp-Source: AGs4zMaLIN23Da0KJFbj32p60IEOO7D+sD3gu/TaC/hDMPQj44E6s7635rQ96Kd77TDuHVygTElS X-Received: by 10.101.82.76 with SMTP id q12mr8898422pgp.140.1510755441156; Wed, 15 Nov 2017 06:17:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510755441; cv=none; d=google.com; s=arc-20160816; b=TEsPNVz8+kYclW8W0FnHcSviCThjP1FpdH0xIW0sGoA3vop1ZR6NIEue2knOV7EW1E vU8XtLrhvKZNSIr4xo3RMm1UzFtFtunXhaPEIOsx6dKUeTzY8Asvya9d8Jr6EXjKo/rN KYysXothj73mTLdZgKVrFhBfu1mUj1S+I2aumaNIyB9E5xSq8Wl5Xl+ILfq7sOgquiUY NPpOYE7DhaJhJaK2oj/GpBNsZq/R4lQsWpy9LEVLXiiAlmlciQBF83AAAqKNnTEBev3a p/6xxjeTgbEhRB48v/VACLIHNSj0hSHzA8JOHaxWNqn/42/+NhlNCu4GiSiNUy9iW60Y C+ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=pa9q6MakX3rmTH7u5WdzW+pd3qBWhj0y5yAka3ni3k4=; b=LduMHIcPq4yf0bDU020OUG4Nhzh8rVx9cw58NbeA6bgGFbHeEdX0qqDOQIGUEXL4VW qEP72DKsKFXgoyTrj1iv3P4cXedKq/cGfE2DT06hxnlbb6L8X5sMJKygqy+HoXZAYauq Kvw1d7BMhuF+kuo35+ixdm+vYe+/ZmIOLocaenB9PGGN+CvzWOhRYtaI9KDKVR4UgzBo bpPPjqMQdS2tN+RSzzow4Nb/dDuNwAg1H6hbhe1sSmLH7kf3jnmiiDjvlchiCgK2mpAp 6rB4xtelyw1mPVDiXFSP6dHqY3aW8Z1rhfo1JShkmG/svbu2/y0Punw1p9FGjlhZz14O cYxg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v2si8260939plg.615.2017.11.15.06.17.08; Wed, 15 Nov 2017 06:17:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758242AbdKOOL6 (ORCPT + 89 others); Wed, 15 Nov 2017 09:11:58 -0500 Received: from mga01.intel.com ([192.55.52.88]:6968 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758041AbdKOOI0 (ORCPT ); Wed, 15 Nov 2017 09:08:26 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Nov 2017 06:08:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,399,1505804400"; d="scan'208";a="2131117" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 15 Nov 2017 06:08:20 -0800 From: Elena Reshetova To: mingo@redhat.com Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, peterz@infradead.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk, tj@kernel.org, hannes@cmpxchg.org, lizefan@huawei.com, acme@kernel.org, alexander.shishkin@linux.intel.com, eparis@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, luto@kernel.org, keescook@chromium.org, tglx@linutronix.de, dvhart@infradead.org, ebiederm@xmission.com, linux-mm@kvack.org, axboe@kernel.dk, Elena Reshetova Subject: [PATCH 07/16] sched/task_struct: convert task_struct.stack_refcount to refcount_t Date: Wed, 15 Nov 2017 16:03:31 +0200 Message-Id: <1510754620-27088-8-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510754620-27088-1-git-send-email-elena.reshetova@intel.com> References: <1510754620-27088-1-git-send-email-elena.reshetova@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova --- include/linux/init_task.h | 3 ++- include/linux/sched.h | 2 +- include/linux/sched/task_stack.h | 2 +- kernel/fork.c | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/init_task.h b/include/linux/init_task.h index 1e35fce..6a87579 100644 --- a/include/linux/init_task.h +++ b/include/linux/init_task.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -207,7 +208,7 @@ extern struct cred init_cred; #ifdef CONFIG_THREAD_INFO_IN_TASK # define INIT_TASK_TI(tsk) \ .thread_info = INIT_THREAD_INFO(tsk), \ - .stack_refcount = ATOMIC_INIT(1), + .stack_refcount = REFCOUNT_INIT(1), #else # define INIT_TASK_TI(tsk) #endif diff --git a/include/linux/sched.h b/include/linux/sched.h index 924a812..c8c6d17 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1098,7 +1098,7 @@ struct task_struct { #endif #ifdef CONFIG_THREAD_INFO_IN_TASK /* A live task holds one reference: */ - atomic_t stack_refcount; + refcount_t stack_refcount; #endif #ifdef CONFIG_LIVEPATCH int patch_state; diff --git a/include/linux/sched/task_stack.h b/include/linux/sched/task_stack.h index cb4828a..4559316 100644 --- a/include/linux/sched/task_stack.h +++ b/include/linux/sched/task_stack.h @@ -61,7 +61,7 @@ static inline unsigned long *end_of_stack(struct task_struct *p) #ifdef CONFIG_THREAD_INFO_IN_TASK static inline void *try_get_task_stack(struct task_struct *tsk) { - return atomic_inc_not_zero(&tsk->stack_refcount) ? + return refcount_inc_not_zero(&tsk->stack_refcount) ? task_stack_page(tsk) : NULL; } diff --git a/kernel/fork.c b/kernel/fork.c index 16df4f5..822efa2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -362,7 +362,7 @@ static void release_task_stack(struct task_struct *tsk) #ifdef CONFIG_THREAD_INFO_IN_TASK void put_task_stack(struct task_struct *tsk) { - if (atomic_dec_and_test(&tsk->stack_refcount)) + if (refcount_dec_and_test(&tsk->stack_refcount)) release_task_stack(tsk); } #endif @@ -380,7 +380,7 @@ void free_task(struct task_struct *tsk) * If the task had a separate stack allocation, it should be gone * by now. */ - WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0); + WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); #endif rt_mutex_debug_task_free(tsk); ftrace_graph_exit_task(tsk); @@ -795,7 +795,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) tsk->stack_vm_area = stack_vm_area; #endif #ifdef CONFIG_THREAD_INFO_IN_TASK - atomic_set(&tsk->stack_refcount, 1); + refcount_set(&tsk->stack_refcount, 1); #endif if (err) -- 2.7.4 From 1583959455936499896@xxx Mon Nov 13 13:57:31 +0000 2017 X-GM-THRID: 1583959455936499896 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread