Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp84403ybi; Thu, 20 Jun 2019 18:22:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqynSNom3Sm7to1GVPEarbeYK8EIabT+/oPFw8RJmTfyNGY8F7IqFfpdOtE4lqQe1QpEPSl2 X-Received: by 2002:a17:90a:9201:: with SMTP id m1mr2908005pjo.38.1561080138603; Thu, 20 Jun 2019 18:22:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561080138; cv=none; d=google.com; s=arc-20160816; b=hsdlAnjpS2IEiNsIMBe5ct+5P/bbd9DZZJVnos79HANPptrd1jEfcQjKuPlZ7ItaJi 7jDfGYJC2bjhBX7YNy6s57V6Hze1sVVHYjxGfqJe951hcRUBDbAGfK19sWH5hl+Xuiix oBnShQ4M2XEG5U92M/6FkXI5LefQgnAbYoImOcJxb4YO8DfPLenxt2f3PIIjP5Ic+aL0 x7jX/yzAfksIt2Mwt2zQltr0/MMoIMQ8w5KRUY6mI0mQqshIZ5bj+nI5e6UBAPfeEfnL dLBQRHC7tivXIqGH0sLuT8vDFLdsDUgJNO5NN645IsNCGrU2ng1Rauv8Oawb/mivtU9b eWnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=hI61hPrGDQuqCDhatsEmznys0LPDvrfd2WbVsV7UPlg=; b=UKCtqFAmgTcUjy0+Rt5yfG4Nk6CS2/Jc4CLtENYqbD5PfaWsV6V5MZdNJZR0ZYuybh J/lw/YAT213l9mpRQvWykvV3tRKhCHj5zuDV7A/RkXjqps6V+FBOLhOInKrF0j59/gcq yKEWkOShs0Ml5uYcf6DjQZ2TKfZE7FKyKSe48r/RBTY6Fh4kGQc0S0G+qULAwdTFmQ7m BisXxPLMgBDswcz91YhiOOQN9ew1RSnPzu9IPopKkoI8K+pT7hkOBEp+2gXvbT+CRo2n sLr4/I1wdFXIDOPCZPIsXtIaMgBDUsNC2YkwtS0oIUSWWazFqOzxJi1a1pS6pNwrI7oQ cH6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=J1p1myPr; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m20si1311268pjn.40.2019.06.20.18.22.03; Thu, 20 Jun 2019 18:22:18 -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=@google.com header.s=20161025 header.b=J1p1myPr; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726977AbfFUBV2 (ORCPT + 99 others); Thu, 20 Jun 2019 21:21:28 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:53067 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726875AbfFUBUy (ORCPT ); Thu, 20 Jun 2019 21:20:54 -0400 Received: by mail-qt1-f202.google.com with SMTP id d26so6076081qte.19 for ; Thu, 20 Jun 2019 18:20:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=hI61hPrGDQuqCDhatsEmznys0LPDvrfd2WbVsV7UPlg=; b=J1p1myPry3G2fUhRn1sffqnju98JLVJqNUMbfDNOtphQZpXO3fpYpw/a4lbjBPniQb aCTgIafzreRY8icd+M+H+5cvzIqfukazZO6CGeR31o5OzYs+jov87odip1NwcsZjGekc lxQPdPdYK28zE4AaDuIVFTMRGVrO6c663/NEihelit38tv6umY5YEpfVMaAX4jJVND8D 4vnCD09qfgMnawFajy6vOuk62JBU1Wgm9LvvtbxEsgneHJflqoPqpUjz6v/cAeqaZI/j l43OJ6g+icDETdpMEk7IBVtMzENAytYUYP+FXyw0wtAhkJyzb1kGCWOpPrUBm07JNpnB 6HJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=hI61hPrGDQuqCDhatsEmznys0LPDvrfd2WbVsV7UPlg=; b=c6JUAM8mb6RiPWzT6b02pGuUHeqrTvCP6zO5niKwqLZHrEj/YAEdBlUUDO3+bquaU0 HlVKFwiDlC1m4NxdlvMS2ZEMf5ZCU+0ZNozeKyp4uA883/ySLNePoSw5MLoqHcJ47r0R 37SmJzrYxKnx3tCBA2YzyIYJEIIYVarR74nlorbrhGQCnXwZv077zJs+fXunXcyN/2vG u+uLm4kTRTmXY/21usKu8fWMf59S5wBpDCHTXgaUucYaNW535tQkdChJmcUEw406Cyib TuTHXCXLeHWDQt/9AuBtrPEEEUF3Z4j124KwSoi7+Te9Huel6a3oKih/kunRGRxOb9Zt NTaA== X-Gm-Message-State: APjAAAXNY69nF3dVHV13kNFAaxzrXBC0ZhzriN39Wh+/w51/zNuKCaJU YDXi4S4OtRNtLsujvO3Sd3aFi5728h9uKMVoPy/SIQ== X-Received: by 2002:a0c:b010:: with SMTP id k16mr42793207qvc.170.1561080052987; Thu, 20 Jun 2019 18:20:52 -0700 (PDT) Date: Thu, 20 Jun 2019 18:19:37 -0700 In-Reply-To: <20190621011941.186255-1-matthewgarrett@google.com> Message-Id: <20190621011941.186255-27-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190621011941.186255-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH V33 26/30] kexec: Allow kexec_file() with appropriate IMA policy when locked down From: Matthew Garrett To: jmorris@namei.org Cc: linux-security@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Matthew Garrett , Matthew Garrett , Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Systems in lockdown mode should block the kexec of untrusted kernels. For x86 and ARM we can ensure that a kernel is trustworthy by validating a PE signature, but this isn't possible on other architectures. On those platforms we can use IMA digital signatures instead. Add a function to determine whether IMA has or will verify signatures for a given event type, and if so permit kexec_file() even if the kernel is otherwise locked down. This is restricted to cases where CONFIG_INTEGRITY_TRUSTED_KEYRING is set in order to prevent an attacker from loading additional keys at runtime. Signed-off-by: Matthew Garrett Acked-by: Mimi Zohar Cc: Dmitry Kasatkin Cc: linux-integrity@vger.kernel.org --- include/linux/ima.h | 9 ++++++ kernel/kexec_file.c | 7 +++- security/integrity/ima/ima.h | 2 ++ security/integrity/ima/ima_main.c | 2 +- security/integrity/ima/ima_policy.c | 50 +++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/include/linux/ima.h b/include/linux/ima.h index dc12fbcf484c..c30954acc660 100644 --- a/include/linux/ima.h +++ b/include/linux/ima.h @@ -132,4 +132,13 @@ static inline int ima_inode_removexattr(struct dentry *dentry, return 0; } #endif /* CONFIG_IMA_APPRAISE */ + +#if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING) +extern bool ima_appraise_signature(enum kernel_read_file_id func); +#else +static inline bool ima_appraise_signature(enum kernel_read_file_id func) +{ + return false; +} +#endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */ #endif /* _LINUX_IMA_H */ diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 455f4fc794f3..8134da6573c6 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -240,7 +240,12 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, ret = 0; - if (security_is_locked_down(LOCKDOWN_KEXEC)) { + /* If IMA is guaranteed to appraise a signature on the kexec + * image, permit it even if the kernel is otherwise locked + * down. + */ + if (!ima_appraise_signature(READING_KEXEC_IMAGE) && + security_is_locked_down(LOCKDOWN_KEXEC)) { ret = -EPERM; goto out; } diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index d213e835c498..3bc62062cfe8 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -115,6 +115,8 @@ struct ima_kexec_hdr { u64 count; }; +extern const int read_idmap[]; + #ifdef CONFIG_HAVE_IMA_KEXEC void ima_load_kexec_buffer(void); #else diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index 357edd140c09..927fe889201a 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -473,7 +473,7 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id) return 0; } -static const int read_idmap[READING_MAX_ID] = { +const int read_idmap[READING_MAX_ID] = { [READING_FIRMWARE] = FIRMWARE_CHECK, [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK, [READING_MODULE] = MODULE_CHECK, diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c index e0cc323f948f..8784449918e2 100644 --- a/security/integrity/ima/ima_policy.c +++ b/security/integrity/ima/ima_policy.c @@ -1339,3 +1339,53 @@ int ima_policy_show(struct seq_file *m, void *v) return 0; } #endif /* CONFIG_IMA_READ_POLICY */ + +#if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING) +/* + * ima_appraise_signature: whether IMA will appraise a given function using + * an IMA digital signature. This is restricted to cases where the kernel + * has a set of built-in trusted keys in order to avoid an attacker simply + * loading additional keys. + */ +bool ima_appraise_signature(enum kernel_read_file_id id) +{ + struct ima_rule_entry *entry; + bool found = false; + enum ima_hooks func; + + if (id >= READING_MAX_ID) + return false; + + func = read_idmap[id] ?: FILE_CHECK; + + rcu_read_lock(); + list_for_each_entry_rcu(entry, ima_rules, list) { + if (entry->action != APPRAISE) + continue; + + /* + * A generic entry will match, but otherwise require that it + * match the func we're looking for + */ + if (entry->func && entry->func != func) + continue; + + /* + * We require this to be a digital signature, not a raw IMA + * hash. + */ + if (entry->flags & IMA_DIGSIG_REQUIRED) + found = true; + + /* + * We've found a rule that matches, so break now even if it + * didn't require a digital signature - a later rule that does + * won't override it, so would be a false positive. + */ + break; + } + + rcu_read_unlock(); + return found; +} +#endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */ -- 2.22.0.410.gd8fdbe21b5-goog