From patchwork Wed Sep 6 08:18:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13375361 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 ACB49EB8FA5 for ; Wed, 6 Sep 2023 08:18:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1999A280007; Wed, 6 Sep 2023 04:18:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 123448E0014; Wed, 6 Sep 2023 04:18:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F056A280007; Wed, 6 Sep 2023 04:18:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D87B18E0014 for ; Wed, 6 Sep 2023 04:18:31 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A44E21CA206 for ; Wed, 6 Sep 2023 08:18:31 +0000 (UTC) X-FDA: 81205470822.21.78DFF7E Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf18.hostedemail.com (Postfix) with ESMTP id D7BDB1C000F for ; Wed, 6 Sep 2023 08:18:27 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.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=1693988309; 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: references; bh=cySan9vzYwg6OeNMYI6496o5udOFhoDgH5aK8yeLjiI=; b=IGuEwKFEOCw7i1nXaLYhjp9cuWxFQ1FJrOthZHsYI4rpDTeEpIQ3FTHKfGwGpiBWqHiVwd +suNbMkFXWSjC8Iq6/l/AWjHlCgASAIqC1McCgx7KPpddUK6wrArtINbmln5ErLQuS4Lq8 +r/EI03gvIVPNAy05klMQ6agXnWYsfg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.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=1693988309; a=rsa-sha256; cv=none; b=W6ZqWAJGWuYZ0NedozgL6uuFC0YYOF1IiQ5XJ9ILgRNUPB62J7Sf9ZHVKCdReKVcWKq3Au BQPoIiFX77ckfzvuKVGpxuW99qB7tRQQ9GjzxX5MfGS9Wkk4i47vllytZ9RF43n1a9OnSD i2S73XpiupiOXAFJVOMy6v/BEhmFaAc= 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-272-IP8p3ZuMPfaowpqSVhG-ZA-1; Wed, 06 Sep 2023 09:18:25 +0100 X-MC-Unique: IP8p3ZuMPfaowpqSVhG-ZA-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; Wed, 6 Sep 2023 09:18:21 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 6 Sep 2023 09:18:21 +0100 From: David Laight To: "linux-kernel@vger.kernel.org" , "'linux-mm@kvack.org'" CC: 'Kees Cook' , 'Vlastimil Babka' , 'Christoph Lameter' , 'Pekka Enberg' , 'David Rientjes' , 'Joonsoo Kim' , 'Andrew Morton' , "'Eric Dumazet'" Subject: [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: AdngmrOo1jL765q1RK2Ib5JvSvHtFw== Date: Wed, 6 Sep 2023 08:18:21 +0000 Message-ID: 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 X-Rspamd-Queue-Id: D7BDB1C000F X-Rspam-User: X-Stat-Signature: fs4xfr5rk61jkgd8hebe983m1k6sqhbe X-Rspamd-Server: rspam01 X-HE-Tag: 1693988307-493455 X-HE-Meta: U2FsdGVkX1/wAyqMxnzXYESoZ7nuomnJB/JHpCXGyU+UQKyn6J6mdc42TdkIwSVx6WwvQ5bAoccwLZfurPQudO92bfN80DK8VDs3+pEseavvieBtbuIe69Y1UMMn+0sto33DA7PcVCufZnX4+49JCkB2itEUD0LDw1x8/Mc5SEsv2SSB/f38gNQcxFZsJ+LrsEK67Yt4kjmwtI4QwxiMvfR2nIy+zC/kbEcpVkjHRNMWqcnHJjuTDlrZFUX6PRHxmOiDCPNczYihL8yFECJcJNLKJbqAYkgR+RedbiQ0f6HOYpa5joWmGgy30M/LnrEp2Zk6vA/AyOjrcOyxnw/UPXOwolITIXUwLIWUa/K9ozZN3zoE9Y/pIRViAoC7MsZsSi8p4bNaqIGfJOFsyiYSMZssOF7OzsYZG+tx9EnhhjjBWGnVutqXCG7ftzjpZZSSPTzFjJm4ILCKsBABlM9mIHYfbZAFvxH3pwJmbavvnsqLzPJiYJKX8pMevJW7w8K3uJeNnZOxipYKdqly0O5oSCG/tvuGx1L0R2LF6SwSJU+0cFA4Jv5T/KCWXQ7+AcX4BZkhFuIiDUXW7Bt0XyAmY3kxF3lrNbvFB/APQjJ/wWmZ0XZ73rQv+3+iBQy5QrCDc9TqOXI1tK7WPdwur4s/0R2rwDxzs0P91RQgS0C4NjSy5EFs+ETi98vJjLDwGnWiLdUCUmRGbk2DTtE2EttQwwphSgWNHVU+rvVe4GAJ9H4RU5go232FRwdGBmdwWzWcLgoohe+mmGl7fdgWyoUMqikBHcCttlUWLAv6FliSTH74HpGz/H4dPja9P836bP5GRTZlZL/baaTiMo6xQOYU5kVMwl1SC4Vi+0m4eue20WQR1Hmxs2/CsDKlEo1lYBICIILSwOfQO+PY+LqrdFr0g/6DYP1Z6UiTu70pBCataOI+q1HTuWWi8k/HKATX8ySGdTVWXha6JLBuvhamfuW ymqkiq6C ZJXy0l/LhSJ6JCZrjS5KKI+mXVgCOXxLdMDuqoizhuyRScm9KcdsSr5wideLH8QCRwIEjO6BPm9F20ag1vBtDFEYUqJ7vvXmRfY9sgkI3ECqLo9/+hzSdd+/MJEftALhdzmgO+YUWRzdTVV7JdHqIymGJXxpG8PlNH+OVKr5ooWu73NXmxJl88LcEF63SNG9H8axP7Hjh5s8mrxaA6CcL0/2fZGeNHF8YfUEdlVHOCMip+/gO6ekWasOPrH7AmG/7B2R5ehV6upvCBK1eqKzxpJlEPC+FkZyMZDaIqOV+ivTUHRkp5LsKAxOTwVVO6yF+4kPat1UvJjbrUUREMeCEqyIl0A== 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: The typical use of kmalloc_size_roundup() is: ptr = kmalloc(sz = kmalloc_size_roundup(size), ...); if (!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. The original patch also added kmalloc_size_roundup() to mm/slob.c that can also round up a value to zero - but has since been removed. mm/slab_common.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index cd71f9581e67..8418eccda8cf 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -747,22 +747,21 @@ size_t kmalloc_size_roundup(size_t size) { struct kmem_cache *c; - /* Short-circuit the 0 size case. */ - if (unlikely(size == 0)) - return 0; - /* Short-circuit saturated "too-large" case. */ - if (unlikely(size == SIZE_MAX)) - return SIZE_MAX; - /* Above the smaller buckets, size is a multiple of page size. */ - if (size > KMALLOC_MAX_CACHE_SIZE) - return PAGE_SIZE << get_order(size); + if (size && size <= KMALLOC_MAX_CACHE_SIZE) { + /* + * The flags don't matter since size_index is common to all. + * Neither does the caller for just getting ->object_size. + */ + c = kmalloc_slab(size, GFP_KERNEL, 0); + return likely(c) ? c->object_size : size; + } - /* - * The flags don't matter since size_index is common to all. - * Neither does the caller for just getting ->object_size. - */ - c = kmalloc_slab(size, GFP_KERNEL, 0); - return c ? c->object_size : 0; + /* Return 'size' for 0 and very large - kmalloc() may fail. */ + if (unlikely((size - 1) >> (sizeof (long) == 8 ? 34 : 30))) + return size; + + /* Above the smaller buckets, size is a multiple of page size. */ + return PAGE_SIZE << get_order(size); } EXPORT_SYMBOL(kmalloc_size_roundup);