Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp210008imn; Fri, 29 Jul 2022 04:54:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vEDL8odi9WiW4MwNheH7VLmcQ9f6xs5HZpmEx5nsoYHY3mbQ7BjKdgWPMGpjiHDzbqt8RX X-Received: by 2002:a17:907:6da4:b0:72b:7f56:63c with SMTP id sb36-20020a1709076da400b0072b7f56063cmr2591732ejc.6.1659095640028; Fri, 29 Jul 2022 04:54:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659095640; cv=none; d=google.com; s=arc-20160816; b=BfMFwTWsBgGfZgW0au4SOz8j0oY9fS0odD+gZbP30YXiHRvQPxGsAgSx/dUxEkiTGf jqcLgRHCcLQ1tcPtX5+SliElNc3TLO2Wni7n1goJM5ZHdn0hQ7vr5A/YHGoEHM2T5big zNUTCRnAU2KXMzcpGj9zv0J1s/szae9ZZcVm/03YAae+YSCUi0VRkZ6PR6CtDwk2NDhk wh3QmzW8yYe7j3GKa8gTLIC2++LkhzTqXALIjWatQmdKhsymflbkkZrOnWZwnzRen6Ri 01qQ9QQgfO6ypJ5ZFopxFAKwBWYVItRMzLBIcefvjmQmnWhZ6iJ8+wmbrX/iJDK96AAG fSsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=eI2PNmEUTHvyF7K2xumYQSpCfUFhWAfxz5uFA0UMNOU=; b=t/lgGU8G7NVccDBT/LCkKLVk9PGDSnhITNo0CmgYP3LQBYIda4Izx7x9Dgibd0kvPK NOYXImKuYQT4K8nzKHSh/8xDNYmN2O6Y2asuswYP1ctzsFdPxSDZujCjonIsBOqQNgWG 02WDqqEjADOjhQH5C5wRStRfsv9c9uT3XAssttChmsfd8fcZD8X2waaDTacvmJoa86TE b7ZPgKFw/U2RXxaywuNN0BFpDokZT9QIJtQl9647pT9PYWIe/q+kyRDGWTt9WXEahdZW wRMY3w7HUSUfcwEBuTIqKB3sV3X+FNfTsWivri5+tM2GoGMb37IRyLXS4hyDWTXzInqO XnSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=N5oMKKaH; dkim=neutral (no key) header.i=@suse.cz; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg3-20020a170907970300b0070ebb93abe4si3167278ejc.117.2022.07.29.04.53.35; Fri, 29 Jul 2022 04:54:00 -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=@suse.cz header.s=susede2_rsa header.b=N5oMKKaH; dkim=neutral (no key) header.i=@suse.cz; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235732AbiG2Luz (ORCPT + 99 others); Fri, 29 Jul 2022 07:50:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233922AbiG2Lux (ORCPT ); Fri, 29 Jul 2022 07:50:53 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07BC2165B7 for ; Fri, 29 Jul 2022 04:50:52 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id BE5FD20DF8; Fri, 29 Jul 2022 11:50:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1659095450; h=from:from:reply-to: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=eI2PNmEUTHvyF7K2xumYQSpCfUFhWAfxz5uFA0UMNOU=; b=N5oMKKaHayNYtAu2F8Q8Xo7Ht8i12r4Z0DonJ3Z0E6RT7a/9tSznb6MojEel+8BgUmgNe3 5pVQDzxij0D1MC984ybztLMv4dD012kYQ8jrjeNM2dmN3tTIGozNlYxOkYOPmxfqQtWaAT sWf26yqTRYynzI8TWOrrsUivTAgvjJI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1659095450; h=from:from:reply-to: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=eI2PNmEUTHvyF7K2xumYQSpCfUFhWAfxz5uFA0UMNOU=; b=0YVDli1nFygU9eU3lQqNrdfkPpGSd80F3/YOB7XONAMyxMy/etQV8zBPEfFLi1pCK+0W7x 5nzyMlJFqFmD/6Cw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 927D213A1B; Fri, 29 Jul 2022 11:50:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id norpIprJ42JoIgAAMHmgww (envelope-from ); Fri, 29 Jul 2022 11:50:50 +0000 Message-ID: Date: Fri, 29 Jul 2022 13:50:50 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH 16/16] mm/sl[au]b: check if large object is valid in __ksize() Content-Language: en-US To: Hyeonggon Yoo <42.hyeyoo@gmail.com>, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Joe Perches , Vasily Averin , Matthew WilCox , Marco Elver Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20220712133946.307181-1-42.hyeyoo@gmail.com> <20220712133946.307181-17-42.hyeyoo@gmail.com> From: Vlastimil Babka In-Reply-To: <20220712133946.307181-17-42.hyeyoo@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_SOFTFAIL autolearn=no 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 7/12/22 15:39, Hyeonggon Yoo wrote: > __ksize() returns size of objects allocated from slab allocator. > When invalid object is passed to __ksize(), returning zero > prevents further memory corruption and makes caller be able to > check if there is an error. > > If address of large object is not beginning of folio or size of > the folio is too small, it must be invalid. Return zero in such cases. > > Suggested-by: Vlastimil Babka > Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Reviewed-by: Vlastimil Babka If you want to change it to BUG() I won't object, no strong opinion. > --- > mm/slab_common.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 1f8db7959366..0d6cbe9d7ad0 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -1013,8 +1013,12 @@ size_t __ksize(const void *object) > > folio = virt_to_folio(object); > > - if (unlikely(!folio_test_slab(folio))) > + if (unlikely(!folio_test_slab(folio))) { > + if (WARN_ON(object != folio_address(folio) || > + folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE)) > + return 0; > return folio_size(folio); > + } > > return slab_ksize(folio_slab(folio)->slab_cache); > }