Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2458521imn; Tue, 2 Aug 2022 03:42:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vKj+h43zIx6iV9cI+9TsZOStYBCu1gl/hxr6L2lKRJk+RhIsa/cV6mXAWXXT5GZSZPzxaA X-Received: by 2002:a05:6402:1e95:b0:437:ce7f:e17a with SMTP id f21-20020a0564021e9500b00437ce7fe17amr19482446edf.169.1659436967823; Tue, 02 Aug 2022 03:42:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659436967; cv=none; d=google.com; s=arc-20160816; b=YEJpP+3S6GIdCRJvdkWXVGNSp7OjEAJXcbXds/H3y500MhICghYfbW2jsMMEUceUTW klyv4qOd0dmQLnohDuPtDE4cNNEmSAB6d4j4oWXJL6PKhABrd7ccViVM9Adl6UkWHJdb QTdhqsBZBKM1nwdnEBHE85c1mmFtpFS4cy6P2B4IaN8BNSpCHmLSqP9c0uvUy3wxLOyR DjHIVQmdIkW/oyvMmgRykEYMazLOUZfvm4gmZvuC0Vv5CTLj1zcFS8VAOcaHhQYpuODC Xy9VmhDkqa45iIc10zxY+hMaZWTLp6ek4u0NNN6iFLDLWS3YqmtKNz2pOU2An6afN6lG P59Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=VJWkHF0viqAGmvAeKO165ehfCthiYYzBnEziZnfhmHU=; b=xw2NXYDfYPZ1Dy8PSe7oSPX+vMUwCEzcjK4eKrNNHf49mey28k5LQL6q6p5YzTeeAY IkR59XJ5kxMV5qQBIb4JOnOj0ur5XlwC7IzC3HxGfmocWKRAvSXo9/cV1JPig0IWEw9v 3P8C2v5b03wdmPXfXNzRHwF+kzkPSFfsKzgBSiZ5rmictYDLH2NazwZpm6q1n2LsjFgy M+b4snIFyAy35+pigHFNheWtmtFfMNBEZDgkFY1q7A29L4b8SfmMcMY5v/G3OvZVdfJm x8N57mL2OxE/m5+sTzT8uzoRQGdc7rcqeyuatvr9VI/0FxW0pNFZNWDtPpn2hd7jjhMU bTqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=TVjyaQFk; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hg3-20020a1709072cc300b007308550af97si4084760ejc.735.2022.08.02.03.42.22; Tue, 02 Aug 2022 03:42:47 -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=@google.com header.s=20210112 header.b=TVjyaQFk; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236219AbiHBKbK (ORCPT + 99 others); Tue, 2 Aug 2022 06:31:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236618AbiHBKbC (ORCPT ); Tue, 2 Aug 2022 06:31:02 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 277E7AE65 for ; Tue, 2 Aug 2022 03:30:58 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id bq11so16201092lfb.5 for ; Tue, 02 Aug 2022 03:30:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=VJWkHF0viqAGmvAeKO165ehfCthiYYzBnEziZnfhmHU=; b=TVjyaQFkcN9o8fogkLgE6rdw7hX7vunbmBDANGWzN8g3RM86mb2LNG4eKbiTTUgiwU YPrPL3I0qOb5vzn45T+NTa9zwmGXB4V6qA4gU65goLvNGUnF2d6GTd2/vuzUJGFxOYMJ No/PdANmugFEa6PVCt+2HT3TDO0mJX1dZQwMpLNifXWanMrac6wgUWCB+f/ccpKdaPmT coZ+wfRKpvDMBpa2PL75q+7zgiGHnfqk2dViTU9vy3GVazHYsHqXDIRZLe1Pc0b13X+r GfLvQRhA2NrEw3Ak144VM2yS3jGehxVYPQMH9YSMhtXOahy8JHemaGXy8P0dn8dVXUYH QJbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=VJWkHF0viqAGmvAeKO165ehfCthiYYzBnEziZnfhmHU=; b=XoN1HzfWhwhWeL46uwiXp3Oi0kAOgrjwA6zLv4txacMXhvCJDOSqRxFb4h3MzZKrXz tUgKoc5bo65uIsskOMxMdL/VNbw5kHiZ4QeQiyYxJ+/zfigO/+iVFZhsFu/4k/GauMtd VtOq7mVecHAW1tRrkI85+kRhT38cDGIpiw6J67bW0xwMROv5xFwmomFXXMs4u3wnHvOI 0bpCsLFD/n9+KRsy5gnrPxhR+1AmaW/Pj6+HbogbPhBv6eryvjTOgyDDSW6FgBEer4Re nEH52Npb1Mk2iTp7ZYu7QSXzywcgH0TmAZe1zfj+4c/4ojopJ1HK6KwOvKca+SpVGekY R/1w== X-Gm-Message-State: ACgBeo2rjQ9eH9nHH2G/OplDR/TVvIBh8UV4HdN47kbEmwbR6qq1YFol 4nu2FBmOts1xA8q2cVpN/BdY88yW+x8uyp/K9VetpA== X-Received: by 2002:a05:6512:250b:b0:48b:2c5:fe1e with SMTP id be11-20020a056512250b00b0048b02c5fe1emr2470061lfb.598.1659436256267; Tue, 02 Aug 2022 03:30:56 -0700 (PDT) MIME-Version: 1.0 References: <20220727071042.8796-4-feng.tang@intel.com> <0e545088-d140-4c84-bbb2-a3be669740b2@suse.cz> <85ec4ea8-ae4c-3592-5491-3db6d0ad8c59@suse.cz> In-Reply-To: <85ec4ea8-ae4c-3592-5491-3db6d0ad8c59@suse.cz> From: Dmitry Vyukov Date: Tue, 2 Aug 2022 12:30:44 +0200 Message-ID: Subject: Re: [mm/slub] 3616799128: BUG_kmalloc-#(Not_tainted):kmalloc_Redzone_overwritten To: Vlastimil Babka Cc: Feng Tang , "Sang, Oliver" , lkp , LKML , "linux-mm@kvack.org" , "lkp@lists.01.org" , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, "Hansen, Dave" , Robin Murphy , John Garry , Kefeng Wang , Andrey Konovalov , Andrey Ryabinin , Alexander Potapenko , "kasan-dev@googlegroups.com" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Tue, 2 Aug 2022 at 11:43, Vlastimil Babka wrote: > > On 8/2/22 09:06, Dmitry Vyukov wrote: > > On Tue, 2 Aug 2022 at 08:55, Feng Tang wrote: > >> > >> On Mon, Aug 01, 2022 at 10:23:23PM +0800, Vlastimil Babka wrote: > >> > On 8/1/22 08:21, Feng Tang wrote: > >> [snip] > >> > > Cc kansan mail list. > >> > > > >> > > This is really related with KASAN debug, that in free path, some > >> > > kmalloc redzone ([orig_size+1, object_size]) area is written by > >> > > kasan to save free meta info. > >> > > > >> > > The callstack is: > >> > > > >> > > kfree > >> > > slab_free > >> > > slab_free_freelist_hook > >> > > slab_free_hook > >> > > __kasan_slab_free > >> > > ____kasan_slab_free > >> > > kasan_set_free_info > >> > > kasan_set_track > >> > > > >> > > And this issue only happens with "kmalloc-16" slab. Kasan has 2 > >> > > tracks: alloc_track and free_track, for x86_64 test platform, most > >> > > of the slabs will reserve space for alloc_track, and reuse the > >> > > 'object' area for free_track. The kasan free_track is 16 bytes > >> > > large, that it will occupy the whole 'kmalloc-16's object area, > >> > > so when kmalloc-redzone is enabled by this patch, the 'overwritten' > >> > > error is triggered. > >> > > > >> > > But it won't hurt other kmalloc slabs, as kasan's free meta won't > >> > > conflict with kmalloc-redzone which stay in the latter part of > >> > > kmalloc area. > >> > > > >> > > So the solution I can think of is: > >> > > * skip the kmalloc-redzone for kmalloc-16 only, or > >> > > * skip kmalloc-redzone if kasan is enabled, or > >> > > * let kasan reserve the free meta (16 bytes) outside of object > >> > > just like for alloc meta > >> > > >> > Maybe we could add some hack that if both kasan and SLAB_STORE_USER is > >> > enabled, we bump the stored orig_size from <16 to 16? Similar to what > >> > __ksize() does. > >> > >> How about the following patch: > >> > >> --- > >> diff --git a/mm/slub.c b/mm/slub.c > >> index added2653bb0..33bbac2afaef 100644 > >> --- a/mm/slub.c > >> +++ b/mm/slub.c > >> @@ -830,6 +830,16 @@ static inline void set_orig_size(struct kmem_cache *s, > >> if (!slub_debug_orig_size(s)) > >> return; > >> > >> +#ifdef CONFIG_KASAN > >> + /* > >> + * When kasan is enabled, it could save its free meta data in the > >> + * start part of object area, so skip the kmalloc redzone check > >> + * for small kmalloc slabs to avoid the data conflict. > >> + */ > >> + if (s->object_size <= 32) > >> + orig_size = s->object_size; > >> +#endif > >> + > >> p += get_info_end(s); > >> p += sizeof(struct track) * 2; > >> > >> I extend the size to 32 for potential's kasan meta data size increase. > >> This is tested locally, if people are OK with it, I can ask for 0Day's > >> help to verify this. > > Is there maybe some KASAN macro we can use instead of hardcoding 32? kasan_free_meta is placed in the object data after freeing, so it can be sizeof(kasan_free_meta) > > Where is set_orig_size() function defined? Don't see it upstream nor > > in linux-next. > > This looks fine but my only concern is that this should not increase > > memory consumption when slub debug tracking is not enabled, which > > should be the main operation mode when KASAN is enabled. But I can't > > figure this out w/o context. > > It won't increase memory consumption even if slub_debug tracking is enabled. > It just fakes a bit the size that was passed to kmalloc() and which we newly > store (thanks to Feng's patches) for statistics and debugging purposes. > > >> Thanks, > >> Feng > >> > >> > > >> > > I don't have way to test kasan's SW/HW tag configuration, which > >> > > is only enabled on arm64 now. And I don't know if there will > >> > > also be some conflict. > >> > > > >> > > Thanks, > >> > > Feng > >> > > > >> > > >> > >> -- > >> You received this message because you are subscribed to the Google Groups "kasan-dev" group. > >> To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > >> To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/YujKCxu2lJJFm73P%40feng-skl. >