Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp51711pxb; Thu, 12 Aug 2021 10:33:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQzraP56XtpsA0Sam/P39zjLvH+wy6uYKKb4AB07Ytgcc8rCl+cg54zli7B6ADO2nRW9CA X-Received: by 2002:a05:6402:394:: with SMTP id o20mr6736729edv.232.1628789584447; Thu, 12 Aug 2021 10:33:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628789584; cv=none; d=google.com; s=arc-20160816; b=mX9DMRZkL8MFmWaKENb4pvShSw6rc+leWxLc3MX3eABxTTJeU8GdWCwo3lly6xnJb3 nwTbUzT1HIXf5i2E+nSEnbVV8p13EDIWzICAysfuR8T97E4Kd1dJKcs473vKpLnGUuK0 oxYkeUcHo0Z7+cd4UOC53XvP1WJ71Ji6m93xSHxIotLlXsKNQcN8Yi1CfnC7owQPhlZ3 oTfEm0Yst59xegDH0ZIkl9ypJTSPpekUqzAjYAxmS7Xia/oYbXGA4I5sqViqTyqh6HP3 QTqtSotkacjGn35Z4/Wd4Ki2m+05m0lc7AzCE77wgj5md3vaSyTy0BtLmRA6m/rDfl/m mXWw== 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=gpLXpLqEdWJax+H+vzAJpIoO8hEwkusrWIf9JpaCtao=; b=QF+3MHp9MYG28NM/yI204kZ5QcGuwfJwuKoY/GBqzDfPcmocrLNhe9GmqsLgA58TaO ZUJBeIhI4EeYAdF+Zju0L9/CsONnKm3n/p69qmAt3webe9HCNrdKGt2PhVZx83Yjd/GH MuWmUyIl2T7+AJXfakynfQfDWa93FxuXYJsYsG1aOviBhTFOc84HbMfWJ8hQsIraiAB6 zRk1saublbWzRQCIwmDVoPu+dshOsfBHJA5zHicE05kRSzmApt5G5HTHU8C0h0gaKsf7 FOzhMYCERcFiN0d5m8uv4fkwxosFiCJKD+3WYkSwKh6iHk0qC+Xy1KYSW/HmYCIYXYc5 wbDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=pYy6jrZ0; 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 hq31si3632202ejc.316.2021.08.12.10.32.40; Thu, 12 Aug 2021 10:33:04 -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=pYy6jrZ0; 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 S237652AbhHLPAu (ORCPT + 99 others); Thu, 12 Aug 2021 11:00:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237129AbhHLPAu (ORCPT ); Thu, 12 Aug 2021 11:00:50 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3565C061756 for ; Thu, 12 Aug 2021 08:00:24 -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=1628780423; 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=gpLXpLqEdWJax+H+vzAJpIoO8hEwkusrWIf9JpaCtao=; b=pYy6jrZ0UtyPZHrLulARW5V77PhfN8wVfZc6/cCLZeNYKKqxESGLPhwQAa8gNYMneY525f Hxc0kIUurbbQx14TTfLftjwSV3B5dfdNs5bAuNlE8daZLwT5+uQruPdf4/iI3LM1tHGZOy II+uPWfWXnWXHbVv3ACqxNLp0aHEdqA= 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 7/8] kasan: test: avoid corrupting memory in copy_user_test Date: Thu, 12 Aug 2021 17:00:21 +0200 Message-Id: <19bf3a5112ee65b7db88dc731643b657b816c5e8.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 copy_user_test() does writes past the allocated object. As the result, it corrupts kernel memory, which might lead to crashes with the HW_TAGS mode, as it neither uses quarantine nor redzones. (Technically, this test can't yet be enabled with the HW_TAGS mode, but this will be implemented in the future.) Adjust the test to only write memory within the aligned kmalloc object. Signed-off-by: Andrey Konovalov --- lib/test_kasan_module.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/test_kasan_module.c b/lib/test_kasan_module.c index f1017f345d6c..fa73b9df0be4 100644 --- a/lib/test_kasan_module.c +++ b/lib/test_kasan_module.c @@ -15,13 +15,11 @@ #include "../mm/kasan/kasan.h" -#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE) - static noinline void __init copy_user_test(void) { char *kmem; char __user *usermem; - size_t size = 10; + size_t size = 128 - KASAN_GRANULE_SIZE; int __maybe_unused unused; kmem = kmalloc(size, GFP_KERNEL); @@ -38,25 +36,25 @@ static noinline void __init copy_user_test(void) } pr_info("out-of-bounds in copy_from_user()\n"); - unused = copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF); + unused = copy_from_user(kmem, usermem, size + 1); pr_info("out-of-bounds in copy_to_user()\n"); - unused = copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF); + unused = copy_to_user(usermem, kmem, size + 1); pr_info("out-of-bounds in __copy_from_user()\n"); - unused = __copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF); + unused = __copy_from_user(kmem, usermem, size + 1); pr_info("out-of-bounds in __copy_to_user()\n"); - unused = __copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF); + unused = __copy_to_user(usermem, kmem, size + 1); pr_info("out-of-bounds in __copy_from_user_inatomic()\n"); - unused = __copy_from_user_inatomic(kmem, usermem, size + 1 + OOB_TAG_OFF); + unused = __copy_from_user_inatomic(kmem, usermem, size + 1); pr_info("out-of-bounds in __copy_to_user_inatomic()\n"); - unused = __copy_to_user_inatomic(usermem, kmem, size + 1 + OOB_TAG_OFF); + unused = __copy_to_user_inatomic(usermem, kmem, size + 1); pr_info("out-of-bounds in strncpy_from_user()\n"); - unused = strncpy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF); + unused = strncpy_from_user(kmem, usermem, size + 1); vm_munmap((unsigned long)usermem, PAGE_SIZE); kfree(kmem); -- 2.25.1