From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FD0C374FB for ; Sat, 9 Mar 2024 07:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709970814; cv=none; b=BZ2dvPDU4sx8aJs9GuFg5vnhpT7ji59gcS5TkFGX0eGLjPfyIE/GxMFSHnZjR5TIhEpXyT5ZPFx2ZNIhMLhdVP3cidEMuZT3z/RtLdO49V0YTbeG/hhM4usDjPHfEI+SRgszPVW5ZVBmTwQf+52ldDDsuWkQ0Qv5Nlg7FP+fpfQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709970814; c=relaxed/simple; bh=ww/Lbi3b5oWhje3vJaXa/bgTB/LbTEF9sD9yQf69RCw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=V8XGBVrhKLVCSdYUQ5OWaTHvoDd0UkSyaZN/FefMzb7cp1XZVFGTnsN/f3EENsNf7yiUpCaHjNsIQa34ePf0Csa6JhZRbRhPSj0Sudnb9ESFLXJUNDs87F64tu9FHejP31obmvbSEWwvra2/2innPruqhRdAm8Tx2T8YBGj2iCI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--gnoack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3pm9GlBU; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--gnoack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3pm9GlBU" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbe9e13775aso4698598276.1 for ; Fri, 08 Mar 2024 23:53:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709970812; x=1710575612; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=pjz0CSqWQYsBSnNDAgYUyOwTU5DFGITSG4sCgpVq7yg=; b=3pm9GlBUQ1KCHjDCDJXKKnrgFitl85Akal9t1r+Epq6HttfAf7ibPxgJXeZHktawAd XkCswQ3GXACkm/ZmoMah+dVALa23d8F8IjBUBcRPreMHuK24rjAovcik4LwsmQ2X74Vc oMvb/S9U7zmv0BA3jywzWm4mOIq8wRtpGEwTsQeBp6GVpJcJCljVzg4kQ3myrk5wPwrM Dac3ds3iDaGwHTYwV9QWOplU4KebQieFiTvpyaAU/ipUjRB1NLU4tOCG2rEUp3xzGlRn vKh4ZtrdUTvmtmGm2Dqrp0Km1O/p93zn00apngR8/TBNRnNJcCjQ1Fh0R4ihKOPOz6WS G7bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709970812; x=1710575612; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=pjz0CSqWQYsBSnNDAgYUyOwTU5DFGITSG4sCgpVq7yg=; b=r2uAwTshZlk93gBYwpnwF0uOXt5Sp9sg7zJvkzkRSBPGOzPG57OMSwwUQbSGQ3E4u7 /GtikZL6knroKLwMphoGDFesNlYDgOkDt2RwCUQrTgn8f3w0IuGtcii6hRD9g6/30jFa nNpGlzjXXokjy+1W9dxmlJaJWOTetgHfsRnVz/Xiu6Guav4OoEXKfxmNMoB5e1N1NJaR U1qf6VFsJpMRpol0QqgxRpSHCqAZZLIYdZpKkKkEVpOa08RDlM48eRJlJvitMDb+iF3U WWV6UoFiJ2Mhr7oye92lPCYay+nLm9+KiJDt6ewzD+8rGFujwpa5wYIpPi8BG0BG24ST Ig0g== X-Gm-Message-State: AOJu0YyAaI0vDbZrf35x+49WMXRky+B6Zw57lZZuHSz/vrNqyKVP+FQv OkTs02XE6i5kFiF+EHohn4f0SSnQZ2YSfkCegmN8Kb3+CCqsKtU/6q/PUK6C+h1HhICFMYVRTLn W19SL+QO4jnwHghamxJy+9OXfXfQeoefWZAGpeVnsCA0zA8JH+csOk5ovClEX8YiIb3hOpCHn1s 9EMyEwmr9QBY+HecQnOg2Ng4mSc9unmMWNexHH1nx8tGQGX9o++nVQ X-Google-Smtp-Source: AGHT+IF/cXaUhgDCBIL4rLLvjR8a16XF+Hd8lFB/gPCt0C4vptQdgtrVoT799pakaFAnGKp5dydrvW1I980= X-Received: from swim.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:1605]) (user=gnoack job=sendgmr) by 2002:a25:c4c3:0:b0:dcd:ac69:eb1a with SMTP id u186-20020a25c4c3000000b00dcdac69eb1amr356402ybf.12.1709970812055; Fri, 08 Mar 2024 23:53:32 -0800 (PST) Date: Sat, 9 Mar 2024 07:53:14 +0000 In-Reply-To: <20240309075320.160128-1-gnoack@google.com> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309075320.160128-1-gnoack@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309075320.160128-4-gnoack@google.com> Subject: [PATCH v10 3/9] selftests/landlock: Test IOCTL support From: "=?UTF-8?q?G=C3=BCnther=20Noack?=" To: linux-security-module@vger.kernel.org, "=?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?=" Cc: Jeff Xu , Arnd Bergmann , Jorge Lucangeli Obes , Allen Webb , Dmitry Torokhov , Paul Moore , Konstantin Meskhidze , Matt Bobrowski , linux-fsdevel@vger.kernel.org, "=?UTF-8?q?G=C3=BCnther=20Noack?=" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Exercises Landlock's IOCTL feature in different combinations of handling and permitting the LANDLOCK_ACCESS_FS_IOCTL_DEV right, and in different combinations of using files and directories. Signed-off-by: G=C3=BCnther Noack --- tools/testing/selftests/landlock/fs_test.c | 236 ++++++++++++++++++++- 1 file changed, 233 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/sel= ftests/landlock/fs_test.c index 0bcbbf594fd7..91567e5db0fd 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -8,6 +8,7 @@ */ =20 #define _GNU_SOURCE +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +25,12 @@ #include #include =20 +/* + * Intentionally included last to work around header conflict. + * See https://sourceware.org/glibc/wiki/Synchronizing_Headers. + */ +#include + #include "common.h" =20 #ifndef renameat2 @@ -735,6 +743,9 @@ static int create_ruleset(struct __test_metadata *const= _metadata, } =20 for (i =3D 0; rules[i].path; i++) { + if (!rules[i].access) + continue; + add_path_beneath(_metadata, ruleset_fd, rules[i].access, rules[i].path); } @@ -3443,7 +3454,7 @@ TEST_F_FORK(layout1, truncate_unhandled) LANDLOCK_ACCESS_FS_WRITE_FILE; int ruleset_fd; =20 - /* Enable Landlock. */ + /* Enables Landlock. */ ruleset_fd =3D create_ruleset(_metadata, handled, rules); =20 ASSERT_LE(0, ruleset_fd); @@ -3526,7 +3537,7 @@ TEST_F_FORK(layout1, truncate) LANDLOCK_ACCESS_FS_TRUNCATE; int ruleset_fd; =20 - /* Enable Landlock. */ + /* Enables Landlock. */ ruleset_fd =3D create_ruleset(_metadata, handled, rules); =20 ASSERT_LE(0, ruleset_fd); @@ -3752,7 +3763,7 @@ TEST_F_FORK(ftruncate, open_and_ftruncate) }; int fd, ruleset_fd; =20 - /* Enable Landlock. */ + /* Enables Landlock. */ ruleset_fd =3D create_ruleset(_metadata, variant->handled, rules); ASSERT_LE(0, ruleset_fd); enforce_ruleset(_metadata, ruleset_fd); @@ -3829,6 +3840,16 @@ TEST_F_FORK(ftruncate, open_and_ftruncate_in_differe= nt_processes) ASSERT_EQ(0, close(socket_fds[1])); } =20 +/* Invokes the FS_IOC_GETFLAGS IOCTL and returns its errno or 0. */ +static int test_fs_ioc_getflags_ioctl(int fd) +{ + uint32_t flags; + + if (ioctl(fd, FS_IOC_GETFLAGS, &flags) < 0) + return errno; + return 0; +} + TEST(memfd_ftruncate) { int fd; @@ -3845,6 +3866,215 @@ TEST(memfd_ftruncate) ASSERT_EQ(0, close(fd)); } =20 +/* clang-format off */ +FIXTURE(ioctl) {}; + +FIXTURE_SETUP(ioctl) {}; + +FIXTURE_TEARDOWN(ioctl) {}; +/* clang-format on */ + +FIXTURE_VARIANT(ioctl) +{ + const __u64 handled; + const __u64 allowed; + const mode_t open_mode; + /* + * TCGETS is used as a characteristic device-specific IOCTL command. + * The logic is the same for other IOCTL commands as well. + */ + const int expected_tcgets_result; /* terminal device IOCTL */ + /* + * FIONREAD is implemented in fs/ioctl.c for regular files, + * but we do not blanket-permit it for devices. + */ + const int expected_fionread_result; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ioctl, handled_i_allowed_none) { + /* clang-format on */ + .handled =3D LANDLOCK_ACCESS_FS_IOCTL_DEV, + .allowed =3D 0, + .open_mode =3D O_RDWR, + .expected_tcgets_result =3D EACCES, + .expected_fionread_result =3D EACCES, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ioctl, handled_i_allowed_i) { + /* clang-format on */ + .handled =3D LANDLOCK_ACCESS_FS_IOCTL_DEV, + .allowed =3D LANDLOCK_ACCESS_FS_IOCTL_DEV, + .open_mode =3D O_RDWR, + .expected_tcgets_result =3D 0, + .expected_fionread_result =3D 0, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ioctl, unhandled) { + /* clang-format on */ + .handled =3D LANDLOCK_ACCESS_FS_EXECUTE, + .allowed =3D LANDLOCK_ACCESS_FS_EXECUTE, + .open_mode =3D O_RDWR, + .expected_tcgets_result =3D 0, + .expected_fionread_result =3D 0, +}; + +static int test_fioqsize_ioctl(int fd) +{ + size_t sz; + + if (ioctl(fd, FIOQSIZE, &sz) < 0) + return errno; + return 0; +} + +static int test_tcgets_ioctl(int fd) +{ + struct termios info; + + if (ioctl(fd, TCGETS, &info) < 0) + return errno; + return 0; +} + +static int test_fionread_ioctl(int fd) +{ + size_t sz =3D 0; + + if (ioctl(fd, FIONREAD, &sz) < 0 && errno =3D=3D EACCES) + return errno; + return 0; +} + +TEST_F_FORK(ioctl, handle_dir_access_file) +{ + const int flag =3D 0; + const struct rule rules[] =3D { + { + .path =3D "/dev", + .access =3D variant->allowed, + }, + {}, + }; + int file_fd, ruleset_fd; + + /* Enables Landlock. */ + ruleset_fd =3D create_ruleset(_metadata, variant->handled, rules); + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + file_fd =3D open("/dev/tty", variant->open_mode); + ASSERT_LE(0, file_fd); + + /* Checks that IOCTL commands return the expected errors. */ + EXPECT_EQ(variant->expected_tcgets_result, test_tcgets_ioctl(file_fd)); + EXPECT_EQ(variant->expected_fionread_result, + test_fionread_ioctl(file_fd)); + + /* Checks that unrestrictable commands are unrestricted. */ + EXPECT_EQ(0, ioctl(file_fd, FIOCLEX)); + EXPECT_EQ(0, ioctl(file_fd, FIONCLEX)); + EXPECT_EQ(0, ioctl(file_fd, FIONBIO, &flag)); + EXPECT_EQ(0, ioctl(file_fd, FIOASYNC, &flag)); + EXPECT_EQ(ENOTTY, test_fioqsize_ioctl(file_fd)); + + ASSERT_EQ(0, close(file_fd)); +} + +TEST_F_FORK(ioctl, handle_dir_access_dir) +{ + const int flag =3D 0; + const struct rule rules[] =3D { + { + .path =3D "/dev", + .access =3D variant->allowed, + }, + {}, + }; + int dir_fd, ruleset_fd; + + /* Enables Landlock. */ + ruleset_fd =3D create_ruleset(_metadata, variant->handled, rules); + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + /* + * Ignore variant->open_mode for this test, as we intend to open a + * directory. If the directory can not be opened, the variant is + * infeasible to test with an opened directory. + */ + dir_fd =3D open("/dev", O_RDONLY); + if (dir_fd < 0) + return; + + /* + * Checks that IOCTL commands return the expected errors. + * We do not use the expected values from the fixture here. + * + * When using IOCTL on a directory, no Landlock restrictions apply. + * TCGETS will fail anyway because it is not invoked on a TTY device. + */ + EXPECT_EQ(ENOTTY, test_tcgets_ioctl(dir_fd)); + EXPECT_EQ(0, test_fionread_ioctl(dir_fd)); + + /* Checks that unrestrictable commands are unrestricted. */ + EXPECT_EQ(0, ioctl(dir_fd, FIOCLEX)); + EXPECT_EQ(0, ioctl(dir_fd, FIONCLEX)); + EXPECT_EQ(0, ioctl(dir_fd, FIONBIO, &flag)); + EXPECT_EQ(0, ioctl(dir_fd, FIOASYNC, &flag)); + EXPECT_EQ(0, test_fioqsize_ioctl(dir_fd)); + + ASSERT_EQ(0, close(dir_fd)); +} + +TEST_F_FORK(ioctl, handle_file_access_file) +{ + const int flag =3D 0; + const struct rule rules[] =3D { + { + .path =3D "/dev/tty0", + .access =3D variant->allowed, + }, + {}, + }; + int file_fd, ruleset_fd; + + if (variant->allowed & LANDLOCK_ACCESS_FS_READ_DIR) { + SKIP(return, "LANDLOCK_ACCESS_FS_READ_DIR " + "can not be granted on files"); + } + + /* Enables Landlock. */ + ruleset_fd =3D create_ruleset(_metadata, variant->handled, rules); + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + file_fd =3D open("/dev/tty0", variant->open_mode); + ASSERT_LE(0, file_fd) + { + TH_LOG("Failed to open /dev/tty0: %s", strerror(errno)); + } + + /* Checks that IOCTL commands return the expected errors. */ + EXPECT_EQ(variant->expected_tcgets_result, test_tcgets_ioctl(file_fd)); + EXPECT_EQ(variant->expected_fionread_result, + test_fionread_ioctl(file_fd)); + + /* Checks that unrestrictable commands are unrestricted. */ + EXPECT_EQ(0, ioctl(file_fd, FIOCLEX)); + EXPECT_EQ(0, ioctl(file_fd, FIONCLEX)); + EXPECT_EQ(0, ioctl(file_fd, FIONBIO, &flag)); + EXPECT_EQ(0, ioctl(file_fd, FIOASYNC, &flag)); + EXPECT_EQ(ENOTTY, test_fioqsize_ioctl(file_fd)); + + ASSERT_EQ(0, close(file_fd)); +} + /* clang-format off */ FIXTURE(layout1_bind) {}; /* clang-format on */ --=20 2.44.0.278.ge034bb2e1d-goog