clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name swapctl.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sbin/swapctl/obj -resource-dir /usr/local/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/sbin/swapctl/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/scan/2024-01-11-140451-98009-1 -x c /usr/src/sbin/swapctl/swapctl.c
| 1 |  | 
| 2 |  | 
| 3 |  | 
| 4 |  | 
| 5 |  | 
| 6 |  | 
| 7 |  | 
| 8 |  | 
| 9 |  | 
| 10 |  | 
| 11 |  | 
| 12 |  | 
| 13 |  | 
| 14 |  | 
| 15 |  | 
| 16 |  | 
| 17 |  | 
| 18 |  | 
| 19 |  | 
| 20 |  | 
| 21 |  | 
| 22 |  | 
| 23 |  | 
| 24 |  | 
| 25 |  | 
| 26 |  | 
| 27 |  | 
| 28 |  | 
| 29 |  | 
| 30 |  | 
| 31 |  | 
| 32 |  | 
| 33 |  | 
| 34 |  | 
| 35 |  | 
| 36 |  | 
| 37 |  | 
| 38 |  | 
| 39 |  | 
| 40 |  | 
| 41 |  | 
| 42 |  | 
| 43 |  | 
| 44 |  | 
| 45 |  | 
| 46 |  | 
| 47 |  | 
| 48 |  | 
| 49 |  | 
| 50 |  | 
| 51 | #include <sys/stat.h> | 
| 52 | #include <sys/swap.h> | 
| 53 | #include <sys/wait.h> | 
| 54 |  | 
| 55 | #include <unistd.h> | 
| 56 | #include <err.h> | 
| 57 | #include <errno.h> | 
| 58 | #include <stdio.h> | 
| 59 | #include <stdlib.h> | 
| 60 | #include <limits.h> | 
| 61 | #include <string.h> | 
| 62 | #include <fstab.h> | 
| 63 | #include <util.h> | 
| 64 |  | 
| 65 | #include "swapctl.h" | 
| 66 |  | 
| 67 | int	command; | 
| 68 |  | 
| 69 |  | 
| 70 |  | 
| 71 |  | 
| 72 | #define	CMD_A		0x01	/* process /etc/fstab */ | 
| 73 | #define	CMD_a		0x02	/* add a swap file/device */ | 
| 74 | #define	CMD_c		0x04	/* change priority of a swap file/device */ | 
| 75 | #define	CMD_d		0x08	/* delete a swap file/device */ | 
| 76 | #define	CMD_l		0x10	/* list swap files/devices */ | 
| 77 | #define	CMD_s		0x20	/* summary of swap files/devices */ | 
| 78 |  | 
| 79 | #define	SET_COMMAND(cmd) \ | 
| 80 | do { \ | 
| 81 | if (command) \ | 
| 82 | usage(); \ | 
| 83 | command = (cmd); \ | 
| 84 | } while (0) | 
| 85 |  | 
| 86 |  | 
| 87 |  | 
| 88 |  | 
| 89 |  | 
| 90 | #define	REQUIRE_PATH	(CMD_a | CMD_c | CMD_d) | 
| 91 | #define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s) | 
| 92 |  | 
| 93 |  | 
| 94 |  | 
| 95 |  | 
| 96 | int	kflag; | 
| 97 | #define	KFLAG_CMDS	(CMD_l | CMD_s) | 
| 98 |  | 
| 99 | int	pflag; | 
| 100 | #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c) | 
| 101 |  | 
| 102 | char	*tflag; | 
| 103 | #define	TFLAG_CMDS	(CMD_A) | 
| 104 |  | 
| 105 | int	pri; | 
| 106 |  | 
| 107 | static	void change_priority(char *); | 
| 108 | static	void add_swap(char *); | 
| 109 | static	void del_swap(char *); | 
| 110 | static	void do_fstab(void); | 
| 111 | static	void usage(void); | 
| 112 | static	int  swapon_command(int, char **); | 
| 113 |  | 
| 114 | extern	char *__progname; | 
| 115 |  | 
| 116 | int | 
| 117 | main(int argc, char *argv[]) | 
| 118 | { | 
| 119 | const char *errstr; | 
| 120 | int	c; | 
| 121 |  | 
| 122 | if (strcmp(__progname, "swapon") == 0) | 
|  | | 1 | Assuming the condition is false |  | 
 | 
|  |  | 
| 123 | return swapon_command(argc, argv); | 
| 124 |  | 
| 125 | while ((c = getopt(argc, argv, "Aacdlkp:st:")) != -1) { | 
|  | | 3 |  | Assuming the condition is false |  | 
 | 
|  | | 4 |  | Loop condition is false. Execution continues on line 174 |  | 
 | 
| 126 | switch (c) { | 
| 127 | case 'A': | 
| 128 | SET_COMMAND(CMD_A); | 
| 129 | break; | 
| 130 |  | 
| 131 | case 'a': | 
| 132 | SET_COMMAND(CMD_a); | 
| 133 | break; | 
| 134 |  | 
| 135 | case 'c': | 
| 136 | SET_COMMAND(CMD_c); | 
| 137 | break; | 
| 138 |  | 
| 139 | case 'd': | 
| 140 | SET_COMMAND(CMD_d); | 
| 141 | break; | 
| 142 |  | 
| 143 | case 'l': | 
| 144 | SET_COMMAND(CMD_l); | 
| 145 | break; | 
| 146 |  | 
| 147 | case 'k': | 
| 148 | kflag = 1; | 
| 149 | break; | 
| 150 |  | 
| 151 | case 'p': | 
| 152 | pflag = 1; | 
| 153 | pri = strtonum(optarg, 0, INT_MAX, &errstr); | 
| 154 | if (errstr) | 
| 155 | errx(1, "-p %s: %s", errstr, optarg); | 
| 156 | break; | 
| 157 |  | 
| 158 | case 's': | 
| 159 | SET_COMMAND(CMD_s); | 
| 160 | break; | 
| 161 |  | 
| 162 | case 't': | 
| 163 | if (tflag != NULL) | 
| 164 | usage(); | 
| 165 | tflag = optarg; | 
| 166 | break; | 
| 167 |  | 
| 168 | default: | 
| 169 | usage(); | 
| 170 |  | 
| 171 | } | 
| 172 | } | 
| 173 |  | 
| 174 | argv += optind; | 
| 175 | argc -= optind; | 
| 176 |  | 
| 177 |  | 
| 178 | if (command == 0) { | 
|  | | 5 |  | Assuming 'command' is not equal to 0 |  | 
 | 
|  |  | 
| 179 | if (argc == 0) | 
| 180 | SET_COMMAND(CMD_l); | 
| 181 | else | 
| 182 | usage(); | 
| 183 | } | 
| 184 |  | 
| 185 | switch (argc) { | 
|  | | 7 |  | Control jumps to 'case 0:'  at line 186 |  | 
 | 
| 186 | case 0: | 
| 187 | if (command & REQUIRE_PATH) | 
|  | | 8 |  | Assuming the condition is false |  | 
 | 
|  |  | 
| 188 | usage(); | 
| 189 | break; | 
| 190 |  | 
| 191 | case 1: | 
| 192 | if (command & REQUIRE_NOPATH) | 
| 193 | usage(); | 
| 194 | break; | 
| 195 |  | 
| 196 | default: | 
| 197 | usage(); | 
| 198 | } | 
| 199 |  | 
| 200 |  | 
| 201 | if ((command == CMD_c) && pflag == 0) | 
|  | | 10 |  | Assuming 'command' is not equal to CMD_c |  | 
 | 
| 202 | usage(); | 
| 203 |  | 
| 204 |  | 
| 205 | if (tflag != NULL) { | 
|  | | 11 |  | Assuming 'tflag' is equal to NULL |  | 
 | 
|  |  | 
| 206 | if (command != CMD_A) | 
| 207 | usage(); | 
| 208 | if (strcmp(tflag, "blk") != 0 && | 
| 209 | strcmp(tflag, "noblk") != 0) | 
| 210 | usage(); | 
| 211 | } | 
| 212 |  | 
| 213 |  | 
| 214 | switch (command) { | 
|  | | 13 |  | Control jumps to 'case 1:'  at line 235 |  | 
 | 
| 215 | case CMD_l: | 
| 216 | list_swap(pri, kflag, pflag, 1); | 
| 217 | break; | 
| 218 |  | 
| 219 | case CMD_s: | 
| 220 | list_swap(pri, kflag, pflag, 0); | 
| 221 | break; | 
| 222 |  | 
| 223 | case CMD_c: | 
| 224 | change_priority(argv[0]); | 
| 225 | break; | 
| 226 |  | 
| 227 | case CMD_a: | 
| 228 | add_swap(argv[0]); | 
| 229 | break; | 
| 230 |  | 
| 231 | case CMD_d: | 
| 232 | del_swap(argv[0]); | 
| 233 | break; | 
| 234 |  | 
| 235 | case CMD_A: | 
| 236 | do_fstab(); | 
|  |  | 
| 237 | break; | 
| 238 | } | 
| 239 |  | 
| 240 | return (0); | 
| 241 | } | 
| 242 |  | 
| 243 |  | 
| 244 |  | 
| 245 |  | 
| 246 | int | 
| 247 | swapon_command(int argc, char **argv) | 
| 248 | { | 
| 249 | int ch, fiztab = 0; | 
| 250 |  | 
| 251 | while ((ch = getopt(argc, argv, "at:")) != -1) { | 
| 252 | switch (ch) { | 
| 253 | case 'a': | 
| 254 | fiztab = 1; | 
| 255 | break; | 
| 256 | case 't': | 
| 257 | if (tflag != NULL) | 
| 258 | usage(); | 
| 259 | tflag = optarg; | 
| 260 | break; | 
| 261 | default: | 
| 262 | goto swapon_usage; | 
| 263 | } | 
| 264 | } | 
| 265 | argc -= optind; | 
| 266 | argv += optind; | 
| 267 |  | 
| 268 | if (fiztab) { | 
| 269 | if (argc) | 
| 270 | goto swapon_usage; | 
| 271 |  | 
| 272 | if (tflag != NULL) { | 
| 273 | if (strcmp(tflag, "blk") != 0 && | 
| 274 | strcmp(tflag, "noblk") != 0) | 
| 275 | usage(); | 
| 276 | } | 
| 277 | do_fstab(); | 
| 278 | return (0); | 
| 279 | } else if (argc == 0 || tflag != NULL) | 
| 280 | goto swapon_usage; | 
| 281 |  | 
| 282 | while (argc) { | 
| 283 | add_swap(argv[0]); | 
| 284 | argc--; | 
| 285 | argv++; | 
| 286 | } | 
| 287 | return (0); | 
| 288 |  | 
| 289 | swapon_usage: | 
| 290 | fprintf(stderr, "usage: %s -a | path\n", __progname); | 
| 291 | return (1); | 
| 292 | } | 
| 293 |  | 
| 294 |  | 
| 295 |  | 
| 296 |  | 
| 297 | void | 
| 298 | change_priority(char *path) | 
| 299 | { | 
| 300 |  | 
| 301 | if (swapctl(SWAP_CTL, path, pri) == -1) | 
| 302 | warn("%s", path); | 
| 303 | } | 
| 304 |  | 
| 305 |  | 
| 306 |  | 
| 307 |  | 
| 308 | void | 
| 309 | add_swap(char *path) | 
| 310 | { | 
| 311 |  | 
| 312 | if (swapctl(SWAP_ON, path, pri) == -1) | 
| 313 | if (errno != EBUSY) | 
| 314 | err(1, "%s", path); | 
| 315 | } | 
| 316 |  | 
| 317 |  | 
| 318 |  | 
| 319 |  | 
| 320 | void | 
| 321 | del_swap(char *path) | 
| 322 | { | 
| 323 |  | 
| 324 | if (swapctl(SWAP_OFF, path, pri) == -1) | 
| 325 | err(1, "%s", path); | 
| 326 | } | 
| 327 |  | 
| 328 | void | 
| 329 | do_fstab(void) | 
| 330 | { | 
| 331 | struct	fstab *fp; | 
| 332 | char	*s; | 
| 333 | long	priority; | 
| 334 | struct	stat st; | 
| 335 | mode_t	rejecttype = 0; | 
| 336 | int	gotone = 0; | 
| 337 |  | 
| 338 |  | 
| 339 |  | 
| 340 |  | 
| 341 |  | 
| 342 | if (tflag != NULL) { | 
|  |  | 
| 343 | if (strcmp(tflag, "blk") == 0) | 
| 344 | rejecttype = S_IFREG; | 
| 345 | else if (strcmp(tflag, "noblk") == 0) | 
| 346 | rejecttype = S_IFBLK; | 
| 347 | } | 
| 348 |  | 
| 349 | #define PRIORITYEQ	"priority=" | 
| 350 | #define NFSMNTPT	"nfsmntpt=" | 
| 351 | #define PATH_MOUNT	"/sbin/mount_nfs" | 
| 352 | while ((fp = getfsent()) != NULL) { | 
|  | | 16 |  | Assuming the condition is true |  | 
 | 
|  | | 17 |  | Loop condition is true.  Entering loop body |  | 
 | 
| 353 | const char *spec; | 
| 354 |  | 
| 355 | if (strcmp(fp->fs_type, "sw") != 0) | 
|  |  | 
| 356 | continue; | 
| 357 |  | 
| 358 | spec = fp->fs_spec; | 
| 359 |  | 
| 360 | if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) { | 
|  | | 19 |  | Assuming the condition is false |  | 
 | 
|  |  | 
| 361 | s += sizeof(PRIORITYEQ) - 1; | 
| 362 | priority = atol(s); | 
| 363 | } else | 
| 364 | priority = pri; | 
| 365 |  | 
| 366 | if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) { | 
|  | | 21 |  | Assuming the condition is true |  | 
 | 
|  |  | 
| 367 | char *t; | 
| 368 | pid_t pid; | 
| 369 | int status; | 
| 370 |  | 
| 371 |  | 
| 372 |  | 
| 373 |  | 
| 374 |  | 
| 375 | if (rejecttype == S_IFREG) | 
|  |  | 
| 376 | continue; | 
| 377 |  | 
| 378 | t = strpbrk(s, ","); | 
| 379 | if (t != 0) | 
|  | | 24 |  | Assuming 't' is equal to null |  | 
 | 
|  |  | 
| 380 | *t = '\0'; | 
| 381 | spec = strdup(s + strlen(NFSMNTPT)); | 
| 382 | if (spec == NULL) | 
|  | | 26 |  | Assuming 'spec' is not equal to NULL |  | 
 | 
|  |  | 
| 383 | err(1, "strdup"); | 
| 384 |  | 
| 385 | if (t != 0) | 
|  |  | 
| 386 | *t = ','; | 
| 387 |  | 
| 388 | if (strlen(spec) == 0) { | 
|  | | 29 |  | Assuming the condition is false |  | 
 | 
|  |  | 
| 389 | warnx("empty mountpoint"); | 
| 390 | free((char *)spec); | 
| 391 | continue; | 
| 392 | } | 
| 393 |  | 
| 394 | switch (pid = vfork()) { | 
|  | | 31 |  | Control jumps to 'case 0:'  at line 397 |  | 
 | 
| 395 | case -1: | 
| 396 | err(1, "vfork"); | 
| 397 | case 0: | 
| 398 | execl(PATH_MOUNT, PATH_MOUNT, fp->fs_spec, spec, | 
| 399 | (char *)NULL); | 
| 400 | err(1, "execl"); | 
|  | | 32 |  | This function call is prohibited after a successful vfork | 
 | 
| 401 | } | 
| 402 | while (waitpid(pid, &status, 0) == -1) | 
| 403 | if (errno != EINTR) | 
| 404 | err(1, "waitpid"); | 
| 405 | if (status != 0) { | 
| 406 | warnx("%s: mount failed", fp->fs_spec); | 
| 407 | free((char *)spec); | 
| 408 | continue; | 
| 409 | } | 
| 410 | } else if (isduid(spec, 0)) { | 
| 411 | if (rejecttype == S_IFBLK) | 
| 412 | continue; | 
| 413 | } else { | 
| 414 |  | 
| 415 |  | 
| 416 |  | 
| 417 |  | 
| 418 | if (rejecttype == S_IFREG) { | 
| 419 | if (strncmp("/dev/", spec, 5) != 0) | 
| 420 | continue; | 
| 421 | } | 
| 422 | if (stat(spec, &st) == -1) { | 
| 423 | warn("%s", spec); | 
| 424 | continue; | 
| 425 | } | 
| 426 | if ((st.st_mode & S_IFMT) == rejecttype) | 
| 427 | continue; | 
| 428 |  | 
| 429 |  | 
| 430 |  | 
| 431 |  | 
| 432 | if (!S_ISREG(st.st_mode) && | 
| 433 | !S_ISBLK(st.st_mode)) | 
| 434 | continue; | 
| 435 | } | 
| 436 |  | 
| 437 | if (swapctl(SWAP_ON, spec, (int)priority) == -1) { | 
| 438 | if (errno != EBUSY) | 
| 439 | warn("%s", spec); | 
| 440 | } else { | 
| 441 | gotone = 1; | 
| 442 | printf("%s: adding %s as swap device at priority %d\n", | 
| 443 | __progname, fp->fs_spec, (int)priority); | 
| 444 | } | 
| 445 |  | 
| 446 | if (spec != fp->fs_spec) | 
| 447 | free((char *)spec); | 
| 448 | } | 
| 449 | if (gotone == 0) | 
| 450 | exit(1); | 
| 451 | } | 
| 452 |  | 
| 453 | void | 
| 454 | usage(void) | 
| 455 | { | 
| 456 |  | 
| 457 | fprintf(stderr, "usage: %s -A [-p priority] [-t blk | noblk]\n", | 
| 458 | __progname); | 
| 459 | fprintf(stderr, "       %s -a [-p priority] path\n", __progname); | 
| 460 | fprintf(stderr, "       %s -c -p priority path\n", __progname); | 
| 461 | fprintf(stderr, "       %s -d path\n", __progname); | 
| 462 | fprintf(stderr, "       %s [[-l] | -s] [-k]\n", __progname); | 
| 463 | exit(1); | 
| 464 | } |