From patchwork Fri Jun 16 16:50:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 13283035 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61ACEEB64D7 for ; Fri, 16 Jun 2023 16:56:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345929AbjFPQ4d (ORCPT ); Fri, 16 Jun 2023 12:56:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345926AbjFPQ4H (ORCPT ); Fri, 16 Jun 2023 12:56:07 -0400 Received: from sonic313-14.consmr.mail.ne1.yahoo.com (sonic313-14.consmr.mail.ne1.yahoo.com [66.163.185.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F423449F7 for ; Fri, 16 Jun 2023 09:54:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1686934459; bh=hkkICAMFOno7zoAkCOb8c92hBVZqHMjIHuFkSovWfEs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=AD8JUZakTlHp0laJVuze9Ur5+xKR8xJm6iowsiHPoODOZO9ULuUYzhAUWc4Q0lPZ7VktMdTGUS2FiSUroUYv4/mY9J/H8NMrUlumA0aZjDjcwVadDma/F0hsKTpDulTwtPylYM8sXFwzXd6e8IExqjcoFm4TeDSmAAf1wX4JNrIJsc1SrEhYmKH93vb31ZHR1R34N0kon0f/W+09NCsTwqkmTX6jV89ruuACISwguED/S+PxOipAH4f2s1Qj1T6RO8jnj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9zv88Wf.kbGqC186Pr_qaNBmAUqH 8MyspH3NeYuFQT_eNVVlWOXkQBrsdzsSajiF664lvWIoMjbEPXgaGm7rWK2TpYO72lpnvRv_OewD YiX6j9_TyLbECEux2ej6aPDQrXweY7B8HpuX_S_xVEAyIKd9R.B_nAazCVmBbLa0KD.369BIH2hd GEA8Tp8leU0aFnaYCE6nI6Ri9lrg6N_vpkkYGix3a7PIx_5PJds.IdqkwJJIVQoqq7PZcBaIzXeL fWqblTm7P.lbZLwHnFPiTdzqpizbtTd7JMQx0kquxU84HRwd2i4SbJvwOkMr9AsINgnBJNCQQpgp xNfUEbRN3NJecMmSJbTmlV5pt8SS08wTd5if34wnxB2vb6c1Us25Pcu4lLBzKvVGqxf3c3Ey9FHT FoKxP4uAuhJY1XWtCZWOZzVKL.CTrIlDPJ_1IYI7LFDXOSYHhAhmxb_K4XqrgXu7k.kdCS9sOosz qXDnlBIsSXLNL.P.TCRbmSYSwm6ep.BNwjpjZjDfAdhT3PUDp0yZCSLB1qjnHW3uDdeoGCsZoVn4 pH0g.KtQcXBq.fXc9kjnE1K9bkxTgzLZmegjwPnXW7rplItUDZE51kZLEkPt33O.w_WTJx_wF2Wv tx1yqT5rrT3XPHr02Hfmho2hJibi1BhrR1DCq3xGvVHYkMoKllUI2NN0lCWy9IDJXVCcFORr5n4E cAlAMqijw8scPVIRR8MnXHAyR77fmz7c_alRAF969RD0_Q44pMN27wRJgwyoi7CSXa43DrSVMEpr eru0cdP2WQAv7X_MSAv8Y X-Sonic-MF: X-Sonic-ID: d358cff8-b066-4f37-be52-f5d74aed472e Received: from sonic.gate.mail.ne1.yahoo.com by sonic313.consmr.mail.ne1.yahoo.com with HTTP; Fri, 16 Jun 2023 16:54:19 +0000 Received: by hermes--production-bf1-54475bbfff-xmg9w (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 5d8e982db405aaa142ed4c0fa62627c5; Fri, 16 Jun 2023 16:54:15 +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 v11 07/11] LSM: Helpers for attribute names and filling lsm_ctx Date: Fri, 16 Jun 2023 09:50:51 -0700 Message-Id: <20230616165055.4705-8-casey@schaufler-ca.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230616165055.4705-1-casey@schaufler-ca.com> References: <20230616165055.4705-1-casey@schaufler-ca.com> MIME-Version: 1.0 Precedence: bulk List-ID: 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 Reviewed-by: Kees Cook --- 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 ca196b585996..3d8c3c3a6d92 100644 --- a/security/security.c +++ b/security/security.c @@ -769,6 +769,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: