summaryrefslogtreecommitdiffstats
path: root/man2/getrandom.2
blob: e4c63a8ad2608aa2304ca93182d487174f5a3152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
.\" Copyright (C) 2014, Theodore Ts'o <tytso@mit.edu>
.\" Copyright (C) 2014,2015 Heinrich Schuchardt <xypron.glpk@gmx.de>
.\" Copyright (C) 2015, Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH getrandom 2 2023-10-31 "Linux man-pages 6.7"
.SH NAME
getrandom \- obtain a series of random bytes
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <sys/random.h>
.P
.BI "ssize_t getrandom(void " buf [. buflen "], size_t " buflen ", \
unsigned int " flags );
.fi
.SH DESCRIPTION
The
.BR getrandom ()
system call fills the buffer pointed to by
.I buf
with up to
.I buflen
random bytes.
These bytes can be used to seed user-space random number generators
or for cryptographic purposes.
.P
By default,
.BR getrandom ()
draws entropy from the
.I urandom
source (i.e., the same source as the
.I /dev/urandom
device).
This behavior can be changed via the
.I flags
argument.
.P
If the
.I urandom
source has been initialized,
reads of up to 256 bytes will always return as many bytes as
requested and will not be interrupted by signals.
No such guarantees apply for larger buffer sizes.
For example, if the call is interrupted by a signal handler,
it may return a partially filled buffer, or fail with the error
.BR EINTR .
.P
If the
.I urandom
source has not yet been initialized, then
.BR getrandom ()
will block, unless
.B GRND_NONBLOCK
is specified in
.IR flags .
.P
The
.I flags
argument is a bit mask that can contain zero or more of the following values
ORed together:
.TP
.B GRND_RANDOM
If this bit is set, then random bytes are drawn from the
.I random
source
(i.e., the same source as the
.I /dev/random
device)
instead of the
.I urandom
source.
The
.I random
source is limited based on the entropy that can be obtained from environmental
noise.
If the number of available bytes in the
.I random
source is less than requested in
.IR buflen ,
the call returns just the available random bytes.
If no random bytes are available, the behavior depends on the presence of
.B GRND_NONBLOCK
in the
.I flags
argument.
.TP
.B GRND_NONBLOCK
By default, when reading from the
.I random
source,
.BR getrandom ()
blocks if no random bytes are available,
and when reading from the
.I urandom
source, it blocks if the entropy pool has not yet been initialized.
If the
.B GRND_NONBLOCK
flag is set, then
.BR getrandom ()
does not block in these cases, but instead immediately returns \-1 with
.I errno
set to
.BR EAGAIN .
.SH RETURN VALUE
On success,
.BR getrandom ()
returns the number of bytes that were copied to the buffer
.IR buf .
This may be less than the number of bytes requested via
.I buflen
if either
.B GRND_RANDOM
was specified in
.I flags
and insufficient entropy was present in the
.I random
source or the system call was interrupted by a signal.
.P
On error, \-1 is returned, and
.I errno
is set to indicate the error.
.SH ERRORS
.TP
.B EAGAIN
The requested entropy was not available, and
.BR getrandom ()
would have blocked if the
.B GRND_NONBLOCK
flag was not set.
.TP
.B EFAULT
The address referred to by
.I buf
is outside the accessible address space.
.TP
.B EINTR
The call was interrupted by a signal
handler; see the description of how interrupted
.BR read (2)
calls on "slow" devices are handled with and without the
.B SA_RESTART
flag in the
.BR signal (7)
man page.
.TP
.B EINVAL
An invalid flag was specified in
.IR flags .
.TP
.B ENOSYS
The glibc wrapper function for
.BR getrandom ()
determined that the underlying kernel does not implement this system call.
.SH STANDARDS
Linux.
.SH HISTORY
Linux 3.17,
glibc 2.25.
.SH NOTES
For an overview and comparison of the various interfaces that
can be used to obtain randomness, see
.BR random (7).
.P
Unlike
.I /dev/random
and
.IR /dev/urandom ,
.BR getrandom ()
does not involve the use of pathnames or file descriptors.
Thus,
.BR getrandom ()
can be useful in cases where
.BR chroot (2)
makes
.I /dev
pathnames invisible,
and where an application (e.g., a daemon during start-up)
closes a file descriptor for one of these files
that was opened by a library.
.\"
.SS Maximum number of bytes returned
As of Linux 3.19 the following limits apply:
.IP \[bu] 3
When reading from the
.I urandom
source, a maximum of 32Mi-1 bytes is returned by a single call to
.BR getrandom ()
on systems where
.I int
has a size of 32 bits.
.IP \[bu]
When reading from the
.I random
source, a maximum of 512 bytes is returned.
.SS Interruption by a signal handler
When reading from the
.I urandom
source
.RB ( GRND_RANDOM
is not set),
.BR getrandom ()
will block until the entropy pool has been initialized
(unless the
.B GRND_NONBLOCK
flag was specified).
If a request is made to read a large number of bytes (more than 256),
.BR getrandom ()
will block until those bytes have been generated and transferred
from kernel memory to
.IR buf .
When reading from the
.I random
source
.RB ( GRND_RANDOM
is set),
.BR getrandom ()
will block until some random bytes become available
(unless the
.B GRND_NONBLOCK
flag was specified).
.P
The behavior when a call to
.BR getrandom ()
that is blocked while reading from the
.I urandom
source is interrupted by a signal handler
depends on the initialization state of the entropy buffer
and on the request size,
.IR buflen .
If the entropy is not yet initialized, then the call fails with the
.B EINTR
error.
If the entropy pool has been initialized
and the request size is large
.RI ( buflen "\ >\ 256),"
the call either succeeds, returning a partially filled buffer,
or fails with the error
.BR EINTR .
If the entropy pool has been initialized and the request size is small
.RI ( buflen "\ <=\ 256),"
then
.BR getrandom ()
will not fail with
.BR EINTR .
Instead, it will return all of the bytes that have been requested.
.P
When reading from the
.I random
source, blocking requests of any size can be interrupted by a signal handler
(the call fails with the error
.BR EINTR ).
.P
Using
.BR getrandom ()
to read small buffers (<=\ 256 bytes) from the
.I urandom
source is the preferred mode of usage.
.P
The special treatment of small values of
.I buflen
was designed for compatibility with
OpenBSD's
.BR getentropy (3),
which is nowadays supported by glibc.
.P
The user of
.BR getrandom ()
.I must
always check the return value,
to determine whether either an error occurred
or fewer bytes than requested were returned.
In the case where
.B GRND_RANDOM
is not specified and
.I buflen
is less than or equal to 256,
a return of fewer bytes than requested should never happen,
but the careful programmer will check for this anyway!
.SH BUGS
As of Linux 3.19, the following bug exists:
.\" FIXME patch proposed https://lkml.org/lkml/2014/11/29/16
.IP \[bu] 3
Depending on CPU load,
.BR getrandom ()
does not react to interrupts before reading all bytes requested.
.SH SEE ALSO
.BR getentropy (3),
.BR random (4),
.BR urandom (4),
.BR random (7),
.BR signal (7)