Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5721635rwl; Tue, 11 Apr 2023 09:06:02 -0700 (PDT) X-Google-Smtp-Source: AKy350ZIUktY7Q5ZoSR74yeTwnh+lbMXt7t11JviAwasu6fwgsphQekwV++N+sDoJuid9dznjxvd X-Received: by 2002:a17:906:2c59:b0:94d:7b7c:4675 with SMTP id f25-20020a1709062c5900b0094d7b7c4675mr4597757ejh.38.1681229162475; Tue, 11 Apr 2023 09:06:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681229162; cv=none; d=google.com; s=arc-20160816; b=x1gw5+zX3Ieh3AGcN95VgTWFzH2dja3lXBYgMKAnSu8il7fPeCDf+H5KNTSNK8gz9c MPPJ2kUPUeBu58oanFFNJX8sPmvgFWGwlzNh8BF7mDFVaAPtcv8oMO++gqcJVffKoJ3P fGP3N7B8R4pUkuPoa8aULajji0tf4UfWXVhCylJiBIS3FrA0kp8G6r3R3twAzVbpC4Iv +fTXhYA3aqdqGpmxKDljjZ4noQu+iWxAA3uWqwaAfmmFI8aC9RPMl68JHHI4gcJZQ3CS oBIp3BG9RNHk1+HRT40AgGpGfSz7Zu2hDNqT2xg3wMEPtL65WNJDl+V52nYgPNTMcOer 9DGA== 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=gX2htUbJVQu70tjeZK7/GhWqFSnOffI4/UYVqiCQJFs=; b=OzBvqtW9z3wgpERkV3mLXp8NQHps7BTnpLza/BUPmPrKbjoq8Zt8XwAVv60Qs1gJWH oOAxop5xTh9UsWb+HJMWzxXMpjQaolKUAque7+N9zK39Ahpd0LEsBuKhmnk1sKIhOGHo uFcVhzPAADMoHAzra9YPJJwcXcfWDZYP0v6B+PrI9ud5qVSO2ogjEV2oy6y0dCdNEVgE 1MEuO6xxJmfv+DK26rLS7gKgHBDfoudqBXMW8bOOEGj32nmnUZTfzHGEK70o6Dgyg0W2 H94Ct2fqUZy7QGW+5ocl5an4mH061ghZ950/IC46u5bXGp/jtUPpYWEi3cSALwOUGvft CsIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=Ue2NVC7K; 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 jl21-20020a17090775d500b0093cf8c96446si390570ejc.686.2023.04.11.09.05.37; Tue, 11 Apr 2023 09:06:02 -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=Ue2NVC7K; 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 S230075AbjDKP75 (ORCPT + 99 others); Tue, 11 Apr 2023 11:59:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231215AbjDKP7r (ORCPT ); Tue, 11 Apr 2023 11:59:47 -0400 Received: from sonic305-27.consmr.mail.ne1.yahoo.com (sonic305-27.consmr.mail.ne1.yahoo.com [66.163.185.153]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E635B4C1D for ; Tue, 11 Apr 2023 08:59:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1681228776; bh=gX2htUbJVQu70tjeZK7/GhWqFSnOffI4/UYVqiCQJFs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=Ue2NVC7KffB9z5iV3j554EVJlVB0/9qe1bPpUoFomjAcJyXMCAGvDe+DpzPhql/fLcvy3RJUljAQPYy3PFamsxhw4F5zZxpMOS2FQKJZmrpMG8F5tI3uGy2xFbHSeHGHB3r8UdkeIYw/y4cUcDWnZ93im69ISefUZpTRGPiRKKIU3IpM1afMs76ri/pMa0Em0Z02XifPkEQzkVQAWnZZ/Bp5AUKMA6E3YCOE75HL6sjzFp3HoCjqC4tpvBu4E99d1GTCQEB+Ue9kBBqaOql14CLaGZ77e/u+uf85Rk0e6kI5jcj9zRkFlYGkX8P0XBY5bLqB2GN3ULK5zwnDKlXG+A== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1681228776; bh=ONirwGak/GEi45uod0w1iuYoDXABE08JVrQWgjmETtj=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=m/cQ34un0rx+JC6BjPu/XLk+v2JxYp1VbxFCxC7G+jDDJ69Fb8P2T0rqfry2EVLb5ZmwDw6uO7g/XAolzNYUBXfqVlAdGsmQK5VJHyZZVBQBcMh7jyR7VDX1oiK5+4AvDhTupvnj53/EMeZ+Zilna8yJ8HK2fTCtZFASaLm1QIJYLjCm+OuFDLYSP2gFuQ5aKoSNJM4eM7Svr8YdW5yx3ZwE5fZYLJUtYIwYcqr6YWvLnsitQpXYd7nfaYiOjYgZgB74JN0/6m+EySDT4mD6aZmPwiLfMJpqbsGxhW+A6+GiacSNMXoz4G6LZZJXbEFEFACL403bIxvpev7uXLPRcg== X-YMail-OSG: mE6zsHcVM1ksw8BrjaAQ.js3WKJQ6X7MxmAVF7iq4YRjlJ59JBeH4vejlB2_4du 1E8aQy8jS0z0XFTPYY.GpzNzCXy3Uy03g1X45ksH62DpL.f8960jOdaoqa7_tY95anAIuNhcJm5y Zn4L3LFE2jSTs6jb_omA_juxlfaB1c3GiwMx9Od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X-Sonic-MF: X-Sonic-ID: c44af405-3986-4b1f-84e8-fe4ddbf60659 Received: from sonic.gate.mail.ne1.yahoo.com by sonic305.consmr.mail.ne1.yahoo.com with HTTP; Tue, 11 Apr 2023 15:59:36 +0000 Received: by hermes--production-bf1-5f9df5c5c4-84ds6 (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID bb1a3f7a7b49e6371e007077b829a9fe; Tue, 11 Apr 2023 15:59:31 +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 02/11] LSM: Maintain a table of LSM attribute data Date: Tue, 11 Apr 2023 08:59:12 -0700 Message-Id: <20230411155921.14716-3-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 As LSMs are registered add their lsm_id pointers to a table. This will be used later for attribute reporting. Determine the number of possible security modules based on their respective CONFIG options. This allows the number to be known at build time. This allows data structures and tables to use the constant. Signed-off-by: Casey Schaufler --- include/linux/security.h | 2 ++ security/security.c | 43 ++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/include/linux/security.h b/include/linux/security.h index 5984d0d550b4..e70fc863b04a 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -138,6 +138,8 @@ enum lockdown_reason { }; extern const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1]; +extern u32 lsm_active_cnt; +extern struct lsm_id *lsm_idlist[]; /* These functions are in security/commoncap.c */ extern int cap_capable(const struct cred *cred, struct user_namespace *ns, diff --git a/security/security.c b/security/security.c index 58828a326024..3f98e5171176 100644 --- a/security/security.c +++ b/security/security.c @@ -28,12 +28,29 @@ #include #include #include +#include #include #define MAX_LSM_EVM_XATTR 2 -/* How many LSMs were built into the kernel? */ -#define LSM_COUNT (__end_lsm_info - __start_lsm_info) +/* + * How many LSMs are built into the kernel as determined at + * build time. Used to determine fixed array sizes. + * The capability module is accounted for by CONFIG_SECURITY + */ +#define LSM_COUNT ( \ + (IS_ENABLED(CONFIG_SECURITY) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_SELINUX) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_SMACK) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_TOMOYO) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_IMA) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_APPARMOR) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_YAMA) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_LOADPIN) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_SAFESETID) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_LOCKDOWN_LSM) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_BPF_LSM) ? 1 : 0) + \ + (IS_ENABLED(CONFIG_SECURITY_LANDLOCK) ? 1 : 0)) /* * These are descriptions of the reasons that can be passed to the @@ -90,7 +107,7 @@ static __initdata const char *chosen_major_lsm; static __initconst const char * const builtin_lsm_order = CONFIG_LSM; /* Ordered list of LSMs to initialize. */ -static __initdata struct lsm_info **ordered_lsms; +static __initdata struct lsm_info *ordered_lsms[LSM_COUNT + 1]; static __initdata struct lsm_info *exclusive; static __initdata bool debug; @@ -341,13 +358,16 @@ static void __init report_lsm_order(void) pr_cont("\n"); } +/* + * Current index to use while initializing the lsm id list. + */ +u32 lsm_active_cnt __lsm_ro_after_init; +struct lsm_id *lsm_idlist[LSM_COUNT] __lsm_ro_after_init; + static void __init ordered_lsm_init(void) { struct lsm_info **lsm; - ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms), - GFP_KERNEL); - if (chosen_lsm_order) { if (chosen_major_lsm) { pr_warn("security=%s is ignored because it is superseded by lsm=%s\n", @@ -387,8 +407,6 @@ static void __init ordered_lsm_init(void) lsm_early_task(current); for (lsm = ordered_lsms; *lsm; lsm++) initialize_lsm(*lsm); - - kfree(ordered_lsms); } int __init early_security_init(void) @@ -513,6 +531,15 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count, { int i; + if (lsm_active_cnt >= LSM_COUNT) + panic("%s Too many LSMs registered.\n", __func__); + /* + * A security module may call security_add_hooks() more + * than once. Landlock is one such case. + */ + if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) + lsm_idlist[lsm_active_cnt++] = lsmid; + for (i = 0; i < count; i++) { hooks[i].lsmid = lsmid; hlist_add_tail_rcu(&hooks[i].list, hooks[i].head); -- 2.39.2