From patchwork Mon Dec 13 23:40:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 12674921 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 96BDDC433F5 for ; Mon, 13 Dec 2021 23:45:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244271AbhLMXpK (ORCPT ); Mon, 13 Dec 2021 18:45:10 -0500 Received: from sonic301-38.consmr.mail.ne1.yahoo.com ([66.163.184.207]:39653 "EHLO sonic301-38.consmr.mail.ne1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238230AbhLMXpK (ORCPT ); Mon, 13 Dec 2021 18:45:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1639439110; bh=lac83L4e5zt6gbRTXcOVn+b4xFE7sEfnnV3rmYMW6GY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=t3vjygkBO83qOZpQOqDn9rfbHPvogXTSBAeCivzktk+EPdrVrhMbzgtehb6RPiwbitHXnQKge68Y5K6bposXDzhieRAKo1a4Xci4rd0ZEyz+MGWxmkQvaw00v4dXMGjb6l7YLkUAzEWHiQjypDlEZj3YVhN68OwFselFZku8kRPsCoYOyKKWlruAIHnTv5/l29rLrLdj2T5JhfcztBQFjrzY7plSenKqQT0RiH+q5MvnT8alwQIkRQi6HxklSyFJfeCIIiuCzAfbl1qDcgSiL+d2sdkYqLlo0zFFnlsDfN1DBpTmhMppabyDBcRo6YMoBXU6FFatwMz725GI4IM1uw== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/rela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4_t9V1inL8MVds6FvjhKKNP_dV1b3umDKGNNnG4MUJHyx6KqlGEtWXrXyZV uU9p6mXK38oa1jmIhVRksIOKa6wfSDMgpyYflqWoVOZexk54cg0CL5Ov_PYl6n5Fje8QSscrb3Ti bTJ9IxIddabwxrF546mqvYyf2bOKomyuyr7SoPHxTXbrQ8y6GzW5HJBddfzwmnvZIAUIgRXLHlkk XCHC3r8L453mRlLZsc6DahqHLd7eSlOyqdgXVNIwejF2COx7woGIfoq3fxoXhLvj6DZrCD37nMPY G1m0yUHDyB5aZGaXRjZ3PvkPsZfOXuQmpCzAcSzt_uB3XUQM22xxn1ibk8.J5IHjdOkPH8WKiF4v VwooCiLdtuemerKsmjWc.LC0Dal.ctXvKKkufQb5qTMigFtzZwK.URTPcdX0ZCTioWOOy1l_p_0_ Vptt10A1GmrpwmaBdW.FAyJ4QGJC7u2wbjbKNol6Vg4sWwtFozl85MSWZbdEg9mUNep0fiJQQe_n vjJFDIlZlffsI.3oyzDc5j1ULc6_kIlp.OS5.DPFr2Xv8dAwSQEL1y4Gb31APozZWzSva89oKPKV 3XbhH5aVrwk3ltC4bYU5EbYTdB5EWb7c- X-Sonic-MF: Received: from sonic.gate.mail.ne1.yahoo.com by sonic301.consmr.mail.ne1.yahoo.com with HTTP; Mon, 13 Dec 2021 23:45:10 +0000 Received: by kubenode545.mail-prod1.omega.gq1.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 83eeef32ece3304efb45e12e25148509; Mon, 13 Dec 2021 23:45:04 +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 v31 04/28] LSM: provide lsm name and id slot mappings Date: Mon, 13 Dec 2021 15:40:10 -0800 Message-Id: <20211213234034.111891-5-casey@schaufler-ca.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211213234034.111891-1-casey@schaufler-ca.com> References: <20211213234034.111891-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 96afea94938f..1bc00edd3a32 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 f5acb607e251..9f3a467fb992 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);