Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp729681pxt; Thu, 12 Aug 2021 08:21:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwlRfubCQ6hKKXCLt67Ni4wn7BFzdiTuTkkMxh+o7uMRDqVOlCmxkt8B4d08v5VpX1UF8dm X-Received: by 2002:aa7:c890:: with SMTP id p16mr5975205eds.280.1628781687570; Thu, 12 Aug 2021 08:21:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628781687; cv=none; d=google.com; s=arc-20160816; b=sDqat/wW6fonx886jKFUKwOlLKuL4/CMthMxZA7T/C8j/TP5uLCdrgn0q+vcDvavjc uQAlvdSFBgwh86PoHottFiwexS2NsPMYuEkdBRFSULttO5ntrEbdFohRJOF6Y8gq5dyd 5BjDI3WOPN68q40kp5zwllMSAz3MXPZshafTzpA+kFreZfmgwrW2KaQjyPR4kaDknM5u azc6yfP4eYdQXO0wZcy3p59QJxdeiAXCPW5FgTq1md1Ozb8WtmR9f2V9sKa9FRXPSRDL ICo9a6Yj5+YfjY30QuWy5KSLkhdInh0Znd15KDPTT1nKU6R7dPVUGhUKfZrus++SwW3z NXEA== 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=jSkGndKoic/cQlP256matKjJ3c5MtLdfeJ1Ag6wLnsDBTTavp7EIdmtzupj7zGT7zh iiWHk/C4sEXKlV7hm0sMhQc9AEp8rnyhLYenV/0kdsHE0o61EQxsvpuB+RMrecdlKN8B KdmiUXfh4Gk2Syz3Dkxjs3TyBl2qKZvliuWRbq4otR/Xg/4GyyCwQluxfc0Q8cGdrCNG x6ZngLvMC+ix/Y/464VTlbRYLzG/MO852c/Xc43JMQ05jRpLigf3hdBE765+rA2T8OYY UrWdD0DRkiyhNtm9iMDWBKYMTrPdK/WSVt4Vh+0K0BIEwR9NEQ0S9mrko3KGVlJTX78p Qt/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=JwmPAbtU; 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 m13si2764764edv.524.2021.08.12.08.21.03; Thu, 12 Aug 2021 08:21:27 -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=JwmPAbtU; 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 S237041AbhHLOyS (ORCPT + 99 others); Thu, 12 Aug 2021 10:54:18 -0400 Received: from out2.migadu.com ([188.165.223.204]:65034 "EHLO out2.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236390AbhHLOyS (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=1628780031; 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=JwmPAbtUb5XmR/gj7ZWfs+KA7FiPq4mvzQRZXnX5BPUygrXO2nZX5gNY+K6Hmml+//Uyd3 KDr9vab4YxrGHJ2qdhbFheEYpVMz4cPFJl482p/w1QkzK5UMSnr1mgfTKqJvioNqqJUt7R tZ4LSm+3Ryu+6Us1ug9CabRU4LEqeQ8= 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 1/8] kasan: test: rework kmalloc_oob_right Date: Thu, 12 Aug 2021 16:53:28 +0200 Message-Id: <474aa8b7b538c6737a4c6d0090350af2e1776bef.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 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