1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010 The FreeBSD Foundation 5 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 6 * All rights reserved. 7 * 8 * This software was developed by Pawel Jakub Dawidek under sponsorship from 9 * the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/types.h> 37 #include <sys/sysctl.h> 38 #include <sys/wait.h> 39 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <paths.h> 43 #include <signal.h> 44 #include <stdbool.h> 45 #include <stdint.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <syslog.h> 50 #include <unistd.h> 51 52 #include <pjdlog.h> 53 54 #include "hooks.h" 55 #include "subr.h" 56 #include "synch.h" 57 58 /* Report processes that are running for too long not often than this value. */ 59 #define REPORT_INTERVAL 60 60 61 /* Are we initialized? */ 62 static bool hooks_initialized = false; 63 64 /* 65 * Keep all processes we forked on a global queue, so we can report nicely 66 * when they finish or report that they are running for a long time. 67 */ 68 #define HOOKPROC_MAGIC_ALLOCATED 0x80090ca 69 #define HOOKPROC_MAGIC_ONLIST 0x80090c0 70 struct hookproc { 71 /* Magic. */ 72 int hp_magic; 73 /* PID of a forked child. */ 74 pid_t hp_pid; 75 /* When process were forked? */ 76 time_t hp_birthtime; 77 /* When we logged previous reported? */ 78 time_t hp_lastreport; 79 /* Path to executable and all the arguments we passed. */ 80 char hp_comm[PATH_MAX]; 81 TAILQ_ENTRY(hookproc) hp_next; 82 }; 83 static TAILQ_HEAD(, hookproc) hookprocs; 84 static pthread_mutex_t hookprocs_lock; 85 86 static void hook_remove(struct hookproc *hp); 87 static void hook_free(struct hookproc *hp); 88 89 static void 90 descriptors(void) 91 { 92 int fd; 93 94 /* 95 * Close all (or almost all) descriptors. 96 */ 97 if (pjdlog_mode_get() == PJDLOG_MODE_STD) { 98 closefrom(MAX(MAX(STDIN_FILENO, STDOUT_FILENO), 99 STDERR_FILENO) + 1); 100 return; 101 } 102 103 closefrom(0); 104 105 /* 106 * Redirect stdin, stdout and stderr to /dev/null. 107 */ 108 fd = open(_PATH_DEVNULL, O_RDONLY); 109 if (fd == -1) { 110 pjdlog_errno(LOG_WARNING, "Unable to open %s for reading", 111 _PATH_DEVNULL); 112 } else if (fd != STDIN_FILENO) { 113 if (dup2(fd, STDIN_FILENO) == -1) { 114 pjdlog_errno(LOG_WARNING, 115 "Unable to duplicate descriptor for stdin"); 116 } 117 close(fd); 118 } 119 fd = open(_PATH_DEVNULL, O_WRONLY); 120 if (fd == -1) { 121 pjdlog_errno(LOG_WARNING, "Unable to open %s for writing", 122 _PATH_DEVNULL); 123 } else { 124 if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) == -1) { 125 pjdlog_errno(LOG_WARNING, 126 "Unable to duplicate descriptor for stdout"); 127 } 128 if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) == -1) { 129 pjdlog_errno(LOG_WARNING, 130 "Unable to duplicate descriptor for stderr"); 131 } 132 if (fd != STDOUT_FILENO && fd != STDERR_FILENO) 133 close(fd); 134 } 135 } 136 137 void 138 hook_init(void) 139 { 140 141 PJDLOG_ASSERT(!hooks_initialized); 142 143 mtx_init(&hookprocs_lock); 144 TAILQ_INIT(&hookprocs); 145 hooks_initialized = true; 146 } 147 148 void 149 hook_fini(void) 150 { 151 struct hookproc *hp; 152 153 PJDLOG_ASSERT(hooks_initialized); 154 155 mtx_lock(&hookprocs_lock); 156 while ((hp = TAILQ_FIRST(&hookprocs)) != NULL) { 157 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST); 158 PJDLOG_ASSERT(hp->hp_pid > 0); 159 160 hook_remove(hp); 161 hook_free(hp); 162 } 163 mtx_unlock(&hookprocs_lock); 164 165 mtx_destroy(&hookprocs_lock); 166 TAILQ_INIT(&hookprocs); 167 hooks_initialized = false; 168 } 169 170 static struct hookproc * 171 hook_alloc(const char *path, char **args) 172 { 173 struct hookproc *hp; 174 unsigned int ii; 175 176 hp = malloc(sizeof(*hp)); 177 if (hp == NULL) { 178 pjdlog_error("Unable to allocate %zu bytes of memory for a hook.", 179 sizeof(*hp)); 180 return (NULL); 181 } 182 183 hp->hp_pid = 0; 184 hp->hp_birthtime = hp->hp_lastreport = time(NULL); 185 (void)strlcpy(hp->hp_comm, path, sizeof(hp->hp_comm)); 186 /* We start at 2nd argument as we don't want to have exec name twice. */ 187 for (ii = 1; args[ii] != NULL; ii++) { 188 (void)snprlcat(hp->hp_comm, sizeof(hp->hp_comm), " %s", 189 args[ii]); 190 } 191 if (strlen(hp->hp_comm) >= sizeof(hp->hp_comm) - 1) { 192 pjdlog_error("Exec path too long, correct configuration file."); 193 free(hp); 194 return (NULL); 195 } 196 hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED; 197 return (hp); 198 } 199 200 static void 201 hook_add(struct hookproc *hp, pid_t pid) 202 { 203 204 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED); 205 PJDLOG_ASSERT(hp->hp_pid == 0); 206 207 hp->hp_pid = pid; 208 mtx_lock(&hookprocs_lock); 209 hp->hp_magic = HOOKPROC_MAGIC_ONLIST; 210 TAILQ_INSERT_TAIL(&hookprocs, hp, hp_next); 211 mtx_unlock(&hookprocs_lock); 212 } 213 214 static void 215 hook_remove(struct hookproc *hp) 216 { 217 218 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST); 219 PJDLOG_ASSERT(hp->hp_pid > 0); 220 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock)); 221 222 TAILQ_REMOVE(&hookprocs, hp, hp_next); 223 hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED; 224 } 225 226 static void 227 hook_free(struct hookproc *hp) 228 { 229 230 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED); 231 PJDLOG_ASSERT(hp->hp_pid > 0); 232 233 hp->hp_magic = 0; 234 free(hp); 235 } 236 237 static struct hookproc * 238 hook_find(pid_t pid) 239 { 240 struct hookproc *hp; 241 242 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock)); 243 244 TAILQ_FOREACH(hp, &hookprocs, hp_next) { 245 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST); 246 PJDLOG_ASSERT(hp->hp_pid > 0); 247 248 if (hp->hp_pid == pid) 249 break; 250 } 251 252 return (hp); 253 } 254 255 void 256 hook_check_one(pid_t pid, int status) 257 { 258 struct hookproc *hp; 259 260 mtx_lock(&hookprocs_lock); 261 hp = hook_find(pid); 262 if (hp == NULL) { 263 mtx_unlock(&hookprocs_lock); 264 pjdlog_debug(1, "Unknown process pid=%u", pid); 265 return; 266 } 267 hook_remove(hp); 268 mtx_unlock(&hookprocs_lock); 269 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 270 pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).", 271 pid, hp->hp_comm); 272 } else if (WIFSIGNALED(status)) { 273 pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).", 274 pid, WTERMSIG(status), hp->hp_comm); 275 } else { 276 pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).", 277 pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1, 278 hp->hp_comm); 279 } 280 hook_free(hp); 281 } 282 283 void 284 hook_check(void) 285 { 286 struct hookproc *hp, *hp2; 287 time_t now; 288 289 PJDLOG_ASSERT(hooks_initialized); 290 291 pjdlog_debug(2, "Checking hooks."); 292 293 /* 294 * Report about processes that are running for a long time. 295 */ 296 now = time(NULL); 297 mtx_lock(&hookprocs_lock); 298 TAILQ_FOREACH_SAFE(hp, &hookprocs, hp_next, hp2) { 299 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST); 300 PJDLOG_ASSERT(hp->hp_pid > 0); 301 302 /* 303 * If process doesn't exists we somehow missed it. 304 * Not much can be done expect for logging this situation. 305 */ 306 if (kill(hp->hp_pid, 0) == -1 && errno == ESRCH) { 307 pjdlog_warning("Hook disappeared (pid=%u, cmd=[%s]).", 308 hp->hp_pid, hp->hp_comm); 309 hook_remove(hp); 310 hook_free(hp); 311 continue; 312 } 313 314 /* 315 * Skip processes younger than 1 minute. 316 */ 317 if (now - hp->hp_lastreport < REPORT_INTERVAL) 318 continue; 319 320 /* 321 * Hook is running for too long, report it. 322 */ 323 pjdlog_warning("Hook is running for %ju seconds (pid=%u, cmd=[%s]).", 324 (uintmax_t)(now - hp->hp_birthtime), hp->hp_pid, 325 hp->hp_comm); 326 hp->hp_lastreport = now; 327 } 328 mtx_unlock(&hookprocs_lock); 329 } 330 331 void 332 hook_exec(const char *path, ...) 333 { 334 va_list ap; 335 336 va_start(ap, path); 337 hook_execv(path, ap); 338 va_end(ap); 339 } 340 341 void 342 hook_execv(const char *path, va_list ap) 343 { 344 struct hookproc *hp; 345 char *args[64]; 346 unsigned int ii; 347 sigset_t mask; 348 pid_t pid; 349 350 PJDLOG_ASSERT(hooks_initialized); 351 352 if (path == NULL || path[0] == '\0') 353 return; 354 355 memset(args, 0, sizeof(args)); 356 args[0] = __DECONST(char *, path); 357 for (ii = 1; ii < sizeof(args) / sizeof(args[0]); ii++) { 358 args[ii] = va_arg(ap, char *); 359 if (args[ii] == NULL) 360 break; 361 } 362 PJDLOG_ASSERT(ii < sizeof(args) / sizeof(args[0])); 363 364 hp = hook_alloc(path, args); 365 if (hp == NULL) 366 return; 367 368 pjdlog_debug(1, "Executing hook: %s", hp->hp_comm); 369 370 pid = fork(); 371 switch (pid) { 372 case -1: /* Error. */ 373 pjdlog_errno(LOG_ERR, "Unable to fork to execute %s", path); 374 hook_free(hp); 375 return; 376 case 0: /* Child. */ 377 descriptors(); 378 PJDLOG_VERIFY(sigemptyset(&mask) == 0); 379 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); 380 /* 381 * Dummy handler set for SIGCHLD in the parent will be restored 382 * to SIG_IGN on execv(3) below, so there is no need to do 383 * anything with it. 384 */ 385 execv(path, args); 386 pjdlog_errno(LOG_ERR, "Unable to execute %s", path); 387 exit(EX_SOFTWARE); 388 default: /* Parent. */ 389 hook_add(hp, pid); 390 break; 391 } 392 } 393