Commit 366617a6 authored by Jeff Xu's avatar Jeff Xu Committed by Mickaël Salaün

selftests/landlock: Skip overlayfs tests when not supported

overlayfs may be disabled in the kernel configuration, causing related
tests to fail.  Check that overlayfs is supported at runtime, so we can
skip layout2_overlay.* accordingly.
Signed-off-by: default avatarJeff Xu <jeffxu@google.com>
Reviewed-by: default avatarGuenter Roeck <groeck@chromium.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20230113053229.1281774-2-jeffxu@google.com
[mic: Reword comments and constify variables]
Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
parent 3e52e5b0
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <linux/landlock.h> #include <linux/landlock.h>
#include <sched.h> #include <sched.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/capability.h> #include <sys/capability.h>
#include <sys/mount.h> #include <sys/mount.h>
...@@ -89,6 +90,40 @@ static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3"; ...@@ -89,6 +90,40 @@ static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3";
* └── s3d3 * └── s3d3
*/ */
static bool fgrep(FILE *const inf, const char *const str)
{
char line[32];
const int slen = strlen(str);
while (!feof(inf)) {
if (!fgets(line, sizeof(line), inf))
break;
if (strncmp(line, str, slen))
continue;
return true;
}
return false;
}
static bool supports_overlayfs(void)
{
bool res;
FILE *const inf = fopen("/proc/filesystems", "r");
/*
* Consider that the filesystem is supported if we cannot get the
* supported ones.
*/
if (!inf)
return true;
res = fgrep(inf, "nodev\toverlay\n");
fclose(inf);
return res;
}
static void mkdir_parents(struct __test_metadata *const _metadata, static void mkdir_parents(struct __test_metadata *const _metadata,
const char *const path) const char *const path)
{ {
...@@ -4001,6 +4036,9 @@ FIXTURE(layout2_overlay) {}; ...@@ -4001,6 +4036,9 @@ FIXTURE(layout2_overlay) {};
FIXTURE_SETUP(layout2_overlay) FIXTURE_SETUP(layout2_overlay)
{ {
if (!supports_overlayfs())
SKIP(return, "overlayfs is not supported");
prepare_layout(_metadata); prepare_layout(_metadata);
create_directory(_metadata, LOWER_BASE); create_directory(_metadata, LOWER_BASE);
...@@ -4037,6 +4075,9 @@ FIXTURE_SETUP(layout2_overlay) ...@@ -4037,6 +4075,9 @@ FIXTURE_SETUP(layout2_overlay)
FIXTURE_TEARDOWN(layout2_overlay) FIXTURE_TEARDOWN(layout2_overlay)
{ {
if (!supports_overlayfs())
SKIP(return, "overlayfs is not supported");
EXPECT_EQ(0, remove_path(lower_do1_fl3)); EXPECT_EQ(0, remove_path(lower_do1_fl3));
EXPECT_EQ(0, remove_path(lower_dl1_fl2)); EXPECT_EQ(0, remove_path(lower_dl1_fl2));
EXPECT_EQ(0, remove_path(lower_fl1)); EXPECT_EQ(0, remove_path(lower_fl1));
...@@ -4068,6 +4109,9 @@ FIXTURE_TEARDOWN(layout2_overlay) ...@@ -4068,6 +4109,9 @@ FIXTURE_TEARDOWN(layout2_overlay)
TEST_F_FORK(layout2_overlay, no_restriction) TEST_F_FORK(layout2_overlay, no_restriction)
{ {
if (!supports_overlayfs())
SKIP(return, "overlayfs is not supported");
ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY)); ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY));
ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY)); ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY));
ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY)); ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY));
...@@ -4231,6 +4275,9 @@ TEST_F_FORK(layout2_overlay, same_content_different_file) ...@@ -4231,6 +4275,9 @@ TEST_F_FORK(layout2_overlay, same_content_different_file)
size_t i; size_t i;
const char *path_entry; const char *path_entry;
if (!supports_overlayfs())
SKIP(return, "overlayfs is not supported");
/* Sets rules on base directories (i.e. outside overlay scope). */ /* Sets rules on base directories (i.e. outside overlay scope). */
ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base); ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base);
ASSERT_LE(0, ruleset_fd); ASSERT_LE(0, ruleset_fd);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment