Skip to content

Commit

Permalink
Add landlock06 test
Browse files Browse the repository at this point in the history
This test verifies LANDLOCK_ACCESS_FS_IOCTL_DEV access in the
landlock sandbox by creating a pipe and testing that ioctl() can
be executed on it. The test is also verifying that some of the I/O
operations can be always executed no matter the sandbox rules.
This feature is available since kernel 6.10.

Reviewed-by: Li Wang <liwang@redhat.com>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
  • Loading branch information
acerv authored and pevik committed Jul 12, 2024
1 parent 7fbd096 commit a35485e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions runtest/syscalls
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ landlock02 landlock02
landlock03 landlock03
landlock04 landlock04
landlock05 landlock05
landlock06 landlock06

lchown01 lchown01
lchown01_16 lchown01_16
Expand Down
1 change: 1 addition & 0 deletions testcases/kernel/syscalls/landlock/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ landlock02
landlock03
landlock04
landlock05
landlock06
112 changes: 112 additions & 0 deletions testcases/kernel/syscalls/landlock/landlock06.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
*/

/*\
* [Description]
*
* This test verifies LANDLOCK_ACCESS_FS_IOCTL_DEV access in the
* landlock sandbox by creating a pipe and testing that ioctl() can be executed
* on it. The test is also verifying that some of the I/O operations can be
* always executed no matter the sandbox rules.
*/

#include "landlock_common.h"
#include <sys/ioctl.h>

#define MNTPOINT "sandbox"
#define FILENAME MNTPOINT"/fifo"

static struct landlock_ruleset_attr *ruleset_attr;
static struct landlock_path_beneath_attr *path_beneath_attr;
static int file_fd;
static int dev_fd;

static void run(void)
{
if (SAFE_FORK())
return;

int flag;
size_t sz = 0;

TST_EXP_PASS(ioctl(file_fd, FIONREAD, &sz));

/* check unrestrictable commands */
TST_EXP_PASS(ioctl(dev_fd, FIOCLEX));
TST_EXP_PASS(ioctl(dev_fd, FIONCLEX));
TST_EXP_PASS(ioctl(dev_fd, FIONBIO, &flag));
TST_EXP_PASS(ioctl(dev_fd, FIOASYNC, &flag));

_exit(0);
}

static void setup(void)
{
int ruleset_fd;

verify_landlock_is_enabled();

SAFE_MKFIFO(FILENAME, 0640);

file_fd = SAFE_OPEN(FILENAME, O_RDONLY | O_NONBLOCK, 0640);
dev_fd = SAFE_OPEN("/dev/zero", O_RDONLY | O_NONBLOCK, 0640);

tst_res(TINFO, "Applying LANDLOCK_ACCESS_FS_IOCTL_DEV");

ruleset_attr->handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV;

ruleset_fd = SAFE_LANDLOCK_CREATE_RULESET(
ruleset_attr, sizeof(struct landlock_ruleset_attr), 0);

apply_landlock_layer(
ruleset_attr,
path_beneath_attr,
MNTPOINT,
LANDLOCK_ACCESS_FS_IOCTL_DEV
);

SAFE_CLOSE(ruleset_fd);
}

static void cleanup(void)
{
if (dev_fd != -1)
SAFE_CLOSE(dev_fd);

if (file_fd != -1)
SAFE_CLOSE(file_fd);
}

static struct tst_test test = {
.test_all = run,
.setup = setup,
.cleanup = cleanup,
.min_kver = "6.10",
.needs_tmpdir = 1,
.needs_root = 1,
.forks_child = 1,
.needs_kconfigs = (const char *[]) {
"CONFIG_SECURITY_LANDLOCK=y",
NULL
},
.bufs = (struct tst_buffers []) {
{&ruleset_attr, .size = sizeof(struct landlock_ruleset_attr)},
{&path_beneath_attr, .size = sizeof(struct landlock_path_beneath_attr)},
{},
},
.caps = (struct tst_cap []) {
TST_CAP(TST_CAP_REQ, CAP_SYS_ADMIN),
{}
},
.format_device = 1,
.mount_device = 1,
.mntpoint = MNTPOINT,
.all_filesystems = 1,
.skip_filesystems = (const char *[]) {
"vfat",
"exfat",
NULL
},
};

0 comments on commit a35485e

Please sign in to comment.