From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51CBCC55178 for ; Mon, 2 Nov 2020 12:32:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BEBEC2417A for ; Mon, 2 Nov 2020 12:32:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="EyZhfqCu" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BEBEC2417A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 379E36B006E; Mon, 2 Nov 2020 07:32:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 32B7C6B0070; Mon, 2 Nov 2020 07:32:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F30F6B0071; Mon, 2 Nov 2020 07:32:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E302C6B006E for ; Mon, 2 Nov 2020 07:32:21 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 813391EE6 for ; Mon, 2 Nov 2020 12:32:21 +0000 (UTC) X-FDA: 77439416082.21.prose01_0717f4b272b0 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 63EF8180442C2 for ; Mon, 2 Nov 2020 12:32:21 +0000 (UTC) X-HE-Tag: prose01_0717f4b272b0 X-Filterd-Recvd-Size: 6841 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Mon, 2 Nov 2020 12:32:20 +0000 (UTC) Received: from localhost.localdomain (unknown [192.30.34.233]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BB355222BA; Mon, 2 Nov 2020 12:32:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604320339; bh=pR0lLs09MNuIjT8UFqkl89v87fssHHRgqxxRCJAIoSA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EyZhfqCu6aBJFN3qmiNYQdFatyVKmXN1QpFwf9vRhlzU8iG48Enacz25lEU0bTqRn OoZqSKJh+W1xIvhiUqa2xNg5t74NPZVLvMnJtMywvbKX1bH8UE0c8LN7zJ2c9FTEWg su188ogt7UTw5LluoD95V+4+fHUjSVEzlkR/A0Xo= From: Arnd Bergmann To: linux-arch@vger.kernel.org Cc: Arnd Bergmann , Alexander Viro , Andrew Morton , Andy Lutomirski , Borislav Petkov , Brian Gerst , Christoph Hellwig , Eric Biederman , Ingo Molnar , "H . Peter Anvin" , Thomas Gleixner , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kexec@lists.infradead.org Subject: [PATCH v2 1/4] kexec: simplify compat_sys_kexec_load Date: Mon, 2 Nov 2020 13:31:48 +0100 Message-Id: <20201102123151.2860165-2-arnd@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201102123151.2860165-1-arnd@kernel.org> References: <20201102123151.2860165-1-arnd@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Arnd Bergmann The compat version of sys_kexec_load() uses compat_alloc_user_space to convert the user-provided arguments into the native format. Move the conversion into the regular implementation with an in_compat_syscall() check to simplify it and avoid the compat_alloc_user_space() call. compat_sys_kexec_load() now behaves the same as sys_kexec_load(). Signed-off-by: Arnd Bergmann --- include/linux/kexec.h | 2 - kernel/kexec.c | 95 +++++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 55 deletions(-) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 9e93bef52968..7b6717cd5c4a 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -88,14 +88,12 @@ struct kexec_segment { size_t memsz; }; =20 -#ifdef CONFIG_COMPAT struct compat_kexec_segment { compat_uptr_t buf; compat_size_t bufsz; compat_ulong_t mem; /* User space sees this as a (void *) ... */ compat_size_t memsz; }; -#endif =20 #ifdef CONFIG_KEXEC_FILE struct purgatory_info { diff --git a/kernel/kexec.c b/kernel/kexec.c index c82c6c06f051..ec04791eea3e 100644 --- a/kernel/kexec.c +++ b/kernel/kexec.c @@ -19,21 +19,46 @@ =20 #include "kexec_internal.h" =20 +static int copy_user_compat_segment_list(struct kimage *image, + unsigned long nr_segments, + void __user *segments) +{ + struct compat_kexec_segment __user *cs =3D segments; + struct compat_kexec_segment segment; + int i; + + for (i=3D0; i < nr_segments; i++) { + if (copy_from_user(&segment, &cs[i], sizeof(segment))) + return -EFAULT; + + image->segment[i] =3D (struct kexec_segment) { + .buf =3D compat_ptr(segment.buf), + .bufsz =3D segment.bufsz, + .mem =3D segment.mem, + .memsz =3D segment.memsz, + }; + } + + return 0; +} + + static int copy_user_segment_list(struct kimage *image, unsigned long nr_segments, struct kexec_segment __user *segments) { - int ret; size_t segment_bytes; =20 /* Read in the segments */ image->nr_segments =3D nr_segments; segment_bytes =3D nr_segments * sizeof(*segments); - ret =3D copy_from_user(image->segment, segments, segment_bytes); - if (ret) - ret =3D -EFAULT; + if (in_compat_syscall()) + return copy_user_compat_segment_list(image, nr_segments, segments); =20 - return ret; + if (copy_from_user(image->segment, segments, segment_bytes)) + return -EFAULT; + + return 0; } =20 static int kimage_alloc_init(struct kimage **rimage, unsigned long entry= , @@ -233,8 +258,9 @@ static inline int kexec_load_check(unsigned long nr_s= egments, return 0; } =20 -SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segm= ents, - struct kexec_segment __user *, segments, unsigned long, flags) +static int kernel_kexec_load(unsigned long entry, unsigned long nr_segme= nts, + struct kexec_segment __user * segments, + unsigned long flags) { int result; =20 @@ -265,57 +291,20 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, u= nsigned long, nr_segments, return result; } =20 +SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segm= ents, + struct kexec_segment __user *, segments, unsigned long, flags) +{ + return kernel_kexec_load(entry, nr_segments, segments, flags); +} + #ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry, compat_ulong_t, nr_segments, struct compat_kexec_segment __user *, segments, compat_ulong_t, flags) { - struct compat_kexec_segment in; - struct kexec_segment out, __user *ksegments; - unsigned long i, result; - - result =3D kexec_load_check(nr_segments, flags); - if (result) - return result; - - /* Don't allow clients that don't understand the native - * architecture to do anything. - */ - if ((flags & KEXEC_ARCH_MASK) =3D=3D KEXEC_ARCH_DEFAULT) - return -EINVAL; - - ksegments =3D compat_alloc_user_space(nr_segments * sizeof(out)); - for (i =3D 0; i < nr_segments; i++) { - result =3D copy_from_user(&in, &segments[i], sizeof(in)); - if (result) - return -EFAULT; - - out.buf =3D compat_ptr(in.buf); - out.bufsz =3D in.bufsz; - out.mem =3D in.mem; - out.memsz =3D in.memsz; - - result =3D copy_to_user(&ksegments[i], &out, sizeof(out)); - if (result) - return -EFAULT; - } - - /* Because we write directly to the reserved memory - * region when loading crash kernels we need a mutex here to - * prevent multiple crash kernels from attempting to load - * simultaneously, and to prevent a crash kernel from loading - * over the top of a in use crash kernel. - * - * KISS: always take the mutex. - */ - if (!mutex_trylock(&kexec_mutex)) - return -EBUSY; - - result =3D do_kexec_load(entry, nr_segments, ksegments, flags); - - mutex_unlock(&kexec_mutex); - - return result; + return kernel_kexec_load(entry, nr_segments, + (struct kexec_segment __user *)segments, + flags); } #endif --=20 2.27.0