1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 Hudson River Trading LLC 5 * Written by: John H. Baldwin <jhb@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #include <sys/procctl.h> 32 #include <sys/types.h> 33 #include <sys/mman.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <stdbool.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <unistd.h> 40 41 static void 42 usage(void) 43 { 44 45 fprintf(stderr, "usage: protect [-i] command\n"); 46 fprintf(stderr, " protect [-cdi] -g pgrp | -p pid\n"); 47 exit(1); 48 } 49 50 static id_t 51 parse_id(char *id) 52 { 53 static bool first = true; 54 long value; 55 char *ch; 56 57 if (!first) { 58 warnx("only one -g or -p flag is permitted"); 59 usage(); 60 } 61 value = strtol(id, &ch, 0); 62 if (*ch != '\0') { 63 warnx("invalid process id"); 64 usage(); 65 } 66 return (value); 67 } 68 69 int 70 main(int argc, char *argv[]) 71 { 72 idtype_t idtype; 73 id_t id; 74 int ch, flags; 75 bool descend, inherit, idset; 76 77 idtype = P_PID; 78 id = getpid(); 79 flags = PPROT_SET; 80 descend = inherit = idset = false; 81 while ((ch = getopt(argc, argv, "cdig:p:")) != -1) 82 switch (ch) { 83 case 'c': 84 flags = PPROT_CLEAR; 85 break; 86 case 'd': 87 descend = true; 88 break; 89 case 'i': 90 inherit = true; 91 break; 92 case 'g': 93 idtype = P_PGID; 94 id = parse_id(optarg); 95 idset = true; 96 break; 97 case 'p': 98 idtype = P_PID; 99 id = parse_id(optarg); 100 idset = true; 101 break; 102 } 103 argc -= optind; 104 argv += optind; 105 106 if ((idset && argc != 0) || (!idset && (argc == 0 || descend))) 107 usage(); 108 109 if (descend) 110 flags |= PPROT_DESCEND; 111 if (inherit) 112 flags |= PPROT_INHERIT; 113 if (procctl(idtype, id, PROC_SPROTECT, &flags) == -1) 114 err(1, "procctl"); 115 116 if (argc != 0) { 117 errno = 0; 118 execvp(*argv, argv); 119 err(errno == ENOENT ? 127 : 126, "%s", *argv); 120 } 121 return (0); 122 } 123