linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/landlock: skip ptrace_test when YAMA is enabled
@ 2022-06-28 22:29 Jeff Xu
  2022-06-30 15:09 ` Mickaël Salaün
  2022-06-30 15:31 ` Mickaël Salaün
  0 siblings, 2 replies; 17+ messages in thread
From: Jeff Xu @ 2022-06-28 22:29 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: linux-security-module, Jeff Xu, Jorge Lucangeli Obes,
	Guenter Roeck, Kees Cook

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

Cc: Jorge Lucangeli Obes <jorgelo@chromium.org>
Cc: Guenter Roeck <groeck@chromium.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Mickaël Salaün <mic@digikod.net>
Tested-by: Jeff Xu <jeffxu@google.com>
Signed-off-by: Jeff Xu <jeffxu@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


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2022-07-18 21:25 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-28 22:29 [PATCH] selftests/landlock: skip ptrace_test when YAMA is enabled Jeff Xu
2022-06-30 15:09 ` Mickaël Salaün
2022-06-30 15:31 ` Mickaël Salaün
2022-07-05 21:49   ` Jeff Xu
2022-07-06 23:41     ` Jeff Xu
2022-07-07 14:24     ` Mickaël Salaün
     [not found]     ` <CALmYWFvcSn_wks0bXo3_DUyYGtVW_MHrrO1dRoYcxY4uiB1KEA@mail.gmail.com>
2022-07-07 14:25       ` Mickaël Salaün
2022-07-13 23:44         ` Jeff Xu
2022-07-14  0:30           ` Guenter Roeck
2022-07-14 18:36             ` Jeff Xu
2022-07-14 20:39               ` Guenter Roeck
2022-07-15  0:35                 ` Jeff Xu
2022-07-15 21:41                   ` Jeff Xu
2022-07-15 22:42                     ` Guenter Roeck
2022-07-16  0:16                       ` Jeff Xu
2022-07-16 21:45                         ` Guenter Roeck
2022-07-18 21:24                           ` 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).