From patchwork Wed Nov 24 01:43:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 12635669 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 A900CC433F5 for ; Wed, 24 Nov 2021 01:48:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233212AbhKXBvQ (ORCPT ); Tue, 23 Nov 2021 20:51:16 -0500 Received: from sonic306-28.consmr.mail.ne1.yahoo.com ([66.163.189.90]:37166 "EHLO sonic306-28.consmr.mail.ne1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229549AbhKXBvQ (ORCPT ); Tue, 23 Nov 2021 20:51:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1637718487; bh=Q3oYFxmyePXUZvb52LUnr5+7j9BYUgqBMyeLytsPrdM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=F4NAoxc88B11wa4uMaQyluzO/7S5f346tFZB+zthUcgfbS3WI76G5uDAOYnD5m6d1zpU800bSKPWgNk6FW6+6ZkAqxT4XU/JXnnh2Wg/fF9AkVSO2WGI/6nP68wZgTpSzeF6uk8KeC0PGE+rs4ar17pwI9toQzONJwavhlQ6MBR0R4kq0fcGK2ohBayPA+DIjZfPv9N+rqYhfMtoiZt8fhRgeTQC2JpWzr/y6rlHHQ/OmCFMLrXcdjqDcnVj2eLUgb17/sjtXB6+4UpCAnk4W7p5zOirXN3rgc7giQ19PrM3knhclawhDH1iLfzWTP71+MVOo4WQ4yV1F7V0qSOuow== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256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WCh1rMBFX8iJIRcXnWuJ19CqLhXO_T8fBJNgNv5Ao4HhDGvLpyepQwMz3QWZSG8aFQLX0Mm qTEd29JEIpU5xhC6IqTTEcqUTzUUKWJD.bA.Cet9Ryk2xS.wMnuwrbSc2.3Mqo_RXwnax0uXRS4b sKvwvNYEx.urtJTzriEwm2n.K5_5Y7b_fI9VhfdIVKwsQpd2llVj2eQisQqEVdAKYJcFc9eNE5EL XJ66I4pxdC3AN9D5lKeTU.uysiKBHie6bdUlARFD97PC8ps0Vhdp8SW1.JvzMy8tC_VGCLrahD9I gwdfPWjDvvlv7b7GQoHIvnYpd5AXk3IK7ym6BKoN_bvYhrN8ya3qHDxCL7jlFdpNUgnGEirY25gM oYFAvqYkQ8Wh1ZJInbE9rrqpCt_d16AdAHiuTCT_SCUFKqa7N0BS3CLbWQBIlch.uCajuIpM03Cl 66vVjTA4kuH6KcJDd.67LSlt7eqYG92lJsl9ct24Vgfj2I2pm5QesYR4amz32t87zANnIjdpNmTY dRTnnKyB19aJVKEJ4EjMA6H_ylHHIWfBlzW9Bl402cnSSKwZ3beKtuMLk.J5K6oTkMhpr4ctLvtZ gKAwx3d1Q2JHnuot3nTkxEW18Pav7 X-Sonic-MF: Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.ne1.yahoo.com with HTTP; Wed, 24 Nov 2021 01:48:07 +0000 Received: by kubenode536.mail-prod1.omega.gq1.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 0bdf7b7fb47115a0eb0172a796184117; Wed, 24 Nov 2021 01:48:03 +0000 (UTC) From: Casey Schaufler To: casey.schaufler@intel.com, jmorris@namei.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org Cc: casey@schaufler-ca.com, linux-audit@redhat.com, keescook@chromium.org, john.johansen@canonical.com, penguin-kernel@i-love.sakura.ne.jp, paul@paul-moore.com, sds@tycho.nsa.gov, linux-kernel@vger.kernel.org Subject: [PATCH v30 04/28] LSM: provide lsm name and id slot mappings Date: Tue, 23 Nov 2021 17:43:08 -0800 Message-Id: <20211124014332.36128-5-casey@schaufler-ca.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211124014332.36128-1-casey@schaufler-ca.com> References: <20211124014332.36128-1-casey@schaufler-ca.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Provide interfaces to map LSM slot numbers and LSM names. Update the LSM registration code to save this information. Acked-by: Paul Moore Reviewed-by: Kees Cook Signed-off-by: Casey Schaufler --- include/linux/security.h | 4 ++++ security/security.c | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/security.h b/include/linux/security.h index 36a8c2d95542..cd68b4199877 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -194,6 +194,10 @@ static inline bool lsmblob_equal(struct lsmblob *bloba, struct lsmblob *blobb) return !memcmp(bloba, blobb, sizeof(*bloba)); } +/* Map lsm names to blob slot numbers */ +extern int lsm_name_to_slot(char *name); +extern const char *lsm_slot_to_name(int slot); + /* These functions are in security/commoncap.c */ extern int cap_capable(const struct cred *cred, struct user_namespace *ns, int cap, unsigned int opts); diff --git a/security/security.c b/security/security.c index 283671b6a5d7..bc169b9a6d77 100644 --- a/security/security.c +++ b/security/security.c @@ -477,6 +477,50 @@ static int lsm_append(const char *new, char **result) * Current index to use while initializing the lsmblob secid list. */ static int lsm_slot __lsm_ro_after_init; +static struct lsm_id *lsm_slotlist[LSMBLOB_ENTRIES] __lsm_ro_after_init; + +/** + * lsm_name_to_slot - Report the slot number for a security module + * @name: name of the security module + * + * Look up the slot number for the named security module. + * Returns the slot number or LSMBLOB_INVALID if @name is not + * a registered security module name. + */ +int lsm_name_to_slot(char *name) +{ + int i; + + for (i = 0; i < lsm_slot; i++) + if (strcmp(lsm_slotlist[i]->lsm, name) == 0) + return i; + + return LSMBLOB_INVALID; +} + +/** + * lsm_slot_to_name - Get the name of the security module in a slot + * @slot: index into the interface LSM slot list. + * + * Provide the name of the security module associated with + * a interface LSM slot. + * + * If @slot is LSMBLOB_INVALID return the value + * for slot 0 if it has been set, otherwise NULL. + * + * Returns a pointer to the name string or NULL. + */ +const char *lsm_slot_to_name(int slot) +{ + if (slot == LSMBLOB_INVALID) + slot = 0; + else if (slot >= LSMBLOB_ENTRIES || slot < 0) + return NULL; + + if (lsm_slotlist[slot] == NULL) + return NULL; + return lsm_slotlist[slot]->lsm; +} /** * security_add_hooks - Add a modules hooks to the hook lists. @@ -498,6 +542,7 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count, if (lsmid->slot == LSMBLOB_NEEDED) { if (lsm_slot >= LSMBLOB_ENTRIES) panic("%s Too many LSMs registered.\n", __func__); + lsm_slotlist[lsm_slot] = lsmid; lsmid->slot = lsm_slot++; init_debug("%s assigned lsmblob slot %d\n", lsmid->lsm, lsmid->slot);