[PATCH] selftests/landlock: skip ptrace_test when YAMA is enabled

Jeff Xu jeffxu at google.com
Tue Jun 28 22:29:41 UTC 2022


ptrace_test assumes YAMA is disabled, skip it if YAMA is enabled.

Cc: Jorge Lucangeli Obes <jorgelo at chromium.org>
Cc: Guenter Roeck <groeck at chromium.org>
Cc: Kees Cook <keescook at chromium.org>
Cc: Mickaël Salaün <mic at digikod.net>
Tested-by: Jeff Xu <jeffxu at google.com>
Signed-off-by: Jeff Xu <jeffxu at google.com>
Change-Id: I623742ca9f20ec706a38c92f6c0bab755f73578f
---
 .../testing/selftests/landlock/ptrace_test.c  | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/tools/testing/selftests/landlock/ptrace_test.c b/tools/testing/selftests/landlock/ptrace_test.c
index c28ef98ff3ac..ef2d36f56764 100644
--- a/tools/testing/selftests/landlock/ptrace_test.c
+++ b/tools/testing/selftests/landlock/ptrace_test.c
@@ -226,6 +226,44 @@ FIXTURE_TEARDOWN(hierarchy)
 {
 }
 
+int open_sysfs(const char *path, int flags, int *fd)
+{
+	*fd = open(path, flags);
+
+	if (fd < 0)
+		return -1;
+
+	return 0;
+}
+
+int read_sysfs_int_fd(int fd, int *val)
+{
+	char buf[2];
+
+	if (read(fd, buf, sizeof(buf)) < 0)
+		return -1;
+
+	buf[sizeof(buf) - 1] = '\0';
+	*val = atoi(buf);
+	return 0;
+}
+
+int read_sysfs_int(const char *path, int *val)
+{
+	int fd;
+
+	if (open_sysfs(path, O_RDONLY, &fd) != 0)
+		return -1;
+
+	if (read_sysfs_int_fd(fd, val) != 0) {
+		close(fd);
+		return -1;
+	}
+
+	close(fd);
+	return 0;
+}
+
 /* Test PTRACE_TRACEME and PTRACE_ATTACH for parent and child. */
 TEST_F(hierarchy, trace)
 {
@@ -235,6 +273,17 @@ TEST_F(hierarchy, trace)
 	char buf_parent;
 	long ret;
 
+	int ptrace_val;
+
+	ASSERT_EQ(0, read_sysfs_int("/proc/sys/kernel/yama/ptrace_scope",
+				    &ptrace_val));
+	if (ptrace_val != 0) {
+		/*
+		 * Yama's scoped ptrace is presumed disabled.  If enabled, skip.
+		 */
+		SKIP(return, "yama is enabled, skip current test");
+	}
+
 	/*
 	 * Removes all effective and permitted capabilities to not interfere
 	 * with cap_ptrace_access_check() in case of PTRACE_MODE_FSCREDS.
-- 
2.37.0.rc0.161.g10f37bed90-goog



More information about the Linux-security-module-archive mailing list