Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp11191rdb; Thu, 7 Sep 2023 11:56:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFr9KBpQvN92rTpcgyU2Z6kNPpo0HLG2m3J48kuUUj0mqMY6xxhf7WFxnE/QcWDV09tYyOM X-Received: by 2002:a05:6871:70e:b0:1d5:1a99:537f with SMTP id f14-20020a056871070e00b001d51a99537fmr505055oap.2.1694113009931; Thu, 07 Sep 2023 11:56:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694113009; cv=none; d=google.com; s=arc-20160816; b=PuCdgW0SfJKk9Q8tWmrSIqAov5ple//nw9muu8fMSnErgsBtulqDjHvujaBwG6sSnG J66h1pG5DapYcWYuiwb5dshAWG8KWuozCcSGWN2BUoML/61eY5AUtbgMEV5JWTV+MdBy ZJo9nWgZ1aByZzBkFS6TRH0wf836ahVFAJRY1cu4UQtzqOFGd4zE7P6v0PJE5ryD4wGC pUViCNMTMQpHQ5M+zxX3nTjhKyzA5p7GrNdXoQ3Er+UwRuA2ysyTmp/X9Epy/wKlhbqg yky6XN4ritmd8DIY9Jyvw8I7hLoXPeafLRgbGB5adxYPxFou19UMAtCyjQMyc31UnyiR asaw== 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:message-id:date:thread-index :thread-topic:subject:cc:to:from; bh=kfOBNVVOsFihiJoo8TqskznDr9SoJw4QVd535NwGx40=; fh=VJzftny5b1h5Id+kgqO6gbdIO9SuGBDW/m00uXYMUvI=; b=DAe4nc6WpoPHZDCUeIMUqaWWId2eqVjMWRZHXKj7aAUiRpw6UI832QQu42MLGbybeK XbBPuIIC7dBra3IBYlRwBvzBHxgA2iKAmITUp73QY7XCnnh/JHPhN8UsB3WEHU9eMwf3 RYmYRV6dK5OJBo+wYDhWlOP3hXTffrE1hr7DIFHUwAYeYUYLBMat1QUCZ7zTkXnaeOFR 0NCyqFUSSKEY4qWmyROcEftjQaPWxt+1Va63HnuKcfX4a0Y/pC5wt2Rm8yr7CNgLydGG gcwOLBXLGsX2Z/RMnJVETmaNgiWSFsbbeljCQsA6q5aoWJK8m3qJkuk9/hafSZR6oWMC rh1Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o24-20020a637318000000b005740eeadb59si381627pgc.518.2023.09.07.11.56.36; Thu, 07 Sep 2023 11:56:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S237642AbjIGQJK convert rfc822-to-8bit (ORCPT + 99 others); Thu, 7 Sep 2023 12:09:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241692AbjIGQIa (ORCPT ); Thu, 7 Sep 2023 12:08:30 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC91F2729 for ; Thu, 7 Sep 2023 09:02:25 -0700 (PDT) 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-251-8f0ph68FMmGbI3MMsr5Spg-1; Thu, 07 Sep 2023 13:42:24 +0100 X-MC-Unique: 8f0ph68FMmGbI3MMsr5Spg-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 7 Sep 2023 13:42:20 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Thu, 7 Sep 2023 13:42:20 +0100 From: David Laight To: 'Vlastimil Babka' , "linux-kernel@vger.kernel.org" , "'linux-mm@kvack.org'" CC: 'Kees Cook' , 'Christoph Lameter' , 'Pekka Enberg' , 'David Rientjes' , 'Joonsoo Kim' , 'Andrew Morton' , 'Eric Dumazet' , "Hyeonggon Yoo" <42.hyeyoo@gmail.com>, Roman Gushchin Subject: Subject: [PATCH v2] slab: kmalloc_size_roundup() must not return 0 for non-zero size Thread-Topic: Subject: [PATCH v2] slab: kmalloc_size_roundup() must not return 0 for non-zero size Thread-Index: Adnhh8rbtLpHk7QBQE+HpPR0NWDZ5g== Date: Thu, 7 Sep 2023 12:42:20 +0000 Message-ID: <4d31a2bf7eb544749023cf491c0eccc8@AcuMS.aculab.com> 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: 8BIT X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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()") --- v2: - Use KMALLOC_MAX_SIZE for upper limit. (KMALLOC_MAX_SIZE + 1 may give better code on some archs!) - Invert test for overlarge for consistency. - Put a likely() on result of kmalloc_slab(). mm/slab_common.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index cd71f9581e67..0fb7c7e19bad 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -747,22 +747,22 @@ 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; + 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; + } + /* Above the smaller buckets, size is a multiple of page size. */ - if (size > KMALLOC_MAX_CACHE_SIZE) + if (size && size <= KMALLOC_MAX_SIZE) return PAGE_SIZE << get_order(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. */ + return size; + } EXPORT_SYMBOL(kmalloc_size_roundup); -- 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)