From patchwork Mon Apr 18 14:59:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Casey Schaufler X-Patchwork-Id: 12816745 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 18234C433EF for ; Mon, 18 Apr 2022 15:36:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345333AbiDRPjU (ORCPT ); Mon, 18 Apr 2022 11:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345209AbiDRPjA (ORCPT ); Mon, 18 Apr 2022 11:39:00 -0400 Received: from sonic311-30.consmr.mail.ne1.yahoo.com (sonic311-30.consmr.mail.ne1.yahoo.com [66.163.188.211]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F443C17 for ; Mon, 18 Apr 2022 08:01:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1650294089; bh=h4uO5wfKE/cDGnftdi/csMqvrdQfPZIUSP+JM76GUTQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=UitKE2gzvIElNrdwR1bhYdlwI3BQTZDvtHTcfbH3tPKjR5wY6fSe8KndSc3XNJF6hUf3RXImo2JOyx2BWaZ748jxJJh81+6C+vflJpP7daNq58gtPpa/p7QCxIB9Aqi5Tao350A0kxB2CEfskkfezpiDWhGglOQpXdNUYMz5TjF9dTEG8QP+JIZBvt/nPRMWZacjw3waJ9Ru4+vuSVbbF0iZofJTXR6V9obr8RbuZnak8ZVVmhEFjceZJQPPTmcUiyDXhcpol2OspoLue13ch2PanrrPTs/pjavIthnzmMLyZYcJNN3RghKiajXlOXN3VDHubHfEcPTL4QAF0KslYQ== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1650294089; bh=7RDt25DgO/1jehz2/1JFYx74yTwn7B4EZrihbypNMS3=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=D9cWQeOTAkfNFb4US45suVsipnFinPqQ+0S4NbZFYT/+bUtm0VdWy+jrjtAwOJ0t2xFUXOB2WbTgXW0z1epV87K3by4oOlNhr3sawXRQFm3iXeI3RiRmtfsqO8yf0dngKBXf8vTtsm4h9eWJ/xxt9Pe2sk9MDSKa3IKV6Q7DhO8unrgqRL6NqyYhx6MjPKc05KHCN5DLP7/BW6fG2lLywh0x2EDURr1TLXvHayJlWdd3g0pmTCyeCtuZHNRlvcvAorG51ULSv6pfAdOCkM8LpaYi9uuGoVfKK/zrMyYlBECPcsIqw2ghheN81OTAhrj9MJINRii9oc3TSaWU7hGnIg== X-YMail-OSG: 71NRkb8VM1k4_YGMFl9rqttulA6RIHKY9eQIEKpe1UEyjVep7cVlGQjTkqQenEK dFiwL0F0HCrhBk1IQrtb6ECCMPMrdzvggQY9x.7Ct8UOs2jUFaQJiia7ow.wCS8V8wf6LZ4PH1L2 HlmUKGaZM4phvoZVybTUQ4O98xu.DcG.mi_o2F2saOHbNWaWDmAMI1x.kzpiiboZwNFiy8YoQjkR tSM4c7LPsE5Zof.zBh.b21cMUrs2K9IvbXzlpKPNkZnqG7rxn0dix0683FYupYWV7SOJbpOAgIOt T72IMW2OrDXpKQWXO_obgLHVdeg9dbWiw9mufH5ey_iMgSSqBpqk0O.jgJiVOd.gYZuvsf7B9eKj 33wwcsUBu0YbjDrh9XiPqRi4JqhQdLj_u8jdB7BuVIa.cYKFDi9ivpiK9YlOrQyx1eayoTRm6AgF rnBY.W8sZ5qmRB0fD130ydBJflLpKUwwWst52L5iwZPXvpV9qRSH_p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aXllDy8sLmKcKS wB94QGY688nNrVpzNQEQNztpkkknvbU6ZrWaU3gcepZq8QSS.RiaHvuyOZ93QX3Bo5sbP6Z3DaLW 5WFS7DVoQWAmHLiSnbXLUO9u_uNgUTAxlFwBiEFwA8ESmufs_NT3j_f1yKghxybzHwkDvsoQjCaq YWjbFbh_kO1DHxNmciGyV8cWlWz4QvyltTYHVY135Bgt8I8VCsxpksBhIpqsH62HBESpbNdRCOfb fGfrd2smPzccELVEGyeetPyUE7IQqH5FIlFvnvuoGRgStiVKjFtjdusG_LGgnYhgZrKa2qmMRuET FpsKI7MozkCF5cinpx0AkXGpAyHhmz1aRyvC0Y5O.6IMoNOJLf.9bXj5ghK5ct_9MRGduMnfx0SK V3oqEUC6ID8ex3eXMr1Ke5Oy4GW.DG5rbceUjzIHLR2nvuv1p.079B5L7CN6xzhgv8ACV694w3j4 2jgmMa9ydELMDHqMyVsw82gYgEDzlJClNjb29QxAmzQs23T.IoLIfuNH_Bmu6e.fzwCgAx3nfa7U iZEVyHUuaeAclWlY4fGQBQJ9hKVbtT5YHxCGMOimlLCivaen3xihyL7CrNROCC9nLkk5Os2u3CMn NLZY86DDjHcn7lN.URQ3mfI3XR4kxYSRQduRRSBCsVjg_jgyAG4Iu4tGPmNM_K5mS1kmE4X0uMH3 9VNYD4wd_4h3LgGhpEBcWlLkni.TtsnFbPbbmBqxliaiK4Z3bX1bY9bERpM7DKLyk_ZeSfSWG05l KY27AZ_u0ULSg4qjqTNU5sFwik49q42C14ox_t1bo7XryEh4TGw-- X-Sonic-MF: Received: from sonic.gate.mail.ne1.yahoo.com by sonic311.consmr.mail.ne1.yahoo.com with HTTP; Mon, 18 Apr 2022 15:01:29 +0000 Received: by hermes--canary-production-ne1-c7c4f6977-qcc8c (VZM Hermes SMTP Server) with ESMTPA ID becb8feefeabb669980d2a553c076f56; Mon, 18 Apr 2022 15:01:28 +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, stephen.smalley.work@gmail.com, linux-kernel@vger.kernel.org Subject: [PATCH v35 04/29] LSM: provide lsm name and id slot mappings Date: Mon, 18 Apr 2022 07:59:20 -0700 Message-Id: <20220418145945.38797-5-casey@schaufler-ca.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220418145945.38797-1-casey@schaufler-ca.com> References: <20220418145945.38797-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 Reviewed-by: John Johansen --- 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 ed51baa94a30..d00870d2b416 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -195,6 +195,10 @@ static inline bool lsmblob_equal(const struct lsmblob *bloba, 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 49fa61028da2..d1ddbb857af1 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);