Rizin
unix-like reverse engineering framework and cli tools
random-sysctl-linux.c File Reference
#include "uv.h"
#include "internal.h"
#include <errno.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  uv__sysctl_args
 

Functions

int uv__random_sysctl (void *buf, size_t buflen)
 

Function Documentation

◆ uv__random_sysctl()

int uv__random_sysctl ( void *  buf,
size_t  buflen 
)

Definition at line 43 of file random-sysctl-linux.c.

43  {
44  static int name[] = {1 /*CTL_KERN*/, 40 /*KERN_RANDOM*/, 6 /*RANDOM_UUID*/};
45  struct uv__sysctl_args args;
46  char uuid[16];
47  char* p;
48  char* pe;
49  size_t n;
50 
51  p = buf;
52  pe = p + buflen;
53 
54  while (p < pe) {
55  memset(&args, 0, sizeof(args));
56 
57  args.name = name;
58  args.nlen = ARRAY_SIZE(name);
59  args.oldval = uuid;
60  args.oldlenp = &n;
61  n = sizeof(uuid);
62 
63  /* Emits a deprecation warning with some kernels but that seems like
64  * an okay trade-off for the fallback of the fallback: this function is
65  * only called when neither getrandom(2) nor /dev/urandom are available.
66  * Fails with ENOSYS on kernels configured without CONFIG_SYSCTL_SYSCALL.
67  * At least arm64 never had a _sysctl system call and therefore doesn't
68  * have a SYS__sysctl define either.
69  */
70 #ifdef SYS__sysctl
71  if (syscall(SYS__sysctl, &args) == -1)
72  return UV__ERR(errno);
73 #else
74  {
75  (void) &args;
76  return UV_ENOSYS;
77  }
78 #endif
79 
80  if (n != sizeof(uuid))
81  return UV_EIO; /* Can't happen. */
82 
83  /* uuid[] is now a type 4 UUID. Bytes 6 and 8 (counting from zero) contain
84  * 4 and 5 bits of entropy, respectively. For ease of use, we skip those
85  * and only use 14 of the 16 bytes.
86  */
87  uuid[6] = uuid[14];
88  uuid[8] = uuid[15];
89 
90  n = pe - p;
91  if (n > 14)
92  n = 14;
93 
94  memcpy(p, uuid, n);
95  p += n;
96  }
97 
98  return 0;
99 }
#define ARRAY_SIZE(a)
#define UV__ERR(x)
Definition: errno.h:29
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
int args
Definition: mipsasm.c:18
int n
Definition: mipsasm.c:19
const char * name
Definition: op.c:541
Definition: z80asm.h:102
const char * syscall
Definition: internal.h:270
ut64 buflen
Definition: core.c:76

References args, ARRAY_SIZE, buflen, count, len, memcpy(), memset(), n, name, NULL, p, req, syscall, and UV__ERR.

Referenced by uv__random().