From patchwork Wed Dec 21 12:50:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 35357 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp3504949wrn; Wed, 21 Dec 2022 04:53:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXsQGktnubKPUww5NgnPd7EC1e2NzwuCuSrMtRO1Lz92UqJKdWT8ViUxIvrX/kJJ22UtGjRS X-Received: by 2002:a50:ef11:0:b0:461:f201:2d98 with SMTP id m17-20020a50ef11000000b00461f2012d98mr1433809eds.3.1671627179891; Wed, 21 Dec 2022 04:52:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671627179; cv=none; d=google.com; s=arc-20160816; b=HWOk9XlJvOHH4+/1Mw3tbZrVfpDUecXD7yB7l/AWIv2WomupVlwM9oBKdg55YUQXVh QbrBZabL2+OV2hxetTviWnnqKIEF0yInATRVppDQcZA/TtR+QU6UK3ajof8OkBIlnAQh /Gg90M42Hfl55H9nj3vH9qIlIvKmSkfoW2IWeYiqRM7H46KWDYvinKPu9yroIFcsk0+5 6y2a/LyBEz4XBxpMM46QarGD4uVcq3NSJYl7iMeGa+t5tkx6psan10GdJtS2Eg53+qpB ym2ovWC6peKZv10ktc4bPHTrFdzt55wLRhC4mh4bh+uZ+dG2Ejj+B1bjl0ojjBENKBDX whPQ== 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=0uo7tfZaiqOin2cp8QemVMZCCRCz92hxCxC8vhk5Wl8=; b=H3z4VGwNcf7Hp8J13LGV+/f1zsvR3cEcUuVPNZy+wXKdHahveWjyNf/KD586h4460T nmpPyr5rf2TjTWveBXN+b6ns9HM3chtPzDLp2A0oCPAjjYo2YTopB/+kiHPYTS192gVI rQpClEp2U1WFF9Nosdoz/+OJD2zQ0I3UqyRm1qhga2qxU3qkwPMuDeeN5tgZyNjI1tpw CGbNeOZA0jdbBM3vKi49rcBSYGezA9GFU1/j1f8fq2XFQDfJiRYcglixUyBs0ovZxAIs NISr7nc5DJ0mZW7IxKRM6+3WLcJDyUt5o6qqEiq/8TsAKonFzQki3KRYa0gYuiqsa8SC lmSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=R2RT15AS; 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 fq32-20020a1709069da000b007c4fda74f69si9774255ejc.241.2022.12.21.04.52.36; Wed, 21 Dec 2022 04:52:59 -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=R2RT15AS; 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 S232455AbiLUMu1 (ORCPT + 99 others); Wed, 21 Dec 2022 07:50:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231818AbiLUMuU (ORCPT ); Wed, 21 Dec 2022 07:50:20 -0500 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2A8422BF5 for ; Wed, 21 Dec 2022 04:50:18 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id fc4so36461562ejc.12 for ; Wed, 21 Dec 2022 04:50:18 -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=0uo7tfZaiqOin2cp8QemVMZCCRCz92hxCxC8vhk5Wl8=; b=R2RT15ASWFQQ+HJe+0sCo+uF2kmwezaj3G470IQ3VwrN9sGIz7hH7YtE7+LcKHt6Cj oJw2V0LJVsPN/SBndPAGkCuI6O+DXEN9nMef2mvpzYidC5dI2ivk5DxCDGzkMzM1FMVl QVCsZJW6B1pti7LEpdEOt0AR1rG8O44Pgjaag= 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=0uo7tfZaiqOin2cp8QemVMZCCRCz92hxCxC8vhk5Wl8=; b=S1grZ1xWhcS/UqvylDTUuTb1lq8v5bmUSVYG8YCjKAi7nP4nVOTFVqvdMssEu+NFe2 aTIElr5Y9BrujuTz0vQeglIkdrjzq+VxZ43ne1rPGMdlYE7BeYXPoJnALkxXrHZtK7jq ms1YAMRS3ZUTnXBjNxsN3yBKFUq1EYhqcm5rxDoMXlBUfJ3+YFhJ/yB3Kr2FAK16kxfG 2RiJDlw72AnnnCXneCLm8B0Fxpr4eHrIVcBEp8gHbNlCpyLAJ0xZHMgIQ3szxH0MvFr6 iLpQFMwYcPLDnzCL2u51vRmt+7EjwmKlAYex/XOFj5KZpMCZ1QUf5hZLz3krkzMGaZuB D1/A== X-Gm-Message-State: AFqh2kr6mmd8F2kDHwgwh92spL50nvm6WabqXXknmvG1rQ7FsN6nWn23 +NQ/RNiVMw+UPPhTzYZQ4tFb9A== X-Received: by 2002:a17:907:d10:b0:82d:e258:fa55 with SMTP id gn16-20020a1709070d1000b0082de258fa55mr1453100ejc.36.1671627018527; Wed, 21 Dec 2022 04:50:18 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:4b82:b1f8:cb4f:fbbd]) by smtp.gmail.com with ESMTPSA id w7-20020a170906b18700b007c0b28b85c5sm7039250ejy.138.2022.12.21.04.50.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Dec 2022 04:50:17 -0800 (PST) From: Ricardo Ribalda Date: Wed, 21 Dec 2022 13:50:02 +0100 Subject: [PATCH v4 2/3] kexec: Factor out kexec_load_permitted MIME-Version: 1.0 Message-Id: <20221114-disable-kexec-reset-v4-2-ab809c8d988c@chromium.org> References: <20221114-disable-kexec-reset-v4-0-ab809c8d988c@chromium.org> In-Reply-To: <20221114-disable-kexec-reset-v4-0-ab809c8d988c@chromium.org> To: Jonathan Corbet , Philipp Rudo , Eric Biederman , "Guilherme G. Piccoli" Cc: kexec@lists.infradead.org, Ricardo Ribalda , Ross Zwisler , Steven Rostedt , "Joel Fernandes (Google)" , Sergey Senozhatsky , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2724; i=ribalda@chromium.org; h=from:subject:message-id; bh=rm7FZlNI1K3HcWDQ5vaPpIW63r8rInBklo9MHxPCtFE=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjowECkFe21vSuqwF6tn2p8L+BKJ7tkuaiu4yiHveG nMtEFu+JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY6MBAgAKCRDRN9E+zzrEiO7dD/ 0XCbh5Qt8a1g8a4Ymj0sdar9KtUlQmWiFta0b5GfuPwcKQF19M9Med6ybxz+sbYW42P4SmN9A2XNec hNqPXR722Yi+HMUx4Qd/B+ioG5bVXLpahjFybWh0OzFejOEsoFCdP+ZIGKqe7i6jIfWWayud7NCbIL 6CXuHhCyrTGg8tpp3wsxfnogoFnEi6Q252E+w1Jgmlyyd/u1tMQbVSJ5XRnOpVTOPWFLecrDwZLRkm vJrOhRDdEnRjGh8q0E12ff/RTZ9giik6eekXfuPzJP6cI9mynKpzBzk8lxlqJzv/c1AdGr1eBvidU7 SwH9z8VvgHgK6vuN/A2DcxIC2Nce4PgvCzE6HLf6N1SaFL1iLZgB1GfFvYRxKf3cstzH9PVF2aYtlm r+QOw7ZwzrUnuYCZ+oULXDrpN9BNvxXwiG8Z2XdLWWqApjvxbZg72DlSXqkoiXK1k67Lj60HEehYyc jW+mz5MmMhqWT9rPRTbOMYdx+ppB4Szq4LEWarobPro9UZ8hhekXyXhYqgdncIvlR707JCUDjuEfpa Y2/uGgzDL2NaIOghN2/Y6X29qA6cfq4aR5wEEKHh8YhUEJBL/lHtsVN//Na8/DHod2e95JHXdO1EFQ JCZkKRC/0AcMQmLyUH832g1pR/uqXtS1FKqQfFmUTzN8WRH+qZmdP3k0Psng== 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?1752828141849981911?= X-GMAIL-MSGID: =?utf-8?q?1752828141849981911?= 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..182e0c11b87b 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_permitted(void); #ifndef kexec_flush_icache_page #define kexec_flush_icache_page(page) diff --git a/kernel/kexec.c b/kernel/kexec.c index cb8e6e6f983c..ce1bca874a8d 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_permitted()) return -EPERM; /* Permit LSMs and IMA to fail the kexec */ diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index ca2743f9c634..a1efc70f4158 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_permitted(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..29efa43ea951 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_permitted()) return -EPERM; /* Make sure we have a legal set of flags */