1e66f31c5Sopenharmony_ci/* Copyright libuv contributors. All rights reserved. 2e66f31c5Sopenharmony_ci * 3e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 4e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 5e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the 6e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 8e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions: 9e66f31c5Sopenharmony_ci * 10e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 11e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software. 12e66f31c5Sopenharmony_ci * 13e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19e66f31c5Sopenharmony_ci * IN THE SOFTWARE. 20e66f31c5Sopenharmony_ci */ 21e66f31c5Sopenharmony_ci 22e66f31c5Sopenharmony_ci#include "uv.h" 23e66f31c5Sopenharmony_ci#include "internal.h" 24e66f31c5Sopenharmony_ci 25e66f31c5Sopenharmony_ci#include <errno.h> 26e66f31c5Sopenharmony_ci#include <string.h> 27e66f31c5Sopenharmony_ci 28e66f31c5Sopenharmony_ci#include <syscall.h> 29e66f31c5Sopenharmony_ci#include <unistd.h> 30e66f31c5Sopenharmony_ci 31e66f31c5Sopenharmony_ci 32e66f31c5Sopenharmony_cistruct uv__sysctl_args { 33e66f31c5Sopenharmony_ci int* name; 34e66f31c5Sopenharmony_ci int nlen; 35e66f31c5Sopenharmony_ci void* oldval; 36e66f31c5Sopenharmony_ci size_t* oldlenp; 37e66f31c5Sopenharmony_ci void* newval; 38e66f31c5Sopenharmony_ci size_t newlen; 39e66f31c5Sopenharmony_ci unsigned long unused[4]; 40e66f31c5Sopenharmony_ci}; 41e66f31c5Sopenharmony_ci 42e66f31c5Sopenharmony_ci 43e66f31c5Sopenharmony_ciint uv__random_sysctl(void* buf, size_t buflen) { 44e66f31c5Sopenharmony_ci static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/}; 45e66f31c5Sopenharmony_ci struct uv__sysctl_args args; 46e66f31c5Sopenharmony_ci char uuid[16]; 47e66f31c5Sopenharmony_ci char* p; 48e66f31c5Sopenharmony_ci char* pe; 49e66f31c5Sopenharmony_ci size_t n; 50e66f31c5Sopenharmony_ci 51e66f31c5Sopenharmony_ci p = buf; 52e66f31c5Sopenharmony_ci pe = p + buflen; 53e66f31c5Sopenharmony_ci 54e66f31c5Sopenharmony_ci while (p < pe) { 55e66f31c5Sopenharmony_ci memset(&args, 0, sizeof(args)); 56e66f31c5Sopenharmony_ci 57e66f31c5Sopenharmony_ci args.name = name; 58e66f31c5Sopenharmony_ci args.nlen = ARRAY_SIZE(name); 59e66f31c5Sopenharmony_ci args.oldval = uuid; 60e66f31c5Sopenharmony_ci args.oldlenp = &n; 61e66f31c5Sopenharmony_ci n = sizeof(uuid); 62e66f31c5Sopenharmony_ci 63e66f31c5Sopenharmony_ci /* Emits a deprecation warning with some kernels but that seems like 64e66f31c5Sopenharmony_ci * an okay trade-off for the fallback of the fallback: this function is 65e66f31c5Sopenharmony_ci * only called when neither getrandom(2) nor /dev/urandom are available. 66e66f31c5Sopenharmony_ci * Fails with ENOSYS on kernels configured without CONFIG_SYSCTL_SYSCALL. 67e66f31c5Sopenharmony_ci * At least arm64 never had a _sysctl system call and therefore doesn't 68e66f31c5Sopenharmony_ci * have a SYS__sysctl define either. 69e66f31c5Sopenharmony_ci */ 70e66f31c5Sopenharmony_ci#ifdef SYS__sysctl 71e66f31c5Sopenharmony_ci if (syscall(SYS__sysctl, &args) == -1) 72e66f31c5Sopenharmony_ci return UV__ERR(errno); 73e66f31c5Sopenharmony_ci#else 74e66f31c5Sopenharmony_ci { 75e66f31c5Sopenharmony_ci (void) &args; 76e66f31c5Sopenharmony_ci return UV_ENOSYS; 77e66f31c5Sopenharmony_ci } 78e66f31c5Sopenharmony_ci#endif 79e66f31c5Sopenharmony_ci 80e66f31c5Sopenharmony_ci if (n != sizeof(uuid)) 81e66f31c5Sopenharmony_ci return UV_EIO; /* Can't happen. */ 82e66f31c5Sopenharmony_ci 83e66f31c5Sopenharmony_ci /* uuid[] is now a type 4 UUID. Bytes 6 and 8 (counting from zero) contain 84e66f31c5Sopenharmony_ci * 4 and 5 bits of entropy, respectively. For ease of use, we skip those 85e66f31c5Sopenharmony_ci * and only use 14 of the 16 bytes. 86e66f31c5Sopenharmony_ci */ 87e66f31c5Sopenharmony_ci uuid[6] = uuid[14]; 88e66f31c5Sopenharmony_ci uuid[8] = uuid[15]; 89e66f31c5Sopenharmony_ci 90e66f31c5Sopenharmony_ci n = pe - p; 91e66f31c5Sopenharmony_ci if (n > 14) 92e66f31c5Sopenharmony_ci n = 14; 93e66f31c5Sopenharmony_ci 94e66f31c5Sopenharmony_ci memcpy(p, uuid, n); 95e66f31c5Sopenharmony_ci p += n; 96e66f31c5Sopenharmony_ci } 97e66f31c5Sopenharmony_ci 98e66f31c5Sopenharmony_ci return 0; 99e66f31c5Sopenharmony_ci} 100