Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5723891rwl; Tue, 11 Apr 2023 09:07:21 -0700 (PDT) X-Google-Smtp-Source: AKy350boGBiGAVUF2tGiocOj6cLuo8SIPSbAwzplXLD586qrfiwUtilDmw2dB9p77Su3Z07FVLEJ X-Received: by 2002:a17:902:da8d:b0:1a0:42c0:b2a5 with SMTP id j13-20020a170902da8d00b001a042c0b2a5mr4950640plx.24.1681229240676; Tue, 11 Apr 2023 09:07:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681229240; cv=none; d=google.com; s=arc-20160816; b=B3adcoWk7X85sW0y2NM6DkYm4JerZNL6WAqm6SPgSqJyeUY9Anla6Wi7uVVisJLmzc MIpLA7mrw3CovANJCmnoEFT0GbVXy+1Jv9Fx/O+h3cJrgx9uzptDndAEtQvr5IYJC60Q oUVcXTdZRRTjqmy2/3wVFOEAaX5OgH4ViQGW+UC2xeZ7ZesJ+MLsDxtyhw8kLa2rNTMb QvrOeNM0Cwy1EyinO8YsiJRUb4QhcJrrYjYIAxrHRU/QqfkcbMOLGOf+P/freQIrd4pE 3hUzsUmeHyk6+aRX+iUcQiuiPRPmA6oMeTNkbiFxg5KyWI80u/4UJmfZBvbC5GEkdVOe P9Fw== 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=eVJCxni8t17GQw5ZIij4Oj+s+3OY2bEqnDuxl2+sidc=; b=UGsZwMhpOuQE6UaOoVtbX5mZhej2AH+n3cERX+VnUqX5qEKKovaiwkuRQ0bQ9nfixw 3NyvNzbTglQKkacyAdg0+YPzabyPjauho7o5Gh23wgaHQnxnjrgrlavHn5jTk3BuxfXv U+CYMZxh7tJqHkW9gH1YvRjvWsCx5CaibPLPUhMs9P+0qLcrifB00os6iKEafs6JFCho bybo6YO4gfLfMuLReDj7wwZJY4e6X/qY6rqyoNeUT+tqm0/eBrxFhVldEJ8mD2i3WBJG fe/yu6ULKTU1E64Qhboaht1fFF+T3ICnRWEES9561sMiyipHD7gx6oziMxDGQ6oFt+xk 999A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=pt1HehdK; 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 a70-20020a639049000000b00518a1800624si8539847pge.307.2023.04.11.09.07.06; Tue, 11 Apr 2023 09:07:20 -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=pt1HehdK; 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 S229581AbjDKQDJ (ORCPT + 99 others); Tue, 11 Apr 2023 12:03:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229765AbjDKQCx (ORCPT ); Tue, 11 Apr 2023 12:02:53 -0400 Received: from sonic307-15.consmr.mail.ne1.yahoo.com (sonic307-15.consmr.mail.ne1.yahoo.com [66.163.190.38]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72B465B8F for ; Tue, 11 Apr 2023 09:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1681228967; bh=eVJCxni8t17GQw5ZIij4Oj+s+3OY2bEqnDuxl2+sidc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=pt1HehdKWtsmzyalhzLfpEYh7OEw90jubYS1SAKcXbLEL+7Bpv8qXlmow8JXfQoBpa8eDyegGUBxG9sVDTHp5AVVnV3In7JPByfcXyTM8ZKOynEJt1laOTbuocmeRmAZ1ev6M8W0sJH0AjIsp/qfsMMRktlgULH9wrVG9g6RmkZd2w6Tm0QvWG3f36fYM4nq0FFem8YN1o8Dc+Ctu9O2p8niVCzOCt7Bpgvk8Dhbs4fv+tjntjpplYdzqTihHt15Ks3w23CeTAs2pSexgH7bMqsbTimL1oKx7k1dxm1kOdjDFmOAq+vpn4jGcKAQQv7n2KTwKLBauIUZSiUY76IJvg== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1681228967; bh=ni6MZigs777zJf57zSD3kUps4dyE9EeDqrYym54MiBx=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=Pat711mieyQa49gyxkDR8b/F9FiV+LFgGxiIi3TZfVYrhkIkRz3FiT1upB0wM2VPuHPjlFPAoiKt/F8ol7JSHEaCnBqSQuebWz1CMCIBo26+jLYbYu3dYsRS2DO0qB6NFwDxmOjQ1O5TmNigleVDY2DLNygWL3xsth/UVPbvkRttpZFsOnOEtVOXU0kj9If6n52s8xsCu8aUgBs0jXH4YUmPe3OhEiL0DwT3B7culK9n7BOz3/q1hBSsW+4JJYDuH7/bjnoTVfPO4VTf5qoDhidhiua6XLiuqsgfASkXLeFYr+5AesYe/ZWbvFpdCVkfaHeh+1ljN+pMAQZKF1KTng== X-YMail-OSG: nLJG26YVM1lpusnXHY6JuS9mk3T2z4DkJbZBvVN5W4.X7Fe7qayZ5IlKC6zfcpp gjGj_r_dDFVd3NYlPa2VHcRrxsKf5.DqLxCvzr51jDmwrVQVRAqNTVI_D8UPrtDzRumZ1jItX7Zb HK0nDBoQbl2TjeBB9FMkuiZi9ctqQpZXRVzQT.b529Rbx6kvq80Q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KE7Sn0es4fIn.WhEd_99YdPk MpwbHfgeGGei40PP6pnCj X-Sonic-MF: X-Sonic-ID: a332e174-cf4f-4f4b-a698-63b22f1da40a Received: from sonic.gate.mail.ne1.yahoo.com by sonic307.consmr.mail.ne1.yahoo.com with HTTP; Tue, 11 Apr 2023 16:02:47 +0000 Received: by hermes--production-gq1-546798879c-8jjxz (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID db610f21750abb164e44e985bdddbdbc; Tue, 11 Apr 2023 16:02:44 +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 v8 07/11] LSM: Helpers for attribute names and filling an lsm_ctx Date: Tue, 11 Apr 2023 08:59:17 -0700 Message-Id: <20230411155921.14716-8-casey@schaufler-ca.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411155921.14716-1-casey@schaufler-ca.com> References: <20230411155921.14716-1-casey@schaufler-ca.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 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 the size of the structure 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 | 48 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/include/linux/security.h b/include/linux/security.h index f7292890b6a2..c96fb56159e3 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 0; +} + 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 0; +} #endif /* CONFIG_SECURITY */ #if defined(CONFIG_SECURITY) && defined(CONFIG_WATCH_QUEUE) diff --git a/security/lsm_syscalls.c b/security/lsm_syscalls.c index 6efbe244d304..67106f642422 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 0; +} + /** * 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 bfe9a1a426b2..453f3ff591ec 100644 --- a/security/security.c +++ b/security/security.c @@ -752,6 +752,54 @@ 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 an integral number of lsm_ctx. + * + * Returns 0 on success, -EFAULT on a copyout error. + */ +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; + u8 *composite; + int rc = 0; + + locallen = sizeof(*ctx); + if (context_size) + locallen += sizeof(*ctx) * ((context_size / sizeof(*ctx)) + 1); + + composite = kzalloc(locallen, GFP_KERNEL); + if (composite == NULL) + return -ENOMEM; + + lctx = (struct lsm_ctx *)composite; + lctx->id = id; + lctx->flags = flags; + lctx->ctx_len = context_size; + lctx->len = locallen; + + memcpy(composite + sizeof(*lctx), context, context_size); + + if (copy_to_user(ctx, composite, locallen)) + rc = -EFAULT; + + kfree(composite); + + return rc; +} + /* * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and * can be accessed with: -- 2.39.2