From patchwork Fri Apr 28 20:26:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 13226791 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 602B0C7EE22 for ; Fri, 28 Apr 2023 20:31:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346583AbjD1UbR (ORCPT ); Fri, 28 Apr 2023 16:31:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345702AbjD1UbO (ORCPT ); Fri, 28 Apr 2023 16:31:14 -0400 Received: from sonic317-38.consmr.mail.ne1.yahoo.com (sonic317-38.consmr.mail.ne1.yahoo.com [66.163.184.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 796893A8D for ; Fri, 28 Apr 2023 13:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1682713810; bh=lQV4qS4iAA6JHAaUPQfN8v1+i+4alOLehWL92wPtJI8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=VJMSF+G63FPYv+EzPs4zgyGfUwYljnMv//V5Q8xFlRuf0RfsPI6DPfdhzEsf+8a4A/yLxk280mu4AQZDfu6Eg7V2i+EFcpWryVihv9cpYG4zExqU+nUq8IfqOmCkgjagvU2G6w2Ioo+ycIxOrSrmV2dliE1Xw90Qe4gnqrbmVLyX/H2Ec437MddkZucXWiVQjKgsDSSaUhIdBjEEqGroK1yMpemUIIQHmTgPxEscmV97oQyi16MSumAq7EcjCnobc/A4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Zpgz2w6Nj2G4Sq6EWZONyBI0edXP60.OyGrCv5az.zV iDfJUVnDafE54VVQ.B7DXJlIS9MEB3o9D8HZp4ld5oGF3enE37jgY0.RkKoJA0k.6jfmNzFXlLbb G8gWPELObpChuPLSnnnGOSIqnkdrntZx4OsnqzR2LtVq8xeYWB9mkolZURiynOWpaweZnejABXxP 59ID.qg10caDyXiu6D__SoJqPVs9lo17oJUYBKEaKaeMxG2cmCXeBjvN2OWVCdq3TgYKXFx2S_Vs _OIKLboHi2MYLMepzSbhEPfSFIGDuobhXg8bve0bMEOTW2iSyOInZwDYijaXAnStmfQmmdOu4m7J j1i7jYJDB8uR3PHXitCgaWAxTfEm4CsiXsZBu82Ndm2s3rht6YR5eY288qrgH1TG63WRPtNyNsRY q.I3K0QlzX3Cbs2PDP7ynKC7DlvhRtDtLlg15Ret4BWztAp1dyfdBw0l2_iOeEqefa7u7RwHOvH5 p9TAZNHMgc8e4ZGWAipysChpqkxc1ZyRqbsvKT3iQ0TK6gFamWZG1mStQnaAvMbvocn9ovVZ3302 f9tL_nusPwterIbt9QUope1iacihD.MtN6IxGNz7fnJI2f0WuhNXr1w1LkcqiOoFva4nOXbP_NEv 83rnJJxYhG9AZiCrjM0Hw7VbQJDlYPpznkGFOIMmPTBQFiAXiEKlLr73gn8hteQ5849bHsbBYu78 NvMr.pMyOeAK8A8mr0S8Sii0PoGzlQ60lEy7EEf3xpsdeokfAwjWqRhn4nAgvzyKv_WgveQSYEC_ w78aGovMgsFHwTAL.QPw- X-Sonic-MF: X-Sonic-ID: 624ab670-8d7e-4b30-b922-5cafcc9fef08 Received: from sonic.gate.mail.ne1.yahoo.com by sonic317.consmr.mail.ne1.yahoo.com with HTTP; Fri, 28 Apr 2023 20:30:10 +0000 Received: by hermes--production-ne1-7dbd98dd99-84p8v (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 16afe408a2c159011f8a869d3993f797; Fri, 28 Apr 2023 20:30:09 +0000 (UTC) From: Casey Schaufler To: casey@schaufler-ca.com, paul@paul-moore.com, linux-security-module@vger.kernel.org Cc: 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 Subject: [PATCH v10 07/11] LSM: Helpers for attribute names and filling lsm_ctx Date: Fri, 28 Apr 2023 13:26:47 -0700 Message-Id: <20230428202651.159828-8-casey@schaufler-ca.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230428202651.159828-1-casey@schaufler-ca.com> References: <20230428202651.159828-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. The .len value is padded to a multiple of 64 bits for alignment. All are used in module specific components of LSM system calls. Signed-off-by: Casey Schaufler --- include/linux/security.h | 13 ++++++++++++ security/lsm_syscalls.c | 24 ++++++++++++++++++++++ security/security.c | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/include/linux/security.h b/include/linux/security.h index 806bff425af9..36ace59f9171 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -263,6 +263,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); @@ -491,6 +492,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) @@ -508,6 +511,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) { } @@ -1420,6 +1428,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 b89cccb2f123..0b225adfe5f7 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 94b78bfd06b9..8c877d639cae 100644 --- a/security/security.c +++ b/security/security.c @@ -761,6 +761,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 = ALIGN(struct_size(lctx, ctx, context_size), 8); + 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: