blob: 5f0b15e0fdf43fac251062daf97ff754ecd15ebe [file] [log] [blame]
Elliott Hughes2b67d7d2014-07-18 15:57:41 -07001/* $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 Albert1c78cb02017-10-11 11:25:25 -070025#include <errno.h>
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070026#include <pthread.h>
27#include <signal.h>
Dan Albert1c78cb02017-10-11 11:25:25 -070028#include <sys/mman.h>
Elliott Hughes99d54652018-08-22 10:36:23 -070029#include <sys/prctl.h>
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070030
Christopher Ferris7a3681e2017-04-24 17:48:32 -070031#include <async_safe/log.h>
32
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070033// 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
39#ifdef __GLIBC__
40extern void *__dso_handle;
41extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
42#define _ARC4_ATFORK(f) __register_atfork(NULL, NULL, (f), __dso_handle)
43#else
44#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
45#endif
46
Elliott Hughes7b0af7a2017-09-15 16:09:22 -070047static inline void _getentropy_fail(void) {
48 async_safe_fatal("getentropy failed: %s", strerror(errno));
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070049}
50
Josh Gaoc80ffec2016-06-24 16:18:21 -070051volatile sig_atomic_t _rs_forked;
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070052
53static inline void
54_rs_forkdetect(void)
55{
56 static pid_t _rs_pid = 0;
57 pid_t pid = getpid();
58
59 if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
60 _rs_pid = pid;
61 _rs_forked = 0;
62 if (rs)
63 memset(rs, 0, sizeof(*rs));
64 }
65}
66
67static inline int
68_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
69{
Elliott Hughesda01fa62016-10-11 12:58:40 -070070 // OpenBSD's arc4random_linux.h allocates two separate mappings, but for
71 // themselves they just allocate both structs into one mapping like this.
72 struct {
73 struct _rs rs;
74 struct _rsx rsx;
75 } *p;
76
77 if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070078 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
79 return (-1);
80
Elliott Hughesda01fa62016-10-11 12:58:40 -070081 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, sizeof(*p), "arc4random data");
Daniel Micay516fcb22015-08-01 22:25:42 -040082
Elliott Hughesda01fa62016-10-11 12:58:40 -070083 *rsp = &p->rs;
84 *rsxp = &p->rsx;
Daniel Micay516fcb22015-08-01 22:25:42 -040085
Elliott Hughes2b67d7d2014-07-18 15:57:41 -070086 return (0);
87}