Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp3595548rwb; Mon, 5 Sep 2022 14:59:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR4yrxIoBqi1rF6XAXXLtUogD/zou3wvU+wjgHvNvvI58t3JZvtwO0zbRrhY7g5ZIYliI4aK X-Received: by 2002:a65:6048:0:b0:412:73c7:cca9 with SMTP id a8-20020a656048000000b0041273c7cca9mr44760599pgp.257.1662415176977; Mon, 05 Sep 2022 14:59:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662415176; cv=none; d=google.com; s=arc-20160816; b=SronHFAbbXWlgSMu2HwwUcTPINubGDLDvaHbZ5Klhhi8c5ZbphOgLPv5L03UDXMpDV ia3/ng6hWVPR69dulWI1fCt4ThpGBieHvEaZbTdPDKU4srOWoTe+aHQ64GgIAYZwxX0+ 42LJlOpG2JaSdsuK/y7tzPtngFMm7uta/MkVcQLyGGsqyCslN6RV1XrVkv4MgGNafdGD uTbXnQ3dP4wIpXVuz56JaXJ9O1z8NmfWeUqkWRKHorZZm3nR5w60O1m186lZV14oHYrR 3KOhABz03e8cQWnaHDVcScIPBD66hh8OfjAZrF0GuNeLWYnRDdKkbYzTBdYAQN0NzqBe lnaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=TrkbSYuQGAnlXN7bh4mnqFPtjr4fL87+nLp2f5EX6NA=; b=oCwJ+BIOKUfZ11zvEXUOsJpvnJwGlM7lmfnJ4agefrpvPEuEAUWtCvkb0SjmuJlEtm 2SO9e0zU/6aZvjSwLNoSJ3Caen4UuecQuv+pr1a/Ox4w8ka4LB1IZIQ/ANie/zlTX4gv 73KbrawpnegcK6CabFnamKzJIMjjM+OX3+bDtWDf/SLnWZ5novkjHL1kqtedioMnGPA6 bQKtsy2Dj2Sw0bNi4B5mcHjhrehKs7Ym9RrJ2HCBo/vu7OmuoHm5v+GYlOi70jAa7FQm HdIbHK3enaxJ35vONLv0SRTRSR3nc4g1V/jdFWZJiY3Z4u3IwUpbGZYLUzNuTrfMhIhx ccaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=hFYsB+Fz; 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=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h185-20020a6383c2000000b0043032aa7183si11961221pge.580.2022.09.05.14.59.26; Mon, 05 Sep 2022 14:59:36 -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=@linux.dev header.s=key1 header.b=hFYsB+Fz; 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=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237933AbiIEVMd (ORCPT + 99 others); Mon, 5 Sep 2022 17:12:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236847AbiIEVLy (ORCPT ); Mon, 5 Sep 2022 17:11:54 -0400 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D01AA389F for ; Mon, 5 Sep 2022 14:11:23 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1662412282; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TrkbSYuQGAnlXN7bh4mnqFPtjr4fL87+nLp2f5EX6NA=; b=hFYsB+FzPJ+FMrutd8rfqpTd1/qyrIHDsk10pkOlOch0JdxywpM+FON5VmXfk94r6IrdeN 2KcXHltXuShi44sD2apcKfLPXWzkG+tZROT87p4SyY5zw97cgHWRkxAWVLWNNRMPYrZrft YKn/EmvzNmY4bEmebCNuwgrpxR4lM1c= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Peter Collingbourne , Evgenii Stepanov , Florian Mayer , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm v3 34/34] kasan: add another use-after-free test Date: Mon, 5 Sep 2022 23:05:49 +0200 Message-Id: <0659cfa15809dd38faa02bc0a59d0b5dbbd81211.1662411800.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 From: Andrey Konovalov Add a new use-after-free test that checks that KASAN detects use-after-free when another object was allocated in the same slot. This test is mainly relevant for the tag-based modes, which do not use quarantine. Once [1] is resolved, this test can be extended to check that the stack traces in the report point to the proper kmalloc/kfree calls. [1] https://bugzilla.kernel.org/show_bug.cgi?id=212203 Signed-off-by: Andrey Konovalov --- Changes v2->v3: - This is a new patch. --- lib/test_kasan.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 58c1b01ccfe2..505f77ffad27 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -612,6 +612,29 @@ static void kmalloc_uaf2(struct kunit *test) kfree(ptr2); } +/* + * Check that KASAN detects use-after-free when another object was allocated in + * the same slot. Relevant for the tag-based modes, which do not use quarantine. + */ +static void kmalloc_uaf3(struct kunit *test) +{ + char *ptr1, *ptr2; + size_t size = 100; + + /* This test is specifically crafted for tag-based modes. */ + KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC); + + ptr1 = kmalloc(size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); + kfree(ptr1); + + ptr2 = kmalloc(size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); + kfree(ptr2); + + KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr1)[8]); +} + static void kfree_via_page(struct kunit *test) { char *ptr; @@ -1382,6 +1405,7 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(kmalloc_uaf), KUNIT_CASE(kmalloc_uaf_memset), KUNIT_CASE(kmalloc_uaf2), + KUNIT_CASE(kmalloc_uaf3), KUNIT_CASE(kfree_via_page), KUNIT_CASE(kfree_via_phys), KUNIT_CASE(kmem_cache_oob), -- 2.25.1