Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1637695pxf; Fri, 19 Mar 2021 11:42:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJynPhcwtjice+S057XCbfswJcgJv1z4R4ruu+8wMHHIK1OmhqdBZ+OxmEY3CouzSHChqvEJ X-Received: by 2002:a17:906:5012:: with SMTP id s18mr5769579ejj.100.1616179359955; Fri, 19 Mar 2021 11:42:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616179359; cv=none; d=google.com; s=arc-20160816; b=fg/WK4fYqxZ1rgE1hJnHo98bpHLpGYvfv5XYJGrE3YerT8BrQkvG9QDgv15pVSB2qY Rvezy5xJKpBYncruoV85LfjKnTHMq9htIur3CS7Sm21H46Fg2r0e16v4rxPBX7omnoeG MmcJtcBFmbiX1qQ3UqUjAGRjvE1YAd9uJN7n+6OWbE7cs1sVIaTKayAikdX8fb0gp43P fzX+hC/tKJyKrGX94oRAPc/yw9LYVvBmwz8+NG1M30hOHi+8qZQdxBVpaHUj3ajpC4W1 jFTT1XDr7QdfYfbEm5/TgXccE7WJsmOA7ESW/dv6K3UQow4gxYxkyYa2OvjLUumwFXGd qnqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=qtUNtNo24kea4ktCcKCsasf3RWmZVGcchWHl2cH2dsI=; b=NebnL5CktJqcS7q5EMycX7DvKDg4a0TXMMNiMBoW3PSq7i4njvXAosVrOVSvaaL/du lpzJQmfbZejmNf2fBs+EOUoPMcdV/jkjt9uIdnsqVjHPYMxRLwRsJEFd/B86xa/1IQge a6TWQLdjcg/Pt06fVJVv7bOhqwUvU/AufD+MedOqR4YMchicd2Qi2wFOkppDJxRDWJUG JXPZxmcf0ZUigD3jzLD+nLJ/rxzIlcvIeXYVgaxlxaQEiR5qH4ALUmKyk8cigsERx3va DfE0RIoQkYy9EMvKv0A/6q77diDdx+kBpdWYbZqkjCYa1w+dIFy2PqVuJ1yF0nYlPOJt f1Yw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d3si4758485edq.276.2021.03.19.11.42.16; Fri, 19 Mar 2021 11:42:39 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230367AbhCSSlE (ORCPT + 99 others); Fri, 19 Mar 2021 14:41:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230228AbhCSSkx (ORCPT ); Fri, 19 Mar 2021 14:40:53 -0400 Received: from smtp-1908.mail.infomaniak.ch (smtp-1908.mail.infomaniak.ch [IPv6:2001:1600:4:17::1908]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3853C06174A for ; Fri, 19 Mar 2021 11:40:52 -0700 (PDT) Received: from smtp-2-0001.mail.infomaniak.ch (unknown [10.5.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4F2CQB6zD9zMq7Rl; Fri, 19 Mar 2021 19:40:50 +0100 (CET) Received: from ns3096276.ip-94-23-54.eu (unknown [23.97.221.149]) by smtp-2-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4F2CQ42r3Czlh8T4; Fri, 19 Mar 2021 19:40:44 +0100 (CET) Subject: Re: [PATCH v30 10/12] selftests/landlock: Add user space tests To: Kees Cook Cc: James Morris , Jann Horn , "Serge E . Hallyn" , Al Viro , Andrew Morton , Andy Lutomirski , Anton Ivanov , Arnd Bergmann , Casey Schaufler , David Howells , Jeff Dike , Jonathan Corbet , Michael Kerrisk , Richard Weinberger , Shuah Khan , Vincent Dagonneau , kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-security-module@vger.kernel.org, x86@kernel.org, =?UTF-8?Q?Micka=c3=abl_Sala=c3=bcn?= , Dmitry Vyukov References: <20210316204252.427806-1-mic@digikod.net> <20210316204252.427806-11-mic@digikod.net> <202103191026.D936362B@keescook> From: =?UTF-8?Q?Micka=c3=abl_Sala=c3=bcn?= Message-ID: Date: Fri, 19 Mar 2021 19:41:00 +0100 User-Agent: MIME-Version: 1.0 In-Reply-To: <202103191026.D936362B@keescook> Content-Type: text/plain; charset=iso-8859-15 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 19/03/2021 18:56, Kees Cook wrote: > On Tue, Mar 16, 2021 at 09:42:50PM +0100, Micka?l Sala?n wrote: >> From: Micka?l Sala?n >> >> Test all Landlock system calls, ptrace hooks semantic and filesystem >> access-control with multiple layouts. >> >> Test coverage for security/landlock/ is 93.6% of lines. The code not >> covered only deals with internal kernel errors (e.g. memory allocation) >> and race conditions. >> >> Cc: James Morris >> Cc: Jann Horn >> Cc: Kees Cook >> Cc: Serge E. Hallyn >> Cc: Shuah Khan >> Signed-off-by: Micka?l Sala?n >> Reviewed-by: Vincent Dagonneau >> Link: https://lore.kernel.org/r/20210316204252.427806-11-mic@digikod.net > > This is terrific. I love the coverage. How did you measure this, BTW? I used gcov: https://www.kernel.org/doc/html/latest/dev-tools/gcov.html > To increase it into memory allocation failures, have you tried > allocation fault injection: > https://www.kernel.org/doc/html/latest/fault-injection/fault-injection.html Yes, it is used by syzkaller, but I don't know how to extract this specific coverage. > >> [...] >> +TEST(inconsistent_attr) { >> + const long page_size = sysconf(_SC_PAGESIZE); >> + char *const buf = malloc(page_size + 1); >> + struct landlock_ruleset_attr *const ruleset_attr = (void *)buf; >> + >> + ASSERT_NE(NULL, buf); >> + >> + /* Checks copy_from_user(). */ >> + ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, 0, 0)); >> + /* The size if less than sizeof(struct landlock_attr_enforce). */ >> + ASSERT_EQ(EINVAL, errno); >> + ASSERT_EQ(-1, landlock_create_ruleset(ruleset_attr, 1, 0)); >> + ASSERT_EQ(EINVAL, errno); > > Almost everywhere you're using ASSERT instead of EXPECT. Is this correct > (in the sense than as soon as an ASSERT fails the rest of the test is > skipped)? I do see you using EXPECT is some places, but I figured I'd > ask about the intention here. I intentionally use ASSERT as much as possible, but I use EXPECT when an error could block a test or when it could stop a cleanup (i.e. teardown). > >> +/* >> + * TEST_F_FORK() is useful when a test drop privileges but the corresponding >> + * FIXTURE_TEARDOWN() requires them (e.g. to remove files from a directory >> + * where write actions are denied). For convenience, FIXTURE_TEARDOWN() is >> + * also called when the test failed, but not when FIXTURE_SETUP() failed. For >> + * this to be possible, we must not call abort() but instead exit smoothly >> + * (hence the step print). >> + */ > > Hm, interesting. I think this should be extracted into a separate patch > and added to the test harness proper. I agree, but it may require some modifications to fit nicely in kselftest_harness.h . For now, it works well for my use case. I'll send patches once Landlock is merged. In fact, I already made kselftest_harness.h available for other users than seccomp. ;) > > Could this be solved with TEARDOWN being called on SETUP failure? The goal of this helper is to still be able to call TEARDOWN when TEST failed, not SETUP. > >> +#define TEST_F_FORK(fixture_name, test_name) \ >> + static void fixture_name##_##test_name##_child( \ >> + struct __test_metadata *_metadata, \ >> + FIXTURE_DATA(fixture_name) *self, \ >> + const FIXTURE_VARIANT(fixture_name) *variant); \ >> + TEST_F(fixture_name, test_name) \ >> + { \ >> + int status; \ >> + const pid_t child = fork(); \ >> + if (child < 0) \ >> + abort(); \ >> + if (child == 0) { \ >> + _metadata->no_print = 1; \ >> + fixture_name##_##test_name##_child(_metadata, self, variant); \ >> + if (_metadata->skip) \ >> + _exit(255); \ >> + if (_metadata->passed) \ >> + _exit(0); \ >> + _exit(_metadata->step); \ >> + } \ >> + if (child != waitpid(child, &status, 0)) \ >> + abort(); \ >> + if (WIFSIGNALED(status) || !WIFEXITED(status)) { \ >> + _metadata->passed = 0; \ >> + _metadata->step = 1; \ >> + return; \ >> + } \ >> + switch (WEXITSTATUS(status)) { \ >> + case 0: \ >> + _metadata->passed = 1; \ >> + break; \ >> + case 255: \ >> + _metadata->passed = 1; \ >> + _metadata->skip = 1; \ >> + break; \ >> + default: \ >> + _metadata->passed = 0; \ >> + _metadata->step = WEXITSTATUS(status); \ >> + break; \ >> + } \ >> + } \ > > This looks like a subset of __wait_for_test()? Could __TEST_F_IMPL() be > updated instead to do this? (Though the fork overhead might not be great > for everyone.) Yes, it will probably be my approach to update kselftest_harness.h .