1 /* 2 * OS specific functions for UNIX/POSIX systems 3 * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 11 #include <time.h> 12 #include <sys/wait.h> 13 14 #ifdef ANDROID 15 #include <sys/capability.h> 16 #include <sys/prctl.h> 17 #include <private/android_filesystem_config.h> 18 #endif /* ANDROID */ 19 20 #ifdef __MACH__ 21 #include <CoreServices/CoreServices.h> 22 #include <mach/mach.h> 23 #include <mach/mach_time.h> 24 #endif /* __MACH__ */ 25 26 #include "os.h" 27 #include "common.h" 28 29 #ifdef WPA_TRACE 30 31 #include "wpa_debug.h" 32 #include "trace.h" 33 #include "list.h" 34 35 static struct dl_list alloc_list = DL_LIST_HEAD_INIT(alloc_list); 36 37 #define ALLOC_MAGIC 0xa84ef1b2 38 #define FREED_MAGIC 0x67fd487a 39 40 struct os_alloc_trace { 41 unsigned int magic; 42 struct dl_list list __attribute__((aligned(16))); 43 size_t len; 44 WPA_TRACE_INFO 45 } __attribute__((aligned(16))); 46 47 #endif /* WPA_TRACE */ 48 49 50 void os_sleep(os_time_t sec, os_time_t usec) 51 { 52 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) 53 const struct timespec req = { sec, usec * 1000 }; 54 55 nanosleep(&req, NULL); 56 #else 57 if (sec) 58 sleep(sec); 59 if (usec) 60 usleep(usec); 61 #endif 62 } 63 64 65 int os_get_time(struct os_time *t) 66 { 67 int res; 68 struct timeval tv; 69 res = gettimeofday(&tv, NULL); 70 t->sec = tv.tv_sec; 71 t->usec = tv.tv_usec; 72 return res; 73 } 74 75 int os_get_reltime(struct os_reltime *t) 76 { 77 #ifndef __MACH__ 78 #if defined(CLOCK_BOOTTIME) 79 static clockid_t clock_id = CLOCK_BOOTTIME; 80 #elif defined(CLOCK_MONOTONIC) 81 static clockid_t clock_id = CLOCK_MONOTONIC; 82 #else 83 static clockid_t clock_id = CLOCK_REALTIME; 84 #endif 85 struct timespec ts; 86 int res; 87 88 if (TEST_FAIL()) 89 return -1; 90 91 while (1) { 92 res = clock_gettime(clock_id, &ts); 93 if (res == 0) { 94 t->sec = ts.tv_sec; 95 t->usec = ts.tv_nsec / 1000; 96 return 0; 97 } 98 switch (clock_id) { 99 #ifdef CLOCK_BOOTTIME 100 case CLOCK_BOOTTIME: 101 clock_id = CLOCK_MONOTONIC; 102 break; 103 #endif 104 #ifdef CLOCK_MONOTONIC 105 /* 106 * FreeBSD has both BOOTTIME and MONOTONIC defined to the same value, since they 107 * mean the same thing. FreeBSD 14.1 and ealier don't, so need this case. 108 */ 109 #if !(defined(CLOCK_BOOTTIME) && CLOCK_BOOTTIME == CLOCK_MONOTONIC) 110 case CLOCK_MONOTONIC: 111 clock_id = CLOCK_REALTIME; 112 break; 113 #endif 114 #endif 115 case CLOCK_REALTIME: 116 return -1; 117 } 118 } 119 #else /* __MACH__ */ 120 uint64_t abstime, nano; 121 static mach_timebase_info_data_t info = { 0, 0 }; 122 123 if (!info.denom) { 124 if (mach_timebase_info(&info) != KERN_SUCCESS) 125 return -1; 126 } 127 128 abstime = mach_absolute_time(); 129 nano = (abstime * info.numer) / info.denom; 130 131 t->sec = nano / NSEC_PER_SEC; 132 t->usec = (nano - (((uint64_t) t->sec) * NSEC_PER_SEC)) / NSEC_PER_USEC; 133 134 return 0; 135 #endif /* __MACH__ */ 136 } 137 138 139 int os_mktime(int year, int month, int day, int hour, int min, int sec, 140 os_time_t *t) 141 { 142 struct tm tm, *tm1; 143 time_t t_local, t1, t2; 144 os_time_t tz_offset; 145 146 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 || 147 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || 148 sec > 60) 149 return -1; 150 151 memset(&tm, 0, sizeof(tm)); 152 tm.tm_year = year - 1900; 153 tm.tm_mon = month - 1; 154 tm.tm_mday = day; 155 tm.tm_hour = hour; 156 tm.tm_min = min; 157 tm.tm_sec = sec; 158 159 t_local = mktime(&tm); 160 161 /* figure out offset to UTC */ 162 tm1 = localtime(&t_local); 163 if (tm1) { 164 t1 = mktime(tm1); 165 tm1 = gmtime(&t_local); 166 if (tm1) { 167 t2 = mktime(tm1); 168 tz_offset = t2 - t1; 169 } else 170 tz_offset = 0; 171 } else 172 tz_offset = 0; 173 174 *t = (os_time_t) t_local - tz_offset; 175 return 0; 176 } 177 178 179 int os_gmtime(os_time_t t, struct os_tm *tm) 180 { 181 struct tm *tm2; 182 time_t t2 = t; 183 184 tm2 = gmtime(&t2); 185 if (tm2 == NULL) 186 return -1; 187 tm->sec = tm2->tm_sec; 188 tm->min = tm2->tm_min; 189 tm->hour = tm2->tm_hour; 190 tm->day = tm2->tm_mday; 191 tm->month = tm2->tm_mon + 1; 192 tm->year = tm2->tm_year + 1900; 193 return 0; 194 } 195 196 197 #ifdef __APPLE__ 198 #include <fcntl.h> 199 static int os_daemon(int nochdir, int noclose) 200 { 201 int devnull; 202 203 if (chdir("/") < 0) 204 return -1; 205 206 devnull = open("/dev/null", O_RDWR); 207 if (devnull < 0) 208 return -1; 209 210 if (dup2(devnull, STDIN_FILENO) < 0) { 211 close(devnull); 212 return -1; 213 } 214 215 if (dup2(devnull, STDOUT_FILENO) < 0) { 216 close(devnull); 217 return -1; 218 } 219 220 if (dup2(devnull, STDERR_FILENO) < 0) { 221 close(devnull); 222 return -1; 223 } 224 225 return 0; 226 } 227 #else /* __APPLE__ */ 228 #define os_daemon daemon 229 #endif /* __APPLE__ */ 230 231 232 int os_daemonize(const char *pid_file) 233 { 234 #if defined(__uClinux__) || defined(__sun__) 235 return -1; 236 #else /* defined(__uClinux__) || defined(__sun__) */ 237 if (os_daemon(0, 0)) { 238 perror("daemon"); 239 return -1; 240 } 241 242 if (pid_file) { 243 FILE *f = fopen(pid_file, "w"); 244 if (f) { 245 fprintf(f, "%u\n", getpid()); 246 fclose(f); 247 } 248 } 249 250 return -0; 251 #endif /* defined(__uClinux__) || defined(__sun__) */ 252 } 253 254 255 void os_daemonize_terminate(const char *pid_file) 256 { 257 if (pid_file) 258 unlink(pid_file); 259 } 260 261 262 int os_get_random(unsigned char *buf, size_t len) 263 { 264 #ifdef TEST_FUZZ 265 size_t i; 266 267 for (i = 0; i < len; i++) 268 buf[i] = i & 0xff; 269 return 0; 270 #else /* TEST_FUZZ */ 271 FILE *f; 272 size_t rc; 273 274 if (TEST_FAIL()) 275 return -1; 276 277 f = fopen("/dev/urandom", "rb"); 278 if (f == NULL) { 279 printf("Could not open /dev/urandom.\n"); 280 return -1; 281 } 282 283 rc = fread(buf, 1, len, f); 284 fclose(f); 285 286 return rc != len ? -1 : 0; 287 #endif /* TEST_FUZZ */ 288 } 289 290 291 unsigned long os_random(void) 292 { 293 return random(); 294 } 295 296 297 char * os_rel2abs_path(const char *rel_path) 298 { 299 char *buf = NULL, *cwd, *ret; 300 size_t len = 128, cwd_len, rel_len, ret_len; 301 int last_errno; 302 303 if (!rel_path) 304 return NULL; 305 306 if (rel_path[0] == '/') 307 return os_strdup(rel_path); 308 309 for (;;) { 310 buf = os_malloc(len); 311 if (buf == NULL) 312 return NULL; 313 cwd = getcwd(buf, len); 314 if (cwd == NULL) { 315 last_errno = errno; 316 os_free(buf); 317 if (last_errno != ERANGE) 318 return NULL; 319 len *= 2; 320 if (len > 2000) 321 return NULL; 322 } else { 323 buf[len - 1] = '\0'; 324 break; 325 } 326 } 327 328 cwd_len = os_strlen(cwd); 329 rel_len = os_strlen(rel_path); 330 ret_len = cwd_len + 1 + rel_len + 1; 331 ret = os_malloc(ret_len); 332 if (ret) { 333 os_memcpy(ret, cwd, cwd_len); 334 ret[cwd_len] = '/'; 335 os_memcpy(ret + cwd_len + 1, rel_path, rel_len); 336 ret[ret_len - 1] = '\0'; 337 } 338 os_free(buf); 339 return ret; 340 } 341 342 343 int os_program_init(void) 344 { 345 unsigned int seed; 346 347 #ifdef ANDROID 348 /* 349 * We ignore errors here since errors are normal if we 350 * are already running as non-root. 351 */ 352 #ifdef ANDROID_SETGROUPS_OVERRIDE 353 gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE }; 354 #else /* ANDROID_SETGROUPS_OVERRIDE */ 355 gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE }; 356 #endif /* ANDROID_SETGROUPS_OVERRIDE */ 357 struct __user_cap_header_struct header; 358 struct __user_cap_data_struct cap; 359 360 setgroups(ARRAY_SIZE(groups), groups); 361 362 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); 363 364 setgid(AID_WIFI); 365 setuid(AID_WIFI); 366 367 header.version = _LINUX_CAPABILITY_VERSION; 368 header.pid = 0; 369 cap.effective = cap.permitted = 370 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW); 371 cap.inheritable = 0; 372 capset(&header, &cap); 373 #endif /* ANDROID */ 374 375 if (os_get_random((unsigned char *) &seed, sizeof(seed)) == 0) 376 srandom(seed); 377 378 return 0; 379 } 380 381 382 void os_program_deinit(void) 383 { 384 #ifdef WPA_TRACE 385 struct os_alloc_trace *a; 386 unsigned long total = 0; 387 dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) { 388 total += a->len; 389 if (a->magic != ALLOC_MAGIC) { 390 wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x " 391 "len %lu", 392 a, a->magic, (unsigned long) a->len); 393 continue; 394 } 395 wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu", 396 a, (unsigned long) a->len); 397 wpa_trace_dump("memleak", a); 398 } 399 if (total) 400 wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes", 401 (unsigned long) total); 402 wpa_trace_deinit(); 403 #endif /* WPA_TRACE */ 404 } 405 406 407 int os_setenv(const char *name, const char *value, int overwrite) 408 { 409 return setenv(name, value, overwrite); 410 } 411 412 413 int os_unsetenv(const char *name) 414 { 415 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \ 416 defined(__OpenBSD__) 417 unsetenv(name); 418 return 0; 419 #else 420 return unsetenv(name); 421 #endif 422 } 423 424 425 char * os_readfile(const char *name, size_t *len) 426 { 427 FILE *f; 428 char *buf; 429 long pos; 430 431 f = fopen(name, "rb"); 432 if (f == NULL) 433 return NULL; 434 435 if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) { 436 fclose(f); 437 return NULL; 438 } 439 *len = pos; 440 if (fseek(f, 0, SEEK_SET) < 0) { 441 fclose(f); 442 return NULL; 443 } 444 445 buf = os_malloc(*len); 446 if (buf == NULL) { 447 fclose(f); 448 return NULL; 449 } 450 451 if (fread(buf, 1, *len, f) != *len) { 452 fclose(f); 453 os_free(buf); 454 return NULL; 455 } 456 457 fclose(f); 458 459 return buf; 460 } 461 462 463 int os_file_exists(const char *fname) 464 { 465 return access(fname, F_OK) == 0; 466 } 467 468 469 #if !defined __DragonFly__ 470 int os_fdatasync(FILE *stream) 471 { 472 if (!fflush(stream)) { 473 #if defined __FreeBSD__ || defined __linux__ 474 return fdatasync(fileno(stream)); 475 #else /* !__linux__ && !__FreeBSD__ */ 476 #ifdef F_FULLFSYNC 477 /* OS X does not implement fdatasync(). */ 478 return fcntl(fileno(stream), F_FULLFSYNC); 479 #else /* F_FULLFSYNC */ 480 return fsync(fileno(stream)); 481 #endif /* F_FULLFSYNC */ 482 #endif /* __linux__ */ 483 } 484 485 return -1; 486 } 487 #endif 488 489 490 #ifndef WPA_TRACE 491 void * os_zalloc(size_t size) 492 { 493 return calloc(1, size); 494 } 495 #endif /* WPA_TRACE */ 496 497 498 size_t os_strlcpy(char *dest, const char *src, size_t siz) 499 { 500 const char *s = src; 501 size_t left = siz; 502 503 if (left) { 504 /* Copy string up to the maximum size of the dest buffer */ 505 while (--left != 0) { 506 if ((*dest++ = *s++) == '\0') 507 break; 508 } 509 } 510 511 if (left == 0) { 512 /* Not enough room for the string; force NUL-termination */ 513 if (siz != 0) 514 *dest = '\0'; 515 while (*s++) 516 ; /* determine total src string length */ 517 } 518 519 return s - src - 1; 520 } 521 522 523 int os_memcmp_const(const void *a, const void *b, size_t len) 524 { 525 const u8 *aa = a; 526 const u8 *bb = b; 527 size_t i; 528 u8 res; 529 530 for (res = 0, i = 0; i < len; i++) 531 res |= aa[i] ^ bb[i]; 532 533 return res; 534 } 535 536 537 void * os_memdup(const void *src, size_t len) 538 { 539 void *r = os_malloc(len); 540 541 if (r && src) 542 os_memcpy(r, src, len); 543 return r; 544 } 545 546 547 #ifdef WPA_TRACE 548 549 #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) 550 char wpa_trace_fail_func[256] = { 0 }; 551 unsigned int wpa_trace_fail_after; 552 553 static int testing_fail_alloc(void) 554 { 555 const char *func[WPA_TRACE_LEN]; 556 size_t i, res, len; 557 char *pos, *next; 558 int match; 559 560 if (!wpa_trace_fail_after) 561 return 0; 562 563 res = wpa_trace_calling_func(func, WPA_TRACE_LEN); 564 i = 0; 565 if (i < res && os_strcmp(func[i], __func__) == 0) 566 i++; 567 if (i < res && os_strcmp(func[i], "os_malloc") == 0) 568 i++; 569 if (i < res && os_strcmp(func[i], "os_zalloc") == 0) 570 i++; 571 if (i < res && os_strcmp(func[i], "os_calloc") == 0) 572 i++; 573 if (i < res && os_strcmp(func[i], "os_realloc") == 0) 574 i++; 575 if (i < res && os_strcmp(func[i], "os_realloc_array") == 0) 576 i++; 577 if (i < res && os_strcmp(func[i], "os_strdup") == 0) 578 i++; 579 if (i < res && os_strcmp(func[i], "os_memdup") == 0) 580 i++; 581 582 pos = wpa_trace_fail_func; 583 584 match = 0; 585 while (i < res) { 586 int allow_skip = 1; 587 int maybe = 0; 588 589 if (*pos == '=') { 590 allow_skip = 0; 591 pos++; 592 } else if (*pos == '?') { 593 maybe = 1; 594 pos++; 595 } 596 next = os_strchr(pos, ';'); 597 if (next) 598 len = next - pos; 599 else 600 len = os_strlen(pos); 601 if (os_memcmp(pos, func[i], len) != 0) { 602 if (maybe && next) { 603 pos = next + 1; 604 continue; 605 } 606 if (allow_skip) { 607 i++; 608 continue; 609 } 610 return 0; 611 } 612 if (!next) { 613 match = 1; 614 break; 615 } 616 pos = next + 1; 617 i++; 618 } 619 if (!match) 620 return 0; 621 622 wpa_trace_fail_after--; 623 if (wpa_trace_fail_after == 0) { 624 wpa_printf(MSG_INFO, "TESTING: fail allocation at %s", 625 wpa_trace_fail_func); 626 for (i = 0; i < res; i++) 627 wpa_printf(MSG_INFO, "backtrace[%d] = %s", 628 (int) i, func[i]); 629 return 1; 630 } 631 632 return 0; 633 } 634 635 636 char wpa_trace_test_fail_func[256] = { 0 }; 637 unsigned int wpa_trace_test_fail_after; 638 639 int testing_test_fail(void) 640 { 641 const char *func[WPA_TRACE_LEN]; 642 size_t i, res, len; 643 char *pos, *next; 644 int match; 645 646 if (!wpa_trace_test_fail_after) 647 return 0; 648 649 res = wpa_trace_calling_func(func, WPA_TRACE_LEN); 650 i = 0; 651 if (i < res && os_strcmp(func[i], __func__) == 0) 652 i++; 653 654 pos = wpa_trace_test_fail_func; 655 656 match = 0; 657 while (i < res) { 658 int allow_skip = 1; 659 int maybe = 0; 660 661 if (*pos == '=') { 662 allow_skip = 0; 663 pos++; 664 } else if (*pos == '?') { 665 maybe = 1; 666 pos++; 667 } 668 next = os_strchr(pos, ';'); 669 if (next) 670 len = next - pos; 671 else 672 len = os_strlen(pos); 673 if (os_memcmp(pos, func[i], len) != 0) { 674 if (maybe && next) { 675 pos = next + 1; 676 continue; 677 } 678 if (allow_skip) { 679 i++; 680 continue; 681 } 682 return 0; 683 } 684 if (!next) { 685 match = 1; 686 break; 687 } 688 pos = next + 1; 689 i++; 690 } 691 if (!match) 692 return 0; 693 694 wpa_trace_test_fail_after--; 695 if (wpa_trace_test_fail_after == 0) { 696 wpa_printf(MSG_INFO, "TESTING: fail at %s", 697 wpa_trace_test_fail_func); 698 for (i = 0; i < res; i++) 699 wpa_printf(MSG_INFO, "backtrace[%d] = %s", 700 (int) i, func[i]); 701 return 1; 702 } 703 704 return 0; 705 } 706 707 #else 708 709 static inline int testing_fail_alloc(void) 710 { 711 return 0; 712 } 713 #endif 714 715 void * os_malloc(size_t size) 716 { 717 struct os_alloc_trace *a; 718 719 if (testing_fail_alloc()) 720 return NULL; 721 722 a = malloc(sizeof(*a) + size); 723 if (a == NULL) 724 return NULL; 725 a->magic = ALLOC_MAGIC; 726 dl_list_add(&alloc_list, &a->list); 727 a->len = size; 728 wpa_trace_record(a); 729 return a + 1; 730 } 731 732 733 void * os_realloc(void *ptr, size_t size) 734 { 735 struct os_alloc_trace *a; 736 size_t copy_len; 737 void *n; 738 739 if (ptr == NULL) 740 return os_malloc(size); 741 742 a = (struct os_alloc_trace *) ptr - 1; 743 if (a->magic != ALLOC_MAGIC) { 744 wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s", 745 a, a->magic, 746 a->magic == FREED_MAGIC ? " (already freed)" : ""); 747 wpa_trace_show("Invalid os_realloc() call"); 748 abort(); 749 } 750 n = os_malloc(size); 751 if (n == NULL) 752 return NULL; 753 copy_len = a->len; 754 if (copy_len > size) 755 copy_len = size; 756 os_memcpy(n, a + 1, copy_len); 757 os_free(ptr); 758 return n; 759 } 760 761 762 void os_free(void *ptr) 763 { 764 struct os_alloc_trace *a; 765 766 if (ptr == NULL) 767 return; 768 a = (struct os_alloc_trace *) ptr - 1; 769 if (a->magic != ALLOC_MAGIC) { 770 wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s", 771 a, a->magic, 772 a->magic == FREED_MAGIC ? " (already freed)" : ""); 773 wpa_trace_show("Invalid os_free() call"); 774 abort(); 775 } 776 dl_list_del(&a->list); 777 a->magic = FREED_MAGIC; 778 779 wpa_trace_check_ref(ptr); 780 free(a); 781 } 782 783 784 void * os_zalloc(size_t size) 785 { 786 void *ptr = os_malloc(size); 787 if (ptr) 788 os_memset(ptr, 0, size); 789 return ptr; 790 } 791 792 793 char * os_strdup(const char *s) 794 { 795 size_t len; 796 char *d; 797 len = os_strlen(s); 798 d = os_malloc(len + 1); 799 if (d == NULL) 800 return NULL; 801 os_memcpy(d, s, len); 802 d[len] = '\0'; 803 return d; 804 } 805 806 #endif /* WPA_TRACE */ 807 808 809 int os_exec(const char *program, const char *arg, int wait_completion) 810 { 811 pid_t pid; 812 int pid_status; 813 814 pid = fork(); 815 if (pid < 0) { 816 perror("fork"); 817 return -1; 818 } 819 820 if (pid == 0) { 821 /* run the external command in the child process */ 822 const int MAX_ARG = 30; 823 char *_program, *_arg, *pos; 824 char *argv[MAX_ARG + 1]; 825 int i; 826 827 _program = os_strdup(program); 828 _arg = os_strdup(arg); 829 830 argv[0] = _program; 831 832 i = 1; 833 pos = _arg; 834 while (i < MAX_ARG && pos && *pos) { 835 while (*pos == ' ') 836 pos++; 837 if (*pos == '\0') 838 break; 839 argv[i++] = pos; 840 pos = os_strchr(pos, ' '); 841 if (pos) 842 *pos++ = '\0'; 843 } 844 argv[i] = NULL; 845 846 execv(program, argv); 847 perror("execv"); 848 os_free(_program); 849 os_free(_arg); 850 exit(0); 851 return -1; 852 } 853 854 if (wait_completion) { 855 /* wait for the child process to complete in the parent */ 856 waitpid(pid, &pid_status, 0); 857 } 858 859 return 0; 860 } 861