Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp6024245rwe; Tue, 18 Apr 2023 15:36:47 -0700 (PDT) X-Google-Smtp-Source: AKy350bqX5nSz8deOQH8/ASPWbMHBdYt+J9BH7quD1aeb1YS5ZKDAuxeLY1NQIXzSLozJsbmvctR X-Received: by 2002:a17:902:f78e:b0:1a4:f1aa:3ea8 with SMTP id q14-20020a170902f78e00b001a4f1aa3ea8mr3119310pln.48.1681857407116; Tue, 18 Apr 2023 15:36:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681857407; cv=none; d=google.com; s=arc-20160816; b=SQvBQqC8q6P/J2qChj+4cHA6U/bApYZ+KkD5BfJ2MKpnMlIB67z4Zo+Wb7lXzy8WU6 KzKsR7/SOTxE/B9TwhUdt/KkyKTEQP+wtTse5L1UtZEoImO3wXX2di2vHBxR/pKq2UmD IZ06ImDwKqYUCOwDZV6cjW1+zK3CNBes8ILiCJbBukVHIxN9xKrwWRNk0T/3Fks+Hnsv XOtMmT90jgPY73q7kBsDYDHA6r+QEPM4DS6gQ+FuA8UYVitRpVjIcBuFBIgdNl6tDiyd GEjMJ/Iw/DY6gOdMb7awmko3o+bL+SneyW5hF/E0DUoF0XFUeq68JRX8VJWGLHKxdfhn nhPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=JGyo6apYPiGSMudiokyK9sWE7nWevsztQxXGHF+lHd4=; b=jzeUgsNDzqKuGUDLnozldA01TeBi/07EZb7yGZZDbbcGeRRtqx8wTylTeH23oiiE52 5WrqtUvsyQOwhFj9Jrtn4vb2SA2ritURn2I0IqlsNhtgEohCSPpcm/Q2d0caB6+jlB24 7fvjq60NninqE8l5PkJBOGbZpH6Gnxyi6NusL1AX6n3D24rYEux71TtKOsRTi1Nxox/g HkFsMAlUeYXf3Hs1UzwMhfsDn/XW7RUUKPLdwXBl0qbiPjcIBQmi+sk3LUmPr/I1TGM9 ObeZsESVLo431MWYYsV2FosC9nDUwTMEnHD+DGKlfam0e8GTLDRaw2OPeRBLfzpxRPl+ qLhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=tTkBViIT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c16-20020a170903235000b0019f359c651esi15517855plh.556.2023.04.18.15.36.32; Tue, 18 Apr 2023 15:36:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=tTkBViIT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229940AbjDRWeU (ORCPT + 99 others); Tue, 18 Apr 2023 18:34:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230523AbjDRWeS (ORCPT ); Tue, 18 Apr 2023 18:34:18 -0400 Received: from sonic307-16.consmr.mail.ne1.yahoo.com (sonic307-16.consmr.mail.ne1.yahoo.com [66.163.190.39]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3326525C for ; Tue, 18 Apr 2023 15:34:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1681857255; bh=JGyo6apYPiGSMudiokyK9sWE7nWevsztQxXGHF+lHd4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From:Subject:Reply-To; b=tTkBViITtOunQ7sPoIGZb7w5/W/A6Lg/6iIrMoJpj4BPvQujVyMdxrnJxugJsbAYy9Dez1pHBV2oGwCR+I6Qzs2MXPHLmc20xBI2dY81SbB0RZ286csdN1QNMa1kD4jMBtxahmjaI/hmRHivp5AoWu/pao/4Ddn/ldB3Ue3PQA7DQM5xQ20wSXjKDS5R+ltZj+RtStVzlwHVY2Lr9cvir9zhL6HO5fXvEeJ7hGYmSVcLQ9Z9ZGa+RK5tz4tQuxn7Zv00mJff0JvuPseqITbHOXuAxCvaxPEF0i7QkWeUQBZU68g1xq7SFJGeV/MaTLBHaIrkSnxqMO3p1Bp/MZOJiA== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1681857255; bh=Hc4eMqTDSPFU7QKxQEO+rukFDu1y2dIm99jZIwfF3I3=; h=X-Sonic-MF:Date:Subject:To:From:From:Subject; b=JYJMS1ReqzCWu6lO0D1RLdDBn5QiGJxxYAk610JHwTxZafWAkwjx++VczR1VQInh2/6E/rZaVX5L+yxoYMRggY0pFvBySEsZhrN4zXClRdU0PudEOnrv0xQiyJYbTs8Ve4t9g2dENp+TGnghuc7kGG+rStTmk/l1yvaoayU11rRrBeNuNe16B/Zk7mPlmjEC5NXK9nXCNeOSxoNHH6jfVMjpijMI+ZgraLYxKq+QsYuNcW5VlksX1eV87dkv7Z//f3Z8/v5uPixGe56P2mA+FrwdkcOpcTdZ0kGu1aiAFAwME934oogssj080IpeBWsxiLI2bzSZJBgOBfsNrjd5WA== X-YMail-OSG: yg6JgrYVM1lQdNBszxHPQr77pat9lNhnrJSVYxOmJAHfNOrS1Svx95yKDYqs1Nx srM4RBCvXHSayFf6MVmucjsyIjb4deMestmLO10N4jmf7Jetl7ydlXMvBbZB.XxA6aUaKBFFO.jI xdtQ.WOcQ.EbqFlEe_4_GPWPvbUHDowYAM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soMUyGRXBO.Ul QjdV2ZPo31A-- X-Sonic-MF: X-Sonic-ID: bf72be51-c9d6-4989-9fc1-077dae8489aa Received: from sonic.gate.mail.ne1.yahoo.com by sonic307.consmr.mail.ne1.yahoo.com with HTTP; Tue, 18 Apr 2023 22:34:15 +0000 Received: by hermes--production-bf1-5f9df5c5c4-5d8pl (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 8df9cfeaa30a726e1a994a50b769cc8a; Tue, 18 Apr 2023 22:34:10 +0000 (UTC) Message-ID: <5f7ee090-1708-c5ac-d194-1cc5c69487a6@schaufler-ca.com> Date: Tue, 18 Apr 2023 15:34:07 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v8 04/11] LSM: syscalls for current process attributes Content-Language: en-US To: Paul Moore Cc: linux-security-module@vger.kernel.org, jmorris@namei.org, keescook@chromium.org, john.johansen@canonical.com, penguin-kernel@i-love.sakura.ne.jp, stephen.smalley.work@gmail.com, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, mic@digikod.net, Casey Schaufler References: <20230411155921.14716-1-casey@schaufler-ca.com> <20230411155921.14716-5-casey@schaufler-ca.com> From: Casey Schaufler In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailer: WebService/1.1.21365 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/18/2023 2:49 PM, Paul Moore wrote: > On Tue, Apr 11, 2023 at 12:01 PM Casey Schaufler wrote: >> Create a system call lsm_get_self_attr() to provide the security >> module maintained attributes of the current process. >> Create a system call lsm_set_self_attr() to set a security >> module maintained attribute of the current process. >> Historically these attributes have been exposed to user space via >> entries in procfs under /proc/self/attr. >> >> The attribute value is provided in a lsm_ctx structure. The structure >> identifies the size of the attribute, and the attribute value. The format >> of the attribute value is defined by the security module. A flags field >> is included for LSM specific information. It is currently unused and must >> be 0. The total size of the data, including the lsm_ctx structure and any >> padding, is maintained as well. >> >> struct lsm_ctx { >> __u64 id; >> __u64 flags; >> __u64 len; >> __u64 ctx_len; >> __u8 ctx[]; >> }; >> >> Two new LSM hooks are used to interface with the LSMs. >> security_getselfattr() collects the lsm_ctx values from the >> LSMs that support the hook, accounting for space requirements. >> security_setselfattr() identifies which LSM the attribute is >> intended for and passes it along. >> >> Signed-off-by: Casey Schaufler >> --- >> Documentation/userspace-api/lsm.rst | 15 +++++ >> include/linux/lsm_hook_defs.h | 4 ++ >> include/linux/lsm_hooks.h | 9 +++ >> include/linux/security.h | 19 ++++++ >> include/linux/syscalls.h | 5 ++ >> include/uapi/linux/lsm.h | 30 +++++++++ >> kernel/sys_ni.c | 4 ++ >> security/Makefile | 1 + >> security/lsm_syscalls.c | 55 ++++++++++++++++ >> security/security.c | 98 +++++++++++++++++++++++++++++ >> 10 files changed, 240 insertions(+) >> create mode 100644 security/lsm_syscalls.c > .. > >> diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h >> index 33a0ee3bcb2e..97487d66dca9 100644 >> --- a/include/linux/syscalls.h >> +++ b/include/linux/syscalls.h >> @@ -71,6 +71,7 @@ struct clone_args; >> struct open_how; >> struct mount_attr; >> struct landlock_ruleset_attr; >> +struct lsm_ctx; >> enum landlock_rule_type; >> >> #include >> @@ -1058,6 +1059,10 @@ asmlinkage long sys_memfd_secret(unsigned int flags); >> asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len, >> unsigned long home_node, >> unsigned long flags); >> +asmlinkage long sys_lsm_get_self_attr(unsigned int attr, struct lsm_ctx *ctx, >> + size_t *size, __u32 flags); >> +asmlinkage long sys_lsm_set_self_attr(unsigned int attr, struct lsm_ctx *ctx, >> + __u32 flags); > As pointed out by the kernel test robot, the above declaration is > missing the @size parameter. Yup. >> diff --git a/include/uapi/linux/lsm.h b/include/uapi/linux/lsm.h >> index f27c9a9cc376..b10dfab8a4d9 100644 >> --- a/include/uapi/linux/lsm.h >> +++ b/include/uapi/linux/lsm.h >> @@ -9,6 +9,36 @@ >> #ifndef _UAPI_LINUX_LSM_H >> #define _UAPI_LINUX_LSM_H >> >> +#include >> +#include >> + >> +/** >> + * struct lsm_ctx - LSM context information >> + * @id: the LSM id number, see LSM_ID_XXX >> + * @flags: LSM specific flags >> + * @len: length of the lsm_ctx struct, @ctx and any other data or padding >> + * @ctx_len: the size of @ctx >> + * @ctx: the LSM context value >> + * >> + * The @len field MUST be equal to the size of the lsm_ctx struct >> + * plus any additional padding and/or data placed after @ctx. >> + * >> + * In all cases @ctx_len MUST be equal to the length of @ctx. >> + * If @ctx is a string value it should be nul terminated with >> + * @ctx_len equal to `strlen(@ctx) + 1`. Binary values are >> + * supported. >> + * >> + * The @flags and @ctx fields SHOULD only be interpreted by the >> + * LSM specified by @id; they MUST be set to zero/0 when not used. >> + */ >> +struct lsm_ctx { >> + __u64 id; >> + __u64 flags; >> + __u64 len; >> + __u64 ctx_len; >> + __u8 ctx[]; >> +}; > Sorry, style nitpick since this needs to be respun anyway for the > syscalls.h fix at the very least ... I *really* dislike when variable > declarations, and field declarations in the case composite variables, > are aligned with the vertically neighboring declarations; just use a > single space please: I'll do it, but the tab after type has been accepted for forever. As an aside, I pulled out my 1978 K&R to prove my point and discovered that it isn't consistent regarding this style. > struct lsm_ctx { > __u64 id; > __u64 flags; > __u64 len; > __u64 ctx_len; > __u8 ctx[]; > } > >> diff --git a/security/security.c b/security/security.c >> index 38ca0e646cac..bfe9a1a426b2 100644 >> --- a/security/security.c >> +++ b/security/security.c >> @@ -2167,6 +2167,104 @@ void security_d_instantiate(struct dentry *dentry, struct inode *inode) >> } >> EXPORT_SYMBOL(security_d_instantiate); >> >> +/** >> + * security_getselfattr - Read an LSM attribute of the current process. >> + * @attr: which attribute to return >> + * @ctx: the user-space destination for the information, or NULL >> + * @size: the size of space available to receive the data >> + * @flags: reserved for future use, must be 0 >> + * >> + * Returns the number of attributes found on success, negative value >> + * on error. @size is reset to the total size of the data. >> + * If @size is insufficient to contain the data -E2BIG is returned. >> + */ >> +int security_getselfattr(unsigned int __user attr, struct lsm_ctx __user *ctx, >> + size_t __user *size, u32 __user flags) >> +{ >> + struct security_hook_list *hp; >> + u8 __user *base = (u8 __user *)ctx; >> + size_t total = 0; >> + size_t entrysize; >> + size_t left; >> + bool toobig = false; >> + int count = 0; >> + int rc; >> + >> + if (attr == 0) >> + return -EINVAL; >> + if (flags) >> + return -EINVAL; > I like Mickaël's idea of supporting a flag (LSM_FLG_SINGLE?) which > allows one to request a single LSM's attribute. I don't, but I'll incorporate it. > I don't think that > support has to be part of this initial patchset, but I do think it > would be good to have it in the same PR that goes up to Linus during > the merge window. As this patch set is intended to be what goes to Linus (isn't it?) I'll put it in. > If that's not something you want to do, let me know > and I'll write up a quick patch on top of this patchset. > >> + if (size == NULL) >> + return -EINVAL; >> + if (get_user(left, size)) >> + return -EFAULT; >> + >> + hlist_for_each_entry(hp, &security_hook_heads.getselfattr, list) { >> + entrysize = left; >> + if (base) >> + ctx = (struct lsm_ctx __user *)(base + total); >> + rc = hp->hook.getselfattr(attr, ctx, &entrysize, flags); >> + if (rc == -EOPNOTSUPP) { >> + rc = 0; >> + continue; >> + } >> + if (rc == -E2BIG) { >> + toobig = true; >> + left = 0; >> + break; > It just occurred to me while reading this that we stop calculating the > potential lsm_ctx size after we hit the first LSM where we go beyond > the size given, `rc == -E2BIG`. I realize that the required size may > change between calls to lsm_get_self_attr(2), but it seems like we > should at least run through all the LSMs and total up the required > buffer size, no? The "break" should be a "continue". Artifact of an earlier version that had a switch statement. Fix forthcoming. > > I may have missed something in the snippet below, but I think the code > change should be pretty minor: > > if (rc == -EOPNOTSUPP) { > rc = 0; > continue; > } else if (rc == -E2BIG) { > toobig = true; > base = NULL; > } else if (rc < 0) { > return rc; > } > left = (toobig ? 0 : left - entrysize); > total += entrysize; > > count += rc; > >> + } >> + if (rc < 0) >> + return rc; >> + >> + left -= entrysize; >> + total += entrysize; >> + count += rc; >> + } >> + if (count == 0) >> + return LSM_RET_DEFAULT(getselfattr); >> + if (put_user(total, size)) >> + return -EFAULT; >> + if (toobig) >> + return -E2BIG; >> + return count; >> +} > -- > paul-moore.com