All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Mickaël Salaün" <mic@digikod.net>
To: Kees Cook <keescook@chromium.org>
Cc: "James Morris" <jmorris@namei.org>,
	"Jann Horn" <jannh@google.com>,
	"Serge E . Hallyn" <serge@hallyn.com>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Anton Ivanov" <anton.ivanov@cambridgegreys.com>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Casey Schaufler" <casey@schaufler-ca.com>,
	"David Howells" <dhowells@redhat.com>,
	"Jeff Dike" <jdike@addtoit.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Michael Kerrisk" <mtk.manpages@gmail.com>,
	"Richard Weinberger" <richard@nod.at>,
	"Shuah Khan" <shuah@kernel.org>,
	"Vincent Dagonneau" <vincent.dagonneau@ssi.gouv.fr>,
	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,
	"Mickaël Salaün" <mic@linux.microsoft.com>,
	"Dmitry Vyukov" <dvyukov@google.com>
Subject: Re: [PATCH v30 10/12] selftests/landlock: Add user space tests
Date: Fri, 19 Mar 2021 19:41:00 +0100	[thread overview]
Message-ID: <e98a1f48-4c35-139d-af88-b6e65fbb5c3f@digikod.net> (raw)
In-Reply-To: <202103191026.D936362B@keescook>


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 <mic@linux.microsoft.com>
>>
>> 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 <jmorris@namei.org>
>> Cc: Jann Horn <jannh@google.com>
>> Cc: Kees Cook <keescook@chromium.org>
>> Cc: Serge E. Hallyn <serge@hallyn.com>
>> Cc: Shuah Khan <shuah@kernel.org>
>> Signed-off-by: Mickaël Salaün <mic@linux.microsoft.com>
>> Reviewed-by: Vincent Dagonneau <vincent.dagonneau@ssi.gouv.fr>
>> 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 .

  reply	other threads:[~2021-03-19 18:41 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-16 20:42 [PATCH v30 00/12] Landlock LSM Mickaël Salaün
2021-03-16 20:42 ` [PATCH v30 01/12] landlock: Add object management Mickaël Salaün
2021-03-19 18:13   ` Kees Cook
2021-03-19 18:57     ` Mickaël Salaün
2021-03-16 20:42 ` [PATCH v30 02/12] landlock: Add ruleset and domain management Mickaël Salaün
2021-03-19 18:40   ` Kees Cook
2021-03-19 19:03     ` Mickaël Salaün
2021-03-19 19:15       ` Kees Cook
2021-03-24 20:31       ` James Morris
2021-03-25  9:29         ` Mickaël Salaün
2021-03-23  0:13   ` Jann Horn
2021-03-16 20:42 ` [PATCH v30 03/12] landlock: Set up the security framework and manage credentials Mickaël Salaün
2021-03-19 18:45   ` Kees Cook
2021-03-19 19:07     ` Mickaël Salaün
2021-03-16 20:42 ` [PATCH v30 04/12] landlock: Add ptrace restrictions Mickaël Salaün
2021-03-19 18:45   ` Kees Cook
2021-03-16 20:42 ` [PATCH v30 05/12] LSM: Infrastructure management of the superblock Mickaël Salaün
2021-03-19 17:24   ` Kees Cook
2021-03-16 20:42 ` [PATCH v30 06/12] fs,security: Add sb_delete hook Mickaël Salaün
2021-03-19 17:24   ` Kees Cook
2021-03-16 20:42 ` [PATCH v30 07/12] landlock: Support filesystem access-control Mickaël Salaün
2021-03-18 23:10   ` James Morris
2021-03-19 18:57   ` Kees Cook
2021-03-19 19:19     ` Mickaël Salaün
2021-03-23 19:30       ` Mickaël Salaün
2021-03-23  0:13   ` Jann Horn
2021-03-23 15:55     ` Mickaël Salaün
2021-03-23 17:49       ` Jann Horn
2021-03-23 19:22         ` Mickaël Salaün
2021-03-24  3:10           ` Jann Horn
2021-03-16 20:42 ` [PATCH v30 08/12] landlock: Add syscall implementations Mickaël Salaün
2021-03-19 19:06   ` Kees Cook
2021-03-19 21:53     ` Mickaël Salaün
2021-03-24 15:03       ` Mickaël Salaün
2021-03-16 20:42 ` [PATCH v30 09/12] arch: Wire up Landlock syscalls Mickaël Salaün
2021-03-16 20:42 ` [PATCH v30 10/12] selftests/landlock: Add user space tests Mickaël Salaün
2021-03-19 17:56   ` Kees Cook
2021-03-19 18:41     ` Mickaël Salaün [this message]
2021-03-19 19:11       ` Kees Cook
2021-03-19 21:57         ` Mickaël Salaün
2021-03-16 20:42 ` [PATCH v30 11/12] samples/landlock: Add a sandbox manager example Mickaël Salaün
2021-03-19 17:26   ` Kees Cook
2021-03-16 20:42 ` [PATCH v30 12/12] landlock: Add user and kernel documentation Mickaël Salaün
2021-03-19 18:03   ` Kees Cook
2021-03-19 18:54     ` Mickaël Salaün
2021-03-23 19:25       ` Mickaël Salaün
2021-03-24 16:21       ` Mickaël Salaün
2021-03-18 23:26 ` [PATCH v30 00/12] Landlock LSM James Morris
2021-03-18 23:26   ` James Morris
2021-03-19 15:52   ` Mickaël Salaün

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e98a1f48-4c35-139d-af88-b6e65fbb5c3f@digikod.net \
    --to=mic@digikod.net \
    --cc=akpm@linux-foundation.org \
    --cc=anton.ivanov@cambridgegreys.com \
    --cc=arnd@arndb.de \
    --cc=casey@schaufler-ca.com \
    --cc=corbet@lwn.net \
    --cc=dhowells@redhat.com \
    --cc=dvyukov@google.com \
    --cc=jannh@google.com \
    --cc=jdike@addtoit.com \
    --cc=jmorris@namei.org \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mic@linux.microsoft.com \
    --cc=mtk.manpages@gmail.com \
    --cc=richard@nod.at \
    --cc=serge@hallyn.com \
    --cc=shuah@kernel.org \
    --cc=vincent.dagonneau@ssi.gouv.fr \
    --cc=viro@zeniv.linux.org.uk \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.