| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [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 |  | 
| Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 25 | #include <errno.h> | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 26 | #include <pthread.h> | 
|  | 27 | #include <signal.h> | 
| Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 28 | #include <sys/mman.h> | 
| Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 29 | #include <sys/prctl.h> | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 30 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 31 | #include <async_safe/log.h> | 
|  | 32 |  | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 33 | // Android gets these from "thread_private.h". | 
|  | 34 | #include "thread_private.h" | 
|  | 35 | //static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; | 
|  | 36 | //#define _ARC4_LOCK()   pthread_mutex_lock(&arc4random_mtx) | 
|  | 37 | //#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx) | 
|  | 38 |  | 
| Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 39 | static inline void _getentropy_fail(void) { | 
|  | 40 | async_safe_fatal("getentropy failed: %s", strerror(errno)); | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Josh Gao | c80ffec | 2016-06-24 16:18:21 -0700 | [diff] [blame] | 43 | volatile sig_atomic_t _rs_forked; | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | static inline void | 
|  | 46 | _rs_forkdetect(void) | 
|  | 47 | { | 
|  | 48 | static pid_t _rs_pid = 0; | 
|  | 49 | pid_t pid = getpid(); | 
|  | 50 |  | 
|  | 51 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { | 
|  | 52 | _rs_pid = pid; | 
|  | 53 | _rs_forked = 0; | 
|  | 54 | if (rs) | 
|  | 55 | memset(rs, 0, sizeof(*rs)); | 
|  | 56 | } | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | static inline int | 
|  | 60 | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) | 
|  | 61 | { | 
| Elliott Hughes | da01fa6 | 2016-10-11 12:58:40 -0700 | [diff] [blame] | 62 | // OpenBSD's arc4random_linux.h allocates two separate mappings, but for | 
|  | 63 | // themselves they just allocate both structs into one mapping like this. | 
|  | 64 | struct { | 
|  | 65 | struct _rs rs; | 
|  | 66 | struct _rsx rsx; | 
|  | 67 | } *p; | 
|  | 68 |  | 
|  | 69 | if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE, | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 70 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | 
|  | 71 | return (-1); | 
|  | 72 |  | 
| Elliott Hughes | da01fa6 | 2016-10-11 12:58:40 -0700 | [diff] [blame] | 73 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, sizeof(*p), "arc4random data"); | 
| Daniel Micay | 516fcb2 | 2015-08-01 22:25:42 -0400 | [diff] [blame] | 74 |  | 
| Elliott Hughes | da01fa6 | 2016-10-11 12:58:40 -0700 | [diff] [blame] | 75 | *rsp = &p->rs; | 
|  | 76 | *rsxp = &p->rsx; | 
| Daniel Micay | 516fcb2 | 2015-08-01 22:25:42 -0400 | [diff] [blame] | 77 |  | 
| Elliott Hughes | 2b67d7d | 2014-07-18 15:57:41 -0700 | [diff] [blame] | 78 | return (0); | 
|  | 79 | } |