Unverified Commit dd6d32af authored by Günther Noack's avatar Günther Noack Committed by Mickaël Salaün
Browse files

selftests/landlock: Test IOCTL with memfds



Because the LANDLOCK_ACCESS_FS_IOCTL_DEV right is associated with the
opened file during open(2), IOCTLs are supposed to work with files
which are opened by means other than open(2).

Signed-off-by: default avatarGünther Noack <gnoack@google.com>
Link: https://lore.kernel.org/r/20240419161122.2023765-4-gnoack@google.com


Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
parent 3ecf19e5
Loading
Loading
Loading
Loading
+36 −8
Original line number Diff line number Diff line
@@ -3849,20 +3849,48 @@ TEST_F_FORK(ftruncate, open_and_ftruncate_in_different_processes)
	ASSERT_EQ(0, close(socket_fds[1]));
}

TEST(memfd_ftruncate)
/* Invokes the FS_IOC_GETFLAGS IOCTL and returns its errno or 0. */
static int test_fs_ioc_getflags_ioctl(int fd)
{
	int fd;
	uint32_t flags;

	if (ioctl(fd, FS_IOC_GETFLAGS, &flags) < 0)
		return errno;
	return 0;
}

TEST(memfd_ftruncate_and_ioctl)
{
	const struct landlock_ruleset_attr attr = {
		.handled_access_fs = ACCESS_ALL,
	};
	int ruleset_fd, fd, i;

	/*
	 * We exercise the same test both with and without Landlock enabled, to
	 * ensure that it behaves the same in both cases.
	 */
	for (i = 0; i < 2; i++) {
		/* Creates a new memfd. */
		fd = memfd_create("name", MFD_CLOEXEC);
		ASSERT_LE(0, fd);

		/*
	 * Checks that ftruncate is permitted on file descriptors that are
		 * Checks that operations associated with the opened file
		 * (ftruncate, ioctl) are permitted on file descriptors that are
		 * created in ways other than open(2).
		 */
		EXPECT_EQ(0, test_ftruncate(fd));
		EXPECT_EQ(0, test_fs_ioc_getflags_ioctl(fd));

		ASSERT_EQ(0, close(fd));

		/* Enables Landlock. */
		ruleset_fd = landlock_create_ruleset(&attr, sizeof(attr), 0);
		ASSERT_LE(0, ruleset_fd);
		enforce_ruleset(_metadata, ruleset_fd);
		ASSERT_EQ(0, close(ruleset_fd));
	}
}

static int test_fionread_ioctl(int fd)