From patchwork Mon Feb 1 19:43:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12059875 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.1 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=unavailable 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 6BD83C43381 for ; Mon, 1 Feb 2021 19:46:34 +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 9766F64EA9 for ; Mon, 1 Feb 2021 19:46:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9766F64EA9 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=hbPBjYRnDd0JigRIWqVG/DKld7U5MI13NEHP1r0Mkjg=; b=wKTLzApub8/42acXP3VD5BJM5 +qpHhrihvMsClVKQlhwK52qMeXnXxSw/SVCMRPcCEJo5n81Aa/3lz/vGK7GVu1blpbknsVfJQ9P0V E/rjR9STwb7Soo934P+4JRp3nlA+0lTs1k9vbzqVfinBh6ZFzuoSnkK9rP7xkd0rn7GaOGY2fsA2U ZXGaiLx3Jq4bcVzbk6qfdNGlhop531PjGPjZshOMWuPe5wcucCaHI+bQ+tyCo1Fm0brgNvEr6osyS 1DVyrQm+l6Xrkhmr47ZHWO4fmzodsXLV60obrqs0ScQb9vM+GzEDkKMoldV7if6Z9ehv0YEAXpB9F 8ptTtuu4g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l6f7r-0002KJ-Oh; Mon, 01 Feb 2021 19:44:43 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l6f7A-0001vj-Fk for linux-arm-kernel@lists.infradead.org; Mon, 01 Feb 2021 19:44:01 +0000 Received: by mail-wm1-x349.google.com with SMTP id z188so393010wme.1 for ; Mon, 01 Feb 2021 11:43:57 -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=yMQgrLxhHIKq4G0a2NCbXbKyDrvusdwY4yeazjCtuyk=; b=ivESTfLfXADjygdQiFNXCX+wPXeMmmyDL7MhSMwYXQFbjxX6om64VWAa9DB0UHgjky FA0IkQ9/NQQQM1j6Es5XovdS1v0MO0KZldXf+YBmCCZn47B20vO09U3YN5ZVtPh9slfU V0695NxHTqBxob7ObPmJFRiD3DbtZI77vvKRyogyBNt+B0g1mlTTZfcvclio6T9srFgt DeT+ymo2vjail6bFhgkunaozgr3fW+qjwQ7TMJkpHXXpDU0/3qtwo9c7+nWPQ+JBZxK0 m6EaK0Hm5acOgUfe9nl3oqyOCXvObbHtXRzj+nbFuJQYf48336Ff8i+FpKXIIffMt9oQ NOjQ== 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=yMQgrLxhHIKq4G0a2NCbXbKyDrvusdwY4yeazjCtuyk=; b=JwXxUBV7HbbJZy+s2sTSzRyRMR6qfoslKM0sZ5KvJaXvrtidbCeYaOp8Iy1g978BNY 0UC3CKDLzZrN/PeumCkqmrUc8PTQxhbd95ulXYF1bKrpduQi6NoLaetHTwhwJ1nKx7Z+ EJDFw0UROWMFo/VpZpQdDjSPJpLia/KEv/GeN6cLdICRXya07NFsOqgaKDrViw1CRXmZ VWXmcIkOx5+P6CVmrXqE6odE+DwhxvgdkIeQWTHo1XWLQlqpcvnppCesa9C72LLk7ARp pPeJWHKalOMwjwCZ3PXkVV/mc0OAmekexFKkK1aurGKmm2wRqs8TUzv2Va5oi1LZ6dVU smxA== X-Gm-Message-State: AOAM532XAWnQTQYq7p4+KCPvtI8oS9mN1PYB1YAYYxCHFAQbuXyG1Utv CaZHUzpBKjh1JCaVNWpX8kBal5+mr4jWOYBo X-Google-Smtp-Source: ABdhPJzOa+LccFfH9kBnsOeOGuqeG8GyRvo7cSnKb8yeZh67FwLtjenT5WmmmRbIZ1nG1AOskfgvEC22yE1+sKw/ X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a1c:7711:: with SMTP id t17mr441041wmi.64.1612208636543; Mon, 01 Feb 2021 11:43:56 -0800 (PST) Date: Mon, 1 Feb 2021 20:43:31 +0100 In-Reply-To: Message-Id: <884e37ddff31b671725f4d83106111c7dcf8fb9b.1612208222.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH 07/12] kasan, mm: remove krealloc side-effect 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-20210201_144400_618045_AA8DE9DA X-CRM114-Status: GOOD ( 17.49 ) 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 Currently, if krealloc() is called on a freed object with KASAN enabled, it allocates and returns a new object, but doesn't copy any memory from the old one as ksize() returns 0. This makes a caller believe that krealloc() succeeded (KASAN report is printed though). This patch adds an accessibility check into __do_krealloc(). If the check fails, krealloc() returns NULL. This check duplicates the one in ksize(); this is fixed in the following patch. This patch also adds a KASAN-KUnit test to check krealloc() behaviour when it's called on a freed object. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- lib/test_kasan.c | 20 ++++++++++++++++++++ mm/slab_common.c | 3 +++ 2 files changed, 23 insertions(+) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 2bb52853f341..61bc894d9f7e 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -359,6 +359,25 @@ static void krealloc_pagealloc_less_oob(struct kunit *test) KMALLOC_MAX_CACHE_SIZE + 201); } +/* + * Check that krealloc() detects a use-after-free, returns NULL, + * and doesn't unpoison the freed object. + */ +static void krealloc_uaf(struct kunit *test) +{ + char *ptr1, *ptr2; + int size1 = 201; + int size2 = 235; + + ptr1 = kmalloc(size1, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); + kfree(ptr1); + + KUNIT_EXPECT_KASAN_FAIL(test, ptr2 = krealloc(ptr1, size2, GFP_KERNEL)); + KUNIT_ASSERT_PTR_EQ(test, (void *)ptr2, NULL); + KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)ptr1); +} + static void kmalloc_oob_16(struct kunit *test) { struct { @@ -1056,6 +1075,7 @@ static struct kunit_case kasan_kunit_test_cases[] = { KUNIT_CASE(krealloc_less_oob), KUNIT_CASE(krealloc_pagealloc_more_oob), KUNIT_CASE(krealloc_pagealloc_less_oob), + KUNIT_CASE(krealloc_uaf), KUNIT_CASE(kmalloc_oob_16), KUNIT_CASE(kmalloc_uaf_16), KUNIT_CASE(kmalloc_oob_in_memset), diff --git a/mm/slab_common.c b/mm/slab_common.c index 39d1a8ff9bb8..dad70239b54c 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1140,6 +1140,9 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size, void *ret; size_t ks; + if (likely(!ZERO_OR_NULL_PTR(p)) && !kasan_check_byte(p)) + return NULL; + ks = ksize(p); if (ks >= new_size) {