Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6367801imu; Sun, 2 Dec 2018 16:33:00 -0800 (PST) X-Google-Smtp-Source: AFSGD/VQctTJ6tW8H3r1Iw/wRHxPHzBI2xpVezrUb4Y4PQ52uE+BdD9k4Xz5aYx/GYpoXvUyQIyk X-Received: by 2002:a62:c683:: with SMTP id x3mr13606011pfk.10.1543797180307; Sun, 02 Dec 2018 16:33:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543797180; cv=none; d=google.com; s=arc-20160816; b=wgaDDBaSTfcM9E4rIYP4Cldsh0zqT8Cm700WxTtPmMEyWejYm8/BWH8OIj83Y/Kt8h AER75H2UdXZ8VS721wBd3FMu7X0DFUy9anq+UmYX2v54AdZ3URpXkJCwqDejxvKDZg+i tjHnrp42cDKROFLHaVrK+0ZQNhD58XyOTawNXD+b//4IZTBjDUi7ytAkjO6l0S7f4daK SEjA3b9S65N7NfDWMYxYPHGwFu4gdLKmVEPPt5J2w+IpLfDLemCiQ9Jtar0gzQQ/Mabr cY1sUE4nqDeGJTArpRaBiwEGAQe1E2IFAfBUCUH44Tfz2LCSLfh0NmtMmYmtVQnagtwz SrVg== 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 :user-agent:references:in-reply-to:message-id:cc:subject:date:to :from; bh=eXdCxbndrLjJPLDBoOcebeeEQjeAhy4igKDbFXU8FFE=; b=AlaTfi7Cwuzu8oN2A2MtAfUbD1s7TAZZc+4Lzz3VVhttuDI6kWCuaSV9X7iSMFxgTG 9O9E0yfgdMzKSlXHwE4Cm0dPt2hJcC97/AUC2qkoqsb4ynA9yTzPXbjBgO3uf9OYlpme bauW2JQcpDZ9JVw7jvm21EG4kSIac663v7qPpGGKGwjjEcF66I+L1PdoH8JmXpsk8zec SSnwVaZY3ElWAMYsPmQfQRFl/cynD3mQt8T1l4VbruaXvgjrCYDxh6a6+PvBgke+1wr1 E8uNwxS9yZ50mqa6exBPLJ1dybs+ligvRXJdpvugWvNjzfZtMfHjdKBsjZZlz26j4juh cC0Q== 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 m30si13590382pff.158.2018.12.02.16.32.45; Sun, 02 Dec 2018 16:33:00 -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 S1725899AbeLCAcI (ORCPT + 99 others); Sun, 2 Dec 2018 19:32:08 -0500 Received: from mx2.suse.de ([195.135.220.15]:40780 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725785AbeLCAcI (ORCPT ); Sun, 2 Dec 2018 19:32:08 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 648BBAD90; Mon, 3 Dec 2018 00:32:02 +0000 (UTC) From: NeilBrown To: "J. Bruce Fields" , Chuck Lever , Jeff Layton , Trond Myklebust , Anna Schumaker Date: Mon, 03 Dec 2018 11:30:30 +1100 Subject: [PATCH 01/23] cred: add cred_fscmp() for comparing creds. Cc: Linux NFS Mailing List , linux-kernel@vger.kernel.org Message-ID: <154379703003.28598.12775774846678611265.stgit@noble> In-Reply-To: <154379689752.28598.6750646657534626618.stgit@noble> References: <154379689752.28598.6750646657534626618.stgit@noble> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org NFS needs to compare to credentials, to see if they can be treated the same w.r.t. filesystem access. Sometimes an ordering is needed when credentials are used as a key to an rbtree. NFS currently has its own private credential management from before 'struct cred' existed. To move it over to more consistent use of 'struct cred' we need a comparison function. This patch adds that function. Signed-off-by: NeilBrown --- include/linux/cred.h | 1 + kernel/cred.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/linux/cred.h b/include/linux/cred.h index 7eed6101c791..f1085767e1b3 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -169,6 +169,7 @@ extern int change_create_files_as(struct cred *, struct inode *); extern int set_security_override(struct cred *, u32); extern int set_security_override_from_ctx(struct cred *, const char *); extern int set_create_files_as(struct cred *, struct inode *); +extern int cred_fscmp(const struct cred *, const struct cred *); extern void __init cred_init(void); /* diff --git a/kernel/cred.c b/kernel/cred.c index ecf03657e71c..0b3ac72bd717 100644 --- a/kernel/cred.c +++ b/kernel/cred.c @@ -19,6 +19,7 @@ #include #include #include +#include #if 0 #define kdebug(FMT, ...) \ @@ -564,6 +565,60 @@ void revert_creds(const struct cred *old) } EXPORT_SYMBOL(revert_creds); +/** + * cred_fscmp - Compare two credentials with respect to filesystem access. + * @a: The first credential + * @b: The second credential + * + * cred_cmp() will return zero if both credentials have the same + * fsuid, fsgid, and supplementary groups. That is, if they will both + * provide the same access to files based on mode/uid/gid. + * If the credentials are different, then either -1 or 1 will + * be returned depending on whether @a comes before or after @b + * respectively in an arbitrary, but stable, ordering of credentials. + * + * Return: -1, 0, or 1 depending on comparison + */ +int cred_fscmp(const struct cred *a, const struct cred *b) +{ + struct group_info *ga, *gb; + int g; + + if (a == b) + return 0; + if (uid_lt(a->fsuid, b->fsuid)) + return -1; + if (uid_gt(a->fsuid, b->fsuid)) + return 1; + + if (gid_lt(a->fsgid, b->fsgid)) + return -1; + if (gid_gt(a->fsgid, b->fsgid)) + return 1; + + ga = a->group_info; + gb = b->group_info; + if (ga == gb) + return 0; + if (ga == NULL) + return -1; + if (gb == NULL) + return 1; + if (ga->ngroups < gb->ngroups) + return -1; + if (ga->ngroups > gb->ngroups) + return 1; + + for (g = 0; g < ga->ngroups; g++) { + if (gid_lt(ga->gid[g], gb->gid[g])) + return -1; + if (gid_gt(ga->gid[g], gb->gid[g])) + return 1; + } + return 0; +} +EXPORT_SYMBOL(cred_fscmp); + /* * initialise the credentials stuff */