From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A4FC2583 for ; Wed, 4 May 2022 16:09:03 +0000 (UTC) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-304-235WxlwhNjKFdg4Xv28oCg-1; Wed, 04 May 2022 17:08:18 +0100 X-MC-Unique: 235WxlwhNjKFdg4Xv28oCg-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.32; Wed, 4 May 2022 17:08:15 +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.033; Wed, 4 May 2022 17:08:15 +0100 From: David Laight To: 'Kees Cook' , Johannes Berg CC: "Gustavo A . R . Silva" , Keith Packard , Francis Laniel , Daniel Axtens , Dan Williams , Vincenzo Frascino , Guenter Roeck , Daniel Vetter , Tadeusz Struk , Alexei Starovoitov , "alsa-devel@alsa-project.org" , Al Viro , Andrew Gabbasov , Andrew Morton , Andy Gross , Andy Lavr , Arend van Spriel , "Baowen Zheng" , Bjorn Andersson , Boris Ostrovsky , Bradley Grove , "brcm80211-dev-list.pdl@broadcom.com" , Christian Brauner , =?iso-8859-1?Q?Christian_G=F6ttsche?= , Christian Lamparter , Chris Zankel , Cong Wang , "David Gow" , David Howells , "David S. Miller" , Dennis Dalessandro , "devicetree@vger.kernel.org" , Dexuan Cui , "Dmitry Kasatkin" , Eli Cohen , "Eric Dumazet" , Eric Paris , "Eugeniu Rosca" , Felipe Balbi , Frank Rowand , Franky Lin , "Greg Kroah-Hartman" , Gregory Greenman , Haiyang Zhang , "Hante Meuleman" , Herbert Xu , Hulk Robot , Jakub Kicinski , "James E.J. Bottomley" , James Morris , Jarkko Sakkinen , Jaroslav Kysela , Jason Gunthorpe , Jens Axboe , Johan Hedberg , John Keeping , Juergen Gross , Kalle Valo , "keyrings@vger.kernel.org" , "kunit-dev@googlegroups.com" , Kuniyuki Iwashima , "K. Y. Srinivasan" , "Lars-Peter Clausen" , Lee Jones , Leon Romanovsky , Liam Girdwood , "linux1394-devel@lists.sourceforge.net" , "linux-afs@lists.infradead.org" , "linux-arm-kernel@lists.infradead.org" , "linux-arm-msm@vger.kernel.org" , "linux-bluetooth@vger.kernel.org" , "linux-hardening@vger.kernel.org" , "linux-hyperv@vger.kernel.org" , "linux-integrity@vger.kernel.org" , "linux-rdma@vger.kernel.org" , "linux-scsi@vger.kernel.org" , "linux-security-module@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-wireless@vger.kernel.org" , "linux-xtensa@linux-xtensa.org" , "llvm@lists.linux.dev" , Loic Poulain , Louis Peens , Luca Coelho , "Luiz Augusto von Dentz" , Marc Dionne , Marcel Holtmann , Mark Brown , "Martin K. Petersen" , "Max Filippov" , Mimi Zohar , Muchun Song , Nathan Chancellor , "netdev@vger.kernel.org" , Nick Desaulniers , =?iso-8859-1?Q?Nuno_S=E1?= , Paolo Abeni , Paul Moore , "Rich Felker" , Rob Herring , Russell King , "selinux@vger.kernel.org" , "Serge E. Hallyn" , "SHA-cyfmac-dev-list@infineon.com" , Simon Horman , Stefano Stabellini , Stefan Richter , Steffen Klassert , Stephen Hemminger , Stephen Smalley , Takashi Iwai , Tom Rix , Udipto Goswami , "wcn36xx@lists.infradead.org" , Wei Liu , "xen-devel@lists.xenproject.org" , Xiu Jianfeng , "Yang Yingliang" Subject: RE: [PATCH 02/32] Introduce flexible array struct memcpy() helpers Thread-Topic: [PATCH 02/32] Introduce flexible array struct memcpy() helpers Thread-Index: AQHYX80GRJFxZRupFEigWcMQWGiaSK0O4MfQ Date: Wed, 4 May 2022 16:08:15 +0000 Message-ID: References: <20220504014440.3697851-1-keescook@chromium.org> <20220504014440.3697851-3-keescook@chromium.org> <202205040819.DEA70BD@keescook> In-Reply-To: <202205040819.DEA70BD@keescook> 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] Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: 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: quoted-printable From: Kees Cook > Sent: 04 May 2022 16:38 ... > > > struct something *instance =3D NULL; > > > int rc; > > > > > > rc =3D mem_to_flex_dup(&instance, byte_array, count, GFP_KERNEL); > > > if (rc) > > > return rc; > > > > This seems rather awkward, having to set it to NULL, then checking rc > > (and possibly needing a separate variable for it), etc. >=20 > I think the errno return is completely required. I had an earlier version > of this that was much more like a drop-in replacement for memcpy that > would just truncate or panic, and when I had it all together, I could > just imagine hearing Linus telling me to start over because it was unsafe > (truncation may be just as bad as overflow) and disruptive ("never BUG"), > and that it should be recoverable. So, I rewrote it all to return a > __must_check errno. >=20 > Requiring instance to be NULL is debatable, but I feel pretty strongly > about it because it does handle a class of mistakes (resource leaks), > and it's not much of a burden to require a known-good starting state. Why not make it look like malloc() since it seems to be malloc(). That gives a much better calling convention. Passing pointers and integers by reference can generate horrid code. (Mostly because it stops the compiler keeping values in registers.) If you want the type information inside the 'function' use a #define so that the use is: =09mem_to_flex_dup(instance, byte_array, count, GFP_KERNEL); =09if (!instance) =09=09return ... (or use ERR_PTR() etc). =09David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)