2 * Copyright (c) 2016 The FreeBSD Foundation
5 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/procctl.h>
52 str2pid(const char *str)
57 res = strtol(str, &tail, 0);
59 warnx("non-numeric pid");
66 #define KPTI_USAGE "|kpti"
75 fprintf(stderr, "Usage: proccontrol -m (aslr|trace|trapcap"
77 "[-s (enable|disable)] [-p pid | command]\n");
82 main(int argc, char *argv[])
84 int arg, ch, error, mode;
86 bool enable, do_command, query;
92 while ((ch = getopt(argc, argv, "m:qs:p:")) != -1) {
95 if (strcmp(optarg, "aslr") == 0)
97 else if (strcmp(optarg, "trace") == 0)
99 else if (strcmp(optarg, "trapcap") == 0)
102 else if (strcmp(optarg, "kpti") == 0)
109 if (strcmp(optarg, "enable") == 0)
111 else if (strcmp(optarg, "disable") == 0)
117 pid = str2pid(optarg);
130 do_command = argc != 0;
132 if (pid != -1 || query)
135 } else if (pid == -1) {
142 error = procctl(P_PID, pid, PROC_ASLR_STATUS, &arg);
145 error = procctl(P_PID, pid, PROC_TRACE_STATUS, &arg);
148 error = procctl(P_PID, pid, PROC_TRAPCAP_STATUS, &arg);
152 error = procctl(P_PID, pid, PROC_KPTI_STATUS, &arg);
160 err(1, "procctl status");
163 switch (arg & ~PROC_ASLR_ACTIVE) {
164 case PROC_ASLR_FORCE_ENABLE:
165 printf("force enabled");
167 case PROC_ASLR_FORCE_DISABLE:
168 printf("force disabled");
170 case PROC_ASLR_NOFORCE:
171 printf("not forced");
174 if ((arg & PROC_ASLR_ACTIVE) != 0)
175 printf(", active\n");
177 printf(", not active\n");
181 printf("disabled\n");
183 printf("enabled, no debugger\n");
185 printf("enabled, traced by %d\n", arg);
189 case PROC_TRAPCAP_CTL_ENABLE:
192 case PROC_TRAPCAP_CTL_DISABLE:
193 printf("disabled\n");
199 switch (arg & ~PROC_KPTI_STATUS_ACTIVE) {
200 case PROC_KPTI_CTL_ENABLE_ON_EXEC:
203 case PROC_KPTI_CTL_DISABLE_ON_EXEC:
207 if ((arg & PROC_KPTI_STATUS_ACTIVE) != 0)
208 printf(", active\n");
210 printf(", not active\n");
217 arg = enable ? PROC_ASLR_FORCE_ENABLE :
218 PROC_ASLR_FORCE_DISABLE;
219 error = procctl(P_PID, pid, PROC_ASLR_CTL, &arg);
222 arg = enable ? PROC_TRACE_CTL_ENABLE :
223 PROC_TRACE_CTL_DISABLE;
224 error = procctl(P_PID, pid, PROC_TRACE_CTL, &arg);
227 arg = enable ? PROC_TRAPCAP_CTL_ENABLE :
228 PROC_TRAPCAP_CTL_DISABLE;
229 error = procctl(P_PID, pid, PROC_TRAPCAP_CTL, &arg);
233 arg = enable ? PROC_KPTI_CTL_ENABLE_ON_EXEC :
234 PROC_KPTI_CTL_DISABLE_ON_EXEC;
235 error = procctl(P_PID, pid, PROC_KPTI_CTL, &arg);
243 err(1, "procctl ctl");
245 error = execvp(argv[0], argv);