Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2218998imm; Thu, 20 Sep 2018 09:30:50 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb8AhzOpSNJH2wNIBZ0a3rPWsKe8cbk22YHx2SoQFgIkInzloxmo405OvZZKyJWs3OA7dv/ X-Received: by 2002:a17:902:145:: with SMTP id 63-v6mr39181743plb.329.1537461050447; Thu, 20 Sep 2018 09:30:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537461050; cv=none; d=google.com; s=arc-20160816; b=G43Jp0h/iWaNo4ofstxqx1rtaybwdIu1yvvqI3CGQW42lXTfzIK52TY4wjTTsJMTSZ ofYcnDWkHb1fzjVaCI/dDfA6ibmMTSjIm0DIYSyYBNksZoHLq1mKyfe/0H27mizq+Y6w z/53RDM0XoySV/qiW2/+ZWA7Ob+4kAWU6NEWGz8bGduLekqF8etKFCLmzQl/96qC79Po lUNS+M/W0jJp98OknYJ24/v0YkU6fyj+mEqWEADmFuGvBQL6rEPd7+7nGQp9tVP+xQHr i5fUWg3Uxxy1ampSyziECTtNujBuRrI4XdINkp5KyvlRzp/OSZy8HTDyxwnMP5dFO68p zG8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=OMVxv/mM/FRrUnvXJYDNolN8Wngtjq4Uk/5dWWv+svs=; b=vhIaaBYJhs340VW+D9INIEu61SkhR4WjxfTPNMbSzvELAduIFUolbWD2KRsH9DPW+8 cVBSQrvhNxtDVroqWCYfUeozNcZqYWDMAaV6kYWqjufpZMEfQSpEdwphVBHGfQIVdTyx OCh3boGrv/nBief2MpllM5ryZGJopj/QqryTZDmQf4uqGcEQBG8SwBabGZ9UFat0qxPd ZRkF24RpxS4QM3btDn4Cc2tio2QWvvjiQUWQHJTVbzm5w3/fbTeGlO8btPMlstCRd6ct Z4FHYtzgsKKnRSc+UHe+1EJGDpa1A92SMWTv8EFH4J3GSmI4ajLNqKDnexfr/h8wdNrM 61/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QJjoUbzD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h17-v6si24528929pgj.214.2018.09.20.09.30.34; Thu, 20 Sep 2018 09:30:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QJjoUbzD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387762AbeITWN5 (ORCPT + 99 others); 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-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