From patchwork Thu Dec 8 16:38:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 31437 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp302539wrr; Thu, 8 Dec 2022 08:48:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf4DM6mRYN+dJin+dzBamCJiZ1tmIEfVirbBxL7dx5I0ylOBikGD6s8OUj8dhX/LfB1NYi2F X-Received: by 2002:a62:e301:0:b0:577:d5ac:8561 with SMTP id g1-20020a62e301000000b00577d5ac8561mr368861pfh.15.1670518084015; Thu, 08 Dec 2022 08:48:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670518084; cv=none; d=google.com; s=arc-20160816; b=w5Wbv3De2yo8s6KaoeLXiBmWovUbBs0BNVOtXh5qIHaCyh/BkokgxDW+HYa9kKLfiA tTfoPdvWJWmFljQwBPRwJpoNz4P0630fbDyabwxuyOQlBBnvjZr54G0kurmJNp0GHGah 4gLiG10lOkhBjo68DwbHGLbZmSBswnDmghUAVAMt4NDQGN8J3xiiv4YCOl+BOGvCWFzl bl/4Y2BUIxlTV/E6E/9CbMAqGqlwg3KCjLiScL7rZ4yG2WsUaGFcrA2L3iZRnUjxuTV0 nxIDmr53dFDGshW83sixv1ThHM9gmFUwgoY+gAFvmNeauqMBX9bS0txs1IqkkqpCN6QI 9yPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=LQ3ul1jQRXTmaEsuvA4DkzO41vWZ0ws2E+AUBitFzrQ=; b=hTrPbDq+rpZcOtPauPj/0VKVjF/NCo6KpwcixNFSpD+l19s1tN8+5g6s61VsH2UAKg PSWG7AKq0KNWyB8vSQHS/e7HY3XTbjASsEZ0dIEOzzQ5330Sg8g7O3B5FEkMcm3x6F8I qBbZIwP3Uq99N5OR0Fhfgq5yrjr60t4Jm1ZG7+aLiyjRXOhki7GFbNcJMO528iXZeIVx CSWy3pGrQyKAEYqp6z5koHrnTupQOxdb2O9vK1KPcdo6za/rjSgrZ396Nh7zz+hMQfTw 7rymo3QKuvF2QnGU+OhvrSBQeDvr1VeirWFug/bNMeNJj/tlrUKSqy8cTl/JnRFBUTAv V4UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="k/7X3z9v"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p16-20020a170902e75000b0018996404dd9si18594157plf.267.2022.12.08.08.47.50; Thu, 08 Dec 2022 08:48:04 -0800 (PST) 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=@chromium.org header.s=google header.b="k/7X3z9v"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230227AbiLHQi0 (ORCPT + 99 others); Thu, 8 Dec 2022 11:38:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229733AbiLHQiV (ORCPT ); Thu, 8 Dec 2022 11:38:21 -0500 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B5C113F1A for ; Thu, 8 Dec 2022 08:38:20 -0800 (PST) Received: by mail-ej1-x62c.google.com with SMTP id fc4so5302646ejc.12 for ; Thu, 08 Dec 2022 08:38:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LQ3ul1jQRXTmaEsuvA4DkzO41vWZ0ws2E+AUBitFzrQ=; b=k/7X3z9v4qZ29jB2yYGyTKe0hRoo77mPXZfDtm/XwjaqyLx2sHjGxg9aBpwD9Ee3qz Np0blameSJM60Ssvq6O9wMJFkHCAokczTYCo3ver8LXm+zG6gA+ypMSfMMzlou1PPHti ELRuj4yf83+9A377gSgGNh1Ubqx45IuT2Fo1E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LQ3ul1jQRXTmaEsuvA4DkzO41vWZ0ws2E+AUBitFzrQ=; b=Zv9VUrxfpgyz9h2V3ZgaTH2vOsC/GudGQp7Rsb+ZsIfNat/7H1o62zgCjtwXmNYVwG rWeca9bwwT3WFstvCs0PFT2N5TJTlKM6d4CgrFgb8vahVjfFXJToPFy4O9Qffh+xul/k Xp1TsoEczUbQYXmzOlROupjZajgmnVXOVfmy3e0jn8oq64btGZYheLJDfHmD4lSVBFyP 2MddbYPf2e//X5kyG0gYIgvkaVvxQBDqJl1cVInfl20RNXSiQlVBrPW5KHyVBeFFMS6K PuZ3NU7j+PmxRojtH32T0gCLax0zwz5IsVMCluwBLDvOnxE6U4ytwrE+AXesYC99eL92 Wjcg== X-Gm-Message-State: ANoB5pmul55LvXs3JeZAW0GHN9jF6SjZ48ev4xHh5PU09w9t4ZWhrLKM 1NEahDsExUsIQ+qSmfdghWbjcA== X-Received: by 2002:a17:906:3716:b0:7c0:bbab:22e5 with SMTP id d22-20020a170906371600b007c0bbab22e5mr2410973ejc.16.1670517498873; Thu, 08 Dec 2022 08:38:18 -0800 (PST) Received: from alco.roam.corp.google.com ([2a00:79e1:abd:5c00:8aae:c483:8219:bdba]) by smtp.gmail.com with ESMTPSA id ck3-20020a0564021c0300b0046778ce5fdfsm3569046edb.10.2022.12.08.08.38.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 08:38:18 -0800 (PST) From: Ricardo Ribalda Date: Thu, 08 Dec 2022 17:38:01 +0100 Subject: [PATCH v2 2/3] kexec: Factor out kexec_load_permited MIME-Version: 1.0 Message-Id: <20221114-disable-kexec-reset-v2-2-c498313c1bb5@chromium.org> References: <20221114-disable-kexec-reset-v2-0-c498313c1bb5@chromium.org> In-Reply-To: <20221114-disable-kexec-reset-v2-0-c498313c1bb5@chromium.org> To: Philipp Rudo , Eric Biederman , Jonathan Corbet Cc: linux-doc@vger.kernel.org, Ricardo Ribalda , Steven Rostedt , "Joel Fernandes (Google)" , kexec@lists.infradead.org, Sergey Senozhatsky , Ross Zwisler , linux-kernel@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2723; i=ribalda@chromium.org; h=from:subject:message-id; bh=oo014ZHz6Xm+AA2th2qtraBO9RjgGW/IoV0rNAHz2pg=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjkhLz9anViJqu4KFmbxZAiE4U2e9028qdN4mFsCU/ 1AN51y+JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY5IS8wAKCRDRN9E+zzrEiA5dD/ 9Pnutdugl35mpmsNND3wkKYkKNVhDCU8yN/9efaLGUCOX18rdNdmF3OzB74ulcQOMbIr2OtHsVwpo8 774SGkYfiSh0s0ZSNKxDlPkWagoP/wxCj6puE4NrwNwCTkvB3kzOeS4FZClutvKDzN/fds75Ulgh9N S1EjqrWZhSjN+O/xNJmUUun3CDqvmnmT6D2U+mTDX8RqDAEAp1bNjEObBHgL1hVqYifBtLOZS667yS YLSakIjNXJt2J+TIHfM0r+gMO7RIbhNef66a9jQ4Xg2BLiReHsLjFTbeU9C3K9GHvPqkIk1kI/FJ88 MQRmL8zMvoSiStw8sG5VHDDS/OFaWUCl4rtasmT+CtoQ4EvL17sn42HGV64EDP0kIERzVUnmnQXxXv 8G/IblodKjK7Yx8/ABySgL3/I3sV1HAwgA0Nfnc6NhEA5yjDHQx//rajtVLww7eodwn+zhovNohNm1 AVd1ovekbd3kDn1Wtpz00x+ED5qhmZaNi1+IA++Kk+l5J/Uzk1GiY8zAhVg7s07Jky/3/c7XVrCRhe rfGeUjRbUmaQnUpA9r7+4wtRsHWQerSnZK6D3nVj2Kz6nc+1Sv2os3AMUPdrbXpKNfHSRzc1QmqDDm W2PLGlbTyb6BM1UX4yi0LbPbZivH+/xZAuHbRinNocOiAsO8108osa8eKAAg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751665170293798157?= X-GMAIL-MSGID: =?utf-8?q?1751665170293798157?= Both syscalls (kexec and kexec_file) do the same check, lets factor it out. Signed-off-by: Ricardo Ribalda --- include/linux/kexec.h | 3 ++- kernel/kexec.c | 2 +- kernel/kexec_core.c | 11 ++++++++++- kernel/kexec_file.c | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 41a686996aaa..e9e1ab5e8006 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -406,7 +406,8 @@ extern int kimage_crash_copy_vmcoreinfo(struct kimage *image); extern struct kimage *kexec_image; extern struct kimage *kexec_crash_image; -extern int kexec_load_disabled; + +bool kexec_load_permited(void); #ifndef kexec_flush_icache_page #define kexec_flush_icache_page(page) diff --git a/kernel/kexec.c b/kernel/kexec.c index cb8e6e6f983c..d83fc9093aff 100644 --- a/kernel/kexec.c +++ b/kernel/kexec.c @@ -193,7 +193,7 @@ static inline int kexec_load_check(unsigned long nr_segments, int result; /* We only trust the superuser with rebooting the system. */ - if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) + if (!kexec_load_permited()) return -EPERM; /* Permit LSMs and IMA to fail the kexec */ diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index ca2743f9c634..18bd90ca9c99 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -928,7 +928,7 @@ int kimage_load_segment(struct kimage *image, struct kimage *kexec_image; struct kimage *kexec_crash_image; -int kexec_load_disabled; +static int kexec_load_disabled; #ifdef CONFIG_SYSCTL static struct ctl_table kexec_core_sysctls[] = { { @@ -952,6 +952,15 @@ static int __init kexec_core_sysctl_init(void) late_initcall(kexec_core_sysctl_init); #endif +bool kexec_load_permited(void) +{ + /* + * Only the superuser can use the kexec syscall and if it has not + * been disabled. + */ + return capable(CAP_SYS_BOOT) && !kexec_load_disabled; +} + /* * No panic_cpu check version of crash_kexec(). This function is called * only when panic_cpu holds the current CPU number; this is the only CPU diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 45637511e0de..701147c118d4 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -330,7 +330,7 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd, struct kimage **dest_image, *image; /* We only trust the superuser with rebooting the system. */ - if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) + if (!kexec_load_permited()) return -EPERM; /* Make sure we have a legal set of flags */