1 /* Small standalone test program to check the existence of Capsicum syscalls */
7 #include <sys/syscall.h>
17 // glibc on Linux caches getpid() return value.
18 int getpid_(void) { return syscall(__NR_getpid); }
20 #define getpid_ getpid
23 static int seen_sigchld = 0;
24 static void handle_signal(int x) {
25 fprintf(stderr, "[%d] received SIGCHLD\n", getpid_());
29 int main(int argc, char *argv[]) {
30 signal(SIGCHLD, handle_signal);
31 int lifetime = 4; /* seconds */
33 lifetime = atoi(argv[1]);
36 /* cap_rights_limit() available? */
38 cap_rights_init(&r_rws, CAP_READ, CAP_WRITE, CAP_SEEK);
39 int cap_fd = dup(STDOUT_FILENO);
40 int rc = cap_rights_limit(cap_fd, &r_rws);
41 fprintf(stderr, "[%d] cap_fd=%d\n", getpid_(), cap_fd);
42 if (rc < 0) fprintf(stderr, "*** cap_rights_limit() failed: errno=%d %s\n", errno, strerror(errno));
44 /* cap_rights_get() available? */
46 cap_rights_init(&rights, 0);
47 rc = cap_rights_get(cap_fd, &rights);
49 cap_rights_describe(&rights, buffer);
50 fprintf(stderr, "[%d] cap_rights_get(cap_fd=%d) rc=%d rights=%s\n", getpid_(), cap_fd, rc, buffer);
51 if (rc < 0) fprintf(stderr, "*** cap_rights_get() failed: errno=%d %s\n", errno, strerror(errno));
53 /* fstat() policed? */
55 rc = fstat(cap_fd, &buf);
56 fprintf(stderr, "[%d] fstat(cap_fd=%d) rc=%d errno=%d\n", getpid_(), cap_fd, rc, errno);
57 if (rc != -1) fprintf(stderr, "*** fstat() unexpectedly succeeded\n");
59 /* pdfork() available? */
62 if (rc < 0) fprintf(stderr, "*** pdfork() failed: errno=%d %s\n", errno, strerror(errno));
64 if (rc == 0) { /* child */
67 fprintf(stderr, " [%d] child alive, parent is ppid=%d\n", getpid_(), getppid());
70 fprintf(stderr, " [%d] child exit(0)\n", getpid_());
73 fprintf(stderr, "[%d] pdfork() rc=%d pd=%d\n", getpid_(), rc, pd);
75 /* pdgetpid() available? */
76 pid_t actual_pid = rc;
78 rc = pdgetpid(pd, &got_pid);
79 if (rc < 0) fprintf(stderr, "*** pdgetpid(pd=%d) failed: errno=%d %s\n", pd, errno, strerror(errno));
80 fprintf(stderr, "[%d] pdgetpid(pd=%d)=%d, pdfork returned %d\n", getpid_(), pd, got_pid, actual_pid);
84 /* pdkill() available? */
85 rc = pdkill(pd, SIGKILL);
86 fprintf(stderr, "[%d] pdkill(pd=%d, SIGKILL) -> rc=%d\n", getpid_(), pd, rc);
87 if (rc < 0) fprintf(stderr, "*** pdkill() failed: errno=%d %s\n", errno, strerror(errno));
88 usleep(50000); /* Allow time for death and signals */
90 /* Death of a pdforked child should be invisible */
91 if (seen_sigchld) fprintf(stderr, "*** SIGCHLD emitted\n");
93 rc = wait4(-1, &status, WNOHANG, NULL);
94 if (rc > 0) fprintf(stderr, "*** wait4(-1, ...) unexpectedly found child %d\n", rc);
96 fprintf(stderr, "[%d] forking off a child process to check cap_enter()\n", getpid_());
98 if (child == 0) { /* child */
99 /* cap_getmode() / cap_enter() available? */
100 unsigned int cap_mode = -1;
101 rc = cap_getmode(&cap_mode);
102 fprintf(stderr, " [%d] cap_getmode() -> rc=%d, cap_mode=%d\n", getpid_(), rc, cap_mode);
103 if (rc < 0) fprintf(stderr, "*** cap_getmode() failed: errno=%d %s\n", errno, strerror(errno));
106 fprintf(stderr, " [%d] cap_enter() -> rc=%d\n", getpid_(), rc);
107 if (rc < 0) fprintf(stderr, "*** cap_enter() failed: errno=%d %s\n", errno, strerror(errno));
109 rc = cap_getmode(&cap_mode);
110 fprintf(stderr, " [%d] cap_getmode() -> rc=%d, cap_mode=%d\n", getpid_(), rc, cap_mode);
111 if (rc < 0) fprintf(stderr, "*** cap_getmode() failed: errno=%d %s\n", errno, strerror(errno));
113 /* open disallowed? */
114 rc = open("/etc/passwd", O_RDONLY);
115 fprintf(stderr, " [%d] open('/etc/passwd') -> rc=%d, errno=%d\n", getpid_(), rc, errno);
116 if (rc != -1) fprintf(stderr, "*** open() unexpectedly succeeded\n");
118 if (errno != ECAPMODE) fprintf(stderr, "*** open() failed with errno %d not ECAPMODE\n", errno);
122 rc = wait4(child, &status, 0, NULL);
123 fprintf(stderr, "[%d] child %d exited with status %x\n", getpid_(), child, status);
125 /* fexecve() available? */
126 char* argv_pass[] = {(char*)"/bin/ls", "-l", "smoketest", NULL};
127 char* null_envp[] = {NULL};
128 int ls_bin = open("/bin/ls", O_RDONLY);
129 fprintf(stderr, "[%d] about to fexecve('/bin/ls', '-l', 'smoketest')\n", getpid_());
130 rc = fexecve(ls_bin, argv_pass, null_envp);
131 /* should never reach here */
132 fprintf(stderr, "*** fexecve(fd=%d) failed: rc=%d errno=%d %s\n", ls_bin, rc, errno, strerror(errno));