xref: /freebsd/contrib/expat/lib/random_getrandom.c (revision 207d96dabfec14d7b3699747abb539ab3c1118ab)
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 2017-2026 Sebastian Pipping <sebastian@pipping.org>
10    Copyright (c) 2017      Chanho Park <chanho61.park@samsung.com>
11    Copyright (c) 2022      Sean McBride <sean@rogue-research.com>
12    Copyright (c) 2026      Matthew Fernandez <matthew.fernandez@gmail.com>
13    Licensed under the MIT license:
14 
15    Permission is  hereby granted,  free of charge,  to any  person obtaining
16    a  copy  of  this  software   and  associated  documentation  files  (the
17    "Software"),  to  deal in  the  Software  without restriction,  including
18    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
19    distribute, sublicense, and/or sell copies of the Software, and to permit
20    persons  to whom  the Software  is  furnished to  do so,  subject to  the
21    following conditions:
22 
23    The above copyright  notice and this permission notice  shall be included
24    in all copies or substantial portions of the Software.
25 
26    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
27    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
28    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
29    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
30    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
31    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
32    USE OR OTHER DEALINGS IN THE SOFTWARE.
33 
34    SPDX-License-Identifier: MIT
35 */
36 
37 #include "expat_config.h" // for HAVE_GETRANDOM, HAVE_SYSCALL_GETRANDOM
38 
39 #include "random_getrandom.h"
40 
41 #if defined(HAVE_GETRANDOM)
42 #  include <sys/random.h> /* getrandom */
43 #endif
44 
45 #if defined(HAVE_SYSCALL_GETRANDOM)
46 #  if ! defined(_GNU_SOURCE)
47 #    define _GNU_SOURCE 1 /* syscall prototype */
48 #  endif
49 #  include <unistd.h>      /* syscall */
50 #  include <sys/syscall.h> /* SYS_getrandom */
51 #endif                     // defined(HAVE_SYSCALL_GETRANDOM)
52 
53 #if ! defined(GRND_NONBLOCK)
54 #  define GRND_NONBLOCK 0x0001
55 #endif /* defined(GRND_NONBLOCK) */
56 
57 #include "memory_sanitizer.h"
58 #include <assert.h>
59 #include <errno.h>
60 #include <limits.h> // for INT_MAX
61 
62 /* Obtain entropy on Linux 3.17+ */
63 bool
writeRandomBytes_getrandom_nonblock(void * target,size_t count)64 writeRandomBytes_getrandom_nonblock(void *target, size_t count) {
65   int success = false; /* full count bytes written? */
66   size_t bytesWrittenTotal = 0;
67   const unsigned int getrandomFlags = GRND_NONBLOCK;
68 
69   do {
70     void *const currentTarget = (char *)target + bytesWrittenTotal;
71     const size_t bytesToWrite = count - bytesWrittenTotal;
72 
73     assert(bytesToWrite <= INT_MAX);
74 
75     errno = 0;
76 
77     const int bytesWrittenMore =
78 #if defined(HAVE_GETRANDOM)
79         (int)getrandom(currentTarget, bytesToWrite, getrandomFlags);
80     // MSan understands `getrandom`, so does not need extra guidance
81 #else
82         (int)syscall(SYS_getrandom, currentTarget, bytesToWrite,
83                      getrandomFlags);
84     // MSan does not understand `syscall`, so explain its effects
85     if (bytesWrittenMore > 0)
86       MSAN_UNPOISON(currentTarget, bytesWrittenMore);
87 #endif
88 
89     if (bytesWrittenMore > 0) {
90       bytesWrittenTotal += bytesWrittenMore;
91       if (bytesWrittenTotal >= count)
92         success = true;
93     }
94   } while (! success && (errno == EINTR));
95 
96   return success;
97 }
98