Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1487244ybk; Thu, 14 May 2020 10:09:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6A0/N0Jr9wMF7vn9QqA3sjq5iS65pqgoPzVb63Gs823C1t6lLwM1eu5Ms0g1rdqbhY2fD X-Received: by 2002:a17:906:1e47:: with SMTP id i7mr4897938ejj.61.1589476170067; Thu, 14 May 2020 10:09:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589476170; cv=none; d=google.com; s=arc-20160816; b=AYgL/VVOzbplrAXvTe6wxHSBv7Ja4v5OBDZk5jPcjF0SKpJCaQrpqYZRbAl0A9L3up q/ZRUax+dIi2TbXuP64aNHUiRI4aDbexbSsJoj8TYBqXwgIvdZjBLx+YDiV292EjmtbL ubcw4MXeAiFrhtOE0SertyiPoQiRIDip5do2cgZkdTiLb16YcxXKgep91RC1GHXEKhj6 oBfjLRFJFY2Y3Xn0jRI/f5ALQHVwGGRoTDm9ThtBUMAuoIh41C+Fu5samemM+FhRW3H9 SnUqrIssZF3+1XM00gs3DwW23EIEcprIqUvbmiT1eTh377qnQPXG7H3nEZaD8m+UPBHE jMMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject:dkim-signature; bh=ywS9qtv92ULtIUX+IJVNzAMPyJHQMKJsF/Xjtg52W9o=; b=UG68A6glVTwZzwYsLRbENP8yGpcz9+LSL+sPBh4NSEWfGR6rgbBZWwS/UDbmRi+eDl HJ2jBDbcMBCi1rGDgfv6eKpGRr+sc5SxI4NuSPMvKPZboCRcYvfw9kdlv4H5oAuRJBO4 6dX4FEwhMf/mzsdHZIshtjQmgoiFjTkxNEZUQj0WWbobuTi4+e6mzuzxNwf7+DXQ6b4h znmKcg5HDWVq0JI5+9Cp+PtPtmX+A5/73zrdmtc/QREd4yaylTVm9mL8zYDVD0bnOntj cycHc4IyM2EV/7uKvh/gVftln0TFZVn8V8kcCfA9ZWxQOBiNdVzEI9+W4kR7PZhyfkMK I/TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=DQcrFaKS; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 4si2244707ejy.347.2020.05.14.10.09.05; Thu, 14 May 2020 10:09:30 -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=@yahoo.com header.s=s2048 header.b=DQcrFaKS; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726778AbgENRGj (ORCPT + 99 others); Thu, 14 May 2020 13:06:39 -0400 Received: from sonic309-27.consmr.mail.ne1.yahoo.com ([66.163.184.153]:36591 "EHLO sonic309-27.consmr.mail.ne1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726659AbgENRGf (ORCPT ); Thu, 14 May 2020 13:06:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1589475993; bh=ywS9qtv92ULtIUX+IJVNzAMPyJHQMKJsF/Xjtg52W9o=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From:Subject; b=DQcrFaKSIhsb21kjfO3auFpJWAvJmspFHvcXa6G+6brIJs0NKyfRa6J1NXix+z7LnVxhIVyzAMe4my677gX68anBQffl5aVuaPJCNKnN/oICaPnr9mLKesZmfITxn7OZIT8pwI15ox/K0CmLA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VEnqj5S7OjvO5rfhCU56jSI bg2HcTbf9Hf8NUyM827Vn8whgu5__6IO9QFX2nzrH0ozWHDcnxv_MOwyCLYu88tzWwyPgYEPZmZo 4iFwaPQjn2GL2YrYSLW.AbhkDCf4SD.2MZsjgjjGvIlrsn_ilJY_8NrO9m7bTfmrUc.3cIsJICbM hrwgtKY8KuX0Wd2bcAkGaRCZPWmx8N5_EP9HODzZ_UZ9HTqsszhSCnf0f5SiZKmJFL5umBxwZ5i5 s4g3_pee7a2zJt6xt2Df0Mtd5JD8k6jIJ02T2qTJ6tvV8WbIdOZMxAtVnDYRCmKRslDstzoxNneh 3.p64Ihom2wvZ24lO11WcHsCm85QBGIXDF4q50qeKkxh3xK6bo7nvSv_DWKbRpfqkfTZjXHsnnUn _hFiJRt3JS.aM3JOBfRMVBN_f75oxuoBKAhuG.AO4OYHEa5i4H65v5k8R3aCMa4dK7KBjJXPefDZ f9JaWPJxci2s8iPIBn_VOv11rEHG6unyzaQMsxkS6q2RDU5CQA60I_LiF4fNVaNOQGNVP86QtCbb c9nVVOqoQYhwbFBcoq2BFtOpZy8E- Received: from sonic.gate.mail.ne1.yahoo.com by sonic309.consmr.mail.ne1.yahoo.com with HTTP; Thu, 14 May 2020 17:06:33 +0000 Received: by smtp412.mail.ne1.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID fc92533db0efb0f134e119ba56e9dc08; Thu, 14 May 2020 17:06:29 +0000 (UTC) Subject: Re: [PATCH] keys: Move permissions checking decisions into the checking code To: David Howells , Stephen Smalley Cc: Jarkko Sakkinen , Paul Moore , keyrings@vger.kernel.org, SElinux list , LSM List , linux-kernel , Casey Schaufler References: <158932282880.2885325.2688622278854566047.stgit@warthog.procyon.org.uk> <3999877.1589475539@warthog.procyon.org.uk> From: Casey Schaufler Autocrypt: addr=casey@schaufler-ca.com; keydata= mQINBFzV9HABEAC/mmv3jeJyF7lR7QhILYg1+PeBLIMZv7KCzBSc/4ZZipoWdmr77Lel/RxQ 1PrNx0Ua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ikcef/E7HXQf/ij Dxf9HG2o5hOlMIAkJq/uLNMvABEBAAGJAjwEGAEIACYWIQQL720fVjJTAhDNQh44pRV8jEPI EQUCXNX0cAIbDAUJEswDAAAKCRA4pRV8jEPIEWkzEACKFUnpp+wIVHpckMfBqN8BE5dUbWJc GyQ7wXWajLtlPdw1nNw0Wrv+ob2RCT7qQlUo6GRLcvj9Fn5tR4hBvR6D3m8aR0AGHbcC62cq I7LjaSDP5j/em4oVL2SMgNTrXgE2w33JMGjAx9oBzkxmKUqprhJomPwmfDHMJ0t7y39Da724 oLPTkQDpJL1kuraM9TC5NyLe1+MyIxqM/8NujoJbWeQUgGjn9uxQAil7o/xSCjrWCP3kZDID vd5ZaHpdl8e1mTExQoKr4EWgaMjmD/a3hZ/j3KfTVNpM2cLfD/QwTMaC2fkK8ExMsz+rUl1H icmcmpptCwOSgwSpPY1Zfio6HvEJp7gmDwMgozMfwQuT9oxyFTxn1X3rn1IoYQF3P8gsziY5 qtTxy2RrgqQFm/hr8gM78RhP54UPltIE96VywviFzDZehMvuwzW//fxysIoK97Y/KBZZOQs+ /T+Bw80Pwk/dqQ8UmIt2ffHEgwCTbkSm711BejapWCfklxkMZDp16mkxSt2qZovboVjXnfuq wQ1QL4o4t1hviM7LyoflsCLnQFJh6RSBhBpKQinMJl/z0A6NYDkQi6vEGMDBWX/M2vk9Jvwa v0cEBfY3Z5oFgkh7BUORsu1V+Hn0fR/Lqq/Pyq+nTR26WzGDkolLsDr3IH0TiAVH5ZuPxyz6 abzjfg== Message-ID: Date: Thu, 14 May 2020 10:06:28 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <3999877.1589475539@warthog.procyon.org.uk> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-Mailer: WebService/1.1.15941 hermes_yahoo Apache-HttpAsyncClient/4.1.4 (Java/11.0.6) Content-Length: 5541 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/14/2020 9:58 AM, David Howells wrote: > How about this then? > > David > --- > commit fa37b6c7e2f86d16ede1e0e3cb73857152d51825 > Author: David Howells > Date: Thu May 14 17:48:55 2020 +0100 > > keys: Move permissions checking decisions into the checking code > > Overhaul the permissions checking, moving the decisions of which permits to > request for what operation and what overrides to allow into the permissions > checking functions in keyrings, SELinux and Smack. > > To this end, the KEY_NEED_* constants are turned into an enum and expanded > in number to cover operation types individually. > > Note that some more tweaking is probably needed to separate kernel uses, > e.g. AFS using rxrpc keys, from direct userspace users. > > Some overrides are available and this needs to be handled specially: > > (1) KEY_FLAG_KEEP in key->flags - The key may not be deleted and/or things > may not be removed from the keyring. > > (2) KEY_FLAG_ROOT_CAN_CLEAR in key->flags - The keyring can be cleared by > CAP_SYS_ADMIN. > > (3) KEY_FLAG_ROOT_CAN_INVAL in key->flags - The key can be invalidated by > CAP_SYS_ADMIN. > > (4) An appropriate auth token being set in cred->request_key_auth that > gives a process transient permission to view and instantiate a key. > This is used by the kernel to delegate instantiation to userspace. > > Note that this requires some tweaks to the testsuite as some of the error > codes change. > > Signed-off-by: David Howells > Reported-by: Stephen Smalley > cc: Jarkko Sakkinen > cc: Paul Moore > cc: Stephen Smalley > cc: Casey Schaufler > cc: keyrings@vger.kernel.org > cc: selinux@vger.kernel.org > ... > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c > index 8c61d175e195..ac9c93c48097 100644 > --- a/security/smack/smack_lsm.c > +++ b/security/smack/smack_lsm.c > @@ -4230,13 +4230,15 @@ static void smack_key_free(struct key *key) > * smack_key_permission - Smack access on a key > * @key_ref: gets to the object > * @cred: the credentials to use > - * @perm: requested key permissions > + * @need_perm: requested key permission > * > * Return 0 if the task has read and write to the object, > * an error code otherwise > */ > static int smack_key_permission(key_ref_t key_ref, > - const struct cred *cred, unsigned perm) > + const struct cred *cred, > + enum key_need_perm need_perm, > + unsigned int flags) > { > struct key *keyp; > struct smk_audit_info ad; > @@ -4244,12 +4246,6 @@ static int smack_key_permission(key_ref_t key_ref, > int request = 0; > int rc; > > - /* > - * Validate requested permissions > - */ > - if (perm & ~KEY_NEED_ALL) > - return -EINVAL; > - > keyp = key_ref_to_ptr(key_ref); > if (keyp == NULL) > return -EINVAL; > @@ -4265,6 +4261,71 @@ static int smack_key_permission(key_ref_t key_ref, > if (tkp == NULL) > return -EACCES; > > + /* > + * Validate requested permissions > + */ > + switch (need_perm) { > + case KEY_NEED_ASSUME_AUTHORITY: > + return 0; > + > + case KEY_NEED_DESCRIBE: > + case KEY_NEED_GET_SECURITY: > + request |= MAY_READ; > + auth_can_override = true; > + break; > + > + case KEY_NEED_CHOWN: > + case KEY_NEED_INVALIDATE: > + case KEY_NEED_JOIN: > + case KEY_NEED_LINK: > + case KEY_NEED_KEYRING_ADD: > + case KEY_NEED_KEYRING_CLEAR: > + case KEY_NEED_KEYRING_DELETE: > + case KEY_NEED_REVOKE: > + case KEY_NEED_SETPERM: > + case KEY_NEED_SET_RESTRICTION: > + case KEY_NEED_UPDATE: > + request |= MAY_WRITE; > + break; > + > + case KEY_NEED_INSTANTIATE: > + auth_can_override = true; > + break; > + > + case KEY_NEED_READ: > + case KEY_NEED_SEARCH: > + case KEY_NEED_USE: > + case KEY_NEED_WATCH: > + request |= MAY_READ; > + break; > + > + case KEY_NEED_SET_TIMEOUT: > + request |= MAY_WRITE; > + auth_can_override = true; > + break; > + > + case KEY_NEED_UNLINK: > + return 0; /* Mustn't prevent this; KEY_FLAG_KEEP is already > + * dealt with. */ > + > + default: > + WARN_ON(1); > + return -EINVAL; > + } > + > + /* Just allow the operation if the process has an authorisation token. > + * The presence of the token means that the kernel delegated > + * instantiation of a key to the process - which is problematic if we > + * then say that the process isn't allowed to get the description of > + * the key or actually instantiate it. > + */ > + if (auth_can_override && cred->request_key_auth) { > + struct request_key_auth *rka = > + cred->request_key_auth->payload.data[0]; > + if (rka->target_key == key) > + *_perm = 0; > + } > + > if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred)) > return 0; > > @@ -4273,10 +4334,6 @@ static int smack_key_permission(key_ref_t key_ref, > ad.a.u.key_struct.key = keyp->serial; > ad.a.u.key_struct.key_desc = keyp->description; > #endif > - if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW)) > - request |= MAY_READ; > - if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR)) > - request |= MAY_WRITE; > rc = smk_access(tkp, keyp->security, request, &ad); > rc = smk_bu_note("key access", tkp, keyp->security, request, rc); > return rc; Better. Thank you.