blob: ae761553d29aec54ec347b7f382516aef88855f7 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -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
25#include <errno.h>
26#include <pthread.h>
27#include <signal.h>
28#include <sys/mman.h>
29
30//#include <async_safe/log.h>
31
32//#include "bionic_prctl.h"
33#include <sys/prctl.h>
34
35// This is only supported by Android kernels, so it's not in the uapi headers.
36#define PR_SET_VMA 0x53564d41
37#define PR_SET_VMA_ANON_NAME 0
38
39
40// Android gets these from "thread_private.h".
41//#include "thread_private.h"
42static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
43#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
44#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
45
46#ifdef __GLIBC__
47extern void *__dso_handle;
48extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
49#define _ARC4_ATFORK(f) __register_atfork(NULL, NULL, (f), __dso_handle)
50#else
51#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
52#endif
53
54// Formats a message to the log (priority 'fatal'), then aborts.
55// Implemented as a macro so that async_safe_fatal isn't on the stack when we crash:
56// we appear to go straight from the caller to abort, saving an uninteresting stack
57// frame.
58#define async_safe_fatal(...) \
59 do { \
60 printf(__VA_ARGS__); \
61 abort(); \
62 } while (0) \
63
64static inline void _getentropy_fail(void) {
65 async_safe_fatal("getentropy failed: %s", strerror(errno));
66}
67
68volatile sig_atomic_t _rs_forked;
69
70static inline void
71_rs_forkdetect(void)
72{
73 static pid_t _rs_pid = 0;
74 pid_t pid = getpid();
75
76 if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
77 _rs_pid = pid;
78 _rs_forked = 0;
79 if (rs)
80 memset(rs, 0, sizeof(*rs));
81 }
82}
83
84static inline int
85_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
86{
87 // OpenBSD's arc4random_linux.h allocates two separate mappings, but for
88 // themselves they just allocate both structs into one mapping like this.
89 struct {
90 struct _rs rs;
91 struct _rsx rsx;
92 } *p;
93
94 if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
95 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
96 return (-1);
97
98 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, sizeof(*p), "arc4random data");
99
100 *rsp = &p->rs;
101 *rsxp = &p->rsx;
102
103 return (0);
104}