Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp238971rwb; Mon, 26 Sep 2022 11:39:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM78l5nBp6qFAqOq73Tj/DkXZrVVEZADCQgWzyQVxdOZgVzYkJIkVPzqi+Fl7/DGR+F0JYXY X-Received: by 2002:a17:907:94c6:b0:77d:7ad3:d065 with SMTP id dn6-20020a17090794c600b0077d7ad3d065mr19746986ejc.149.1664217553979; Mon, 26 Sep 2022 11:39:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664217553; cv=none; d=google.com; s=arc-20160816; b=ZGmChJOluFTw4gi7eDWJzTi3qEr+4QUsMF/1qKsUtI+0UDyUNVXlksze5sDq99ux+i DyfEFoiufnTyXYDSxeLMIkUP8rnQhuGU53hZevZpZFPp8FOkZeWxCitHp3uilrKC2CEi 2EcQ6npmHTdm9zauOALelx2lJAn+5BrWaJwwgiCUUa6d72YmKDWBkTG4QVVvEk7M5hdz 23yPlAD2/6u18twik9fnC4X7d/2Ho85AJGkxiR6htpcvWHCK4cj292p1Dc9nTY8O1ga7 KT2FzUZxIURO9QkcHxDJg0sjJCT8jIcnqlVtpDg5v7K3vQDCKiQ6N4pF/tWYnOALy6eq E9UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=4OBTQRXHJygCpY/2rBSvNKQlNNozBBa3mtJlOeJ/EyY=; b=ObhVXGbJkFw53MzX3cRaig2S/+vxgId+bWT0YXkyJvJLj/aNRROCwWhUhBJlZv+Qfu 5tk7t5QOJNcgPuUA2z94WOV10t+1RGTIOpcfAmdpGUfuu574fTpc7+WQdFD+pyAATkRC ugNOBsexQXVqAwBpEEV93Tqut+dkXsSRO/rNigr+4aT/Md2B3HflyT8cSqbEFqutCAzY Oby9+Jz7cY0IrF7Pk1O8p89LNnD55P1BRiyDrRsmvNvVWQEAAUf6pQBsVbA5GTJ4doNe 1vXyRleVhNukg/CFpIv/GFvh9Q7c0nwkGYX9xw8CzwYsud6l8DtvULHlGgmTTqSISttX zyIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cSzvTXKu; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n4-20020a170906840400b00741a16e8562si459207ejx.826.2022.09.26.11.38.47; Mon, 26 Sep 2022 11:39:13 -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; dkim=pass header.i=@chromium.org header.s=google header.b=cSzvTXKu; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230245AbiIZS0V (ORCPT + 99 others); Mon, 26 Sep 2022 14:26:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229704AbiIZS0F (ORCPT ); Mon, 26 Sep 2022 14:26:05 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40A4A4330C for ; Mon, 26 Sep 2022 11:24:40 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id e68so7550527pfe.1 for ; Mon, 26 Sep 2022 11:24:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=4OBTQRXHJygCpY/2rBSvNKQlNNozBBa3mtJlOeJ/EyY=; b=cSzvTXKu85TucB7q+g9CQdjC/FdnYJW7ESEBDVMDQZhpqkoHzMT6tB5vmqaaVmtq+c HBo/BI5kdAn7jf+xxq5MiOLWI7cJumnHX29hS+SWLXo9wgZxYqgGZ4IjXwCG6/+UPFiS mSjP6/8kEubWj+KXOsoeUGGJwGHBMRMvzL4Ek= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=4OBTQRXHJygCpY/2rBSvNKQlNNozBBa3mtJlOeJ/EyY=; b=pQGe369Qb/atpqr4uFrWskndg368NXp7bb24PbBPnKjAThG77gzqO/Nw/+b74VJ2Wi MeaQD2Ul2kPqXf6fIPdtsk6jzR0ZriSzliwsICUAa2WifOUDZHEU434J+CLY3nvnZPd8 SCgT5N30M8LSlyidM11Rezey2T4TJ5MVhKSadDyXW6rUU2n+PsnuBjyl+bqDB2bD2oIe mZnCrIoNJ7RwukB0NwIySYICR45bMzbNifPLbO9vuy7N3bdTIpU36qh/zEaf8JtCvREJ WEbRFE2wxbCOnSMA71RxeP8GozD/0+qs4b2r0jL+apbhNcdUhEwlYvQy4vKBTC6f08Cf rAig== X-Gm-Message-State: ACrzQf0RW8dcRy+ARwmPLUgCXOf/N7/WtpeSad6YX2pVRMQzIRvZIZ3i Gtg1WlKRDIfTVFqZQlqLDfA6Gw== X-Received: by 2002:a63:4750:0:b0:43c:dac:9e4b with SMTP id w16-20020a634750000000b0043c0dac9e4bmr21137736pgk.300.1664216679159; Mon, 26 Sep 2022 11:24:39 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p2-20020a170902c70200b0016f85feae65sm11305644plp.87.2022.09.26.11.24.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Sep 2022 11:24:38 -0700 (PDT) Date: Mon, 26 Sep 2022 11:24:37 -0700 From: Kees Cook To: Vlastimil Babka Cc: Andrew Morton , linux-mm@kvack.org, "Ruhl, Michael J" , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Greg Kroah-Hartman , Nick Desaulniers , Alex Elder , Josef Bacik , David Sterba , Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Jesse Brandeburg , Daniel Micay , Yonghong Song , Marco Elver , Miguel Ojeda , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-fsdevel@vger.kernel.org, intel-wired-lan@lists.osuosl.org, dev@openvswitch.org, x86@kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: Re: [PATCH v2 13/16] mempool: Use kmalloc_size_roundup() to match ksize() usage Message-ID: <202209261123.B2CBAE87E0@keescook> References: <20220923202822.2667581-1-keescook@chromium.org> <20220923202822.2667581-14-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 On Mon, Sep 26, 2022 at 03:50:43PM +0200, Vlastimil Babka wrote: > On 9/23/22 22:28, Kees Cook wrote: > > Round up allocations with kmalloc_size_roundup() so that mempool's use > > of ksize() is always accurate and no special handling of the memory is > > needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE. > > > > Cc: Andrew Morton > > Cc: linux-mm@kvack.org > > Signed-off-by: Kees Cook > > --- > > mm/mempool.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/mm/mempool.c b/mm/mempool.c > > index 96488b13a1ef..0f3107b28e6b 100644 > > --- a/mm/mempool.c > > +++ b/mm/mempool.c > > @@ -526,7 +526,7 @@ EXPORT_SYMBOL(mempool_free_slab); > > */ > > void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data) > > { > > - size_t size = (size_t)pool_data; > > + size_t size = kmalloc_size_roundup((size_t)pool_data); > > Hm it is kinda wasteful to call into kmalloc_size_roundup for every > allocation that has the same input. We could do it just once in > mempool_init_node() for adjusting pool->pool_data ? > > But looking more closely, I wonder why poison_element() and > kasan_unpoison_element() in mm/mempool.c even have to use ksize()/__ksize() > and not just operate on the requested size (again, pool->pool_data). If no > kmalloc mempool's users use ksize() to write beyond requested size, then we > don't have to unpoison/poison that area either? Yeah, I think that's a fair point. I will adjust this. -- Kees Cook