Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp728229pxt; Thu, 12 Aug 2021 08:19:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxlJSzQKcaLhcDo/2XVLW7zScmIm+kzA/FBJeg+iPIcoqoVxjxy1hh+PO6xtwTpp95Q44Ek X-Received: by 2002:a17:906:d52:: with SMTP id r18mr4151589ejh.47.1628781581923; Thu, 12 Aug 2021 08:19:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628781581; cv=none; d=google.com; s=arc-20160816; b=gwG45sEf3XDNp/u5g2fiuWS6y59WU+L4M0KhRG2vuh7Zx4IDmGlhq7Z8QIzVTb4+7V 57aee/8jclE2XTqRXuakrc/MfYAl+JqOqHZO3/p4vvq3xrSLJAzWJZXAy6TEahoK/+Pu v1gOpYfTJdVjvdSW8trpiBcO75fXb2Yj6LGIWqDKCk12O0/qbSpNj7nzbdoMGKHnZuP0 FqwsjW+TePe76ay0zOIrM3siwnEGAg+pzvGf8QyL+tduE39FwXWPanFCkQvemDerqGF9 V3upPBnksIyHC9ZZ6KhQCA3RkeeBafBFFVGX/cXPFr6Cu2P5gBbg9IP/oCm8ZFw6B2wN O9Ig== 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=rCxgc2eXwoBM9Jq6kKWMWlEDWeYYkjt8bVAWDL+S3zY=; b=JdsZG6kLCRas541jBxYK7soUYmGNSHjDpVyONDfplv+HWG7KIF+zJPuVRE5gl+kCKS 8pZ3H+T6BiFq7R3X01kN/hqG992V3j5x5qIMVq1EfKr5dMnWZlow3FodxLzG7vdH2Cy3 nGG7bq8I2zpxry1UfY3RNEYwy594C4Brd7trBVQjm9WzTE9MxxxWn7Yg33dYmKprGFDk v1ek6OklaG9z9aXGZJk0/hCgo53i8Cyir3hHnI1KI0yNlxAs2CkQ4094ayWvneVAVnoH CHhF0ZijvPBjQC4GbeblEh6xSexoXQJ0Msa2tXkWN/Z8V6uHZSlR/GQ9IkarBjdXHXfw 8+/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=AZHYWSAY; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s18si3278656eji.591.2021.08.12.08.19.18; Thu, 12 Aug 2021 08:19:41 -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; dkim=pass header.i=@linux.dev header.s=key1 header.b=AZHYWSAY; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238201AbhHLOyW (ORCPT + 99 others); Thu, 12 Aug 2021 10:54:22 -0400 Received: from out2.migadu.com ([188.165.223.204]:65054 "EHLO out2.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236964AbhHLOyS (ORCPT ); Thu, 12 Aug 2021 10:54:18 -0400 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=1628780032; 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=rCxgc2eXwoBM9Jq6kKWMWlEDWeYYkjt8bVAWDL+S3zY=; b=AZHYWSAY6u8of2S9Uh2HPisrpdnDgrGOTBNn0OU4NMs5Xph99AC3MMNtoiZbG0ooZfvUGB 1xMOSi1ab2RYa2PZuVFEdN7lOV8BmnsbYHnFYYq5EWokVpS/33QjTeageOmCGbVJs9sj0q DWzig2XdXx2kAKZkga+UFP0g/rry4DE= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Andrey Ryabinin , Marco Elver , Dmitry Vyukov , Alexander Potapenko , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/8] kasan: test: avoid corrupting memory via memset Date: Thu, 12 Aug 2021 16:53:30 +0200 Message-Id: <64fd457668a16e7b58d094f14a165f9d5170c5a9.1628779805.git.andreyknvl@gmail.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: andrey.konovalov@linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Konovalov kmalloc_oob_memset_*() tests do writes past the allocated objects. As the result, they corrupt memory, which might lead to crashes with the HW_TAGS mode, as it neither uses quarantine nor redzones. Adjust the tests to only write memory within the aligned kmalloc objects. Also add a comment mentioning that memset tests are designed to touch both valid and invalid memory. Signed-off-by: Andrey Konovalov --- lib/test_kasan.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index c82a82eb5393..db73bc9e3fa2 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -428,64 +428,70 @@ static void kmalloc_uaf_16(struct kunit *test) kfree(ptr1); } +/* + * Note: in the memset tests below, the written range touches both valid and + * invalid memory. This makes sure that the instrumentation does not only check + * the starting address but the whole range. + */ + static void kmalloc_oob_memset_2(struct kunit *test) { char *ptr; - size_t size = 8; + size_t size = 128 - KASAN_GRANULE_SIZE; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 7 + OOB_TAG_OFF, 0, 2)); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2)); kfree(ptr); } static void kmalloc_oob_memset_4(struct kunit *test) { char *ptr; - size_t size = 8; + size_t size = 128 - KASAN_GRANULE_SIZE; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 5 + OOB_TAG_OFF, 0, 4)); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4)); kfree(ptr); } - static void kmalloc_oob_memset_8(struct kunit *test) { char *ptr; - size_t size = 8; + size_t size = 128 - KASAN_GRANULE_SIZE; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 8)); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8)); kfree(ptr); } static void kmalloc_oob_memset_16(struct kunit *test) { char *ptr; - size_t size = 16; + size_t size = 128 - KASAN_GRANULE_SIZE; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + 1 + OOB_TAG_OFF, 0, 16)); + KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16)); kfree(ptr); } static void kmalloc_oob_in_memset(struct kunit *test) { char *ptr; - size_t size = 666; + size_t size = 128 - KASAN_GRANULE_SIZE; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr, 0, size + 5 + OOB_TAG_OFF)); + KUNIT_EXPECT_KASAN_FAIL(test, + memset(ptr, 0, size + KASAN_GRANULE_SIZE)); kfree(ptr); } -- 2.25.1