From patchwork Fri Oct 18 09:42:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 11197991 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C71E614ED for ; Fri, 18 Oct 2019 09:43:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8A66820820 for ; Fri, 18 Oct 2019 09:43:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="s/0DMqth" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8A66820820 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3EA918E0028; Fri, 18 Oct 2019 05:43:53 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3740E8E0003; Fri, 18 Oct 2019 05:43:53 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 239948E0028; Fri, 18 Oct 2019 05:43:53 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0003.hostedemail.com [216.40.44.3]) by kanga.kvack.org (Postfix) with ESMTP id F3FC58E0003 for ; Fri, 18 Oct 2019 05:43:52 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id A53D518253E07 for ; Fri, 18 Oct 2019 09:43:52 +0000 (UTC) X-FDA: 76056418704.01.army90_791d256b2365b X-Spam-Summary: 2,0,0,ba77070058caf69d,d41d8cd98f00b204,3v4mpxqykcaqkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com,:glider@google.com:vegard.nossum@oracle.com:dvyukov@google.com:,RULES_HIT:2:41:69:152:355:379:541:800:960:966:973:988:989:1042:1260:1277:1313:1314:1345:1359:1437:1516:1518:1535:1593:1594:1605:1730:1747:1777:1792:2196:2199:2393:2538:2553:2559:2562:3138:3139:3140:3141:3142:3152:3865:3866:3867:3868:3870:3871:3872:3873:3874:4049:4120:4250:4321:4385:5007:6119:6120:6261:6653:7901:7903:7904:8660:9969:10004:11026:11232:11473:11658:11914:12043:12291:12294:12295:12296:12297:12438:12555:12683:12895:12986:13148:13161:13229:13230:13846:14394:14659:21080:21365:21444:21451:21627:21740:21772:30054:30056:30064:30067:30070:30090,0,RBL:209.85.222.201:@flex--glider.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_ SUMMARY: X-HE-Tag: army90_791d256b2365b X-Filterd-Recvd-Size: 9028 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Fri, 18 Oct 2019 09:43:52 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id o133so5019467qke.4 for ; Fri, 18 Oct 2019 02:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=p9HVcKssM0/JxHv7Ium7oVkbT6/yYco02v9RVD8kuHk=; b=s/0DMqthBE1YsHpcsljNveryfBffnFauhbA8NMOnU9byJE/kpMBEn8zPa6VB3jCL3F c4xRvOBP+QjG8HfWlv3Dbhk4fS6/y2bTop74tPG1uyN2CPca8D8JUl2BCM8Fi+sLo73R M67QLEqFZMSYXQeUl50CkxlAFgBiHODloHrXzB+VEaXJQeLLbFARZeb1EoU84zKof3N7 r5iU2tuAGEJvzfYQoBhLzVDkQbdxFxKoRnfFbjwzQaGg303s8v6HuUP2cWaiqMrE2Yb+ g4spl54PtLAaeWCLF0e/2PkC4rvC8L0N3Z+hf2ptL33RGu2LShWZW+tTBnWK7nZpMCax j5iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=p9HVcKssM0/JxHv7Ium7oVkbT6/yYco02v9RVD8kuHk=; b=aXvuRr9dMNZ+g5gcbNd1O08gKc6xEX9xf1YUugaWxH2y2/t7GuOwDm8+5EcruK9JT7 GclwVCGt5aZ0SPEoiZYZhA6m/iedoto335qflj+J7u/hU1cP/RazjsjuH6sMhQiHl79h 02mR7QvN5C8hUipSF0Hycc4iYtaq2opSdSE1kGKktim4WZdrMzv0hrtyitsJqcqp1nju 36Mn/gtDaUBF8TYXmwExorP2iSRZBBIYzmSgcmT4QLVcTtYL8itFVWpTLMzFl4qO/ZW8 kqZcAKDZTHSphIK3gufGCpIx85hZoxoZdrQSVhHxF4S9qUHoQZ+dI45/2sddCPpxclXy EtGw== X-Gm-Message-State: APjAAAWonkUIft6E6aBcNZXV9BWO/WzJjKf6+bUXLJTvRRAwobDrL6Ip a8bLxDk6X1KmB+NKQ3Lu50/v37hwWu4= X-Google-Smtp-Source: APXvYqyx451t+EiqYgnYURj2Db3o4R25SHcg/Wb4iJ24zFPy0f4LLHBzHzNT2pRa1UD8HZxS9Fq3llS2Txo= X-Received: by 2002:a37:4ac8:: with SMTP id x191mr7995192qka.85.1571391831539; Fri, 18 Oct 2019 02:43:51 -0700 (PDT) Date: Fri, 18 Oct 2019 11:42:51 +0200 In-Reply-To: <20191018094304.37056-1-glider@google.com> Message-Id: <20191018094304.37056-14-glider@google.com> Mime-Version: 1.0 References: <20191018094304.37056-1-glider@google.com> X-Mailer: git-send-email 2.23.0.866.gb869b98d4c-goog Subject: [PATCH RFC v1 13/26] kmsan: add tests for KMSAN From: glider@google.com To: Alexander Potapenko Cc: Vegard Nossum , Dmitry Vyukov , linux-mm@kvack.org X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The initial commit adds several tests that trigger KMSAN warnings in simple cases. To use, build the kernel with CONFIG_TEST_KMSAN and do `insmod test_kmsan.ko` Signed-off-by: Alexander Potapenko To: Alexander Potapenko Cc: Vegard Nossum Cc: Dmitry Vyukov Cc: linux-mm@kvack.org --- Change-Id: I287e86ae83a82b770f2baa46e5bbdce1dfa65195 --- lib/Makefile | 1 + lib/test_kmsan.c | 221 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 lib/test_kmsan.c diff --git a/lib/Makefile b/lib/Makefile index c5892807e06f..cb44262c38ee 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -65,6 +65,7 @@ CFLAGS_test_kasan.o += $(call cc-disable-warning, vla) obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o CFLAGS_test_ubsan.o += $(call cc-disable-warning, vla) UBSAN_SANITIZE_test_ubsan.o := y +obj-$(CONFIG_TEST_KMSAN) += test_kmsan.o obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o obj-$(CONFIG_TEST_LKM) += test_module.o diff --git a/lib/test_kmsan.c b/lib/test_kmsan.c new file mode 100644 index 000000000000..97051444c6c4 --- /dev/null +++ b/lib/test_kmsan.c @@ -0,0 +1,221 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Module for testing KMSAN. + * + * Copyright (C) 2017-2019 Google LLC + * Author: Alexander Potapenko + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +/* + * Tests below use noinline and volatile to work around compiler optimizations + * that may mask KMSAN bugs. + */ +#define pr_fmt(fmt) "kmsan test: %s : " fmt, __func__ + +#include +#include +#include +#include +#include + +#define CHECK(x) \ + do { \ + if (x) \ + pr_info(#x " is true\n"); \ + else \ + pr_info(#x " is false\n"); \ + } while (0) + +noinline void use_integer(int cond) +{ + CHECK(cond); +} + +int signed_sum3(int a, int b, int c) +{ + return a + b + c; +} + +noinline void uninit_kmalloc_test(void) +{ + int *ptr; + + pr_info("-----------------------------\n"); + pr_info("uninitialized kmalloc test (UMR report)\n"); + ptr = kmalloc(sizeof(int), GFP_KERNEL); + pr_info("kmalloc returned %p\n", ptr); + CHECK(*ptr); +} +noinline void init_kmalloc_test(void) +{ + int *ptr; + + pr_info("-----------------------------\n"); + pr_info("initialized kmalloc test (no reports)\n"); + ptr = kmalloc(sizeof(int), GFP_KERNEL); + memset(ptr, 0, sizeof(int)); + pr_info("kmalloc returned %p\n", ptr); + CHECK(*ptr); +} + +noinline void init_kzalloc_test(void) +{ + int *ptr; + + pr_info("-----------------------------\n"); + pr_info("initialized kzalloc test (no reports)\n"); + ptr = kzalloc(sizeof(int), GFP_KERNEL); + pr_info("kzalloc returned %p\n", ptr); + CHECK(*ptr); +} + +noinline void uninit_multiple_args_test(void) +{ + volatile int a; + volatile char b = 3, c; + + pr_info("-----------------------------\n"); + pr_info("uninitialized local passed to fn (UMR report)\n"); + CHECK(signed_sum3(a, b, c)); +} + +noinline void uninit_stack_var_test(void) +{ + int cond; + + pr_info("-----------------------------\n"); + pr_info("uninitialized stack variable (UMR report)\n"); + CHECK(cond); +} + +noinline void init_stack_var_test(void) +{ + volatile int cond = 1; + + pr_info("-----------------------------\n"); + pr_info("initialized stack variable (no reports)\n"); + CHECK(cond); +} + +noinline void two_param_fn_2(int arg1, int arg2) +{ + CHECK(arg1); + CHECK(arg2); +} + +noinline void one_param_fn(int arg) +{ + two_param_fn_2(arg, arg); + CHECK(arg); +} + +noinline void two_param_fn(int arg1, int arg2) +{ + int init = 0; + + one_param_fn(init); + CHECK(arg1); + CHECK(arg2); +} + +void params_test(void) +{ + int uninit, init = 1; + + two_param_fn(uninit, init); +} + +noinline void do_uninit_local_array(char *array, int start, int stop) +{ + int i; + volatile char uninit; + + for (i = start; i < stop; i++) + array[i] = uninit; +} + +noinline void uninit_kmsan_check_memory_test(void) +{ + volatile char local_array[8]; + + pr_info("-----------------------------\n"); + pr_info("kmsan_check_memory() called on uninit local (UMR report)\n"); + do_uninit_local_array((char *)local_array, 5, 7); + + kmsan_check_memory((char *)local_array, 8); +} + +noinline void init_kmsan_vmap_vunmap_test(void) +{ + const int npages = 2; + struct page *pages[npages]; + void *vbuf; + int i; + + pr_info("-----------------------------\n"); + pr_info("pages initialized via vmap (no reports)\n"); + + for (i = 0; i < npages; i++) + pages[i] = alloc_page(GFP_KERNEL); + vbuf = vmap(pages, npages, VM_MAP, PAGE_KERNEL); + memset(vbuf, 0xfe, npages * PAGE_SIZE); + for (i = 0; i < npages; i++) + kmsan_check_memory(page_address(pages[i]), PAGE_SIZE); + + if (vbuf) + vunmap(vbuf); + for (i = 0; i < npages; i++) + if (pages[i]) + __free_page(pages[i]); +} + +noinline void init_vmalloc(void) +{ + char *buf; + int npages = 8, i; + + pr_info("-----------------------------\n"); + pr_info("pages initialized via vmap (no reports)\n"); + buf = vmalloc(PAGE_SIZE * npages); + buf[0] = 1; + memset(buf, 0xfe, PAGE_SIZE * npages); + CHECK(buf[0]); + for (i = 0; i < npages; i++) + kmsan_check_memory(&buf[PAGE_SIZE * i], PAGE_SIZE); + vfree(buf); +} + +noinline void uaf_test(void) +{ + volatile int *var; + + pr_info("-----------------------------\n"); + pr_info("use-after-free in kmalloc-ed buffer (UMR report)\n"); + var = kmalloc(80, GFP_KERNEL); + var[3] = 0xfeedface; + kfree((int *)var); + CHECK(var[3]); +} + +static noinline int __init kmsan_tests_init(void) +{ + uninit_kmalloc_test(); + init_kmalloc_test(); + init_kzalloc_test(); + uninit_multiple_args_test(); + uninit_stack_var_test(); + init_stack_var_test(); + uninit_kmsan_check_memory_test(); + init_kmsan_vmap_vunmap_test(); + init_vmalloc(); + uaf_test(); + return -EAGAIN; +} + +module_init(kmsan_tests_init); +MODULE_LICENSE("GPL");