xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [diff] [blame] | 1 | /* $OpenBSD: arc4random_linux.h,v 1.7 2014/07/20 20:51:13 bcook Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| 5 | * Copyright (c) 2008, Damien Miller <djm@openbsd.org> |
| 6 | * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> |
| 7 | * |
| 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Stub functions for portability. |
| 23 | */ |
| 24 | |
| 25 | #include <errno.h> |
| 26 | #include <pthread.h> |
| 27 | #include <signal.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | //#include <async_safe/log.h> |
| 32 | |
| 33 | //#include "bionic_prctl.h" |
| 34 | #include <sys/prctl.h> |
| 35 | |
| 36 | // This is only supported by Android kernels, so it's not in the uapi headers. |
| 37 | #define PR_SET_VMA 0x53564d41 |
| 38 | #define PR_SET_VMA_ANON_NAME 0 |
| 39 | |
| 40 | |
| 41 | // Android gets these from "thread_private.h". |
| 42 | //#include "thread_private.h" |
| 43 | static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; |
| 44 | #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx) |
| 45 | #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx) |
| 46 | |
| 47 | #ifdef __GLIBC__ |
| 48 | extern void *__dso_handle; |
| 49 | extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *); |
| 50 | #define _ARC4_ATFORK(f) __register_atfork(NULL, NULL, (f), __dso_handle) |
| 51 | #else |
| 52 | #define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f)) |
| 53 | #endif |
| 54 | |
| 55 | // Formats a message to the log (priority 'fatal'), then aborts. |
| 56 | // Implemented as a macro so that async_safe_fatal isn't on the stack when we crash: |
| 57 | // we appear to go straight from the caller to abort, saving an uninteresting stack |
| 58 | // frame. |
| 59 | #define async_safe_fatal(...) \ |
| 60 | do { \ |
| 61 | printf(__VA_ARGS__); \ |
| 62 | abort(); \ |
| 63 | } while (0) \ |
| 64 | |
| 65 | static inline void _getentropy_fail(void) { |
| 66 | async_safe_fatal("getentropy failed: %s", strerror(errno)); |
| 67 | } |
| 68 | |
| 69 | volatile sig_atomic_t _rs_forked; |
| 70 | |
| 71 | static inline void |
| 72 | _rs_forkdetect(void) |
| 73 | { |
| 74 | static pid_t _rs_pid = 0; |
| 75 | pid_t pid = getpid(); |
| 76 | |
| 77 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { |
| 78 | _rs_pid = pid; |
| 79 | _rs_forked = 0; |
| 80 | if (rs) |
| 81 | memset(rs, 0, sizeof(*rs)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static inline int |
| 86 | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) |
| 87 | { |
| 88 | // OpenBSD's arc4random_linux.h allocates two separate mappings, but for |
| 89 | // themselves they just allocate both structs into one mapping like this. |
| 90 | struct { |
| 91 | struct _rs rs; |
| 92 | struct _rsx rsx; |
| 93 | } *p; |
| 94 | |
| 95 | if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE, |
| 96 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) |
| 97 | return (-1); |
| 98 | |
| 99 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, sizeof(*p), "arc4random data"); |
| 100 | |
| 101 | *rsp = &p->rs; |
| 102 | *rsxp = &p->rsx; |
| 103 | |
| 104 | return (0); |
| 105 | } |