Received: by 10.223.176.5 with SMTP id f5csp891379wra; Tue, 30 Jan 2018 21:33:34 -0800 (PST) X-Google-Smtp-Source: AH8x225N5qI7LgQqKsBpZpABjOwAaUdl0OBnx3BnsCXjL7XGkg1aPDtiXvZV/iOCL1NPro4Rn1SN X-Received: by 10.98.15.27 with SMTP id x27mr33060875pfi.197.1517376814417; Tue, 30 Jan 2018 21:33:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517376814; cv=none; d=google.com; s=arc-20160816; b=PExtUzgjoOfh4Lc2WbZzcfMbMk3SEuMbccJVP3piIVKjWAJqdSp89BrGucZ7hlXer1 9dgIOBC0msL6h+86Qda/Gjqloysqp+Dau+U4bMEdZnglXeFyju8NfiwiBEvHR/rnrqGX KhGmaCx944vaIHVWZw6JrCTvwREZxcbiUhumFZ4IlZn1M4awI75CH+trJEDE0uzhHd1S cqeQapDDIqG8XtkFdxFvCfZxlxGrsmIOHmLMSwlZHkhdAOrDsqBwbDNsPrvG/4qDUvBH EmWWvWjRcxSJkJI8kr1U9tpWxG86ex6c/DwZeJuX7XQsbtt+ydZPTUjWerc8h5WKzbYK GHBA== 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:arc-authentication-results; bh=DGNF/jk663eMkFqB+zWbtLU1u7hjabIFqHAYzbO4AQs=; b=oR8UyjQouQx6as9EdsOB2OyRmUs6XQ3uPX7RPG0OqH1dsDtCPnc81h7AiB2sUJGq7z fuKHzwGj59tR1p7GThpmN9xBhNzp3CHqoaqESUneyxRnHJ0L31dilsBXv7CJn0J+p6Q3 uG3bjih8YONpDdYxrPt5liRy4vJKl6fZvA4X3Ar5LJAHHqt0On9vkaQiNdmAjNI3migI 22vqskMK4l1CUVECW9tCC6T31w9b6veoJq3MakdZsGaWRiFzKCVzrhJLNY5FsdYIceLB 0vRhvKzN/USUZy+80PlPrn0tWWVHx0bQXNRQOdBzKo2qYawc5P62ISoYlUnDgErBF+nb v2Iw== 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 k91-v6si64498pld.129.2018.01.30.21.33.20; Tue, 30 Jan 2018 21:33:34 -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 S1752276AbeAaFQk (ORCPT + 99 others); Wed, 31 Jan 2018 00:16:40 -0500 Received: from mx2.suse.de ([195.135.220.15]:35047 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751728AbeAaFQf (ORCPT ); Wed, 31 Jan 2018 00:16:35 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 75883AB40; Wed, 31 Jan 2018 05:16:34 +0000 (UTC) From: NeilBrown To: David Howells , Andrew Morton , Ingo Molnar , Anna Schumaker Date: Wed, 31 Jan 2018 16:15:39 +1100 Subject: [PATCH 1/4] cred: add cred_fscmp() for comparing creds. Cc: NFS , lkml Message-ID: <151737573947.14845.5248732383783116263.stgit@noble> In-Reply-To: <151737571564.14845.2874586176125198504.stgit@noble> References: <151737571564.14845.2874586176125198504.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 current 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 631286535d0f..6dd51e503f23 100644 --- a/include/linux/cred.h +++ b/include/linux/cred.h @@ -164,6 +164,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..4ce75c6fb752 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 to 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 */