Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1692422pxj; Wed, 19 May 2021 11:35:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxTMYKkv6LUb5dIPzvaWYLNuXQmMrwE8HIGwNUpoGhKFdzdJsbgOCiur4CnEnhFKKtP4dQe X-Received: by 2002:a02:7702:: with SMTP id g2mr340256jac.111.1621449346349; Wed, 19 May 2021 11:35:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621449346; cv=none; d=google.com; s=arc-20160816; b=Qg8IzK/rsQbN4+4IGZA9ByYzWqTMy9msFhI9G56QRgvchk+97qMD5sgNS2n/Nz87zX 8rz0phovPKxul/Gq+8uuNr8PKKM0Clxl3lGIc0LNMis8hC7/Bc7ELPDjO8Cnqo78fxgf xvi2PyGru6X9gGKlRAi9wHm6Hsxky2BfxYkHSyu2VEIaVn6sTF08wDXk/Dlz8EnkkHAA Z82fqBReJ6LB1oLVknH0bjaM6cEcXI2dYpHHdP9srQWnCEw6+wunHJY7m2UuX3pHAZTf 0fEAy4SCDRIaxfaIE88z5Ow854zEpxcJknnVcdJXwdz+CcO14GLXPB5IUJ39QkbTXP6x m0Tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:cc:to:from; bh=NmIFdI+sYubGxqcalvl/1+Gwvj12dEYPv3rgrn6dYE4=; b=Zzw0Sle3/TC32EzsW0hxyqK9Txsi4XHEh0hc2YaKGSrZWR+NfAWPs8/Cqu2mv/wAFj m6CZMWiw/qzlnhln6ZNJmYRtiSHlcm28cEqoc2e1xxbw+IANZkuzeOL/L2M1VorsuMmX PpfCscE9t7EKQXkV8oZ0CejWKfkndAOBMvDHyYC2ujbQBHlPb9IJOiI7RovzHd0dcFeo Ef9m4Es6G6G+RxUsA9kl5Hp4IYTZEfhnCeaa4WI0MT6xkute6wNIhwQ6DROakHyWeIOR SWK4Cv6by09ytOcVS7EzT6Xlb+8mVDFw9ZbnuEWzdPj81PSHOSDJ5H1TpCuqjUyv/Vp3 qKEw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p5si388022ilm.11.2021.05.19.11.35.33; Wed, 19 May 2021 11:35:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232261AbhERUtK convert rfc822-to-8bit (ORCPT + 99 others); Tue, 18 May 2021 16:49:10 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([185.58.86.151]:45378 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238356AbhERUtJ (ORCPT ); Tue, 18 May 2021 16:49:09 -0400 Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-225-zZ4PUMaUP8CQ45Z6-Pf8hg-1; Tue, 18 May 2021 21:47:38 +0100 X-MC-Unique: zZ4PUMaUP8CQ45Z6-Pf8hg-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 18 May 2021 21:47:36 +0100 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.015; Tue, 18 May 2021 21:47:36 +0100 From: David Laight To: 'Arnd Bergmann' , "linux-arch@vger.kernel.org" CC: Arnd Bergmann , Christoph Hellwig , Alexander Viro , Andrew Morton , Borislav Petkov , Brian Gerst , Eric Biederman , Ingo Molnar , "H. Peter Anvin" , Thomas Gleixner , Linux ARM , "linux-kernel@vger.kernel.org" , Linux-MM , "kexec@lists.infradead.org" Subject: RE: [PATCH v3 1/4] kexec: simplify compat_sys_kexec_load Thread-Topic: [PATCH v3 1/4] kexec: simplify compat_sys_kexec_load Thread-Index: AQHXS1wVPpN9Wz+83EGX6Ch7EQlMgqrptkWg Date: Tue, 18 May 2021 20:47:36 +0000 Message-ID: References: <20210517203343.3941777-1-arnd@kernel.org> <20210517203343.3941777-2-arnd@kernel.org> In-Reply-To: <20210517203343.3941777-2-arnd@kernel.org> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arnd Bergmann > Sent: 17 May 2021 21:34 > > 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 0c994ae37729..f61e310d7a85 100644 > --- a/include/linux/kexec.h > +++ b/include/linux/kexec.h > @@ -88,14 +88,12 @@ struct kexec_segment { > size_t memsz; > }; > > -#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 > > #ifdef CONFIG_KEXEC_FILE > struct purgatory_info { > diff --git a/kernel/kexec.c b/kernel/kexec.c > index c82c6c06f051..6618b1d9f00b 100644 > --- a/kernel/kexec.c > +++ b/kernel/kexec.c > @@ -19,21 +19,46 @@ > > #include "kexec_internal.h" > > +static int copy_user_compat_segment_list(struct kimage *image, > + unsigned long nr_segments, > + void __user *segments) > +{ > + struct compat_kexec_segment __user *cs = segments; > + struct compat_kexec_segment segment; > + int i; > + > + for (i = 0; i < nr_segments; i++) { > + if (copy_from_user(&segment, &cs[i], sizeof(segment))) > + return -EFAULT; How many segments are there? The multiple copy_from_user() will be slow. > + > + image->segment[i] = (struct kexec_segment) { > + .buf = compat_ptr(segment.buf), > + .bufsz = segment.bufsz, > + .mem = segment.mem, > + .memsz = 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; > > /* Read in the segments */ > image->nr_segments = nr_segments; > segment_bytes = nr_segments * sizeof(*segments); Should there be a bound check on nr_segments? I can't see one in the code in this patch. > - ret = copy_from_user(image->segment, segments, segment_bytes); > - if (ret) > - ret = -EFAULT; > + if (in_compat_syscall()) > + return copy_user_compat_segment_list(image, nr_segments, segments); > > - return ret; > + if (copy_from_user(image->segment, segments, segment_bytes)) > + return -EFAULT; > + > + return 0; An alternate sequence (which Eric will like even less!) is to do a single copy_from_user() for the entire compat size array into the 'normal' buffer and then do a reverse order conversion of each array entry from 'compat' to '64 bit'. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)