user_environment.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*-
  2. * Copyright (c) 2009-2010 Brad Penoff
  3. * Copyright (c) 2009-2010 Humaira Kamal
  4. * Copyright (c) 2011-2012 Irene Ruengeler
  5. * Copyright (c) 2011-2012 Michael Tuexen
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* __Userspace__ */
  31. #if defined(_WIN32)
  32. #if !defined(_CRT_RAND_S) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  33. #define _CRT_RAND_S
  34. #endif
  35. #else
  36. #include <stdint.h>
  37. #include <netinet/sctp_os_userspace.h>
  38. #endif
  39. #ifdef INVARIANTS
  40. #include <netinet/sctp_pcb.h>
  41. #endif
  42. #include <user_environment.h>
  43. #include <sys/types.h>
  44. /* #include <sys/param.h> defines MIN */
  45. #if !defined(MIN)
  46. #define MIN(arg1,arg2) ((arg1) < (arg2) ? (arg1) : (arg2))
  47. #endif
  48. #define uHZ 1000
  49. /* See user_include/user_environment.h for comments about these variables */
  50. int maxsockets = 25600;
  51. int hz = uHZ;
  52. int ip_defttl = 64;
  53. int ipport_firstauto = 49152, ipport_lastauto = 65535;
  54. int nmbclusters = 65536;
  55. /* Source ip_output.c. extern'd in ip_var.h */
  56. u_short ip_id = 0; /*__Userspace__ TODO Should it be initialized to zero? */
  57. /* used in user_include/user_atomic.h in order to make the operations
  58. * defined there truly atomic
  59. */
  60. userland_mutex_t atomic_mtx;
  61. /* If the entropy device is not loaded, make a token effort to
  62. * provide _some_ kind of randomness. This should only be used
  63. * inside other RNG's, like arc4random(9).
  64. */
  65. #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  66. #include <string.h>
  67. void
  68. init_random(void)
  69. {
  70. return;
  71. }
  72. void
  73. read_random(void *buf, size_t size)
  74. {
  75. memset(buf, 'A', size);
  76. return;
  77. }
  78. void
  79. finish_random(void)
  80. {
  81. return;
  82. }
  83. /* This define can be used to optionally use OpenSSL's random number utility,
  84. * which is capable of bypassing the chromium sandbox which normally would
  85. * prevent opening files, including /dev/urandom.
  86. */
  87. #elif defined(SCTP_USE_OPENSSL_RAND)
  88. #include <openssl/rand.h>
  89. /* Requiring BoringSSL because it guarantees that RAND_bytes will succeed. */
  90. #ifndef OPENSSL_IS_BORINGSSL
  91. #error Only BoringSSL is supported with SCTP_USE_OPENSSL_RAND.
  92. #endif
  93. void
  94. init_random(void)
  95. {
  96. return;
  97. }
  98. void
  99. read_random(void *buf, size_t size)
  100. {
  101. RAND_bytes((uint8_t *)buf, size);
  102. return;
  103. }
  104. void
  105. finish_random(void)
  106. {
  107. return;
  108. }
  109. #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(__Bitrig__)
  110. #include <stdlib.h>
  111. void
  112. init_random(void)
  113. {
  114. return;
  115. }
  116. void
  117. read_random(void *buf, size_t size)
  118. {
  119. arc4random_buf(buf, size);
  120. return;
  121. }
  122. void
  123. finish_random(void)
  124. {
  125. return;
  126. }
  127. #elif defined(_WIN32)
  128. #include <stdlib.h>
  129. void
  130. init_random(void)
  131. {
  132. return;
  133. }
  134. void
  135. read_random(void *buf, size_t size)
  136. {
  137. unsigned int randval;
  138. size_t position, remaining;
  139. position = 0;
  140. while (position < size) {
  141. if (rand_s(&randval) == 0) {
  142. remaining = MIN(size - position, sizeof(unsigned int));
  143. memcpy((char *)buf + position, &randval, remaining);
  144. position += sizeof(unsigned int);
  145. }
  146. }
  147. return;
  148. }
  149. void
  150. finish_random(void)
  151. {
  152. return;
  153. }
  154. #elif (defined(__ANDROID__) && (__ANDROID_API__ < 28)) || defined(__QNX__) || defined(__EMSCRIPTEN__)
  155. #include <fcntl.h>
  156. static int fd = -1;
  157. void
  158. init_random(void)
  159. {
  160. fd = open("/dev/urandom", O_RDONLY);
  161. return;
  162. }
  163. void
  164. read_random(void *buf, size_t size)
  165. {
  166. size_t position;
  167. ssize_t n;
  168. position = 0;
  169. while (position < size) {
  170. n = read(fd, (char *)buf + position, size - position);
  171. if (n > 0) {
  172. position += n;
  173. }
  174. }
  175. return;
  176. }
  177. void
  178. finish_random(void)
  179. {
  180. close(fd);
  181. return;
  182. }
  183. #elif defined(__ANDROID__) && (__ANDROID_API__ >= 28)
  184. #include <sys/random.h>
  185. void
  186. init_random(void)
  187. {
  188. return;
  189. }
  190. void
  191. read_random(void *buf, size_t size)
  192. {
  193. size_t position;
  194. ssize_t n;
  195. position = 0;
  196. while (position < size) {
  197. n = getrandom((char *)buf + position, size - position, 0);
  198. if (n > 0) {
  199. position += n;
  200. }
  201. }
  202. return;
  203. }
  204. void
  205. finish_random(void)
  206. {
  207. return;
  208. }
  209. #elif defined(__linux__)
  210. #include <fcntl.h>
  211. #include <unistd.h>
  212. #include <sys/syscall.h>
  213. #if defined(__has_feature)
  214. #if __has_feature(memory_sanitizer)
  215. void __msan_unpoison(void *, size_t);
  216. #endif
  217. #endif
  218. #ifdef __NR_getrandom
  219. #if !defined(GRND_NONBLOCK)
  220. #define GRND_NONBLOCK 1
  221. #endif
  222. static int getrandom_available = 0;
  223. #endif
  224. static int fd = -1;
  225. void
  226. init_random(void)
  227. {
  228. #ifdef __NR_getrandom
  229. char dummy;
  230. ssize_t n = syscall(__NR_getrandom, &dummy, sizeof(dummy), GRND_NONBLOCK);
  231. if (n > 0 || errno == EINTR || errno == EAGAIN) {
  232. /* Either getrandom succeeded, was interrupted or is waiting for entropy;
  233. * all of which mean the syscall is available.
  234. */
  235. getrandom_available = 1;
  236. } else {
  237. #ifdef INVARIANTS
  238. if (errno != ENOSYS) {
  239. panic("getrandom syscall returned unexpected error: %d", errno);
  240. }
  241. #endif
  242. /* If the syscall isn't available, fall back to /dev/urandom. */
  243. #endif
  244. fd = open("/dev/urandom", O_RDONLY);
  245. #ifdef __NR_getrandom
  246. }
  247. #endif
  248. return;
  249. }
  250. void
  251. read_random(void *buf, size_t size)
  252. {
  253. size_t position;
  254. ssize_t n;
  255. position = 0;
  256. while (position < size) {
  257. #ifdef __NR_getrandom
  258. if (getrandom_available) {
  259. /* Using syscall directly because getrandom isn't present in glibc < 2.25.
  260. */
  261. n = syscall(__NR_getrandom, (char *)buf + position, size - position, 0);
  262. if (n > 0) {
  263. #if defined(__has_feature)
  264. #if __has_feature(memory_sanitizer)
  265. /* Need to do this because MSan doesn't realize that syscall has
  266. * initialized the output buffer.
  267. */
  268. __msan_unpoison(buf + position, n);
  269. #endif
  270. #endif
  271. position += n;
  272. } else if (errno != EINTR && errno != EAGAIN) {
  273. #ifdef INVARIANTS
  274. panic("getrandom syscall returned unexpected error: %d", errno);
  275. #endif
  276. }
  277. } else
  278. #endif /* __NR_getrandom */
  279. {
  280. n = read(fd, (char *)buf + position, size - position);
  281. if (n > 0) {
  282. position += n;
  283. }
  284. }
  285. }
  286. return;
  287. }
  288. void
  289. finish_random(void)
  290. {
  291. if (fd != -1) {
  292. close(fd);
  293. }
  294. return;
  295. }
  296. #elif defined(__Fuchsia__)
  297. #include <zircon/syscalls.h>
  298. void
  299. init_random(void)
  300. {
  301. return;
  302. }
  303. void
  304. read_random(void *buf, size_t size)
  305. {
  306. zx_cprng_draw(buf, size);
  307. return;
  308. }
  309. void
  310. finish_random(void)
  311. {
  312. return;
  313. }
  314. #elif defined(__native_client__)
  315. #include <nacl/nacl_random.h>
  316. void
  317. init_random(void)
  318. {
  319. return;
  320. }
  321. void
  322. read_random(void *buf, size_t size)
  323. {
  324. size_t position;
  325. size_t n;
  326. position = 0;
  327. while (position < size) {
  328. if (nacl_secure_random((char *)buf + position, size - position, &n) == 0)
  329. position += n;
  330. }
  331. }
  332. return;
  333. }
  334. void
  335. finish_random(void)
  336. {
  337. return;
  338. }
  339. #else
  340. #error "Unknown platform. Please provide platform specific RNG."
  341. #endif