From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3828C00140 for ; Tue, 2 Aug 2022 10:31:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 20AD28E0001; Tue, 2 Aug 2022 06:31:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 193AE6B0072; Tue, 2 Aug 2022 06:31:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 033B68E0001; Tue, 2 Aug 2022 06:31:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E0E956B0071 for ; Tue, 2 Aug 2022 06:31:07 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B1A86AB91D for ; Tue, 2 Aug 2022 10:31:07 +0000 (UTC) X-FDA: 79754284974.18.05270F8 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) by imf02.hostedemail.com (Postfix) with ESMTP id 2FEBF80108 for ; Tue, 2 Aug 2022 10:30:57 +0000 (UTC) Received: by mail-lf1-f53.google.com with SMTP id y11so21275112lfs.6 for ; Tue, 02 Aug 2022 03:30:57 -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=B+VG+M/qkCTnwih7G6rBW/egGXUSV6QeFe/cM3FPnX+7RuqjcKLiTusFnJQFyE8cAX 7Kb8Yx3/4cYfUAz3IcnU6QdEFcDjRiIw8bLIC//mVm33HmZSo69vZkyE7UQNx/bVBNzT 5BGJtHR6KT+0C+TCWSZkF8r0mNDuIJMGLG+XUHMqNTH8s8/jx2reG44B1kAezeXGTDgj cYhGK3V6UkUyydwuLDKPElU2YOBeF0PVU2wvhuRjleccsCLJVwqlJfiao1Sifl8w4yEd oTOnS1KV+6+Mj9yYyLuW8J4ehHGKVhRm/62G2o40Ly0IuOpxMOEb9NkURhTuTYfreb4k lr/Q== X-Gm-Message-State: ACgBeo1D1VhgjSj4qxA7QSxcCIT/jjwJI3o202FwuJXtiAtQrvUCPiR2 cn9mdAo0E++8XIF4ejKrK775C/0GTJVN2TQSyrwBnQ== X-Google-Smtp-Source: AA6agR7ODqtSUYvuKaQj45LbVVBPP0mYn+fPduaZ5Yhq09wKIYjojeNH5TxfDXJ6abn4tdh8zrvJ3XBWR2taKds5bJc= 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-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TVjyaQFk; spf=temperror (imf02.hostedemail.com: error in processing during lookup of dvyukov@google.com: DNS error) smtp.mailfrom=dvyukov@google.com X-Stat-Signature: xqp54gssf4xjhaf1gdre75t3ixx3kaag X-Rspamd-Queue-Id: 2FEBF80108 X-Rspamd-Server: rspam10 X-HE-Tag: 1659436257-143846 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: .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. >