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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8B54C433F5 for ; Thu, 24 Feb 2022 06:13:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BBF18D0002; Thu, 24 Feb 2022 01:13:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 56BB58D0001; Thu, 24 Feb 2022 01:13:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45AC98D0002; Thu, 24 Feb 2022 01:13:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id 205638D0001 for ; Thu, 24 Feb 2022 01:13:29 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id A650D181D903F for ; Thu, 24 Feb 2022 06:13:28 +0000 (UTC) X-FDA: 79176656496.20.768DCE2 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 09B3E40003 for ; Thu, 24 Feb 2022 06:13:27 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id m11so840662pls.5 for ; Wed, 23 Feb 2022 22:13:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=Asma8WNQQUT3FBNGCvGXdievYO+L9dWKrHZUkqpLwYo=; b=YP0hTw4BhQTutsHE3lgb2hwfeMrAAi2vjHqX4ZmQJDQvgozj5g8q/QqqcI09WdFnPd sD3El4ySr3YOjFuLIb49Bd1UUSdf60EoRhU0SNdE7vvX3rGUTe17TFup32D3dKf11GqR BCjntqEmhiz32jfO4BE5StMUcD6q3Zr8fLQE0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=Asma8WNQQUT3FBNGCvGXdievYO+L9dWKrHZUkqpLwYo=; b=LRGSRxIoOaoXBjp2rGInLcaYysXpvWujjrY6HFSGVlEW3CDrmgMAhrXZRY63YUu8Tr bRaNFV6KJIUS7WUrgY0lR4el48rkEbd0M2T3q7VJv7Hj+JSXGVO29ptkQSKynd6EQr0F pndcq1djHxK5JwwjPfUGrcyPGuD50ZiU6Fux1ncrdpgTldG3M3p0gck/RJS8bHEKCS7p rGgyhyevFKh0tC4GXA0Vy2NXMrRf4/kzvUTC4qVEnRmcEwja2WI6+R0O4h0iPo/HN3nq uMmSWAXAeY8TfGKQjSvsdOuTGZ5toBQ9i63WZ1vhjvAh6sq2OYEIP2UDHhZ3S05NFyJt SbpQ== X-Gm-Message-State: AOAM532TfybbhgZ/TdVmcEE7ZW/fYAp+rpfYY9vBRJAaKLihIU0j6zTo A/wTnEGOaYdZvETsh8tOeN4ccdvPdXUsnA== X-Google-Smtp-Source: ABdhPJyRPCtuqkCcj9377exCaz5pyA9MMqZh33gOYt1MVstAjPz3jqClc+zk9NooYp5PwP7tqlaPNA== X-Received: by 2002:a17:90a:a78d:b0:1bc:d11c:ad40 with SMTP id f13-20020a17090aa78d00b001bcd11cad40mr24287pjq.246.1645683206850; Wed, 23 Feb 2022 22:13:26 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 142sm1830697pfy.11.2022.02.23.22.13.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 22:13:26 -0800 (PST) Date: Wed, 23 Feb 2022 22:13:25 -0800 From: Kees Cook To: Daniel Latypov , Steven Rostedt Cc: Eric Biederman , David Gow , Alexey Dobriyan , Magnus =?iso-8859-1?Q?Gro=DF?= , kunit-dev@googlegroups.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] binfmt_elf: Introduce KUnit test Message-ID: <202202232208.B416701@keescook> References: <20220224054332.1852813-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 09B3E40003 X-Stat-Signature: k6pyrgu8a3cynfa5s34bru4rfdufw5ot Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=YP0hTw4B; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf04.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.172 as permitted sender) smtp.mailfrom=keescook@chromium.org X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1645683207-303127 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 Wed, Feb 23, 2022 at 10:07:04PM -0800, Daniel Latypov wrote: > On Wed, Feb 23, 2022 at 9:43 PM Kees Cook wrote= : > > > > Adds simple KUnit test for some binfmt_elf internals: specifically a > > regression test for the problem fixed by commit 8904d9cd90ee ("ELF: > > fix overflow in total mapping size calculation"). > > > > Cc: Eric Biederman > > Cc: David Gow > > Cc: Alexey Dobriyan > > Cc: "Magnus Gro=DF" > > Cc: kunit-dev@googlegroups.com > > Cc: linux-fsdevel@vger.kernel.org > > Signed-off-by: Kees Cook > > --- > > I'm exploring ways to mock copy_to_user() for more tests in here. > > kprobes doesn't seem to let me easily hijack a function... >=20 > Yeah, there doesn't seem to be a good way to do so. It seems more > feasible if one is willing to write arch-specific code, but I'm not > quite sure if that works either. Yeah, I'm hoping maybe Steven has some ideas. Steven, I want to do fancy live-patch kind or things to replace functions= , but it doesn't need to be particularly fancy because KUnit tests (usually= ) run single-threaded, etc. It looks like kprobes could almost do it, but I don't see a way to have it _avoid_ making a function call. > https://kunit.dev/mocking.html has some thoughts on this. > Not sure if there's anything there that would be useful to you, but > perhaps it can give you some ideas. Yeah, I figure a small refactoring to use a passed task_struct can avoid the "current" uses in load_elf_binary(), etc, but the copy_to_user() is more of a problem. I have considered inverting the Makefile logic, though, and having binfmt_elf_test.c include binfmt_elf.c and have it just use a #define to redirect copy_to_user, kind of how all the compat handling is already done. But it'd be nice to have a "cleaner" mocking solution... >=20 > > --- > > fs/Kconfig.binfmt | 17 +++++++++++ > > fs/binfmt_elf.c | 4 +++ > > fs/binfmt_elf_test.c | 64 ++++++++++++++++++++++++++++++++++++++++= ++ > > fs/compat_binfmt_elf.c | 2 ++ > > 4 files changed, 87 insertions(+) > > create mode 100644 fs/binfmt_elf_test.c > > > > diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt > > index 4d5ae61580aa..8e14589ee9cc 100644 > > --- a/fs/Kconfig.binfmt > > +++ b/fs/Kconfig.binfmt > > @@ -28,6 +28,23 @@ config BINFMT_ELF > > ld.so (check the file for loca= tion and > > latest version). > > > > +config BINFMT_ELF_KUNIT_TEST > > + bool "Build KUnit tests for ELF binary support" if !KUNIT_ALL= _TESTS > > + depends on KUNIT=3Dy && BINFMT_ELF=3Dy > > + default KUNIT_ALL_TESTS > > + help > > + This builds the ELF loader KUnit tests. > > + > > + KUnit tests run during boot and output the results to the d= ebug log > > + in TAP format (https://testanything.org/). Only useful for = kernel devs >=20 > Tangent: should we update the kunit style guide to not refer to TAP > anymore as it's not accurate? > The KTAP spec is live on kernel.org at > https://www.kernel.org/doc/html/latest/dev-tools/ktap.html >=20 > We can leave this patch as-is and update later, or have it be the > guinea pig for the new proposed wording. Oops, good point. I was actually thinking it doesn't make too much sense to keep repeating the same long boilerplate generally. > (I'm personally in favor of people not copy-pasting these paragraphs > in the first place, but that is what the style-guide currently > recommends) Let's change the guide? :) >=20 > > + running KUnit test harness and are not for inclusion into a > > + production build. > > + > > + For more information on KUnit and unit tests in general ple= ase refer > > + to the KUnit documentation in Documentation/dev-tools/kunit= /. > > + > > + If unsure, say N. > > + > > config COMPAT_BINFMT_ELF > > def_bool y > > depends on COMPAT && BINFMT_ELF > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > > index 76ff2af15ba5..9bea703ed1c2 100644 > > --- a/fs/binfmt_elf.c > > +++ b/fs/binfmt_elf.c > > @@ -2335,3 +2335,7 @@ static void __exit exit_elf_binfmt(void) > > core_initcall(init_elf_binfmt); > > module_exit(exit_elf_binfmt); > > MODULE_LICENSE("GPL"); > > + > > +#ifdef CONFIG_BINFMT_ELF_KUNIT_TEST > > +#include "binfmt_elf_test.c" > > +#endif > > diff --git a/fs/binfmt_elf_test.c b/fs/binfmt_elf_test.c > > new file mode 100644 > > index 000000000000..486ad419f763 > > --- /dev/null > > +++ b/fs/binfmt_elf_test.c > > @@ -0,0 +1,64 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +#include > > + > > +static void total_mapping_size_test(struct kunit *test) > > +{ > > + struct elf_phdr empty[] =3D { > > + { .p_type =3D PT_LOAD, .p_vaddr =3D 0, .p_memsz =3D 0= , }, > > + { .p_type =3D PT_INTERP, .p_vaddr =3D 10, .p_memsz =3D= 999999, }, > > + }; > > + /* > > + * readelf -lW /bin/mount | grep '^ .*0x0' | awk '{print "\t= \t{ .p_type =3D PT_" \ > > + * $1 ", .p_vaddr =3D " $3 ", .p= _memsz =3D " $6 ", },"}' > > + */ > > + struct elf_phdr mount[] =3D { > > + { .p_type =3D PT_PHDR, .p_vaddr =3D 0x00000040, .p_me= msz =3D 0x0002d8, }, > > + { .p_type =3D PT_INTERP, .p_vaddr =3D 0x00000318, .p_= memsz =3D 0x00001c, }, > > + { .p_type =3D PT_LOAD, .p_vaddr =3D 0x00000000, .p_me= msz =3D 0x0033a8, }, > > + { .p_type =3D PT_LOAD, .p_vaddr =3D 0x00004000, .p_me= msz =3D 0x005c91, }, > > + { .p_type =3D PT_LOAD, .p_vaddr =3D 0x0000a000, .p_me= msz =3D 0x0022f8, }, > > + { .p_type =3D PT_LOAD, .p_vaddr =3D 0x0000d330, .p_me= msz =3D 0x000d40, }, > > + { .p_type =3D PT_DYNAMIC, .p_vaddr =3D 0x0000d928, .p= _memsz =3D 0x000200, }, > > + { .p_type =3D PT_NOTE, .p_vaddr =3D 0x00000338, .p_me= msz =3D 0x000030, }, > > + { .p_type =3D PT_NOTE, .p_vaddr =3D 0x00000368, .p_me= msz =3D 0x000044, }, > > + { .p_type =3D PT_GNU_PROPERTY, .p_vaddr =3D 0x0000033= 8, .p_memsz =3D 0x000030, }, > > + { .p_type =3D PT_GNU_EH_FRAME, .p_vaddr =3D 0x0000b49= 0, .p_memsz =3D 0x0001ec, }, > > + { .p_type =3D PT_GNU_STACK, .p_vaddr =3D 0x00000000, = .p_memsz =3D 0x000000, }, > > + { .p_type =3D PT_GNU_RELRO, .p_vaddr =3D 0x0000d330, = .p_memsz =3D 0x000cd0, }, > > + }; > > + size_t mount_size =3D 0xE070; > > + /* https://lore.kernel.org/lkml/YfF18Dy85mCntXrx@fractal.loca= ldomain */ >=20 > Slight nit, it looks like that message wasn't sent to lkml. > lore gives a suggestion to change to > https://lore.kernel.org/linux-fsdevel/YfF18Dy85mCntXrx@fractal.localdom= ain/ Ah, thank you. I was replacing the /r/ that used to be in that URL, and got lost. :) --=20 Kees Cook