From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B27B9C3F68F for ; Thu, 5 Dec 2019 14:31:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4DBB024651 for ; Thu, 5 Dec 2019 14:31:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cEEgBDPT" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4DBB024651 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 BBD746B10A1; Thu, 5 Dec 2019 09:31:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B6D106B10A2; Thu, 5 Dec 2019 09:31:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A82796B10A3; Thu, 5 Dec 2019 09:31:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0197.hostedemail.com [216.40.44.197]) by kanga.kvack.org (Postfix) with ESMTP id 936B46B10A1 for ; Thu, 5 Dec 2019 09:31:01 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id 50AD3180AD802 for ; Thu, 5 Dec 2019 14:31:01 +0000 (UTC) X-FDA: 76231324722.05.glue51_25e563006a03c X-HE-Tag: glue51_25e563006a03c X-Filterd-Recvd-Size: 13892 Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Thu, 5 Dec 2019 14:31:00 +0000 (UTC) Received: by mail-wm1-f67.google.com with SMTP id b11so4051055wmj.4 for ; Thu, 05 Dec 2019 06:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=iiv2CZzH9D1/WpC5eCHGoQA1N9QcgtNLj+39902RUM4=; b=cEEgBDPThpe6p0cvaJXSCWs4F5/vochKwukbpyYqNQsfzgMOsHELC8R32dS1uxeFoQ pg0M67mRt7SeVUGLM1by2a64Rcf74FZKoq2ivR3ALHZQsCzMi9g+/NCyBviUSkv1SOvg +9Dx0pI87mZsHt+KvSr4Yxb/OuVu9RZ6pFllu2QrTElLJXkQu8NbsKsXjCLoI583lYUl /IAslENMEHd8bxsTG5bWMccq5nVDw+gkkvGF3gbiv3TQHZr/n2m5Zno2Anel0lg5fNFp wh+zunHD/9uZY6fOhnmD8nJgXgueDwp5RpUY0YVeMMy4wpDN4DwcYdLasbm/o5sH0dB+ wclg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=iiv2CZzH9D1/WpC5eCHGoQA1N9QcgtNLj+39902RUM4=; b=MNIeFfOePqH039+VsKL86up4vQOOdu41DrnxMA6DNfctMQMy8WH2zM80aboBZHt8pj ixC+7B4NuGzUF7YXBkIzXUnV79CKxI/MpXRGTr8StMdKmxEieMA6fBnCptsqCTh6dvO7 mXrycfa03pZMnbq7aAFzFR5BD9Sc5BXrNEVK1aAN5gVBgtP0/d+fFTCDspYtc1RLsnq9 ASmH9s+Rf85ytI78UlMdidpwtFejk4JbSSMcOevOQteMP0u5cT6BUC1mzN2KeE1AN60w 9nL3I9m01t2/15NOZ3/Obf7eAorS3UAZtt49QvpMpWgLS74vcM81G2oHXCP0VNLzY3w/ XG3g== X-Gm-Message-State: APjAAAUf8/Isbq4wIUa4XqWcNfU3iprClptbv5mN1hJW1l0bW4cG6X4z fCA6O16YuBwnBi+G4bI53ZQL2i93gwfs5QsZMAXWnA== X-Google-Smtp-Source: APXvYqw/eUpL3KGphrC0D0yBBRtuLyt5JgpD7M/wwtTGBtl1glFmD8rmrSPUDeGU/oJalI72veCwf4Ww8pSvndm1PJQ= X-Received: by 2002:a1c:3b82:: with SMTP id i124mr5906827wma.122.1575556258595; Thu, 05 Dec 2019 06:30:58 -0800 (PST) MIME-Version: 1.0 References: <20191122112621.204798-1-glider@google.com> <20191122112621.204798-16-glider@google.com> In-Reply-To: From: Alexander Potapenko Date: Thu, 5 Dec 2019 15:30:47 +0100 Message-ID: Subject: Re: [PATCH RFC v3 15/36] kmsan: add tests for KMSAN To: Andrey Konovalov Cc: Vegard Nossum , Dmitry Vyukov , Linux Memory Management List , Alexander Viro , Andreas Dilger , Andrew Morton , Andrey Ryabinin , Andy Lutomirski , Ard Biesheuvel , Arnd Bergmann , Christoph Hellwig , Christoph Hellwig , "Darrick J. Wong" , "David S. Miller" , Dmitry Torokhov , Eric Biggers , Eric Dumazet , Eric Van Hensbergen , Greg Kroah-Hartman , Harry Wentland , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jason Wang , Jens Axboe , Marek Szyprowski , Marco Elver , Mark Rutland , "Martin K. Petersen" , Martin Schwidefsky , Matthew Wilcox , "Michael S . Tsirkin" , Michal Simek , Petr Mladek , Qian Cai , Randy Dunlap , Robin Murphy , Sergey Senozhatsky , Steven Rostedt , Takashi Iwai , "Theodore Ts'o" , Thomas Gleixner , Vasily Gorbik , Wolfram Sang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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: On Fri, Nov 29, 2019 at 3:15 PM Andrey Konovalov wr= ote: > > On Fri, Nov 22, 2019 at 12:27 PM wrote: > > > > 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 > > --- > > v2: > > - added printk_test() > > > > Change-Id: I287e86ae83a82b770f2baa46e5bbdce1dfa65195 > > --- > > lib/Makefile | 1 + > > lib/test_kmsan.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++ > > 2 files changed, 232 insertions(+) > > create mode 100644 lib/test_kmsan.c > > > > diff --git a/lib/Makefile b/lib/Makefile > > index 58a3e1b1a868..08fcb37499a0 100644 > > --- a/lib/Makefile > > +++ b/lib/Makefile > > @@ -65,6 +65,7 @@ CFLAGS_test_kasan.o +=3D $(call cc-disable-warning, v= la) > > obj-$(CONFIG_TEST_UBSAN) +=3D test_ubsan.o > > CFLAGS_test_ubsan.o +=3D $(call cc-disable-warning, vla) > > UBSAN_SANITIZE_test_ubsan.o :=3D y > > +obj-$(CONFIG_TEST_KMSAN) +=3D test_kmsan.o > > obj-$(CONFIG_TEST_KSTRTOX) +=3D test-kstrtox.o > > obj-$(CONFIG_TEST_LIST_SORT) +=3D test_list_sort.o > > obj-$(CONFIG_TEST_LKM) +=3D test_module.o > > diff --git a/lib/test_kmsan.c b/lib/test_kmsan.c > > new file mode 100644 > > index 000000000000..dcbe02adbdb0 > > --- /dev/null > > +++ b/lib/test_kmsan.c > > @@ -0,0 +1,231 @@ > > +// 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 modif= y > > + * 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 optim= izations > > + * 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); > > +} > > This function seems to be unused. Removed it, thanks! > > + > > +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 =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D 0; > > + > > + one_param_fn(init); > > + CHECK(arg1); > > + CHECK(arg2); > > +} > > + > > +void params_test(void) > > +{ > > + int uninit, init =3D 1; > > + > > + two_param_fn(uninit, init); > > +} > > This one as well. Added params_test to kmsan_tests_init() below. > > + > > +noinline void do_uninit_local_array(char *array, int start, int stop) > > +{ > > + int i; > > + volatile char uninit; > > + > > + for (i =3D start; i < stop; i++) > > + array[i] =3D 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 repor= t)\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 =3D 2; > > + struct page *pages[npages]; > > + void *vbuf; > > + int i; > > + > > + pr_info("-----------------------------\n"); > > + pr_info("pages initialized via vmap (no reports)\n"); > > + > > + for (i =3D 0; i < npages; i++) > > + pages[i] =3D alloc_page(GFP_KERNEL); > > + vbuf =3D vmap(pages, npages, VM_MAP, PAGE_KERNEL); > > + memset(vbuf, 0xfe, npages * PAGE_SIZE); > > + for (i =3D 0; i < npages; i++) > > + kmsan_check_memory(page_address(pages[i]), PAGE_SIZE); > > + > > + if (vbuf) > > + vunmap(vbuf); > > + for (i =3D 0; i < npages; i++) > > + if (pages[i]) > > + __free_page(pages[i]); > > +} > > + > > +noinline void init_vmalloc(void) > > +{ > > + char *buf; > > + int npages =3D 8, i; > > + > > + pr_info("-----------------------------\n"); > > + pr_info("pages initialized via vmap (no reports)\n"); > > + buf =3D vmalloc(PAGE_SIZE * npages); > > + buf[0] =3D 1; > > + memset(buf, 0xfe, PAGE_SIZE * npages); > > + CHECK(buf[0]); > > + for (i =3D 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 =3D kmalloc(80, GFP_KERNEL); > > + var[3] =3D 0xfeedface; > > + kfree((int *)var); > > + CHECK(var[3]); > > +} > > + > > +noinline void printk_test(void) > > +{ > > + volatile int uninit; > > + > > + pr_info("-----------------------------\n"); > > + pr_info("uninit local passed to pr_info() (UMR report)\n"); > > + pr_info("%px contains %d\n", &uninit, uninit); > > +} > > + > > +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(); > > Perhaps s/init_vmalloc/init_vmalloc_test? Done > > > + uaf_test(); > > + printk_test(); > > + return -EAGAIN; > > +} > > + > > +module_init(kmsan_tests_init); > > +MODULE_LICENSE("GPL"); > > -- > > 2.24.0.432.g9d3f5f5b63-goog > > -- Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg