Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4465700pxf; Tue, 16 Mar 2021 14:22:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqcxxnDsHuY60q3MxaBLy8qLclBCy9sefTkBmGuHKG2DbuCAZpEm39JYZ1HFM6y6wZqHnf X-Received: by 2002:a05:6402:b41:: with SMTP id bx1mr38794685edb.69.1615929744509; Tue, 16 Mar 2021 14:22:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615929744; cv=none; d=google.com; s=arc-20160816; b=WoDQ2/vUeD7W4EZUMLpqXzjm6PtD/sd7yLjTSGyilVyYKpZbeZ8Zg8AB7L+3Z9Kiwm 6jnXxeeikmBqUHP09sgj9yT3JeOSw1+Fpc6J4hKBfFlw64rroJfl/afTS/oDJ6VaksPl 2ehbJ0QW6QDaYgiE/UiGe1IPeBBkqwdD9yOPVnoVQNECplfBKQfiVo+GfDjDJvekOvwj KCs8hjNf32wTRWaaXktZMtXzJKcGj42AcDA8igPcQgH4YBIOJyogmbhZFkHCkQsDCRZP +JkpXE3Ia1Oyu9y1mK+FJ8A8VuTNaEY5WTqIxhbJ737CqGmxwuVmHM5wgh/gCW/qLYqY URaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=ma94Z6Q3aLPBbVkToLQTi1asTgK2QMtjqNCaTaW2dvM=; b=hoIAR5f81eT56heGsQy82JN5qF1i3ymk4nQzjFbVc8cf2ZOQIYDhfmXo/IIUFTMJ0V UwEukm+uQHzjTU8XgWg5z7s1we+vrW3TWLSPu4AweKQrbVaCJ0BN2FCoAR4k5v7xGspR HfPg5GWsJG2UIP3CqqLp917EHorO8FNQA7r4UJerl9G/qHlb7Hod+umqy7zAuqBrJfbz /uVEWa8QwucMwRp8CDUCy6eFzIcjdBuKLiyK96TV6lTFqfnCnSOO8lZerwhMyGbfBMpG 8Dln3vbNao9WWFGEMiKClfpkiU7UDYBLLLetksYv7DIllZD651lpXByS82FG1I0TUj+I 5fgA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce10si14070321ejb.123.2021.03.16.14.22.01; Tue, 16 Mar 2021 14:22:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240356AbhCPTWx (ORCPT + 99 others); Tue, 16 Mar 2021 15:22:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:59836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236324AbhCPTWo (ORCPT ); Tue, 16 Mar 2021 15:22:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C7A6A6505E; Tue, 16 Mar 2021 19:22:42 +0000 (UTC) Date: Tue, 16 Mar 2021 19:22:40 +0000 From: Catalin Marinas To: Marco Elver Cc: Luis Henriques , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: Issue with kfence and kmemleak Message-ID: <20210316192240.GC28565@arm.com> References: <20210316181938.GA28565@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 16, 2021 at 07:47:00PM +0100, Marco Elver wrote: > One thing I've just run into: "BUG: KFENCE: out-of-bounds read in > scan_block+0x6b/0x170 mm/kmemleak.c:1244" > > Probably because kmemleak is passed the rounded size for the size-class, > and not the real allocation size. Can this be fixed with > kmemleak_ignore() only called on the KFENCE guard pages? If it's only on the occasional object, you can do a kmemleak_scan_area() but some care needed as this in turn allocates memory for kmemleak internal metadata. > I'd like kmemleak to scan the valid portion of an object allocated > through KFENCE, but no further than that. > > Or do we need to fix the size if it's a kfence object: > > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index c0014d3b91c1..fe6e3ae8e8c6 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -97,6 +97,7 @@ > #include > > #include > +#include > #include > #include > > @@ -589,7 +590,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, > atomic_set(&object->use_count, 1); > object->flags = OBJECT_ALLOCATED; > object->pointer = ptr; > - object->size = size; > + object->size = kfence_ksize((void *)ptr) ?: size; > object->excess_ref = 0; > object->min_count = min_count; > object->count = 0; /* white color initially */ > > The alternative is to call kfence_ksize() in slab_post_alloc_hook() when > calling kmemleak_alloc. One of these is probably the easiest. If kfence only works on slab objects, better to pass the right size from slab_post_alloc_hook(). If you plan to expand it later to vmalloc(), just fix the size in create_object(). -- Catalin