Received: by 10.223.164.202 with SMTP id h10csp677246wrb; Wed, 15 Nov 2017 06:16:13 -0800 (PST) X-Google-Smtp-Source: AGs4zMb2Y5kLVYKuzqORUTVLjlM0h15csiOjRDnM5Myk6NjhWfDzhyk2gUzNVlo/d+Z6B9bi+nFl X-Received: by 10.84.211.105 with SMTP id b96mr15510881pli.86.1510755373336; Wed, 15 Nov 2017 06:16:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510755373; cv=none; d=google.com; s=arc-20160816; b=Fd+nEUaG2ZSxk2Zzpc99JVq8F01xlUXvuxKCZgtAYY0xbH/Eb/Ve6r8AAP7t0CeSUi vvZ7qEmwwOaXZSduG1MazfLS5KiTt/t4QN+sPnTK9lKG45niyThvYeTYxmIOX06wCwe/ 9j0ZLdBNGVe6aiHmXqCI7TZjsZRKpSulQktxmabjqBUvPAdXhtMyCR3JDvJS97xYjOKQ 9Scnj5da+UIIeI0W9+StERSmfw7C73pfx+MtvOVOXho0f4NGmN8hZ77UaXuf+pMs9/5h S/Edj9L6iEKijl1tRhypf5/QGv0i3hNRbnTeTeWRuYMvxNKNxtPR1mQfiflrdT2ixcP3 MIPA== 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=nrykIsG9lDK3LPRZTG8yeMP19OdFQAkC0rZB2bt0XM4=; b=e2sMA/H+EHDOoXWKonGaJb0yliRiB67b1emD+8JvsEzpN64GSINshrTFv2syCUmvM1 QmAsWXLjfH6e80Xdvb12+5IsZB8JTdZRmtoFd19PMar1u5HioIj/MqKFXHoIMBeG3xEu 675fhS7+OOv2m0FWk7vC0vOjj/iBWz0FT5Xb+SkFKifP6Qwfyw0orkHyr/Zc5GycNEZ4 KgOdxpfXWsGYcozvwOlm+eXQ1z92N7SHtTutx+4jy8GQeZoD43wlEULObxuxEBDdkYl/ zkyZ+Vus+qlu9QhV9YPZpZbjvxLFBi4BdbaxpjMAgzIKe7+REnvgzyxEcBMCB7XNTmZC Llvg== 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 32si18164226plg.399.2017.11.15.06.16.00; Wed, 15 Nov 2017 06:16:13 -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 S1758153AbdKOOJn (ORCPT + 89 others); Wed, 15 Nov 2017 09:09:43 -0500 Received: from mga09.intel.com ([134.134.136.24]:49150 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758090AbdKOOI6 (ORCPT ); Wed, 15 Nov 2017 09:08:58 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Nov 2017 06:08:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,399,1505804400"; d="scan'208";a="2131249" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 15 Nov 2017 06:08:51 -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 13/16] groups: convert group_info.usage to refcount_t Date: Wed, 15 Nov 2017 16:03:37 +0200 Message-Id: <1510754620-27088-14-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 group_info.usage 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 group_info.usage it might make a difference in following places: - put_group_info(): 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/cred.h | 7 ++++--- kernel/cred.c | 2 +- kernel/groups.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/linux/cred.h b/include/linux/cred.h index 099058e..00948dd 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -28,7 +29,7 @@ struct inode; * COW Supplementary groups list */ struct group_info { - atomic_t usage; + refcount_t usage; int ngroups; kgid_t gid[0]; } __randomize_layout; @@ -44,7 +45,7 @@ struct group_info { */ static inline struct group_info *get_group_info(struct group_info *gi) { - atomic_inc(&gi->usage); + refcount_inc(&gi->usage); return gi; } @@ -54,7 +55,7 @@ static inline struct group_info *get_group_info(struct group_info *gi) */ #define put_group_info(group_info) \ do { \ - if (atomic_dec_and_test(&(group_info)->usage)) \ + if (refcount_dec_and_test(&(group_info)->usage)) \ groups_free(group_info); \ } while (0) diff --git a/kernel/cred.c b/kernel/cred.c index 0192a94..9604c1a 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -36,7 +36,7 @@ do { \ static struct kmem_cache *cred_jar; /* init to 2 - one for init_task, one to ensure it is never freed */ -struct group_info init_groups = { .usage = ATOMIC_INIT(2) }; +struct group_info init_groups = { .usage = REFCOUNT_INIT(2) }; /* * The initial credentials for the initial task diff --git a/kernel/groups.c b/kernel/groups.c index e357bc8..2ab0e56 100644 --- a/kernel/groups.c +++ b/kernel/groups.c @@ -24,7 +24,7 @@ struct group_info *groups_alloc(int gidsetsize) if (!gi) return NULL; - atomic_set(&gi->usage, 1); + refcount_set(&gi->usage, 1); gi->ngroups = gidsetsize; return gi; } -- 2.7.4 From 1584310886136647602@xxx Fri Nov 17 11:03:21 +0000 2017 X-GM-THRID: 1584141790357511580 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread