* [PATCH] selftests/landlock: skip overlayfs test when kernel not support it
@ 2022-08-20 0:45 jeffxu
2022-08-22 12:55 ` Mickaël Salaün
0 siblings, 1 reply; 2+ messages in thread
From: jeffxu @ 2022-08-20 0:45 UTC (permalink / raw)
To: mic; +Cc: jorgelo, keescook, linux-security-module, groeck, Jeff Xu,
Jeff Xu
From: Jeff Xu <jeffxu@google.com>
Overlayfs can be disabled in kernel config, causing related tests to fail.
Add check for overlayfs’s supportability at runtime, so we can call SKIP()
when needed.
Signed-off-by: Jeff Xu <jeffxu@chromium.org>
---
tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
1 file changed, 53 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index 21a2ce8fa739..f604165dbd21 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -11,6 +11,7 @@
#include <fcntl.h>
#include <linux/landlock.h>
#include <sched.h>
+#include <stdio.h>
#include <string.h>
#include <sys/capability.h>
#include <sys/mount.h>
@@ -3398,12 +3399,53 @@ static const char (*merge_sub_files[])[] = {
* └── work
*/
+static char *fgrep(FILE *inf, const char *str)
+{
+ char line[256];
+ int slen = strlen(str);
+
+ while (!feof(inf)) {
+ if (!fgets(line, 256, inf))
+ break;
+ if (strncmp(line, str, slen))
+ continue;
+
+ return strdup(line);
+ }
+
+ return NULL;
+}
+
+static bool check_overlayfs_support(void)
+{
+ FILE *inf = fopen("/proc/filesystems", "r");
+ char *res;
+ bool ret = false;
+
+ if (!inf)
+ return false;
+
+ res = fgrep(inf, "nodev\toverlay\n");
+
+ if (res) {
+ ret = true;
+ free(res);
+ }
+
+ fclose(inf);
+
+ return ret;
+}
+
/* clang-format off */
FIXTURE(layout2_overlay) {};
/* clang-format on */
FIXTURE_SETUP(layout2_overlay)
{
+ int rc;
+ bool support;
+
prepare_layout(_metadata);
create_directory(_metadata, LOWER_BASE);
@@ -3431,9 +3473,17 @@ FIXTURE_SETUP(layout2_overlay)
create_directory(_metadata, MERGE_DATA);
set_cap(_metadata, CAP_SYS_ADMIN);
set_cap(_metadata, CAP_DAC_OVERRIDE);
- ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
- "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
- ",workdir=" UPPER_WORK));
+
+ rc = mount("overlay", MERGE_DATA, "overlay", 0,
+ "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
+ ",workdir=" UPPER_WORK);
+ if (rc < 0) {
+ TH_LOG("mount overlay failed: errorno=%s", strerror(errno));
+ support = check_overlayfs_support();
+ ASSERT_FALSE(support);
+ SKIP(return, "overlayfs is not supported");
+ }
+
clear_cap(_metadata, CAP_DAC_OVERRIDE);
clear_cap(_metadata, CAP_SYS_ADMIN);
}
--
2.37.1.595.g718a3a8f04-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] selftests/landlock: skip overlayfs test when kernel not support it
2022-08-20 0:45 [PATCH] selftests/landlock: skip overlayfs test when kernel not support it jeffxu
@ 2022-08-22 12:55 ` Mickaël Salaün
0 siblings, 0 replies; 2+ messages in thread
From: Mickaël Salaün @ 2022-08-22 12:55 UTC (permalink / raw)
To: jeffxu; +Cc: jorgelo, keescook, linux-security-module, groeck, Jeff Xu
On 20/08/2022 02:45, jeffxu@google.com wrote:
> From: Jeff Xu <jeffxu@google.com>
>
> Overlayfs can be disabled in kernel config, causing related tests to fail.
> Add check for overlayfs’s supportability at runtime, so we can call SKIP()
> when needed.
>
> Signed-off-by: Jeff Xu <jeffxu@chromium.org>
> ---
> tools/testing/selftests/landlock/fs_test.c | 56 ++++++++++++++++++++--
> 1 file changed, 53 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
> index 21a2ce8fa739..f604165dbd21 100644
> --- a/tools/testing/selftests/landlock/fs_test.c
> +++ b/tools/testing/selftests/landlock/fs_test.c
> @@ -11,6 +11,7 @@
> #include <fcntl.h>
> #include <linux/landlock.h>
> #include <sched.h>
> +#include <stdio.h>
> #include <string.h>
> #include <sys/capability.h>
> #include <sys/mount.h>
> @@ -3398,12 +3399,53 @@ static const char (*merge_sub_files[])[] = {
> * └── work
> */
>
> +static char *fgrep(FILE *inf, const char *str)
Please move these two helpers just before prepare_layout(). I plan to
use them for other filesystems.
> +{
> + char line[256];
I guess we can safely set this array size to 32 for now.
> + int slen = strlen(str);
> +
> + while (!feof(inf)) {
> + if (!fgets(line, 256, inf))
Please use sizeof(line)
> + break;
> + if (strncmp(line, str, slen))
> + continue;
> +
> + return strdup(line);
No need to duplicate the string, just return a boolean.
> + }
> +
> + return NULL;
> +}
> +
> +static bool check_overlayfs_support(void)
Can be renamed to supports_overlayfs().
> +{
> + FILE *inf = fopen("/proc/filesystems", "r");
Just move the fopen() call at the end of variable declaration and add a
const.
> + char *res;
> + bool ret = false;
> +
> + if (!inf)
> + return false;
Let's assume that a failed attempt to open /proc/filesystems means the
filesystem is supported (default behavior). This can help detect such
missing file (which should not happen). You can add a comment to explain
the rational.
> +
> + res = fgrep(inf, "nodev\toverlay\n");
> +
> + if (res) {
> + ret = true;
> + free(res);
> + }
> +
> + fclose(inf);
> +
> + return ret;
> +}
> +
> /* clang-format off */
> FIXTURE(layout2_overlay) {};
> /* clang-format on */
>
> FIXTURE_SETUP(layout2_overlay)
> {
> + int rc;
Let's stick to "ret".
> + bool support;
s/support/is_supported/
> +
> prepare_layout(_metadata);
>
> create_directory(_metadata, LOWER_BASE);
> @@ -3431,9 +3473,17 @@ FIXTURE_SETUP(layout2_overlay)
> create_directory(_metadata, MERGE_DATA);
> set_cap(_metadata, CAP_SYS_ADMIN);
> set_cap(_metadata, CAP_DAC_OVERRIDE);
> - ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0,
> - "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
> - ",workdir=" UPPER_WORK));
> +
> + rc = mount("overlay", MERGE_DATA, "overlay", 0,
> + "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA
> + ",workdir=" UPPER_WORK);
Please format with clang-format-14, otherwise I'll do it myself.
> + if (rc < 0) {
Please check errno with ASSERT_EQ() to differentiate from a skippable
mount and an unexpected error. The next TH_LOG() would then not be needed.
> + TH_LOG("mount overlay failed: errorno=%s", strerror(errno));
> + support = check_overlayfs_support();
> + ASSERT_FALSE(support);
ASSERT_FALSE(supports_overlayfs());
> + SKIP(return, "overlayfs is not supported");
> + }
> +
Please keep these clear_cap() calls just after the mount call.
> clear_cap(_metadata, CAP_DAC_OVERRIDE);
> clear_cap(_metadata, CAP_SYS_ADMIN);
> }
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-08-22 12:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-20 0:45 [PATCH] selftests/landlock: skip overlayfs test when kernel not support it jeffxu
2022-08-22 12:55 ` Mickaël Salaün
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).