Module Name:    src
Committed By:   kamil
Date:           Sun Nov  6 16:24:16 UTC 2016

Modified Files:
        src/tests/kernel: t_ptrace.c

Log Message:
Add new tests attach_pid0 and attach_pid1 to t_ptrace

attach_pid0 asserts that it is not valid to attach PID 0 as it is a special
kernel process.

assert_pid1 asserts that non-root user cannot attach to PID 1 as it is the
/dev/init process. This tests is skipped if run as root.

Sponsored by <The NetBSD Foundation>.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/tests/kernel/t_ptrace.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/kernel/t_ptrace.c
diff -u src/tests/kernel/t_ptrace.c:1.10 src/tests/kernel/t_ptrace.c:1.11
--- src/tests/kernel/t_ptrace.c:1.10	Sat Nov  5 15:52:35 2016
+++ src/tests/kernel/t_ptrace.c	Sun Nov  6 16:24:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_ptrace.c,v 1.10 2016/11/05 15:52:35 kamil Exp $	*/
+/*	$NetBSD: t_ptrace.c,v 1.11 2016/11/06 16:24:16 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace.c,v 1.10 2016/11/05 15:52:35 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace.c,v 1.11 2016/11/06 16:24:16 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -622,6 +622,37 @@ ATF_TC_BODY(attach1, tc)
 	ATF_REQUIRE(close(fds_totracer[1]) == 0);
 }
 
+ATF_TC(attach_pid0);
+ATF_TC_HEAD(attach_pid0, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Assert that a debugger cannot attach to PID 0");
+}
+
+ATF_TC_BODY(attach_pid0, tc)
+{
+	errno = 0;
+	ATF_REQUIRE(ptrace(PT_ATTACH, 0, NULL, 0) == -1);
+	ATF_REQUIRE(errno == EPERM);
+}
+
+ATF_TC(attach_pid1);
+ATF_TC_HEAD(attach_pid1, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Assert that a debugger cannot attach to PID 1 (as non-root)");
+}
+
+ATF_TC_BODY(attach_pid1, tc)
+{
+	if (getuid() == 0)
+		atf_tc_skip("Test must be run as non-root");
+
+	errno = 0;
+	ATF_REQUIRE(ptrace(PT_ATTACH, 1, NULL, 0) == -1);
+	ATF_REQUIRE(errno == EPERM);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, traceme1);
@@ -629,6 +660,8 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, traceme3);
 	ATF_TP_ADD_TC(tp, traceme4);
 	ATF_TP_ADD_TC(tp, attach1);
+	ATF_TP_ADD_TC(tp, attach_pid0);
+	ATF_TP_ADD_TC(tp, attach_pid1);
 
 	return atf_no_error();
 }

Reply via email to