From patchwork Wed Jan 13 16:21:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12017317 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DF1AC433DB for ; Wed, 13 Jan 2021 16:25:48 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1EA9F23370 for ; Wed, 13 Jan 2021 16:25:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1EA9F23370 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=n0FdZS2EzZnceMD0z6VNo2IOkhliHIbLSkJYBUxw178=; b=w8v+JTw3t0hMS02PcXZrysJa8 RIf4Ke7FT/qdUixQBpD3Lpmcqz4zcSVKjYZnXfvN+JeWNnbKWTMxP1obrk+6lVBqw+Rr1+IWF7eAD 3sVpEYUbi7LSGCtl4SR+zCE/drz/i9xJoN7+tOe1hBl4Pg65GAg0+n7ME1fFA24pI8N+JR3jGGxZZ dyX/HAR6B3DDHePmT1qhTA62oCQr3A0NJzJj8ZtfIRycrCr3hucTynXLEy+Xf2sIVhTABmOcyMd3g d7pFnX/3qxaKUnOU+pxiqNUdRtCLffNY0YucnbL2DGOtZHyMEhRxOjtPGSsBfk/TJZoKYUr8H0keW uWs9dhXeQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kzivW-0007fC-NP; Wed, 13 Jan 2021 16:23:18 +0000 Received: from mail-qt1-x849.google.com ([2607:f8b0:4864:20::849]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kziuR-0007Gl-B0 for linux-arm-kernel@lists.infradead.org; Wed, 13 Jan 2021 16:22:28 +0000 Received: by mail-qt1-x849.google.com with SMTP id a11so1717510qto.16 for ; Wed, 13 Jan 2021 08:22:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=AYU8RiBMDoH8LqbcE5Bsl4HVhMUvTlIJH5ygAkCw1uo=; b=sK8w4/7+bFlRogPbbwsMzQZGIyJaLfruaGQq1NyTZoKuRV8kkSuJYSTajYObeb5BhB j5B0aO7AYdjA/X7h/55sqn/8eJ3L98sNWt0usa6ZKYeOu2Bu8A8yH7kyedWNIqOTdyrK 7CS2smIDxfMMUEU7+r5sUDMO6AU5v0dCsYRHfTm3snA0bdsu5Ihim6C+QorZWPCWRicw Qj0JSwE5rlQKQmMPulAWV7Qb3+JbCq4sEEKb5k2sIlwQpL768ladXTwg9PV88ssTPbGC ghBTXXxjBAM5WcVjFFrHK6/Ml9RcY1l1gksA/BDruf31RYSvG4PiPSYoNNlzTZKxcYQA r4hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=AYU8RiBMDoH8LqbcE5Bsl4HVhMUvTlIJH5ygAkCw1uo=; b=dYvJMjxtvjWJqHbHlFwHpOEJ4cnF2gV1AEDMnM6QojKGgSy9tGcv9oF5aek1dd+W59 QoHRd9a2zeInqrd08UlQejIj+noPCMuNQ6mt+fGhW6HaC5/UubL+gUyu2hN2upo2eY9h 8PQ7K5380etXETLSCKITle78etVWrjNUWI2OGeZlXkpt5KGA4btEkRWlObI2JltpXF+a f+8AnPzm/P354k4FoQ8jOmS1owLqAUB6WIi6fXTm4CSkgvqpE7Y6L9Fhju4DSuVywmFg HHOfTtoj+7MILvWgR505jRMJ3u9xQL0Rs6NQ2woBsKkb9emX5dJSSVRLPjbZA34Xidab FP4g== X-Gm-Message-State: AOAM530FXVmWsJnScAHw0WkI3QyFFXz3eilTxS96e8H6EyWV3CyFz1XD sV5KpUnLHr2o8Ecs9Izq17wN9CpEHH2Khol6 X-Google-Smtp-Source: ABdhPJzAk8lbAA2oeZ/5LkBz1EFW7U5GssDQ0xOiO1RZBV9k+c3FKD7ufLGB+DknkADCLYH+T0s3FGzAptjFgwj6 X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:fe91:: with SMTP id d17mr3133660qvs.50.1610554927401; Wed, 13 Jan 2021 08:22:07 -0800 (PST) Date: Wed, 13 Jan 2021 17:21:36 +0100 In-Reply-To: Message-Id: <1b884616c85091d6d173f7c1a8647d25424f1e7e.1610554432.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH v2 09/14] kasan: adapt kmalloc_uaf2 test to HW_TAGS mode From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210113_112211_494286_C5183C6E X-CRM114-Status: GOOD ( 13.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Branislav Rankov , Andrey Konovalov , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Andrew Morton , Peter Collingbourne , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In the kmalloc_uaf2() test, the pointers to the two allocated memory blocks might happen to be the same, and the test will fail. With the software tag-based mode, the probability of the that is 1/254, so it's hard to observe the failure. For the hardware tag-based mode though, the probablity is 1/14, which is quite noticable. Allow up to 16 attempts at generating different tags for the tag-based modes. Link: https://linux-review.googlesource.com/id/Ibfa458ef2804ff465d8eb07434a300bf36388d55 Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko --- lib/test_kasan.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 283feda9882a..a1a35d75ee1e 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -382,7 +382,9 @@ static void kmalloc_uaf2(struct kunit *test) { char *ptr1, *ptr2; size_t size = 43; + int counter = 0; +again: ptr1 = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); @@ -391,6 +393,15 @@ static void kmalloc_uaf2(struct kunit *test) ptr2 = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2); + /* + * For tag-based KASAN ptr1 and ptr2 tags might happen to be the same. + * Allow up to 16 attempts at generating different tags. + */ + if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && ptr1 == ptr2 && counter++ < 16) { + kfree(ptr2); + goto again; + } + KUNIT_EXPECT_KASAN_FAIL(test, ptr1[40] = 'x'); KUNIT_EXPECT_PTR_NE(test, ptr1, ptr2);