Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp680149ybg; Fri, 12 Jun 2020 11:39:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVIBUDo2ulMmImcA850wHhbcgiJ1zAk6MDQqOFjEAsgF+OthuVimwFNTgVxmhXlyvoKzPW X-Received: by 2002:a05:6402:b21:: with SMTP id bo1mr12416380edb.169.1591987173771; Fri, 12 Jun 2020 11:39:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591987173; cv=none; d=google.com; s=arc-20160816; b=KbshuKCUohNbPymmZ5uxxfwvsJpLMlrKd527tZJm+seDutSBGStZnzsqq/i7yQS3qQ O9v/6P11FcilyOi5/2gpS+6qJJyvXkCFkNXI4Vo2gG35oKR3Ke6dIE4KnREniVD0X7SO p/bd4s0EqyH5wzVUOSvsa+BBVHVabHYUUoxBQ+InaKlHM8mU02jrHBvjBJSxCP9ZsXzU JkfnJilYdvLT91lR2ye+AeDGUnwE0VBlHE8dKESWTsNtKubJ5i0WSU3VOCKiQrf2ni3O iWcyN3USO5wdclLSmbmpl5JnGfobFO5JSMeXEHUEfHVhLjBWipeW1+Ip7IwGeiChT6X7 ig/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lo23MkK3oKdy4eP+VnzY6Z8lJ6ELcmkBOBeirsdef6w=; b=r1retvh/nM+8MvCjit6WiXPmru7HDY1PamL2zzcrxvh3K8tvfY7MeaetRxwqycgQnm iv6kNSuSoFWDNWOYXZzP4kO8XUY8M1/oydAihk4X6sIhd1bn5tMAvVYIcHcshzc7JjUj D9LN7tzNBE3jdKVb998X1bKN/kn0Mx/ZtlfHhZSMcp49EIvxPKGBoBO7rth4WHY3dn0v 1i+GdMAWwZgbjsKCxrXcV3xwRsUkHerslk19wA6WcpzlVe5NWHenF73LxmSfUL/v9bMe 3uNOEE6EmaAHnPTSA+WMkHEYPtY9vN35+AgoT3QQvUNaxS88SOCf/l2/zF6IWm41S0ep ogiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=S74xKWuA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a8si4199523edq.164.2020.06.12.11.39.10; Fri, 12 Jun 2020 11:39:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=S74xKWuA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726357AbgFLSfD (ORCPT + 99 others); Fri, 12 Jun 2020 14:35:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726309AbgFLSfD (ORCPT ); Fri, 12 Jun 2020 14:35:03 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 306A4C03E96F for ; Fri, 12 Jun 2020 11:35:03 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id h185so4706914pfg.2 for ; Fri, 12 Jun 2020 11:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lo23MkK3oKdy4eP+VnzY6Z8lJ6ELcmkBOBeirsdef6w=; b=S74xKWuAXXukOQlwacSEcEdt1GgjV38lUB4QRPuRWJL/ZEWtRpBCmS/DR2BDqixQq4 dpuZrg+TFAqn5gtk0gQyAk/PzbCJTSlH7/y7DRq+ZRALtZr9lzyQaNnS7uzDNAHjeCKq 0onKFvecNb5QdESMgLCpN6ALow3kidAVUSwL4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lo23MkK3oKdy4eP+VnzY6Z8lJ6ELcmkBOBeirsdef6w=; b=WQFuJIZOTe9FJkgNL7KOqCi3K3E7ORP/gnPIfpaGCuZYw6XNFx4wSPNq3nyCGpDT+u w/FSexLfBhRRK08qRpYQrtfBI3mbKXVIF1Wn/elmuFmJiFx3CucwE+7IvCImbdlgX3Y3 uRWWK17xs6K8DoMXmWkYtEHQKvxgWqwlIsh5f1SIs69poKd9NkA3K26aO+ZUQ32u+4Nu bX6C+mMdlL+5wINT+pfNd/Qf4qVm/hG7xsuOUAVQ1z88BfWqdLTZ3pE/GZqAmIYnG9DS uscJJ/miCFWdQQOMt+bW/bWwGr3aih6EOYcLKgGFDzMEut7U2nIrQcK9WpOLO1NBGcNV zdig== X-Gm-Message-State: AOAM533Hh7GjMyPYF5eOcTwfd8Qx4nS6oYA464352w545KfQBR0Wg1+G yahsJePf5pHZoSOMuYCnlXItuA== X-Received: by 2002:a63:3587:: with SMTP id c129mr12357594pga.190.1591986902726; Fri, 12 Jun 2020 11:35:02 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id w190sm6750641pfw.35.2020.06.12.11.35.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jun 2020 11:35:01 -0700 (PDT) From: Kees Cook To: Peter Zijlstra Cc: Kees Cook , Elena Reshetova , David Windsor , Hans Liljestrand , Xiaoming Ni , Paul Moore , edumazet@google.com, paulmck@kernel.org, David Howells , shakeelb@google.com, James Morris , alex.huangjianhui@huawei.com, dylix.dailei@huawei.com, chenzefeng2@huawei.com, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] groups: convert group_info.usage to refcount_t Date: Fri, 12 Jun 2020 11:34:49 -0700 Message-Id: <20200612183450.4189588-3-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200612183450.4189588-1-keescook@chromium.org> References: <20200612183450.4189588-1-keescook@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Elena Reshetova 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. Please check Documentation/core-api/refcount-vs-atomic.rst for more information. 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 ACQUIRE ordering on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook Signed-off-by: Elena Reshetova Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Link: https://lore.kernel.org/r/20190306110549.7628-3-elena.reshetova@intel.com Signed-off-by: Kees Cook --- 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 18639c069263..32be0daf5a32 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -23,7 +24,7 @@ struct inode; * COW Supplementary groups list */ struct group_info { - atomic_t usage; + refcount_t usage; int ngroups; kgid_t gid[0]; } __randomize_layout; @@ -39,7 +40,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; } @@ -49,7 +50,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 421b1149c651..ed95117d07c4 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -33,7 +33,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 6ee6691f6839..22f2892dbaf3 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.25.1