clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name parse.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/usr.sbin/dhcpd/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/usr.sbin/dhcpd/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/usr.sbin/dhcpd/parse.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 | #include <sys/types.h> |
| 44 | #include <sys/socket.h> |
| 45 | |
| 46 | #include <net/if.h> |
| 47 | |
| 48 | #include <netinet/in.h> |
| 49 | #include <netinet/if_ether.h> |
| 50 | |
| 51 | #include <ctype.h> |
| 52 | #include <errno.h> |
| 53 | #include <stdarg.h> |
| 54 | #include <stdint.h> |
| 55 | #include <stdio.h> |
| 56 | #include <stdlib.h> |
| 57 | #include <string.h> |
| 58 | #include <syslog.h> |
| 59 | #include <time.h> |
| 60 | #include <unistd.h> |
| 61 | |
| 62 | #include "dhcp.h" |
| 63 | #include "tree.h" |
| 64 | #include "dhcpd.h" |
| 65 | #include "dhctoken.h" |
| 66 | #include "log.h" |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | void |
| 84 | skip_to_semi(FILE *cfile) |
| 85 | { |
| 86 | int token; |
| 87 | char *val; |
| 88 | int brace_count = 0; |
| 89 | |
| 90 | do { |
| 91 | token = peek_token(&val, cfile); |
| 92 | if (token == '}') { |
| 93 | if (brace_count) { |
| 94 | token = next_token(&val, cfile); |
| 95 | if (!--brace_count) |
| 96 | return; |
| 97 | } else |
| 98 | return; |
| 99 | } else if (token == '{') { |
| 100 | brace_count++; |
| 101 | } else if (token == ';' && !brace_count) { |
| 102 | token = next_token(&val, cfile); |
| 103 | return; |
| 104 | } else if (token == '\n') { |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | token = next_token(&val, cfile); |
| 112 | return; |
| 113 | } |
| 114 | token = next_token(&val, cfile); |
| 115 | } while (token != EOF); |
| 116 | } |
| 117 | |
| 118 | int |
| 119 | parse_semi(FILE *cfile) |
| 120 | { |
| 121 | int token; |
| 122 | char *val; |
| 123 | |
| 124 | token = next_token(&val, cfile); |
| 125 | if (token != ';') { |
| 126 | parse_warn("semicolon expected."); |
| 127 | skip_to_semi(cfile); |
| 128 | return (0); |
| 129 | } |
| 130 | return (1); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | char * |
| 137 | parse_string(FILE *cfile) |
| 138 | { |
| 139 | char *val, *s; |
| 140 | int token; |
| 141 | |
| 142 | token = next_token(&val, cfile); |
| 143 | if (token != TOK_STRING) { |
| 144 | parse_warn("filename must be a string"); |
| 145 | skip_to_semi(cfile); |
| 146 | return (NULL); |
| 147 | } |
| 148 | s = strdup(val); |
| 149 | if (s == NULL) |
| 150 | fatalx("no memory for string %s.", val); |
| 151 | |
| 152 | if (!parse_semi(cfile)) { |
| 153 | free(s); |
| 154 | return (NULL); |
| 155 | } |
| 156 | return (s); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | char * |
| 163 | parse_host_name(FILE *cfile) |
| 164 | { |
| 165 | char *val, *s, *t; |
| 166 | int token, len = 0; |
| 167 | pair c = NULL; |
| 168 | |
| 169 | |
| 170 | do { |
| 171 | |
| 172 | token = next_token(&val, cfile); |
| 173 | if (!is_identifier(token)) { |
| 174 | parse_warn("expecting an identifier in hostname"); |
| 175 | skip_to_semi(cfile); |
| 176 | return (NULL); |
| 177 | } |
| 178 | |
| 179 | s = strdup(val); |
| 180 | if (s == NULL) |
| 181 | fatalx("can't allocate temp space for hostname."); |
| 182 | c = cons((caddr_t) s, c); |
| 183 | len += strlen(s) + 1; |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | token = peek_token(&val, cfile); |
| 189 | if (token == '.') |
| 190 | token = next_token(&val, cfile); |
| 191 | } while (token == '.'); |
| 192 | |
| 193 | |
| 194 | if (!(s = malloc(len))) |
| 195 | fatalx("can't allocate space for hostname."); |
| 196 | t = s + len; |
| 197 | *--t = '\0'; |
| 198 | while (c) { |
| 199 | pair cdr = c->cdr; |
| 200 | int l = strlen((char *)c->car); |
| 201 | |
| 202 | t -= l; |
| 203 | memcpy(t, (char *)c->car, l); |
| 204 | |
| 205 | free(c->car); |
| 206 | free(c); |
| 207 | c = cdr; |
| 208 | if (t != s) |
| 209 | *--t = '.'; |
| 210 | } |
| 211 | return (s); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | void |
| 219 | parse_hardware_param(FILE *cfile, struct hardware *hardware) |
| 220 | { |
| 221 | char *val; |
| 222 | int token, hlen = 0; |
| 223 | unsigned char *e, *t = NULL; |
| 224 | |
| 225 | token = next_token(&val, cfile); |
| 226 | switch (token) { |
| 1 | Control jumps to 'case 261:' at line 227 | |
|
| 227 | case TOK_ETHERNET: |
| 228 | hardware->htype = HTYPE_ETHER; |
| 229 | break; |
| 2 | | Execution continues on line 241 | |
|
| 230 | case TOK_IPSEC_TUNNEL: |
| 231 | hardware->htype = HTYPE_IPSEC_TUNNEL; |
| 232 | break; |
| 233 | default: |
| 234 | parse_warn("expecting a network hardware type"); |
| 235 | skip_to_semi(cfile); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | if (hardware->htype == HTYPE_ETHER) { |
| |
| 242 | token = peek_token(&val, cfile); |
| 243 | hlen = sizeof(struct ether_addr); |
| 244 | if ((e = malloc(hlen)) == NULL) |
| 4 | | Assuming the condition is false | |
|
| |
| 245 | fatalx("can't allocate space for ethernet address."); |
| 246 | if (ether_hostton(val, (struct ether_addr *)e) == 0) { |
| 6 | | Assuming the condition is false | |
|
| |
| 247 | (void)next_token(&val, cfile); |
| 248 | t = e; |
| 249 | } else |
| 250 | free(e); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | if (!t) |
| |
| 263 | t = parse_numeric_aggregate(cfile, NULL, &hlen, ':', 16, 8); |
| 9 | | Calling 'parse_numeric_aggregate' | |
|
| 264 | |
| 265 | if (!t) |
| 266 | return; |
| 267 | if (hlen > sizeof(hardware->haddr)) { |
| 268 | free(t); |
| 269 | parse_warn("hardware address too long"); |
| 270 | } else { |
| 271 | hardware->hlen = hlen; |
| 272 | memcpy((unsigned char *)&hardware->haddr[0], t, |
| 273 | hardware->hlen); |
| 274 | if (hlen < sizeof(hardware->haddr)) |
| 275 | memset(&hardware->haddr[hlen], 0, |
| 276 | sizeof(hardware->haddr) - hlen); |
| 277 | free(t); |
| 278 | } |
| 279 | |
| 280 | token = next_token(&val, cfile); |
| 281 | if (token != ';') { |
| 282 | parse_warn("expecting semicolon."); |
| 283 | skip_to_semi(cfile); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | void |
| 291 | parse_lease_time(FILE *cfile, time_t *timep) |
| 292 | { |
| 293 | const char *errstr; |
| 294 | char *val; |
| 295 | uint32_t value; |
| 296 | |
| 297 | next_token(&val, cfile); |
| 298 | |
| 299 | value = strtonum(val, 0, UINT32_MAX, &errstr); |
| 300 | if (errstr) { |
| 301 | parse_warn("lease time is %s: %s", errstr, val); |
| 302 | skip_to_semi(cfile); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | *timep = value; |
| 307 | |
| 308 | parse_semi(cfile); |
| 309 | } |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | unsigned char * |
| 320 | parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max, |
| 321 | int separator, int base, int size) |
| 322 | { |
| 323 | char *val, *t; |
| 324 | int token, count = 0; |
| 325 | unsigned char *bufp = buf, *s = NULL; |
| 326 | pair c = NULL; |
| 327 | |
| 328 | if (!bufp && *max) { |
| |
| 329 | bufp = malloc(*max * size / 8); |
| |
| 330 | if (!bufp) |
| 12 | | Assuming 'bufp' is non-null | |
|
| |
| 331 | fatalx("can't allocate space for numeric aggregate"); |
| 332 | } else |
| 333 | s = bufp; |
| 334 | |
| 335 | do { |
| 336 | if (count) { |
| |
| 337 | token = peek_token(&val, cfile); |
| 338 | if (token != separator) { |
| 339 | if (!*max) |
| 340 | break; |
| 341 | if (token != '{' && token != '}') |
| 342 | token = next_token(&val, cfile); |
| 343 | parse_warn("too few numbers."); |
| 344 | if (token != ';') |
| 345 | skip_to_semi(cfile); |
| 346 | return (NULL); |
| 347 | } |
| 348 | token = next_token(&val, cfile); |
| 349 | } |
| 350 | token = next_token(&val, cfile); |
| 351 | |
| 352 | if (token == EOF) { |
| 15 | | Assuming the condition is false | |
|
| 353 | parse_warn("unexpected end of file"); |
| 354 | break; |
| 355 | } |
| 356 | if (token != TOK_NUMBER && token != TOK_NUMBER_OR_NAME) { |
| 16 | | Assuming 'token' is not equal to TOK_NUMBER | |
|
| 17 | | Assuming 'token' is not equal to TOK_NUMBER_OR_NAME | |
|
| |
| 357 | parse_warn("expecting numeric value."); |
| 19 | | Potential leak of memory pointed to by 'bufp' |
|
| 358 | skip_to_semi(cfile); |
| 359 | return (NULL); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | if (s) { |
| 366 | convert_num(s, val, base, size); |
| 367 | s += size / 8; |
| 368 | } else { |
| 369 | t = strdup(val); |
| 370 | if (t == NULL) |
| 371 | fatalx("no temp space for number."); |
| 372 | c = cons(t, c); |
| 373 | } |
| 374 | } while (++count != *max); |
| 375 | |
| 376 | |
| 377 | if (c) { |
| 378 | bufp = malloc(count * size / 8); |
| 379 | if (!bufp) |
| 380 | fatalx("can't allocate space for numeric aggregate."); |
| 381 | s = bufp + count - size / 8; |
| 382 | *max = count; |
| 383 | } |
| 384 | while (c) { |
| 385 | pair cdr = c->cdr; |
| 386 | convert_num(s, (char *)c->car, base, size); |
| 387 | s -= size / 8; |
| 388 | |
| 389 | free(c->car); |
| 390 | free(c); |
| 391 | c = cdr; |
| 392 | } |
| 393 | return (bufp); |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | convert_num(unsigned char *buf, char *str, int base, int size) |
| 398 | { |
| 399 | int negative = 0, tval, max; |
| 400 | u_int32_t val = 0; |
| 401 | char *ptr = str; |
| 402 | |
| 403 | if (*ptr == '-') { |
| 404 | negative = 1; |
| 405 | ptr++; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | if (!base) { |
| 410 | if (ptr[0] == '0') { |
| 411 | if (ptr[1] == 'x') { |
| 412 | base = 16; |
| 413 | ptr += 2; |
| 414 | } else if (isascii((unsigned char)ptr[1]) && |
| 415 | isdigit((unsigned char)ptr[1])) { |
| 416 | base = 8; |
| 417 | ptr += 1; |
| 418 | } else |
| 419 | base = 10; |
| 420 | } else |
| 421 | base = 10; |
| 422 | } |
| 423 | |
| 424 | do { |
| 425 | tval = *ptr++; |
| 426 | |
| 427 | if (tval >= 'a') |
| 428 | tval = tval - 'a' + 10; |
| 429 | else if (tval >= 'A') |
| 430 | tval = tval - 'A' + 10; |
| 431 | else if (tval >= '0') |
| 432 | tval -= '0'; |
| 433 | else { |
| 434 | log_warnx("Bogus number: %s.", str); |
| 435 | break; |
| 436 | } |
| 437 | if (tval >= base) { |
| 438 | log_warnx("Bogus number: %s: digit %d not in base %d", |
| 439 | str, tval, base); |
| 440 | break; |
| 441 | } |
| 442 | val = val * base + tval; |
| 443 | } while (*ptr); |
| 444 | |
| 445 | if (negative) |
| 446 | max = (1 << (size - 1)); |
| 447 | else |
| 448 | max = (1 << (size - 1)) + ((1 << (size - 1)) - 1); |
| 449 | if (val > max) { |
| 450 | switch (base) { |
| 451 | case 8: |
| 452 | log_warnx("value %s%o exceeds max (%d) for precision.", |
| 453 | negative ? "-" : "", val, max); |
| 454 | break; |
| 455 | case 16: |
| 456 | log_warnx("value %s%x exceeds max (%d) for precision.", |
| 457 | negative ? "-" : "", val, max); |
| 458 | break; |
| 459 | default: |
| 460 | log_warnx("value %s%u exceeds max (%d) for precision.", |
| 461 | negative ? "-" : "", val, max); |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (negative) { |
| 467 | switch (size) { |
| 468 | case 8: |
| 469 | *buf = -(unsigned long)val; |
| 470 | break; |
| 471 | case 16: |
| 472 | putShort(buf, -(unsigned long)val); |
| 473 | break; |
| 474 | case 32: |
| 475 | putLong(buf, -(unsigned long)val); |
| 476 | break; |
| 477 | default: |
| 478 | log_warnx("Unexpected integer size: %d", size); |
| 479 | break; |
| 480 | } |
| 481 | } else { |
| 482 | switch (size) { |
| 483 | case 8: |
| 484 | *buf = (u_int8_t)val; |
| 485 | break; |
| 486 | case 16: |
| 487 | putUShort(buf, (u_int16_t)val); |
| 488 | break; |
| 489 | case 32: |
| 490 | putULong(buf, val); |
| 491 | break; |
| 492 | default: |
| 493 | log_warnx("Unexpected integer size: %d", size); |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | time_t |
| 508 | parse_date(FILE *cfile) |
| 509 | { |
| 510 | struct tm tm; |
| 511 | char timestr[26]; |
| 512 | char *val, *p; |
| 513 | size_t n; |
| 514 | time_t guess; |
| 515 | int token; |
| 516 | |
| 517 | memset(timestr, 0, sizeof(timestr)); |
| 518 | |
| 519 | do { |
| 520 | token = peek_token(NULL, cfile); |
| 521 | switch (token) { |
| 522 | case TOK_NAME: |
| 523 | case TOK_NUMBER: |
| 524 | case TOK_NUMBER_OR_NAME: |
| 525 | case '/': |
| 526 | case ':': |
| 527 | token = next_token(&val, cfile); |
| 528 | n = strlcat(timestr, val, sizeof(timestr)); |
| 529 | if (n >= sizeof(timestr)) { |
| 530 | |
| 531 | parse_warn("time string too long"); |
| 532 | skip_to_semi(cfile); |
| 533 | return (0); |
| 534 | } |
| 535 | break; |
| 536 | case';': |
| 537 | break; |
| 538 | default: |
| 539 | parse_warn("invalid time string"); |
| 540 | skip_to_semi(cfile); |
| 541 | return (0); |
| 542 | } |
| 543 | } while (token != ';'); |
| 544 | |
| 545 | parse_semi(cfile); |
| 546 | |
| 547 | memset(&tm, 0, sizeof(tm)); |
| 548 | p = strptime(timestr, DB_TIMEFMT, &tm); |
| 549 | if (p == NULL || *p != '\0') { |
| 550 | p = strptime(timestr, OLD_DB_TIMEFMT, &tm); |
| 551 | if (p == NULL || *p != '\0') { |
| 552 | parse_warn("unparseable time string"); |
| 553 | return (0); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | guess = timegm(&tm); |
| 558 | if (guess == -1) { |
| 559 | parse_warn("time could not be represented"); |
| 560 | return (0); |
| 561 | } |
| 562 | |
| 563 | return (guess); |
| 564 | } |
| 565 | |
| 566 | int warnings_occurred; |
| 567 | |
| 568 | int |
| 569 | parse_warn(char *fmt, ...) |
| 570 | { |
| 571 | static char fbuf[1024]; |
| 572 | static char mbuf[1024]; |
| 573 | static char spaces[81]; |
| 574 | va_list list; |
| 575 | int i; |
| 576 | |
| 577 | snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf); |
| 578 | va_start(list, fmt); |
| 579 | vsnprintf(mbuf, sizeof(mbuf), fbuf, list); |
| 580 | va_end(list); |
| 581 | |
| 582 | log_warnx("%s", mbuf); |
| 583 | log_warnx("%s", token_line); |
| 584 | if (lexchar < sizeof(spaces)) { |
| 585 | memset(spaces, 0, sizeof(spaces)); |
| 586 | for (i = 0; i < lexchar - 1; i++) { |
| 587 | if (token_line[i] == '\t') |
| 588 | spaces[i] = '\t'; |
| 589 | else |
| 590 | spaces[i] = ' '; |
| 591 | } |
| 592 | } |
| 593 | log_warnx("%s^", spaces); |
| 594 | |
| 595 | warnings_occurred = 1; |
| 596 | |
| 597 | return (0); |
| 598 | } |