Debian Codegraph @

upstream/debian-trixie/man2/getrandom.2

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