From patchwork Thu Jun 29 19:55:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 114418 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9879193vqr; Thu, 29 Jun 2023 13:01:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlHpu9kKJK3eWBABcJNBlsBKW+j2oU7NQ6mQLNeb/RhkggQiTXJnzCcOJnHH66KltLYj2t4n X-Received: by 2002:a05:6e02:549:b0:345:baef:842b with SMTP id i9-20020a056e02054900b00345baef842bmr187093ils.25.1688068911099; Thu, 29 Jun 2023 13:01:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688068911; cv=none; d=google.com; s=arc-20160816; b=n574IYtIwAcQrPVuP3kYEh3xbG10YPoZYqOa6jAx/Pj+djNAefgTgQfWQvUXP/I/UL 1PZbdlXbscNLLltwUYn1FzH+xodQUGMz4WG4MzMjsC6mAYa296LUbu12m229lj+h9ams ZzYDiWxY4m42etkutCx0mHud7BHkAs2cZvpsienGeAJ8ZyNZy4CKMqvR6iq75W/wprHq atHyVu7EwWO7a0qYASFujdlxI/eB6kURDcv13crVmbIgHxjLVsjtIZB6loWhv5Z2TffG kxDhMJ460tqB0YOtE/dRGSYGdBuB0zqULmzyw5fSI5N0EjoB5eTsvf4MxqDfpMvoNZ9N n6ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VDC7nSb24+TiQjjwtfbSyQDZ6atY+2bn2kkmV6iO130=; fh=7l6lIzbvUTVopTUGEbVTeJAEhLjNPwxt7k1ZzZ51Gbc=; b=yMdM4m+2IwSAf0BdL9PHyI4K7SV/4a0/RzX89yBRVBuNCJoBNjZWfGViiylMW1ujM8 eKR/l6Bti7T7TQmI7p39Kho/CuglrY84laUjtQ1IiZqXBz1yJr1WXkfEuLTzT9yVmH64 hJZG3EjaQYmhhYCyHyiaFn5q50Tc6EotqfteswgdNmVvzz24ajzyEZ9XG9OekCWuzrDU AgDimQxpZ7knb5JUlE8cKwwPsVDxrCmMrqEfPgMWRtMcWhJjdEFbfchAXRJcDoIj57wk MEHPkN5KV3IeG8ddfcEWlWEQP+sN2PbOqc+g14mdA9s5j1nkZJlk0hQh72oEmPgwHZQ+ 6X1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=X6utnhH2; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q11-20020a635c0b000000b00553ebb05d13si11349632pgb.65.2023.06.29.13.01.24; Thu, 29 Jun 2023 13:01:51 -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=X6utnhH2; 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 S232316AbjF2T7I (ORCPT + 99 others); Thu, 29 Jun 2023 15:59:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232201AbjF2T7B (ORCPT ); Thu, 29 Jun 2023 15:59:01 -0400 Received: from sonic309-27.consmr.mail.ne1.yahoo.com (sonic309-27.consmr.mail.ne1.yahoo.com [66.163.184.153]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 293DD2D5B for ; Thu, 29 Jun 2023 12:59:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1688068739; bh=VDC7nSb24+TiQjjwtfbSyQDZ6atY+2bn2kkmV6iO130=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=X6utnhH2CbigCO/704tZ8do7uKV7H7fEsx73z+2iwiXzeDWsMDi6m5k25n76XuBhRKlG50MTYUUVT/xpabMR6/MVbCMQW/pb89CGrpAv+SRUApmCDt7hybtevj16qgJZMQDkn1vMFe83wHgvpxK0qZx8iiQjd/k/XoxSwzHGmCzcbuH0jOuQCL41+6PGuhniVAgwrHJ/YEn6htOm19l6UfoEDytRDQjD8Oof5t8/sBvM4oPg04+uJX26CD8CZmDUuIZ1x2MotzgQYHLgyopvwOVtYO1a9sKRRoYDN8V/F+HaMd7f6DUoaiNUPmyfGyYfSnP0eHhOdYBgeKl4V3pWFA== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1688068739; bh=1DFK0hdy92/LnwyeY33pVbqXS24sXmzmoyFQ3Hl8eJF=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=IK1+wbfEXQ5n6/xIz0KHo8u1A4lGVMemlMepIgFg6/znIQYFjnrjEzmrbe9tczqzGqKVfyiBAELjY94nXjK/p6kp/83fgAlAfA/QyJkyEOhrdE1LXDI2OQOqa8ToMhm47uTaZYFbl4PyZJzSqt9CnzMYu9qZcfdF+SUeGbWUo1n3Ghoo8gT6BhCjsOcernXRTbKkMJlToaATOxOYyYja1hVxo6M/pEVNZLJ1SNpm8WJhNluomoDtEISHM+9AzJ+TgWHp//x2h2oq923jDNTeMTYlIkGk4ttjPZjZC4wlCHPzTYGgSLEqlgFzwgOfwI3cawWCOJdcxbZdQPv99QB5uA== X-YMail-OSG: VCkSsXEVM1m3jPoY8hz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7x9OrG6BljPba1Whd20FIL10CFt6aj5xv3FWJaV A2ZwkZ9zl.5T.Ld.e0coZ5iewHhn4o52IGlJ4R4ZgHGFTZjwBRPIxRY1HzoRWuz1qoht8QLziWbW XMBrO36oGMtkr5B1.BJr2_Wm9v_oGfc4Qlem5rDYQfEb4ggM7BCuL1nbbbgJqcwzVOOvV1iVP6PQ AqvbanouOvacBWKmVwws- X-Sonic-MF: X-Sonic-ID: a913fd76-249a-4faa-b59d-caf7740e2ff0 Received: from sonic.gate.mail.ne1.yahoo.com by sonic309.consmr.mail.ne1.yahoo.com with HTTP; Thu, 29 Jun 2023 19:58:59 +0000 Received: by hermes--production-bf1-5d96b4b9f-dbx7v (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 179208bf33e8d1ee67f1c353255c48d0; Thu, 29 Jun 2023 19:58:54 +0000 (UTC) From: Casey Schaufler To: casey@schaufler-ca.com, paul@paul-moore.com, linux-security-module@vger.kernel.org Cc: jmorris@namei.org, serge@hallyn.com, 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 Subject: [PATCH v12 07/11] LSM: Helpers for attribute names and filling lsm_ctx Date: Thu, 29 Jun 2023 12:55:31 -0700 Message-Id: <20230629195535.2590-8-casey@schaufler-ca.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230629195535.2590-1-casey@schaufler-ca.com> References: <20230629195535.2590-1-casey@schaufler-ca.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770068546438227270?= X-GMAIL-MSGID: =?utf-8?q?1770068546438227270?= Add lsm_name_to_attr(), which translates a text string to a LSM_ATTR value if one is available. Add lsm_fill_user_ctx(), which fills a struct lsm_ctx, including the trailing attribute value. All are used in module specific components of LSM system calls. Signed-off-by: Casey Schaufler Reviewed-by: Serge Hallyn --- include/linux/security.h | 14 +++++++++++++ security/lsm_syscalls.c | 24 ++++++++++++++++++++++ security/security.c | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/include/linux/security.h b/include/linux/security.h index 475d0abfebda..75ac91223c2d 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -32,6 +32,7 @@ #include #include #include +#include struct linux_binprm; struct cred; @@ -263,6 +264,7 @@ int unregister_blocking_lsm_notifier(struct notifier_block *nb); /* prototypes */ extern int security_init(void); extern int early_security_init(void); +extern u64 lsm_name_to_attr(const char *name); /* Security operations */ int security_binder_set_context_mgr(const struct cred *mgr); @@ -488,6 +490,8 @@ int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen); int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen); int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen); int security_locked_down(enum lockdown_reason what); +int lsm_fill_user_ctx(struct lsm_ctx __user *ctx, void *context, + size_t context_size, u64 id, u64 flags); #else /* CONFIG_SECURITY */ static inline int call_blocking_lsm_notifier(enum lsm_event event, void *data) @@ -505,6 +509,11 @@ static inline int unregister_blocking_lsm_notifier(struct notifier_block *nb) return 0; } +static inline u64 lsm_name_to_attr(const char *name) +{ + return LSM_ATTR_UNDEF; +} + static inline void security_free_mnt_opts(void **mnt_opts) { } @@ -1408,6 +1417,11 @@ static inline int security_locked_down(enum lockdown_reason what) { return 0; } +static inline int lsm_fill_user_ctx(struct lsm_ctx __user *ctx, void *context, + size_t context_size, u64 id, u64 flags) +{ + return -EOPNOTSUPP; +} #endif /* CONFIG_SECURITY */ #if defined(CONFIG_SECURITY) && defined(CONFIG_WATCH_QUEUE) diff --git a/security/lsm_syscalls.c b/security/lsm_syscalls.c index f03f2d17ab49..bc22f05e2d8c 100644 --- a/security/lsm_syscalls.c +++ b/security/lsm_syscalls.c @@ -17,6 +17,30 @@ #include #include +/** + * lsm_name_to_attr - map an LSM attribute name to its ID + * @name: name of the attribute + * + * Returns the LSM attribute value associated with @name, or 0 if + * there is no mapping. + */ +u64 lsm_name_to_attr(const char *name) +{ + if (!strcmp(name, "current")) + return LSM_ATTR_CURRENT; + if (!strcmp(name, "exec")) + return LSM_ATTR_EXEC; + if (!strcmp(name, "fscreate")) + return LSM_ATTR_FSCREATE; + if (!strcmp(name, "keycreate")) + return LSM_ATTR_KEYCREATE; + if (!strcmp(name, "prev")) + return LSM_ATTR_PREV; + if (!strcmp(name, "sockcreate")) + return LSM_ATTR_SOCKCREATE; + return LSM_ATTR_UNDEF; +} + /** * sys_lsm_set_self_attr - Set current task's security module attribute * @attr: which attribute to set diff --git a/security/security.c b/security/security.c index 199db23581f1..72ad7197b2c9 100644 --- a/security/security.c +++ b/security/security.c @@ -770,6 +770,50 @@ static int lsm_superblock_alloc(struct super_block *sb) return 0; } +/** + * lsm_fill_user_ctx - Fill a user space lsm_ctx structure + * @ctx: an LSM context to be filled + * @context: the new context value + * @context_size: the size of the new context value + * @id: LSM id + * @flags: LSM defined flags + * + * Fill all of the fields in a user space lsm_ctx structure. + * Caller is assumed to have verified that @ctx has enough space + * for @context. + * + * The total length is padded to a multiple of 64 bits to + * accomodate possible alignment issues. + * + * Returns 0 on success, -EFAULT on a copyout error, -ENOMEM + * if memory can't be allocated. + */ +int lsm_fill_user_ctx(struct lsm_ctx __user *ctx, void *context, + size_t context_size, u64 id, u64 flags) +{ + struct lsm_ctx *lctx; + size_t locallen = struct_size(lctx, ctx, context_size); + int rc = 0; + + lctx = kzalloc(locallen, GFP_KERNEL); + if (lctx == NULL) + return -ENOMEM; + + lctx->id = id; + lctx->flags = flags; + lctx->ctx_len = context_size; + lctx->len = locallen; + + memcpy(lctx->ctx, context, context_size); + + if (copy_to_user(ctx, lctx, locallen)) + rc = -EFAULT; + + kfree(lctx); + + return rc; +} + /* * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and * can be accessed with: