-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlrng_interface_random_user.c
104 lines (89 loc) · 2.74 KB
/
lrng_interface_random_user.c
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
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
/*
* LRNG Common user space interfaces compliant to random(4), random(7) and
* getrandom(2) man pages.
*
* Copyright (C) 2022, Stephan Mueller <smueller@chronox.de>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/random.h>
#include <linux/syscalls.h>
#include "lrng_es_mgr.h"
#include "lrng_interface_dev_common.h"
static ssize_t lrng_drng_read(struct file *file, char __user *buf,
size_t nbytes, loff_t *ppos)
{
if (!lrng_state_min_seeded())
pr_notice_ratelimited("%s - use of insufficiently seeded DRNG (%zu bytes read)\n",
current->comm, nbytes);
else if (!lrng_state_operational())
pr_debug_ratelimited("%s - use of not fully seeded DRNG (%zu bytes read)\n",
current->comm, nbytes);
return lrng_read_common(buf, nbytes, false);
}
const struct file_operations random_fops = {
.read = lrng_drng_read_block,
.write = lrng_drng_write,
.poll = lrng_random_poll,
.unlocked_ioctl = lrng_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.fasync = lrng_fasync,
.llseek = noop_llseek,
};
const struct file_operations urandom_fops = {
.read = lrng_drng_read,
.write = lrng_drng_write,
.unlocked_ioctl = lrng_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.fasync = lrng_fasync,
.llseek = noop_llseek,
};
/*
* GRND_SEED
*
* This flag requests to provide the data directly from the entropy sources.
*
* The behavior of the call is exactly as outlined for the function
* lrng_get_seed in lrng.h.
*/
#define GRND_SEED 0x0010
/*
* GRND_FULLY_SEEDED
*
* This flag indicates whether the caller wants to reseed a DRNG that is already
* fully seeded. See esdm_get_seed in lrng.h for details.
*/
#define GRND_FULLY_SEEDED 0x0020
SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count,
unsigned int, flags)
{
if (flags & ~(GRND_NONBLOCK|GRND_RANDOM|GRND_INSECURE|
GRND_SEED|GRND_FULLY_SEEDED))
return -EINVAL;
/*
* Requesting insecure and blocking randomness at the same time makes
* no sense.
*/
if ((flags &
(GRND_INSECURE|GRND_RANDOM)) == (GRND_INSECURE|GRND_RANDOM))
return -EINVAL;
if ((flags &
(GRND_INSECURE|GRND_SEED)) == (GRND_INSECURE|GRND_SEED))
return -EINVAL;
if ((flags &
(GRND_RANDOM|GRND_SEED)) == (GRND_RANDOM|GRND_SEED))
return -EINVAL;
if (count > INT_MAX)
count = INT_MAX;
if (flags & GRND_INSECURE) {
return lrng_drng_read(NULL, buf, count, NULL);
} else if (flags & GRND_SEED) {
unsigned int seed_flags = (flags & GRND_NONBLOCK) ?
LRNG_GET_SEED_NONBLOCK : 0;
seed_flags |= (flags & GRND_FULLY_SEEDED) ?
LRNG_GET_SEED_FULLY_SEEDED : 0;
return lrng_read_seed(buf, count, seed_flags);
}
return lrng_read_common_block(flags & GRND_NONBLOCK,
flags & GRND_RANDOM, buf, count);
}