kexec: factor out kexec_load_permitted
authorRicardo Ribalda <ribalda@chromium.org>
Wed, 4 Jan 2023 14:38:47 +0000 (15:38 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 3 Feb 2023 06:50:04 +0000 (22:50 -0800)
Both syscalls (kexec and kexec_file) do the same check, let's factor it
out.

Link: https://lkml.kernel.org/r/20221114-disable-kexec-reset-v6-2-6a8531a09b9a@chromium.org
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Acked-by: Baoquan He <bhe@redhat.com>
Cc: Bagas Sanjaya <bagasdotme@gmail.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Guilherme G. Piccoli <gpiccoli@igalia.com>
Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Philipp Rudo <prudo@redhat.com>
Cc: Ross Zwisler <zwisler@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/kexec.h
kernel/kexec.c
kernel/kexec_core.c
kernel/kexec_file.c

index 5dd4343c1bbe53f3b6ad5ff17e4e7311bdb2e04e..f18a3c9e813b532482c1cfa2d63f311b5206f9b4 100644 (file)
@@ -403,7 +403,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)
index cb8e6e6f983c7900e9950ed2e0b719ff4a524f03..ce1bca874a8d063449ca94fa793efba9bc4fceaa 100644 (file)
@@ -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 */
index 969e8f52f7da9ca12cf1bd16442fd080a23a1dd5..d51ebbaeb1b29bdae7c69e3e44af935bff625464 100644 (file)
@@ -922,7 +922,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[] = {
        {
@@ -946,6 +946,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
index dd5983010b7b09764cc7c766067c67722c417367..c897eb4b8c8cda93cbdc5935b1cb4d8ea39a03c3 100644 (file)
@@ -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 */