1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-10-18 02:19:39 +00:00

proccontrol: use a table for modes

Add a central table of modes and loop over it rather than spelling out
10 essentialy identical strcmp if statemnts.  Use the stable to generate
usage as well reducing the number of ifdefs.

Disallow multiple -m options.  Previouly multiple were allowed, but only
the last one was used and there was no indication this happened.

Reviewed by:	kib, markj
Differential Revision:	https://reviews.freebsd.org/D46426
This commit is contained in:
Brooks Davis 2024-09-12 12:35:04 +01:00
parent 3acf7e0da4
commit 7ca260df8c

View File

@ -26,7 +26,7 @@
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/procctl.h>
#include <err.h>
#include <stdbool.h>
@ -35,9 +35,9 @@
#include <string.h>
#include <unistd.h>
enum {
MODE_ASLR,
enum mode {
MODE_INVALID,
MODE_ASLR,
MODE_TRACE,
MODE_TRAPCAP,
MODE_PROTMAX,
@ -53,6 +53,26 @@ enum {
#endif
};
static const struct {
enum mode mode;
const char *name;
} modes[] = {
{ MODE_ASLR, "aslr" },
{ MODE_TRACE, "trace" },
{ MODE_TRAPCAP, "trapcap" },
{ MODE_PROTMAX, "protmax" },
{ MODE_STACKGAP, "stackgap" },
{ MODE_NO_NEW_PRIVS, "nonewprivs" },
{ MODE_WXMAP, "wxmap" },
#ifdef PROC_KPTI_CTL
{ MODE_KPTI, "kpti" },
#endif
#ifdef PROC_LA_CTL
{ MODE_LA57, "la57" },
{ MODE_LA48, "la48" },
#endif
};
static pid_t
str2pid(const char *str)
{
@ -67,17 +87,6 @@ str2pid(const char *str)
return (res);
}
#ifdef PROC_KPTI_CTL
#define KPTI_USAGE "|kpti"
#else
#define KPTI_USAGE
#endif
#ifdef PROC_LA_CTL
#define LA_USAGE "|la48|la57"
#else
#define LA_USAGE
#endif
static void __dead2
usage(void)
{
@ -85,10 +94,10 @@ usage(void)
fprintf(stderr, " proccontrol -m mode -s (enable|disable) "
"(-p pid | command)\n");
fprintf(stderr, " proccontrol -m mode -q [-p pid]\n");
fprintf(stderr, "Modes: "
"aslr|protmax|trace|trapcap|stackgap|nonewprivs|wxmap"
KPTI_USAGE LA_USAGE
"\n");
fprintf(stderr, "Modes: ");
for (size_t i = 0; i < nitems(modes); i++)
fprintf(stderr, "%s%s", i == 0 ? "" : "|", modes[i].name);
fprintf(stderr, "\n");
exit(1);
}
@ -106,31 +115,15 @@ main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "m:qs:p:")) != -1) {
switch (ch) {
case 'm':
if (strcmp(optarg, "aslr") == 0)
mode = MODE_ASLR;
else if (strcmp(optarg, "protmax") == 0)
mode = MODE_PROTMAX;
else if (strcmp(optarg, "trace") == 0)
mode = MODE_TRACE;
else if (strcmp(optarg, "trapcap") == 0)
mode = MODE_TRAPCAP;
else if (strcmp(optarg, "stackgap") == 0)
mode = MODE_STACKGAP;
else if (strcmp(optarg, "nonewprivs") == 0)
mode = MODE_NO_NEW_PRIVS;
else if (strcmp(optarg, "wxmap") == 0)
mode = MODE_WXMAP;
#ifdef PROC_KPTI_CTL
else if (strcmp(optarg, "kpti") == 0)
mode = MODE_KPTI;
#endif
#ifdef PROC_LA_CTL
else if (strcmp(optarg, "la57") == 0)
mode = MODE_LA57;
else if (strcmp(optarg, "la48") == 0)
mode = MODE_LA48;
#endif
else
if (mode != MODE_INVALID)
usage();
for (size_t i = 0; i < nitems(modes); i++) {
if (strcmp(optarg, modes[i].name) == 0) {
mode = modes[i].mode;
break;
}
}
if (mode == MODE_INVALID)
usage();
break;
case 's':