From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.5 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84C73C433F4 for ; Thu, 20 Sep 2018 16:29:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4033D2147D for ; Thu, 20 Sep 2018 16:29:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="QJjoUbzD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4033D2147D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387812AbeITWN5 (ORCPT ); Thu, 20 Sep 2018 18:13:57 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:38101 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732577AbeITWNz (ORCPT ); Thu, 20 Sep 2018 18:13:55 -0400 Received: by mail-pg1-f193.google.com with SMTP id t84-v6so4676960pgb.5 for ; Thu, 20 Sep 2018 09:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OMVxv/mM/FRrUnvXJYDNolN8Wngtjq4Uk/5dWWv+svs=; b=QJjoUbzD9oq0tyVU10SP6ZNpYGIF4w0Q6U6p3REGjcOex7DaqVGoDrvR8SwFKFkDac mosFjX9J0FmHG4lr4C9jOkgzWlTjFYkLld1LPNW/rTqFtIsmxfhS7LNwBqdA3BdivXML 152Dguuq4gYyb7POSkGfLehxp0ryOPeTyqYbY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OMVxv/mM/FRrUnvXJYDNolN8Wngtjq4Uk/5dWWv+svs=; b=U5I3+kojCXs9VTJh145FAIxhaHb6bgQdodA//NCcHRnp0ITeKv35r2CKcMzme87ria ueuxBkl3C/AXHE1Bjy9o8yon86SM7j7hvzaVDQOk9JcpTiOIVKZCVypC0ovkDG5jB3Ek 9XroKqBWbCw4pzfrl7V8uKOPWBNZMhoDrpTgPF1HNqSNpPJWDkeXKy4JfBMBXVGghnSt gKA6m5lfTArcmmn9F6c0obTDcJnhPU+Len/pqFkdueYibiZstHWxjG9SQLEuBIA25kJl 86Rfc6a6gtxkb6pbmQznYekPW7P8Z+EFFli6WrRNAyGSePatxn5Pu0kPVm1v/NgWrT9v QQSw== X-Gm-Message-State: APzg51B4NOYmawde129zsU49F+lBVbWsCwbfGZ2h+Bj1cpLTSdyAbU59 b7Gh5HB40+ldcovDrT3Z9zFiqQ== X-Google-Smtp-Source: ANB0VdbBmfcyhsWW+AiGrOHR7fzlwnYIjDFCLIHPq3WhkZ4PpJaDKGn7QWCBLiIw8jUxj/JK01GDgA== X-Received: by 2002:a63:ed4f:: with SMTP id m15-v6mr35229465pgk.147.1537460978098; Thu, 20 Sep 2018 09:29:38 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id s195-v6sm53078190pgs.76.2018.09.20.09.29.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Sep 2018 09:29:34 -0700 (PDT) From: Kees Cook To: James Morris Cc: Kees Cook , Casey Schaufler , John Johansen , Tetsuo Handa , Paul Moore , Stephen Smalley , "Schaufler, Casey" , LSM , Jonathan Corbet , linux-doc@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH security-next v2 18/26] LSM: Build ordered list of ordered LSMs for init Date: Thu, 20 Sep 2018 09:23:30 -0700 Message-Id: <20180920162338.21060-19-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180920162338.21060-1-keescook@chromium.org> References: <20180920162338.21060-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This constructs a list of ordered LSMs to initialize, using a hard-coded list of only "integrity": minor LSMs continue to have direct hook calls, and major LSMs continue to initialize separately. Signed-off-by: Kees Cook --- security/security.c | 59 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/security/security.c b/security/security.c index 25a019cc4a2b..2541a512a0f7 100644 --- a/security/security.c +++ b/security/security.c @@ -34,6 +34,9 @@ #define MAX_LSM_EVM_XATTR 2 +/* How many LSMs were built into the kernel? */ +#define LSM_COUNT (__end_lsm_info - __start_lsm_info) + struct security_hook_heads security_hook_heads __lsm_ro_after_init; static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain); @@ -41,6 +44,9 @@ char *lsm_names; /* Boot-time LSM user choice */ static __initdata const char *chosen_major_lsm; +/* Ordered list of LSMs to initialize. */ +static __initdata struct lsm_info **ordered_lsms; + static bool debug __initdata; #define init_debug(...) \ do { \ @@ -74,6 +80,45 @@ static void __init set_enabled(struct lsm_info *lsm, bool enabled) } } +/* Is an LSM already listed in the ordered LSMs list? */ +static bool __init exists_ordered_lsm(struct lsm_info *lsm) +{ + struct lsm_info **check; + + for (check = ordered_lsms; *check; check++) + if (*check == lsm) + return true; + + return false; +} + +/* Append an LSM to the list of ordered LSMs to initialize. */ +static int last_lsm __initdata; +static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from) +{ + /* Ignore duplicate selections. */ + if (exists_ordered_lsm(lsm)) + return; + + if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from)) + return; + + ordered_lsms[last_lsm++] = lsm; + init_debug("%s ordering: %s (%sabled)\n", from, lsm->name, + (!lsm->enabled || *lsm->enabled) ? "en" : "dis"); +} + +/* Populate ordered LSMs list from hard-coded list of LSMs. */ +static void __init prepare_lsm_order(void) +{ + struct lsm_info *lsm; + + for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { + if (strcmp(lsm->name, "integrity") == 0) + append_ordered_lsm(lsm, "builtin"); + } +} + /* Is an LSM allowed to be enabled? */ static bool __init lsm_allowed(struct lsm_info *lsm) { @@ -104,14 +149,10 @@ static void __init maybe_initialize_lsm(struct lsm_info *lsm) static void __init ordered_lsm_init(void) { - struct lsm_info *lsm; - - for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { - if ((lsm->flags & LSM_FLAG_LEGACY_MAJOR) != 0) - continue; + struct lsm_info **lsm; - maybe_initialize_lsm(lsm); - } + for (lsm = ordered_lsms; *lsm; lsm++) + maybe_initialize_lsm(*lsm); } static void __init major_lsm_init(void) @@ -141,6 +182,8 @@ int __init security_init(void) for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct hlist_head); i++) INIT_HLIST_HEAD(&list[i]); + ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms), + GFP_KERNEL); /* Process "security=", if given. */ if (!chosen_major_lsm) @@ -169,6 +212,7 @@ int __init security_init(void) loadpin_add_hooks(); /* Load LSMs in specified order. */ + prepare_lsm_order(); ordered_lsm_init(); /* @@ -176,6 +220,7 @@ int __init security_init(void) */ major_lsm_init(); + kfree(ordered_lsms); return 0; } -- 2.17.1