Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4862245rdh; Wed, 29 Nov 2023 12:55:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEE3RsprXj8uLG5RJo92TsvPYAp0MhWzfiXs3EG6hGox/obYDFYp28e4zcdgSLa9a1o8WN+ X-Received: by 2002:a17:902:ced2:b0:1cc:4a84:27cd with SMTP id d18-20020a170902ced200b001cc4a8427cdmr17344589plg.31.1701291351916; Wed, 29 Nov 2023 12:55:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701291351; cv=none; d=google.com; s=arc-20160816; b=R/8dnb9LsuJHcAgTppKCeAu0uMZAHtWVthE5x7w3Q3EZgPdbPz4/9V5dY4ukDbdddf P/SwD3DiUsBrjX8Uub2iQ3iaXeTh6V4GgolVBtVKt/JF6k5n3Gn6ecCN4tSB/YGVEHUY BMKxfg8cCxiMskUtV4m6vi0+V1tWSEVBONDLe75sacs7I6KJIpVHFHZau1LDt/0EDWS+ 5tVEAHhIaiNHrhoIayyFDGXmyEmlqSWzS8flnw/cbJULWHZe781KrpOSeMHCV+2gcwEm 7SXdKaybdv1KW6WwmwZPDxxQiZjiiMHgE7HxAZma5u0pHd5MOKY2BczZaOD+RaBvBgD7 MKzw== 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=mh8mfkKmbZyezogReiTmoJ2/ZEYTC/jfxX54yQ12YVc=; fh=tsIHYQd+95QiwZEAV54eWstSQej7ClBaiAjrSoM77lA=; b=xaDDlFszkdNFxrdWT7YOYAZTmpha5o7dlWiPmZcCpM3ygT/nfshanlDCYiQoJmNY8x G9kBqC4fazirccGTNzd6XhhvswN1MUwA4Sc9Hy9WwSbNiCw/MmjQ+O1VToREkjxtuC21 cybpmtQpoPYYG6W4/q4HI+pppNmf343yY57Yl2HPY/krGAuIfUsJt0pA9HrFUbxlHDCW arMls5FgtbuZbDp6HDqT1X8a6n9O63ZiUtl2XyjV0U8Z2CLzHCvJWLQxMnC2sLBSulNi 2mOJq/5IPfKFGc7l4gw/KZF8ILwUIOkTDAGmPde6Zr1hwi+qsjr4HVbp9it02nEkaO/F C0Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jFfsOIk4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id e7-20020a170902744700b001cfb43d6b36si10467505plt.619.2023.11.29.12.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 12:55:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jFfsOIk4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 16EBB807F2AD; Wed, 29 Nov 2023 12:55:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234146AbjK2Uz3 (ORCPT + 99 others); Wed, 29 Nov 2023 15:55:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229556AbjK2Uz1 (ORCPT ); Wed, 29 Nov 2023 15:55:27 -0500 Received: from mail-vs1-xe32.google.com (mail-vs1-xe32.google.com [IPv6:2607:f8b0:4864:20::e32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07896D67 for ; Wed, 29 Nov 2023 12:55:34 -0800 (PST) Received: by mail-vs1-xe32.google.com with SMTP id ada2fe7eead31-462e19c29baso52209137.0 for ; Wed, 29 Nov 2023 12:55:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701291333; x=1701896133; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=mh8mfkKmbZyezogReiTmoJ2/ZEYTC/jfxX54yQ12YVc=; b=jFfsOIk4af1CGZLS53I/gWvS4Nl8m6ju0Xm2BXXNjzP3TXnU7cYL2CZN5x7w5BOg4C TT3teqGSTN4/uY2zf1O0ChqgkPengLrDWWFBZCRTy0arb6gP96T3rdY8NsVYKthpfpiU QjPLGht1tF/mOU3Z1Ma4hBbB36UHpb5WgXDBVDVU7HJKYrlvG9AU2oc0SVe7KsMCQLkS Uebze4zgGETguXNLHUMu3nBh+z+iBC00KzvMczCtlHv0G5RM8FdvbUvg3M2d8sPDh6Vc +zGijiLY50KXhercsRmgztvdg0pTrwoPVYkYInvOl9k68kusGQKF/dvWlMzZfZhIsgTS C2gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701291333; x=1701896133; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mh8mfkKmbZyezogReiTmoJ2/ZEYTC/jfxX54yQ12YVc=; b=Q6eH5KshMCazyzPQNYO3IPAcpIt6phUIQoL3PQm/EB+lmFwYjTG5cduxvUY85RIaa9 kDbapUA2Ds7vY5X3Pni2PQgwQ6YWK56tCln90lhzyhAUcZsRTZkv3qcgQETvl7ynArI0 EVy7DMhpY4Gnx1rH0JmqiTv5mCq99CaSI+3gWsEzMbRAnKB7Xt6BlsoNKkIyPlRFWQpZ MVSNFn/UPF1Rc2a/T7Wafgp3PtoZsq1OTIS7eu3Oj/Ipat7sQTZ5+LNS8Iai2RSTW7UG qUg6dJqJ+XN3o96Kn0flBgmtMrmGArO4jHAAp29L3uf8e6Vs/XT/lU3FwYPFzBZomQq5 Gytw== X-Gm-Message-State: AOJu0Yx7dGPKjwwTihCybUbH6tytUfQafovU+mVtFgD0c008Z2padbmh n3oIaOT7E/F8S+7B4vmHGOfKxWcNDuXHoXC5DaljCg== X-Received: by 2002:a05:6102:244f:b0:464:4d97:2c01 with SMTP id g15-20020a056102244f00b004644d972c01mr2523133vss.31.1701291332997; Wed, 29 Nov 2023 12:55:32 -0800 (PST) MIME-Version: 1.0 References: <20231127234946.2514120-1-swboyd@chromium.org> <202311291219.A6E3E58@keescook> In-Reply-To: From: Marco Elver Date: Wed, 29 Nov 2023 21:54:54 +0100 Message-ID: Subject: Re: [PATCH] lkdtm: Add kfence read after free crash type To: Stephen Boyd Cc: Kees Cook , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Arnd Bergmann , Greg Kroah-Hartman , Alexander Potapenko , Dmitry Vyukov , kasan-dev@googlegroups.com, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 29 Nov 2023 12:55:49 -0800 (PST) On Wed, 29 Nov 2023 at 21:42, Stephen Boyd wrote: > > Adding kfence folks (will add on v2). > > Quoting Kees Cook (2023-11-29 12:22:27) > > On Mon, Nov 27, 2023 at 03:49:45PM -0800, Stephen Boyd wrote: > > > Add the ability to allocate memory from kfence and trigger a read after > > > free on that memory to validate that kfence is working properly. This is > > > used by ChromeOS integration tests to validate that kfence errors can be > > > collected on user devices and parsed properly. > > > > This looks really good; thanks for adding this! > > > > > > > > Signed-off-by: Stephen Boyd > > > --- > > > drivers/misc/lkdtm/heap.c | 64 +++++++++++++++++++++++++++++++++++++++ > > > 1 file changed, 64 insertions(+) > > > > > > diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c > > > index 0ce4cbf6abda..608872bcc7e0 100644 > > > --- a/drivers/misc/lkdtm/heap.c > > > +++ b/drivers/misc/lkdtm/heap.c > > > @@ -4,6 +4,7 @@ > > > * page allocation and slab allocations. > > > */ > > > #include "lkdtm.h" > > > +#include > > > #include > > > #include > > > #include > > > @@ -132,6 +133,66 @@ static void lkdtm_READ_AFTER_FREE(void) > > > kfree(val); > > > } > > > > > > +#if IS_ENABLED(CONFIG_KFENCE) > > > > I really try hard to avoid having tests disappear depending on configs, > > and instead report the expected failure case (as you have). Can this be > > built without the IS_ENABLED() tests? > > > > We need IS_ENABLED() for the kfence_sample_interval variable. I suppose > if the config isn't set that variable can be assumed as zero and then > the timeout would hit immediately. We can either define the name > 'kfence_sample_interval' as 0 in the header, or put an ifdef in the > function. I think it's fair to put it in the kfence header, so you don't need the #ifdefs in the test code. We didn't think anyone should depend on kfence_sample_interval outside KFENCE code, but probably only tests would anyway. > ---8<--- > diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c > index 4f467d3972a6..574d0aa726dc 100644 > --- a/drivers/misc/lkdtm/heap.c > +++ b/drivers/misc/lkdtm/heap.c > @@ -138,6 +138,14 @@ static void lkdtm_KFENCE_READ_AFTER_FREE(void) > int *base, val, saw; > unsigned long timeout, resched_after; > size_t len = 1024; > + unsigned long interval; > + > +#ifdef CONFIG_KFENCE > + interval = kfence_sample_interval; > +#else > + interval = 0; > +#endif > + > /* > * The slub allocator will use the either the first word or > * the middle of the allocation to store the free pointer, > @@ -150,13 +158,13 @@ static void lkdtm_KFENCE_READ_AFTER_FREE(void) > * 100x the sample interval should be more than enough to ensure we get > * a KFENCE allocation eventually. > */ > - timeout = jiffies + msecs_to_jiffies(100 * kfence_sample_interval); > + timeout = jiffies + msecs_to_jiffies(100 * interval); > /* > * Especially for non-preemption kernels, ensure the allocation-gate > * timer can catch up: after @resched_after, every failed allocation > * attempt yields, to ensure the allocation-gate timer is scheduled. > */ > - resched_after = jiffies + msecs_to_jiffies(kfence_sample_interval); > + resched_after = jiffies + msecs_to_jiffies(interval); > do { > base = kmalloc(len, GFP_KERNEL); > if (!base) { > > ---8<---- > diff --git a/include/linux/kfence.h b/include/linux/kfence.h > index 401af4757514..88100cc9caba 100644 > --- a/include/linux/kfence.h > +++ b/include/linux/kfence.h > @@ -223,6 +223,8 @@ bool __kfence_obj_info(struct kmem_obj_info *kpp, > void *object, struct slab *sla > > #else /* CONFIG_KFENCE */ > > +#define kfence_sample_interval (0) > + > static inline bool is_kfence_address(const void *addr) { return false; } > static inline void kfence_alloc_pool_and_metadata(void) { } > static inline void kfence_init(void) { } Acked-by: Marco Elver FWIW, I've occasionally been using repeatedly invoked READ_AFTER_FREE to test if KFENCE is working. Having a dedicated test like this seems more reliable though.