18c2ecf20Sopenharmony_ci#include <signal.h>
28c2ecf20Sopenharmony_ci#include <stdio.h>
38c2ecf20Sopenharmony_ci#include <stdlib.h>
48c2ecf20Sopenharmony_ci#include <unistd.h>
58c2ecf20Sopenharmony_ci#include <errno.h>
68c2ecf20Sopenharmony_ci#include <fcntl.h>
78c2ecf20Sopenharmony_ci#include <string.h>
88c2ecf20Sopenharmony_ci#include <stddef.h>
98c2ecf20Sopenharmony_ci#include <sys/sysmacros.h>
108c2ecf20Sopenharmony_ci#include <sys/types.h>
118c2ecf20Sopenharmony_ci#include <sys/wait.h>
128c2ecf20Sopenharmony_ci#include <sys/socket.h>
138c2ecf20Sopenharmony_ci#include <sys/stat.h>
148c2ecf20Sopenharmony_ci#include <sys/mman.h>
158c2ecf20Sopenharmony_ci#include <sys/syscall.h>
168c2ecf20Sopenharmony_ci#include <sys/user.h>
178c2ecf20Sopenharmony_ci#include <sys/ioctl.h>
188c2ecf20Sopenharmony_ci#include <sys/ptrace.h>
198c2ecf20Sopenharmony_ci#include <sys/mount.h>
208c2ecf20Sopenharmony_ci#include <linux/limits.h>
218c2ecf20Sopenharmony_ci#include <linux/filter.h>
228c2ecf20Sopenharmony_ci#include <linux/seccomp.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int seccomp(unsigned int op, unsigned int flags, void *args)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	errno = 0;
298c2ecf20Sopenharmony_ci	return syscall(__NR_seccomp, op, flags, args);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int send_fd(int sock, int fd)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct msghdr msg = {};
358c2ecf20Sopenharmony_ci	struct cmsghdr *cmsg;
368c2ecf20Sopenharmony_ci	char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
378c2ecf20Sopenharmony_ci	struct iovec io = {
388c2ecf20Sopenharmony_ci		.iov_base = &c,
398c2ecf20Sopenharmony_ci		.iov_len = 1,
408c2ecf20Sopenharmony_ci	};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	msg.msg_iov = &io;
438c2ecf20Sopenharmony_ci	msg.msg_iovlen = 1;
448c2ecf20Sopenharmony_ci	msg.msg_control = buf;
458c2ecf20Sopenharmony_ci	msg.msg_controllen = sizeof(buf);
468c2ecf20Sopenharmony_ci	cmsg = CMSG_FIRSTHDR(&msg);
478c2ecf20Sopenharmony_ci	cmsg->cmsg_level = SOL_SOCKET;
488c2ecf20Sopenharmony_ci	cmsg->cmsg_type = SCM_RIGHTS;
498c2ecf20Sopenharmony_ci	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
508c2ecf20Sopenharmony_ci	*((int *)CMSG_DATA(cmsg)) = fd;
518c2ecf20Sopenharmony_ci	msg.msg_controllen = cmsg->cmsg_len;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (sendmsg(sock, &msg, 0) < 0) {
548c2ecf20Sopenharmony_ci		perror("sendmsg");
558c2ecf20Sopenharmony_ci		return -1;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int recv_fd(int sock)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct msghdr msg = {};
648c2ecf20Sopenharmony_ci	struct cmsghdr *cmsg;
658c2ecf20Sopenharmony_ci	char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c';
668c2ecf20Sopenharmony_ci	struct iovec io = {
678c2ecf20Sopenharmony_ci		.iov_base = &c,
688c2ecf20Sopenharmony_ci		.iov_len = 1,
698c2ecf20Sopenharmony_ci	};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	msg.msg_iov = &io;
728c2ecf20Sopenharmony_ci	msg.msg_iovlen = 1;
738c2ecf20Sopenharmony_ci	msg.msg_control = buf;
748c2ecf20Sopenharmony_ci	msg.msg_controllen = sizeof(buf);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (recvmsg(sock, &msg, 0) < 0) {
778c2ecf20Sopenharmony_ci		perror("recvmsg");
788c2ecf20Sopenharmony_ci		return -1;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	cmsg = CMSG_FIRSTHDR(&msg);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return *((int *)CMSG_DATA(cmsg));
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int user_trap_syscall(int nr, unsigned int flags)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct sock_filter filter[] = {
898c2ecf20Sopenharmony_ci		BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
908c2ecf20Sopenharmony_ci			offsetof(struct seccomp_data, nr)),
918c2ecf20Sopenharmony_ci		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, nr, 0, 1),
928c2ecf20Sopenharmony_ci		BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_USER_NOTIF),
938c2ecf20Sopenharmony_ci		BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
948c2ecf20Sopenharmony_ci	};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	struct sock_fprog prog = {
978c2ecf20Sopenharmony_ci		.len = (unsigned short)ARRAY_SIZE(filter),
988c2ecf20Sopenharmony_ci		.filter = filter,
998c2ecf20Sopenharmony_ci	};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int handle_req(struct seccomp_notif *req,
1058c2ecf20Sopenharmony_ci		      struct seccomp_notif_resp *resp, int listener)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	char path[PATH_MAX], source[PATH_MAX], target[PATH_MAX];
1088c2ecf20Sopenharmony_ci	int ret = -1, mem;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	resp->id = req->id;
1118c2ecf20Sopenharmony_ci	resp->error = -EPERM;
1128c2ecf20Sopenharmony_ci	resp->val = 0;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (req->data.nr != __NR_mount) {
1158c2ecf20Sopenharmony_ci		fprintf(stderr, "huh? trapped something besides mount? %d\n", req->data.nr);
1168c2ecf20Sopenharmony_ci		return -1;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* Only allow bind mounts. */
1208c2ecf20Sopenharmony_ci	if (!(req->data.args[3] & MS_BIND))
1218c2ecf20Sopenharmony_ci		return 0;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/*
1248c2ecf20Sopenharmony_ci	 * Ok, let's read the task's memory to see where they wanted their
1258c2ecf20Sopenharmony_ci	 * mount to go.
1268c2ecf20Sopenharmony_ci	 */
1278c2ecf20Sopenharmony_ci	snprintf(path, sizeof(path), "/proc/%d/mem", req->pid);
1288c2ecf20Sopenharmony_ci	mem = open(path, O_RDONLY);
1298c2ecf20Sopenharmony_ci	if (mem < 0) {
1308c2ecf20Sopenharmony_ci		perror("open mem");
1318c2ecf20Sopenharmony_ci		return -1;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/*
1358c2ecf20Sopenharmony_ci	 * Now we avoid a TOCTOU: we referred to a pid by its pid, but since
1368c2ecf20Sopenharmony_ci	 * the pid that made the syscall may have died, we need to confirm that
1378c2ecf20Sopenharmony_ci	 * the pid is still valid after we open its /proc/pid/mem file. We can
1388c2ecf20Sopenharmony_ci	 * ask the listener fd this as follows.
1398c2ecf20Sopenharmony_ci	 *
1408c2ecf20Sopenharmony_ci	 * Note that this check should occur *after* any task-specific
1418c2ecf20Sopenharmony_ci	 * resources are opened, to make sure that the task has not died and
1428c2ecf20Sopenharmony_ci	 * we're not wrongly reading someone else's state in order to make
1438c2ecf20Sopenharmony_ci	 * decisions.
1448c2ecf20Sopenharmony_ci	 */
1458c2ecf20Sopenharmony_ci	if (ioctl(listener, SECCOMP_IOCTL_NOTIF_ID_VALID, &req->id) < 0) {
1468c2ecf20Sopenharmony_ci		fprintf(stderr, "task died before we could map its memory\n");
1478c2ecf20Sopenharmony_ci		goto out;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/*
1518c2ecf20Sopenharmony_ci	 * Phew, we've got the right /proc/pid/mem. Now we can read it. Note
1528c2ecf20Sopenharmony_ci	 * that to avoid another TOCTOU, we should read all of the pointer args
1538c2ecf20Sopenharmony_ci	 * before we decide to allow the syscall.
1548c2ecf20Sopenharmony_ci	 */
1558c2ecf20Sopenharmony_ci	if (lseek(mem, req->data.args[0], SEEK_SET) < 0) {
1568c2ecf20Sopenharmony_ci		perror("seek");
1578c2ecf20Sopenharmony_ci		goto out;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	ret = read(mem, source, sizeof(source));
1618c2ecf20Sopenharmony_ci	if (ret < 0) {
1628c2ecf20Sopenharmony_ci		perror("read");
1638c2ecf20Sopenharmony_ci		goto out;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (lseek(mem, req->data.args[1], SEEK_SET) < 0) {
1678c2ecf20Sopenharmony_ci		perror("seek");
1688c2ecf20Sopenharmony_ci		goto out;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	ret = read(mem, target, sizeof(target));
1728c2ecf20Sopenharmony_ci	if (ret < 0) {
1738c2ecf20Sopenharmony_ci		perror("read");
1748c2ecf20Sopenharmony_ci		goto out;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/*
1788c2ecf20Sopenharmony_ci	 * Our policy is to only allow bind mounts inside /tmp. This isn't very
1798c2ecf20Sopenharmony_ci	 * interesting, because we could do unprivlieged bind mounts with user
1808c2ecf20Sopenharmony_ci	 * namespaces already, but you get the idea.
1818c2ecf20Sopenharmony_ci	 */
1828c2ecf20Sopenharmony_ci	if (!strncmp(source, "/tmp/", 5) && !strncmp(target, "/tmp/", 5)) {
1838c2ecf20Sopenharmony_ci		if (mount(source, target, NULL, req->data.args[3], NULL) < 0) {
1848c2ecf20Sopenharmony_ci			ret = -1;
1858c2ecf20Sopenharmony_ci			perror("actual mount");
1868c2ecf20Sopenharmony_ci			goto out;
1878c2ecf20Sopenharmony_ci		}
1888c2ecf20Sopenharmony_ci		resp->error = 0;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* Even if we didn't allow it because of policy, generating the
1928c2ecf20Sopenharmony_ci	 * response was be a success, because we want to tell the worker EPERM.
1938c2ecf20Sopenharmony_ci	 */
1948c2ecf20Sopenharmony_ci	ret = 0;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciout:
1978c2ecf20Sopenharmony_ci	close(mem);
1988c2ecf20Sopenharmony_ci	return ret;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ciint main(void)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	int sk_pair[2], ret = 1, status, listener;
2048c2ecf20Sopenharmony_ci	pid_t worker = 0 , tracer = 0;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) {
2078c2ecf20Sopenharmony_ci		perror("socketpair");
2088c2ecf20Sopenharmony_ci		return 1;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	worker = fork();
2128c2ecf20Sopenharmony_ci	if (worker < 0) {
2138c2ecf20Sopenharmony_ci		perror("fork");
2148c2ecf20Sopenharmony_ci		goto close_pair;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (worker == 0) {
2188c2ecf20Sopenharmony_ci		listener = user_trap_syscall(__NR_mount,
2198c2ecf20Sopenharmony_ci					     SECCOMP_FILTER_FLAG_NEW_LISTENER);
2208c2ecf20Sopenharmony_ci		if (listener < 0) {
2218c2ecf20Sopenharmony_ci			perror("seccomp");
2228c2ecf20Sopenharmony_ci			exit(1);
2238c2ecf20Sopenharmony_ci		}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci		/*
2268c2ecf20Sopenharmony_ci		 * Drop privileges. We definitely can't mount as uid 1000.
2278c2ecf20Sopenharmony_ci		 */
2288c2ecf20Sopenharmony_ci		if (setuid(1000) < 0) {
2298c2ecf20Sopenharmony_ci			perror("setuid");
2308c2ecf20Sopenharmony_ci			exit(1);
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci		/*
2348c2ecf20Sopenharmony_ci		 * Send the listener to the parent; also serves as
2358c2ecf20Sopenharmony_ci		 * synchronization.
2368c2ecf20Sopenharmony_ci		 */
2378c2ecf20Sopenharmony_ci		if (send_fd(sk_pair[1], listener) < 0)
2388c2ecf20Sopenharmony_ci			exit(1);
2398c2ecf20Sopenharmony_ci		close(listener);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		if (mkdir("/tmp/foo", 0755) < 0) {
2428c2ecf20Sopenharmony_ci			perror("mkdir");
2438c2ecf20Sopenharmony_ci			exit(1);
2448c2ecf20Sopenharmony_ci		}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		/*
2478c2ecf20Sopenharmony_ci		 * Try a bad mount just for grins.
2488c2ecf20Sopenharmony_ci		 */
2498c2ecf20Sopenharmony_ci		if (mount("/dev/sda", "/tmp/foo", NULL, 0, NULL) != -1) {
2508c2ecf20Sopenharmony_ci			fprintf(stderr, "huh? mounted /dev/sda?\n");
2518c2ecf20Sopenharmony_ci			exit(1);
2528c2ecf20Sopenharmony_ci		}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		if (errno != EPERM) {
2558c2ecf20Sopenharmony_ci			perror("bad error from mount");
2568c2ecf20Sopenharmony_ci			exit(1);
2578c2ecf20Sopenharmony_ci		}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		/*
2608c2ecf20Sopenharmony_ci		 * Ok, we expect this one to succeed.
2618c2ecf20Sopenharmony_ci		 */
2628c2ecf20Sopenharmony_ci		if (mount("/tmp/foo", "/tmp/foo", NULL, MS_BIND, NULL) < 0) {
2638c2ecf20Sopenharmony_ci			perror("mount");
2648c2ecf20Sopenharmony_ci			exit(1);
2658c2ecf20Sopenharmony_ci		}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci		exit(0);
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/*
2718c2ecf20Sopenharmony_ci	 * Get the listener from the child.
2728c2ecf20Sopenharmony_ci	 */
2738c2ecf20Sopenharmony_ci	listener = recv_fd(sk_pair[0]);
2748c2ecf20Sopenharmony_ci	if (listener < 0)
2758c2ecf20Sopenharmony_ci		goto out_kill;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/*
2788c2ecf20Sopenharmony_ci	 * Fork a task to handle the requests. This isn't strictly necessary,
2798c2ecf20Sopenharmony_ci	 * but it makes the particular writing of this sample easier, since we
2808c2ecf20Sopenharmony_ci	 * can just wait ofr the tracee to exit and kill the tracer.
2818c2ecf20Sopenharmony_ci	 */
2828c2ecf20Sopenharmony_ci	tracer = fork();
2838c2ecf20Sopenharmony_ci	if (tracer < 0) {
2848c2ecf20Sopenharmony_ci		perror("fork");
2858c2ecf20Sopenharmony_ci		goto out_kill;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (tracer == 0) {
2898c2ecf20Sopenharmony_ci		struct seccomp_notif *req;
2908c2ecf20Sopenharmony_ci		struct seccomp_notif_resp *resp;
2918c2ecf20Sopenharmony_ci		struct seccomp_notif_sizes sizes;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		if (seccomp(SECCOMP_GET_NOTIF_SIZES, 0, &sizes) < 0) {
2948c2ecf20Sopenharmony_ci			perror("seccomp(GET_NOTIF_SIZES)");
2958c2ecf20Sopenharmony_ci			goto out_close;
2968c2ecf20Sopenharmony_ci		}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci		req = malloc(sizes.seccomp_notif);
2998c2ecf20Sopenharmony_ci		if (!req)
3008c2ecf20Sopenharmony_ci			goto out_close;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		resp = malloc(sizes.seccomp_notif_resp);
3038c2ecf20Sopenharmony_ci		if (!resp)
3048c2ecf20Sopenharmony_ci			goto out_req;
3058c2ecf20Sopenharmony_ci		memset(resp, 0, sizes.seccomp_notif_resp);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		while (1) {
3088c2ecf20Sopenharmony_ci			memset(req, 0, sizes.seccomp_notif);
3098c2ecf20Sopenharmony_ci			if (ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, req)) {
3108c2ecf20Sopenharmony_ci				perror("ioctl recv");
3118c2ecf20Sopenharmony_ci				goto out_resp;
3128c2ecf20Sopenharmony_ci			}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci			if (handle_req(req, resp, listener) < 0)
3158c2ecf20Sopenharmony_ci				goto out_resp;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci			/*
3188c2ecf20Sopenharmony_ci			 * ENOENT here means that the task may have gotten a
3198c2ecf20Sopenharmony_ci			 * signal and restarted the syscall. It's up to the
3208c2ecf20Sopenharmony_ci			 * handler to decide what to do in this case, but for
3218c2ecf20Sopenharmony_ci			 * the sample code, we just ignore it. Probably
3228c2ecf20Sopenharmony_ci			 * something better should happen, like undoing the
3238c2ecf20Sopenharmony_ci			 * mount, or keeping track of the args to make sure we
3248c2ecf20Sopenharmony_ci			 * don't do it again.
3258c2ecf20Sopenharmony_ci			 */
3268c2ecf20Sopenharmony_ci			if (ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, resp) < 0 &&
3278c2ecf20Sopenharmony_ci			    errno != ENOENT) {
3288c2ecf20Sopenharmony_ci				perror("ioctl send");
3298c2ecf20Sopenharmony_ci				goto out_resp;
3308c2ecf20Sopenharmony_ci			}
3318c2ecf20Sopenharmony_ci		}
3328c2ecf20Sopenharmony_ciout_resp:
3338c2ecf20Sopenharmony_ci		free(resp);
3348c2ecf20Sopenharmony_ciout_req:
3358c2ecf20Sopenharmony_ci		free(req);
3368c2ecf20Sopenharmony_ciout_close:
3378c2ecf20Sopenharmony_ci		close(listener);
3388c2ecf20Sopenharmony_ci		exit(1);
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	close(listener);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (waitpid(worker, &status, 0) != worker) {
3448c2ecf20Sopenharmony_ci		perror("waitpid");
3458c2ecf20Sopenharmony_ci		goto out_kill;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (umount2("/tmp/foo", MNT_DETACH) < 0 && errno != EINVAL) {
3498c2ecf20Sopenharmony_ci		perror("umount2");
3508c2ecf20Sopenharmony_ci		goto out_kill;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (remove("/tmp/foo") < 0 && errno != ENOENT) {
3548c2ecf20Sopenharmony_ci		perror("remove");
3558c2ecf20Sopenharmony_ci		exit(1);
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (!WIFEXITED(status) || WEXITSTATUS(status)) {
3598c2ecf20Sopenharmony_ci		fprintf(stderr, "worker exited nonzero\n");
3608c2ecf20Sopenharmony_ci		goto out_kill;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	ret = 0;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ciout_kill:
3668c2ecf20Sopenharmony_ci	if (tracer > 0)
3678c2ecf20Sopenharmony_ci		kill(tracer, SIGKILL);
3688c2ecf20Sopenharmony_ci	if (worker > 0)
3698c2ecf20Sopenharmony_ci		kill(worker, SIGKILL);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ciclose_pair:
3728c2ecf20Sopenharmony_ci	close(sk_pair[0]);
3738c2ecf20Sopenharmony_ci	close(sk_pair[1]);
3748c2ecf20Sopenharmony_ci	return ret;
3758c2ecf20Sopenharmony_ci}
376