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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84E66EE14D0 for ; Thu, 7 Sep 2023 08:56:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F8E6280016; Thu, 7 Sep 2023 04:56:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A8F18E000F; Thu, 7 Sep 2023 04:56:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7709F280016; Thu, 7 Sep 2023 04:56:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 68FA28E000F for ; Thu, 7 Sep 2023 04:56:01 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 040331CA32D for ; Thu, 7 Sep 2023 08:56:00 +0000 (UTC) X-FDA: 81209194122.29.E85E77C Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf21.hostedemail.com (Postfix) with ESMTP id 2AFE31C0004 for ; Thu, 7 Sep 2023 08:55:56 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694076959; a=rsa-sha256; cv=none; b=R+PY/5PGfCiPxf5FAf2p5CiQM9dgsY0TsvR03unL3SiXF2nIvKLNAHEoOBNn7MG9WZCkMi q2OtNsSnx+xZsV6mrLETP4wmnyUaAx8WcsIJ3FjFxSwOzZJ0EER0EFTRZ7DbeTEkcwe3WG acApOkrNBxaKlKpTqO32ShPyQqoKUp8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694076959; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NYcLNChHA5SNlkbLjo8wXdg7xQfj6Tql0V1Ewoa7iPA=; b=eWf2o5TIJD7Rmimpb9ewZN0LyivHmpGxd/t8HziX1+rSJrK6qGfY0yLrtgoI0qy41u1XWa x8PDgIsiNOqASIforEuHnjiG5bkCa1QK0lrED+nxOnbNTwxECmrglZyRV1shsMIzlxJavV zDhyn3bhvkRA3sCs3GHyzXnyTBnI4FQ= Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-142-mAsshBkwN4ycognTopktyQ-1; Thu, 07 Sep 2023 09:55:54 +0100 X-MC-Unique: mAsshBkwN4ycognTopktyQ-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 7 Sep 2023 09:55:51 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Thu, 7 Sep 2023 09:55:51 +0100 From: David Laight To: 'Kees Cook' CC: "linux-kernel@vger.kernel.org" , "'linux-mm@kvack.org'" , 'Vlastimil Babka' , 'Christoph Lameter' , 'Pekka Enberg' , 'David Rientjes' , 'Joonsoo Kim' , 'Andrew Morton' , "'Eric Dumazet'" Subject: RE: [PATCH] slab: kmalloc_size_roundup() must not return 0 for non-zero size Thread-Topic: [PATCH] slab: kmalloc_size_roundup() must not return 0 for non-zero size Thread-Index: AdngmrOo1jL765q1RK2Ib5JvSvHtFwASyysAACBZzlA= Date: Thu, 7 Sep 2023 08:55:51 +0000 Message-ID: References: <202309061106.C0690BDBB@keescook> In-Reply-To: <202309061106.C0690BDBB@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] MIME-Version: 1.0 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 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2AFE31C0004 X-Stat-Signature: es3p4pju4ebzktspyfe9d39edypgcowz X-HE-Tag: 1694076956-451605 X-HE-Meta: U2FsdGVkX18lCdBwZmjcltx78TOHgZzyjeJvzFconLHdEgeqftjmYcYcqU72OE7YjtCZdy1m4QdOxGP6o60HtpcBgGc5QR1yfzk7YKouOctmMLmVhElC83r/I57Q/3qiAf7R1YSY5x5HxNKDIdK9Wz9J1Wa2XrJmANTTymVjSkplqT2VdjLYXud49VQl6pl40eXXISLzfOcoCsN4JZzvOZA7/3RsIydpDCgliHBF0zlYAuj7rA41qR6GVn/TNIxIiJDPLjWtkfRkG37b+J6rvvlh+JtH2wFxghZ2Hg6W30RHk/lyaQlsn8wt9TxpmEWnTKKDX3KUU1wkvnB5aeGzu1CDrRILDYwPuccVf9SW0/4Ad8gnPCe8U1UCoROZ3I/JwaxeRvQI8vkPFnmozlqY0AwH64HhLzV7cdZarrFWFGttizQAC1rsZpX5el89FErgPCjBddVLOXxHdNO/A8QVs/PZFMfFXP4yWhQ8M5z+dCTz3E8SuJ0kWDvBHrBV4QWHrttgPRTYYYOk2EhfniDSetgQDPSqo/d36DW3SPQddDkaMzT8JVjdwUVdpA4WjlXv/jbE7+NsKa5EhQdPrHW2GrEZWAtQeQ9b0n/u4Rl6KjrZvoeu+6cj+mH95Bd9zlnT6edpWZdTCt7QA+JAPSRpjFb6T4eqCY5Il/f3YxUaGYpcDLBBZBLGK0nxZAFpG1vkA4ATY/hANAQWHIytGKI4d8N7guSAraUDkAFz6NFtsaoIyg94SUc8lQj3kc8TyBX8t8e+MvrHg6bzjbWZZY6K77zbgbhJyzEYhqZP8qnGKs4dzcRBSAKz0yZjf2BUAalvx4iWZamuaDZAoSSvT9STQlrjpuVzjCOMnGJOjO2auy2JW715k6W2II3ghPwzAooONryrlnOJBpzJFPGd+bcNb1dLwn/dJqq21fp9MzRkVyyf+ALPDWoyni4L+GvrBCa/7m7V4xlJdJWGSSZsWeQ aYk1x3ch KkpzHeLa8Kprkqr83bqiKotIyIzEqKTiRQrNT1QiKGSmarOyUejLkMTUqRcBPWnDL6PNFSukL+t+kknFBBkrsm8ptVUZ4E9T8kc8C8HpnV0cred/IIRBzEsUVUXZeaztwIEnjqxy5++NXid3fnFhJbyUgKPcrIruT12YssJFfs2y3W/9XlSC0/r7JDFUYwwf6JzUPPnY9k6OR4eZ+VQGjXVfgJt1Y/Ma1DHKnRshn5Iv2mtuQJaiFXZGP5B1MwlvTMPENPmq3EhKzacf43IrT7MwOaVgn/i5RP9ssvYrSy3A33rR9swAu/8O7AJeuZSj0RNbtoyewV+YPuH0yUH1iTpBGOg== 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: Kees Cook > Sent: 06 September 2023 19:17 >=20 > On Wed, Sep 06, 2023 at 08:18:21AM +0000, David Laight wrote: > > The typical use of kmalloc_size_roundup() is: > > =09ptr =3D kmalloc(sz =3D kmalloc_size_roundup(size), ...); > > =09if (!ptr) return -ENOMEM. > > This means it is vitally important that the returned value isn't > > less than the argument even if the argument is insane. > > In particular if kmalloc_slab() fails or the value is above > > (MAX_ULONG - PAGE_SIZE) zero is returned and kmalloc() will return > > it's single zero-length buffer. > > > > Fix by returning the input size on error or if the size exceeds > > a 'sanity' limit. > > kmalloc() will then return NULL is the size really is too big. > > > > Signed-off-by: David Laight > > Fixes: 05a940656e1eb ("slab: Introduce kmalloc_size_roundup()") > > --- > > The 'sanity limit' value doesn't really matter (even if too small) > > It could be 'MAX_ORDER + PAGE_SHIFT' but one ppc64 has MAX_ORDER 16 > > and I don't know if that also has large pages. > > Maybe it could be 1ul << 30 on 64bit, but it really doesn't matter > > if it is too big. >=20 > I agree that returning 0 for an (impossible to reach) non-zero > is wrong, but the problem seen in netdev was that a truncation happened > for a value returned by kmalloc_size_roundup(). >=20 > So, for the first, it shouldn't be possible for "c" to ever be NULL here: If it isn't possible there is no need to check :-) >=20 > =09c =3D kmalloc_slab(size, GFP_KERNEL, 0); > =09return c ? c->object_size : 0; >=20 > But sure, we can return KMALLOC_MAX_SIZE for that. Isn't KMALLOC_MAX_SIZE actually valid? - so would be wrong. Returning 'size' is always valid, the later kmalloc() will almost certainly fail, but it is also ok if it suceeds. > The pathological case was this: s/pathological/failing/ >=20 > =09unsigned int truncated; > =09size_t fullsize =3D UINT_MAX + 1; >=20 > =09ptr =3D kmalloc(truncated =3D kmalloc_size_roundup(fullsize), ...); The actual pathological case is: =09kmalloc(kmalloc_size_roundup(~0ULL - PAGESIZE/2), ...) which is kmalloc(0, ...) and suceeds. =09 > Should the logic be changed to return KMALLOC_MAX_SIZE for anything > larger than KMALLOC_MAX_SIZE? This seems like a different kind of > foot-gun. >=20 > Everything else in the allocator sanity checking (e.g. struct_size(), > etc) uses SIZE_MAX as the saturation value, which is why > kmalloc_size_roundup() did too. SIZE_MAX (aka ~0ull) seems far too large for sanity checking lengths. (Even without the issue of having no headroom.) A limit related to an upper bound for vmalloc() would probably be more appropriate, or maybe just a limit based on kernel VA. So for 32bit 2^30 is way too large for any kind of allocate. For 64bit you can go higher (even if the allocators can't support the values), maybe 2^48? =09David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)