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.6 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 1518BC35E15 for ; Thu, 27 Feb 2020 19:35:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C13A32468E for ; Thu, 27 Feb 2020 19:35:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="fhU4ZWts" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C13A32468E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B8E2D6B0006; Thu, 27 Feb 2020 14:35:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC8556B0007; Thu, 27 Feb 2020 14:35:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B5B46B0008; Thu, 27 Feb 2020 14:35:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0235.hostedemail.com [216.40.44.235]) by kanga.kvack.org (Postfix) with ESMTP id 81F5B6B0006 for ; Thu, 27 Feb 2020 14:35:24 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 6E052B9E9 for ; Thu, 27 Feb 2020 19:35:24 +0000 (UTC) X-FDA: 76536910968.28.music86_12089389c9b0f X-HE-Tag: music86_12089389c9b0f X-Filterd-Recvd-Size: 8352 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Thu, 27 Feb 2020 19:35:23 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id t14so199143plr.8 for ; Thu, 27 Feb 2020 11:35:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TZN5E5934TvMDmRyA6YbiUZA3oVW+HYGx9jzlGs1fas=; b=fhU4ZWtsMwSX2mupe2qPd38EZqvMNduMdASxFGXOeV4vpARzuW/YSPVaTMFGN08Hhc XliIaYkwUiNY8jqwNeYkG4jQoqhhnqvlU2l4wKiF6qSNMzXt1xWnxElExOLM5ZL41m7y Z7z1AWZH5EHP1O9W7xlNQcsJwhj4eFUdEyy5Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TZN5E5934TvMDmRyA6YbiUZA3oVW+HYGx9jzlGs1fas=; b=ToSmDGoB7H6YFzXto2bzYloX8r0UNr45M7QvUkCbxk+71NsxYYFI8ONKtc9tyKTwrP hFS0xz9nnKUMTN7NEwz7uLZ/SLcLCz/AHSj/rGAt8H4PmLccjdu6a9aSZEMPUyxbz4Rb HM5yE8IaIr3Irq+C1q3hjmq8Se5Mm/tgDCq+cdSf9dB9FXogiBzJnsyrV+/Fl6D413oA tlQdWibr5tkstxp2GV9X131PWGNRTd976jL4k77BAoRufkPi/PsBhcpU9rqW1ipCVJfl +If6wOd5zteThlWaGQxXRjmWGyTA+nXOqisU9ua9R/3+c4yCnP98ad/LEwBX85oHlkon vR4A== X-Gm-Message-State: APjAAAVBuOUXHWtnNZemSw1J+j/A1ngB2nOH7DLg1rI8lQGQa3xRjM9j tn6wvDRZO6gJ116odopP4OqRXA== X-Google-Smtp-Source: APXvYqy8bVQH818HJGFZc2ql+d1GQcz5CnyZ/Lu/Mb9ddeqTtnjcaB2N07vWUnQwqSXTy5cLxuMyQA== X-Received: by 2002:a17:902:7048:: with SMTP id h8mr383251plt.64.1582832122570; Thu, 27 Feb 2020 11:35:22 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x7sm4244205pgp.0.2020.02.27.11.35.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2020 11:35:20 -0800 (PST) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Elena Petrova , Andrey Ryabinin , Dmitry Vyukov , Andrey Konovalov , Alexander Potapenko , Dan Carpenter , "Gustavo A. R. Silva" , Arnd Bergmann , Ard Biesheuvel , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, syzkaller@googlegroups.com Subject: [PATCH v5 2/6] ubsan: Split "bounds" checker from other options Date: Thu, 27 Feb 2020 11:35:12 -0800 Message-Id: <20200227193516.32566-3-keescook@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200227193516.32566-1-keescook@chromium.org> References: <20200227193516.32566-1-keescook@chromium.org> MIME-Version: 1.0 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: In order to do kernel builds with the bounds checker individually available, introduce CONFIG_UBSAN_BOUNDS, with the remaining options under CONFIG_UBSAN_MISC. For example, using this, we can start to expand the coverage syzkaller is providing. Right now, all of UBSan is disabled for syzbot builds because taken as a whole, it is too noisy. This will let us focus on one feature at a time. For the bounds checker specifically, this provides a mechanism to eliminate an entire class of array overflows with close to zero performance overhead (I cannot measure a difference). In my (mostly) defconfig, enabling bounds checking adds ~4200 checks to the kernel. Performance changes are in the noise, likely due to the branch predictors optimizing for the non-fail path. Some notes on the bounds checker: - it does not instrument {mem,str}*()-family functions, it only instruments direct indexed accesses (e.g. "foo[i]"). Dealing with the {mem,str}*()-family functions is a work-in-progress around CONFIG_FORTIFY_SOURCE[1]. - it ignores flexible array members, including the very old single byte (e.g. "int foo[1];") declarations. (Note that GCC's implementation appears to ignore _all_ trailing arrays, but Clang only ignores empty, 0, and 1 byte arrays[2].) [1] https://github.com/KSPP/linux/issues/6 [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D92589 Suggested-by: Elena Petrova Signed-off-by: Kees Cook Reviewed-by: Andrey Ryabinin Acked-by: Dmitry Vyukov --- lib/Kconfig.ubsan | 29 ++++++++++++++++++++++++----- scripts/Makefile.ubsan | 7 ++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan index 9deb655838b0..48469c95d78e 100644 --- a/lib/Kconfig.ubsan +++ b/lib/Kconfig.ubsan @@ -2,7 +2,7 @@ config ARCH_HAS_UBSAN_SANITIZE_ALL bool =20 -config UBSAN +menuconfig UBSAN bool "Undefined behaviour sanity checker" help This option enables the Undefined Behaviour sanity checker. @@ -10,9 +10,10 @@ config UBSAN behaviours at runtime. For more details, see: Documentation/dev-tools/ubsan.rst =20 +if UBSAN + config UBSAN_TRAP bool "On Sanitizer warnings, abort the running kernel code" - depends on UBSAN depends on $(cc-option, -fsanitize-undefined-trap-on-error) help Building kernels with Sanitizer features enabled tends to grow @@ -25,9 +26,26 @@ config UBSAN_TRAP the system. For some system builders this is an acceptable trade-off. =20 +config UBSAN_BOUNDS + bool "Perform array index bounds checking" + default UBSAN + help + This option enables detection of directly indexed out of bounds + array accesses, where the array size is known at compile time. + Note that this does not protect array overflows via bad calls + to the {str,mem}*cpy() family of functions (that is addressed + by CONFIG_FORTIFY_SOURCE). + +config UBSAN_MISC + bool "Enable all other Undefined Behavior sanity checks" + default UBSAN + help + This option enables all sanity checks that don't have their + own Kconfig options. Disable this if you only want to have + individually selected checks. + config UBSAN_SANITIZE_ALL bool "Enable instrumentation for the entire kernel" - depends on UBSAN depends on ARCH_HAS_UBSAN_SANITIZE_ALL =20 # We build with -Wno-maybe-uninitilzed, but we still want to @@ -44,7 +62,6 @@ config UBSAN_SANITIZE_ALL =20 config UBSAN_NO_ALIGNMENT bool "Disable checking of pointers alignment" - depends on UBSAN default y if HAVE_EFFICIENT_UNALIGNED_ACCESS help This option disables the check of unaligned memory accesses. @@ -57,7 +74,9 @@ config UBSAN_ALIGNMENT =20 config TEST_UBSAN tristate "Module for testing for undefined behavior detection" - depends on m && UBSAN + depends on m help This is a test module for UBSAN. It triggers various undefined behavior, and detect it. + +endif # if UBSAN diff --git a/scripts/Makefile.ubsan b/scripts/Makefile.ubsan index 668a91510bfe..5b15bc425ec9 100644 --- a/scripts/Makefile.ubsan +++ b/scripts/Makefile.ubsan @@ -5,14 +5,19 @@ ifdef CONFIG_UBSAN_ALIGNMENT CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dalignment) endif =20 +ifdef CONFIG_UBSAN_BOUNDS + CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dbounds) +endif + +ifdef CONFIG_UBSAN_MISC CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dshift) CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dinteger-divide-by= -zero) CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dunreachable) CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dsigned-integer-ov= erflow) - CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dbounds) CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dobject-size) CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Dbool) CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize=3Denum) +endif =20 ifdef CONFIG_UBSAN_TRAP CFLAGS_UBSAN +=3D $(call cc-option, -fsanitize-undefined-trap-on-e= rror) --=20 2.20.1