Received: by 10.223.164.202 with SMTP id h10csp818035wrb; Wed, 15 Nov 2017 08:23:24 -0800 (PST) X-Google-Smtp-Source: AGs4zMYfG3Kh/rAPW+oIPSSF25jzgWv6bMcc8Xpi7ML9pudarVucAyvy57RI8OrDsHVnNS9/XrQL X-Received: by 10.99.126.6 with SMTP id z6mr14511935pgc.126.1510763004697; Wed, 15 Nov 2017 08:23:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510763004; cv=none; d=google.com; s=arc-20160816; b=SEPj/AnKv1PNPfpAyFPnoLVcufMmzJAwkWUeh4OJOCMa2QUIrxQKI5GS3GOTJE+zK2 mFcZAtZUCti9vdUybR717BWY4fCwWZkSTcyEZ5Ewyo50r6oUnpzZFZUtsZYSgrVbBDaA nZ/7sT/vQ2JXjCME5swgkRNRC2UsupOpuAzJyzGv+AJACmrx7IPr6gzFotgx9d51FVI1 RdzlMYfAy06RpceTGZN2oM++QN5CFdybZvGMMfCUrEWZxTQqEH5J/zMnR+lfxK/8z1qA mnaiIV2fjMEjemx2Zp44OIh2bmOILoHazTOTSdl5fE3z2CJrIo1+1F7Vgp8Dp+VjE5Dk nERg== 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=7+TlZk/2q9CXJi9cH8/GBC3/a8TWNAdRyx/bWy+ByvE=; b=Vfu/Gx4JJqntdqSOWx4VqIU+9YeUz83KA45FZfE7dDSMcakugWla17DUe6UNu5bmLT Zpv5T1WTXsTGBPgQKp+WdkRY80VFzh0GW4HXV4YCVUVEOzh7+aIMa88xs2PC3kwfeTAs ql7qFXTQJaaFBNMKiD2q+8bag6QHhubK4pF00TYN9Z41lgNYGwabP3dVF/lzTuWeS/NT 7SgnQmbDUWcYwNe/uMlmXZu+be1ct6/gYtNDDrT6MOTBdQd6kMAldwqQ2ltpwBns3HWV 0XafPHVjtLSWsC7w8SW2IvEWfpnRMDrOIu10NLw8o4ckJRFx29hgG8XpC7bdZSXGrP4Q 7ZMQ== 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 y2si17780409pgo.89.2017.11.15.08.23.11; Wed, 15 Nov 2017 08:23:24 -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 S1758008AbdKOOIB (ORCPT + 89 others); Wed, 15 Nov 2017 09:08:01 -0500 Received: from mga01.intel.com ([192.55.52.88]:57729 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755276AbdKOOHy (ORCPT ); Wed, 15 Nov 2017 09:07:54 -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:07:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,399,1505804400"; d="scan'208";a="2130921" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 15 Nov 2017 06:07:49 -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 01/16] futex: convert futex_pi_state.refcount to refcount_t Date: Wed, 15 Nov 2017 16:03:25 +0200 Message-Id: <1510754620-27088-2-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 futex_pi_state.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 futex_pi_state.refcount it might make a difference in following places: - get_pi_state() and exit_pi_state_list(): increment in refcount_inc_not_zero() only guarantees control dependency on success vs. fully ordered atomic counterpart - put_pi_state(): 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 --- kernel/futex.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/kernel/futex.c b/kernel/futex.c index 76ed592..907055f 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -67,6 +67,7 @@ #include #include #include +#include #include @@ -209,7 +210,7 @@ struct futex_pi_state { struct rt_mutex pi_mutex; struct task_struct *owner; - atomic_t refcount; + refcount_t refcount; union futex_key key; } __randomize_layout; @@ -795,7 +796,7 @@ static int refill_pi_state_cache(void) INIT_LIST_HEAD(&pi_state->list); /* pi_mutex gets initialized later */ pi_state->owner = NULL; - atomic_set(&pi_state->refcount, 1); + refcount_set(&pi_state->refcount, 1); pi_state->key = FUTEX_KEY_INIT; current->pi_state_cache = pi_state; @@ -815,7 +816,7 @@ static struct futex_pi_state *alloc_pi_state(void) static void get_pi_state(struct futex_pi_state *pi_state) { - WARN_ON_ONCE(!atomic_inc_not_zero(&pi_state->refcount)); + WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount)); } /* @@ -827,7 +828,7 @@ static void put_pi_state(struct futex_pi_state *pi_state) if (!pi_state) return; - if (!atomic_dec_and_test(&pi_state->refcount)) + if (!refcount_dec_and_test(&pi_state->refcount)) return; /* @@ -857,7 +858,7 @@ static void put_pi_state(struct futex_pi_state *pi_state) * refcount is at 0 - put it back to 1. */ pi_state->owner = NULL; - atomic_set(&pi_state->refcount, 1); + refcount_set(&pi_state->refcount, 1); current->pi_state_cache = pi_state; } } @@ -918,7 +919,7 @@ void exit_pi_state_list(struct task_struct *curr) * In that case; drop the locks to let put_pi_state() make * progress and retry the loop. */ - if (!atomic_inc_not_zero(&pi_state->refcount)) { + if (!refcount_inc_not_zero(&pi_state->refcount)) { raw_spin_unlock_irq(&curr->pi_lock); cpu_relax(); raw_spin_lock_irq(&curr->pi_lock); @@ -1074,7 +1075,7 @@ static int attach_to_pi_state(u32 __user *uaddr, u32 uval, * and futex_wait_requeue_pi() as it cannot go to 0 and consequently * free pi_state before we can take a reference ourselves. */ - WARN_ON(!atomic_read(&pi_state->refcount)); + WARN_ON(!refcount_read(&pi_state->refcount)); /* * Now that we have a pi_state, we can acquire wait_lock -- 2.7.4 From 1584169966668155114@xxx Wed Nov 15 21:43:30 +0000 2017 X-GM-THRID: 1584169966668155114 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread