Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp4725394pxt; Wed, 11 Aug 2021 12:34:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPfOeyEVGQ43BhSrTlZ3JWBCWM/60sc8gJlWS3IRASiee0V+Fmcl/sfcJMr8/1YFy2GBM2 X-Received: by 2002:a92:7b10:: with SMTP id w16mr37912ilc.241.1628710476600; Wed, 11 Aug 2021 12:34:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628710476; cv=none; d=google.com; s=arc-20160816; b=mao78zI8/dppr57RwMPc0nDgLXnn5F73z5UxpHIRRYxo7werRg1P1gANF8KaMaLrxY xgUow2ymD39PWmjsJt/+jwVTwIrG49N7RPkK8Up32fZ/HKu7ka/drU0JdlUtgLHkQ0yB LWb0QYOR9sjsdvA68scVskWJn/bhfjehSrLq3rrwy8P1YZfvnRQjdJ4SW+GxsQOQ8Lzz LZ3j9gom+vnEiu1B5UvwknCfh+cbJHX4oq7bMEEM4QwjcyaqzoC7rG/QW1jXufTo0K4u 3qy5te1nnfwTzOv7N0r2a0bRCTyyMuua5bvBblwxLFbyJOFpQcjGdKlj+uHyAmxRQFkS fRbw== 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=VOkjIsl20MkGYT1/6lPiBM0ahFv6WpCTzFaVnqKATqarjES6HRBcJ0haSsuVV6rKoE thUV7HNYHvoSCO0NTaljPgq2TTDQ0vwekFGcbQegE4/NMEK9aPI6BRQULEFCh4jssFQA iX6j2Y6mP7ExIc5UMPY41J1OE6gWOTaQh5DSYa+gPrv4HpWjBswowwACUvlBZm9M4Rsw 6rEBaGNM+iEa4F/s619OFWfv/IisZRef2QoC4Dqsj9hBD0LzKJCDQmIggqRdV1P9amuF BxXSlsP4Byk40k2m+lOGnoHJzzLEbXBIS+QmCoPCRHVXQT6h9JEEN7W5BFe+pWW6kSVj tNsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=pSu9Edxg; 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.34.25; Wed, 11 Aug 2021 12:34:36 -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=pSu9Edxg; 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 S231515AbhHKTag (ORCPT + 99 others); Wed, 11 Aug 2021 15:30:36 -0400 Received: from out0.migadu.com ([94.23.1.103]:38177 "EHLO out0.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229655AbhHKTag (ORCPT ); Wed, 11 Aug 2021 15:30:36 -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=1628710208; 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=pSu9Edxg3jDKMLO55j/mpxb0e5E4TaHYxP2V2UWbyaHTec/NyCS7a6c/sYYn5d6ySAftC6 eWFXDmrhCifqdx5Y/ia8oJz67CNw2MONnOHlmGS+/kePm1R2efvQMqKrQq8I89kLaBmf9u IYS6LOrB3xcCivzT7j7VSpsagZ/FEW0= 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 7/8] kasan: test: avoid corrupting memory in copy_user_test Date: Wed, 11 Aug 2021 21:30:06 +0200 Message-Id: <17b812a3c28024acfca9b1a9e45c8235b35efa32.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 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