Received: by 2002:ab2:6046:0:b0:1fb:d597:ff75 with SMTP id a6csp6748lqm; Tue, 4 Jun 2024 17:49:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWrjH1Q6jb67DeTEcj/qgA+9rsen8NJ8sJfZ8JW3SnvzHdXTuuwk3XWlra+F9rF6P4AACCT7YqAEcZxTk8KuGrXtvuA44gnnh1yPdIiug== X-Google-Smtp-Source: AGHT+IEVqxATPJEGF9yRHGmjUZZqatMgh/AbeU0Kv7JLxknOpZaRAK0kucX9VLocVCy40zkOG4XU X-Received: by 2002:a05:6359:45a9:b0:19c:5645:235 with SMTP id e5c5f4694b2df-19c6690febfmr123532655d.0.1717548573993; Tue, 04 Jun 2024 17:49:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717548573; cv=pass; d=google.com; s=arc-20160816; b=vCNF6GN4J5CGfJAuWUeayMR/O8trkarsfeS+aLzyv2TFDz1sqHtLONM3LJsQaI4rdN /7yiCieiqFm3i4FaPYoYxo202MOBmGzfxBtRvk49rq6YxOajt4lYTkQYM2hLcu9eVvSH gwEZR8/tlprHVN1M8I1A6Wct4a0zCcPO9uTozx3NAm6JIqa7nnmS241vZ2+UYWTVjIFe qqKJT6gF2kOL10RMBBTZ5T8x5rYiqeAcnK/eSZKaTl1UDcJhu44LVRG5BfYjuhof9BLW XfwMdlWgw368NeNibJTBIOs5PfaLM9H/SoxrDlnRHjOAWNF+jkABvKEcdZiE3tpXiXHM yxFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ucx0GPSA5HE0Wjd+vELpgu507AfJEN0NGInIEXksQ0w=; fh=t39f+r+QWB0I22R4ZWsV3kcdY89x7VR8nVv+Seq/cYc=; b=mBuDCuLd8PuilTkuYyCh/dLn6cbBeu2TPW+QmAekc6NtOyP/4Y4+nvIvcyRypaEUBn VPXCvBpRd5/2KRuk5OlOuY0/CAqFObfWuM8siYIlIGZqzIw4HCzdnySL8wKXQsuZmiEV HHsNdpFwmxEzbfIDDVxCu3u3Marc4vDh0X+512lFEvQuF+pq6+rbFaf3o6bfl5Rflp59 GRRx7OgfqieZaXvzvsny9zgU4G0EigFrnJl0G6KF21+3/6d3cd2GgZgTsEK2VOTwA/Qd pmzG6gheLPos6CthQ3n4h8WunbF994wvNRQ3alZ24i8nV2yZ7hwSYgOOTawSd1ywZJxg RuMA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MrP0zECe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201629-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201629-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6c35c55275asi8931038a12.667.2024.06.04.17.49.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 17:49:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-201629-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MrP0zECe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201629-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201629-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 93147282E89 for ; Wed, 5 Jun 2024 00:49:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A14164A3D; Wed, 5 Jun 2024 00:49:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MrP0zECe" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B1DF2163; Wed, 5 Jun 2024 00:49:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717548561; cv=none; b=T5lWdtdAhE52P9sDejCuhzC30cb1MtfhASHL0Eey8bJPEoCDWc70pPfC2LcSR1uO96oHPQpNxNXhjKh2LE5hmFdOh12D9kHGbPu43lcgwfszzrfseD/UWdwo4t0c0fGeEq+V8fbHH2DFsOMmb5+bglTIfPRUXyvejf+1bqdbEt0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717548561; c=relaxed/simple; bh=CR+MCqNdStHiS0XLFyUo6nxzn/REAjoufJhdXky59KA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=srM3Y+UVCszaaEsPw7MQ99wnNGOxenGmXvh9PuwEJr6mvW1dBNws5wKEUFGN1iq7t4d10cFmba7/67jsJTnaBtTQ12CSHaAUa0Zr+a3nMRVP8ptUronIj6GB5kJJXXk/IOB4+aIDOI0OuH9Ag6mfm/+A1zoGNfy8nbbvxzrtbvQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MrP0zECe; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75901C2BBFC; Wed, 5 Jun 2024 00:49:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717548561; bh=CR+MCqNdStHiS0XLFyUo6nxzn/REAjoufJhdXky59KA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MrP0zECelgyKwrURJfgOosDnxpIQQ5F5AK1p/r00kffo/Mgut/vP1+QwIgHoCtlYg VLgMqlp2VbEdHQnyVdddKLExUxaZQHSOObBXvsKo/O3dZf9aafy/JJCliTn3rqiDEr 58va7PeALemCo5HmhjUJcE32TjpUGEeYenj+LIjpv/LaaZTFxAfdc9HxSgg+Hgfm1p vVBe7wojcXtK3JR9K6E0CgkXP231fblNe/4/bvWy8+cvaeaEdcfMndba79P1/YpXUA 2p4T7ckStMD2as5t+zPkOF1g/TGq4y+CyxfKIShA2rwvn3BwTFsjDWdYNG75C9r0vT 7zTEzC8+/5VyQ== Date: Tue, 4 Jun 2024 17:49:20 -0700 From: Kees Cook To: Tycho Andersen Cc: Simon Horman , Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, "GONG, Ruiqi" , Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: Re: [PATCH v4 4/6] mm/slab: Introduce kmem_buckets_create() and family Message-ID: <202406041749.27CAE270@keescook> References: <20240531191304.it.853-kees@kernel.org> <20240531191458.987345-4-kees@kernel.org> <20240604150228.GS491852@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Jun 04, 2024 at 04:13:32PM -0600, Tycho Andersen wrote: > On Tue, Jun 04, 2024 at 04:02:28PM +0100, Simon Horman wrote: > > On Fri, May 31, 2024 at 12:14:56PM -0700, Kees Cook wrote: > > > + for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) { > > > + char *short_size, *cache_name; > > > + unsigned int cache_useroffset, cache_usersize; > > > + unsigned int size; > > > + > > > + if (!kmalloc_caches[KMALLOC_NORMAL][idx]) > > > + continue; > > > + > > > + size = kmalloc_caches[KMALLOC_NORMAL][idx]->object_size; > > > + if (!size) > > > + continue; > > > + > > > + short_size = strchr(kmalloc_caches[KMALLOC_NORMAL][idx]->name, '-'); > > > + if (WARN_ON(!short_size)) > > > + goto fail; > > > + > > > + cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1); > > > + if (WARN_ON(!cache_name)) > > > + goto fail; > > > + > > > + if (useroffset >= size) { > > > + cache_useroffset = 0; > > > + cache_usersize = 0; > > > + } else { > > > + cache_useroffset = useroffset; > > > + cache_usersize = min(size - cache_useroffset, usersize); > > > + } > > > + (*b)[idx] = kmem_cache_create_usercopy(cache_name, size, > > > + align, flags, cache_useroffset, > > > + cache_usersize, ctor); > > > + kfree(cache_name); > > > + if (WARN_ON(!(*b)[idx])) > > > + goto fail; > > > + } > > > + > > > + return b; > > > + > > > +fail: > > > + for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) { > > > + if ((*b)[idx]) > > > + kmem_cache_destroy((*b)[idx]); > > > > nit: I don't think it is necessary to guard this with a check for NULL. > > Isn't it? What if a kasprintf() fails halfway through the loop? He means that kmem_cache_destroy() already checks for NULL. Quite right! void kmem_cache_destroy(struct kmem_cache *s) { int err = -EBUSY; bool rcu_set; if (unlikely(!s) || !kasan_check_byte(s)) return; -- Kees Cook