Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp4717962pxt; Wed, 11 Aug 2021 12:24:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmW3JErmuXlKD8dUin+94iy5phQP2QCzSxCYGD6GPcrdtMjSRY7lDRn35ZAGC1+e23yFUn X-Received: by 2002:a92:3607:: with SMTP id d7mr23530ila.5.1628709854036; Wed, 11 Aug 2021 12:24:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628709854; cv=none; d=google.com; s=arc-20160816; b=gXSXqB6ueYntEkTOkMvGYd//b8aIpiwVvsxbf9+WcfYNnqLHTokHRa7mc9nHQ78Smz wxO92nCWl3CWiqVAFXetoQX3HIqZydQSpL4QFRsNQ5DFOeh2pCoR2p4I8ti/xdFbcyRk 6t4f6FgUcdI+g4q4wtOyQ3kGFcqhHu6QpB/OKmNOag0SWUZ+/cdhF3TMHoXsf+1e98Oq QH1fneXTj/VnsqHsQXHuWPWavhK7PoVqFM/WNHmD9ozrl32UgAlaydDI+5Xksvr1PAa8 Bsya1SFPmnoLfjOl04u/VEBWfJd356jvRIEKhnwuGo2z8aJQomR3QW8M2g5ImD93VMQg eYkw== 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=hJvl0j61Zq+oSt6HYk9qiIcFyGt4nhGk7HpIREtRZqk=; b=L+3uhsjd7U618fHg3OclRu0kEOt/TSKLMakGHgNuroY0GtAPfNIxzlIyikWPUTbGjM bZFNUBf3dOe9r3KrK0TTHZ8dRMoTi/83s3MmzE2ySSDJCowIBqgPQpIhdkDHGtsfzs7r xYktqWofjkMwJgIbB9wYXyp+mHCtZzmUXTKOCFdIjl27ng5/rI7P324csOf07mJ9gdpR baJfIK5D8/CYng+Xa0RE5jnj9XrC5I29vHuaNYfZs6hwLUcMcEs9bYw6ZRLB1ACC1dF8 RBYAK/OPEKtcma1CRNtINuQNOFW/cfCbJNAFKbpRFDka8yqJ3KIGFZvHKyLr8C/TH3KB BSQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=hOF2jU13; 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 o1si298072ill.7.2021.08.11.12.24.01; Wed, 11 Aug 2021 12:24:14 -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=hOF2jU13; 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 S231585AbhHKTWL (ORCPT + 99 others); Wed, 11 Aug 2021 15:22:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231377AbhHKTWH (ORCPT ); Wed, 11 Aug 2021 15:22:07 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41C13C061765 for ; Wed, 11 Aug 2021 12:21:43 -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=1628709701; 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=hJvl0j61Zq+oSt6HYk9qiIcFyGt4nhGk7HpIREtRZqk=; b=hOF2jU13WMxGXYYEG436TSXwGCcE2aedIoEPoNI5CQoEycB/bVTKm6f5ZmS+2dFlBGDiDx v7t95qegZNw2qvMTS8poe8JntrzUbYka8gmE8QAGtrXSxRHexl9wtstvVbaTsFnWQXAgt0 rwlNza06rHWoAtKW+JVFHbx1Da21aX8= 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 1/8] kasan: test: rework kmalloc_oob_right Date: Wed, 11 Aug 2021 21:21:17 +0200 Message-Id: <474aa8b7b538c6737a4c6d0090350af2e1776bef.1628709663.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 Rework kmalloc_oob_right() to do these bad access checks: 1. An unaligned access one byte past the requested kmalloc size (can only be detected by KASAN_GENERIC). 2. An aligned access into the first out-of-bounds granule that falls within the aligned kmalloc object. 3. Out-of-bounds access past the aligned kmalloc object. Test #3 deliberately uses a read access to avoid corrupting memory. Otherwise, this test might lead to crashes with the HW_TAGS mode, as it neither uses quarantine nor redzones. Signed-off-by: Andrey Konovalov --- lib/test_kasan.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/test_kasan.c b/lib/test_kasan.c index 8f7b0b2f6e11..1bc3cdd2957f 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -122,12 +122,28 @@ static void kasan_test_exit(struct kunit *test) static void kmalloc_oob_right(struct kunit *test) { char *ptr; - size_t size = 123; + size_t size = 128 - KASAN_GRANULE_SIZE - 5; ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); - KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 'x'); + /* + * An unaligned access past the requested kmalloc size. + * Only generic KASAN can precisely detect these. + */ + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x'); + + /* + * An aligned access into the first out-of-bounds granule that falls + * within the aligned kmalloc object. + */ + KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y'); + + /* Out-of-bounds access past the aligned kmalloc object. */ + KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = + ptr[size + KASAN_GRANULE_SIZE + 5]); + kfree(ptr); } -- 2.25.1