-
Notifications
You must be signed in to change notification settings - Fork 2
/
bsdrngd.c
302 lines (278 loc) · 7.19 KB
/
bsdrngd.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
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
296
297
298
299
300
301
302
/*
* Copyright (c) 2017, W. Dean Freeman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/capsicum.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#define MAX_CONF_LINE_BUF FILENAME_MAX + 9
#define DELIMETER "="
/* structure containing configuration file items */
typedef struct conf {
char entropy_device[FILENAME_MAX];
char read_bytes[5];
char sleep_seconds[3];
} conf_t;
static volatile sig_atomic_t wantdie = 0;
static void
usage(void)
{
fprintf(stderr,"USAGE: bsdrngd [-d] [-c config_file]\nBy default, will run in the foreground and load its config from /usr/local/etc/bsd-rngd.conf\n");
exit(1);
}
static void
dodie(int signo)
{
wantdie = signo;
}
/* read entropy from the trng device */
void
read_entropy(int fd, char *buf, uint32_t n)
{
ssize_t rv;
flock(fd, LOCK_EX);
rv = read(fd,buf,n);
if ( rv < 0 )
{
syslog(LOG_ERR, "Error reading bytes from entropy source: %s", strerror(errno));
exit(-1);
}
flock(fd, LOCK_UN);
}
void
write_entropy(int d, char *buf, int n)
{
ssize_t rv = 0;
rv = write(d,buf,n);
if ( rv < 0 )
{
syslog(LOG_ERR, "Unable to write to /dev/random: %s", strerror(errno));
exit(-1);
}
}
/* main daemon child loop */
void entropy_feed(char *dev, uint32_t n, uint32_t s)
{
cap_rights_t rights;
FILE *trng = fopen(dev,"r");
if (trng == NULL)
{
syslog(LOG_ERR, "Unable to open trng device for read: %s", strerror(errno));
exit(-1);
}
cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
if (cap_rights_limit(fileno(trng), &rights) < 0 && errno != ENOSYS)
{
syslog(LOG_ERR, "Unable to set capsicum rights limit on trng file handle: %s", strerror(errno));
exit(-1);
}
FILE *rnd = fopen("/dev/random", "w");
if (rnd == NULL)
{
syslog(LOG_ERR, "Unable to open /dev/random for writing: %s", strerror(errno));
exit(-1);
}
cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
if (cap_rights_limit(fileno(rnd), &rights) < 0 && errno != ENOSYS)
{
syslog(LOG_ERR, "Unable to set capsicum rights limut on /dev/random file handle: %s", strerror(errno));
exit(-1);
}
syslog(LOG_NOTICE, "bsd-rngd: entropy gathering daemon started for device %s", dev);
cap_enter();
char buf[n];
explicit_bzero(buf,n);
/* main loop to do the thing */
for(;;)
{
if(wantdie)
return;
read_entropy(fileno(trng),buf,n);
fpurge(trng);
if (n <= 16)
{
write_entropy(fileno(rnd),buf,n);
}
else
{
char ent[8];
explicit_bzero(ent,8);
for(int i = 0; i < (n - 8); i += 8)
{
memcpy(ent,buf,8);
write_entropy(fileno(rnd),ent,8);
}
explicit_bzero(ent,8);
}
explicit_bzero(buf,n);
sleep(s);
}
}
/* Perl-like utility function */
void
chomp(char *s)
{
if (s[strlen(s)-1] == '\n')
s[strlen(s)-1] = '\0';
}
/* read in the configuration file */
void
read_config(conf_t *c, char *f, int d)
{
FILE *fh = fopen(f,"r");
if (fh == NULL)
{
if (d == 1)
{
syslog(LOG_ERR, "Unable to open %s for read: %s", f, strerror(errno));
exit(-1);
}
else
{
fprintf(stderr, "Unable to open %s for read: %s\n", f, strerror(errno));
exit(-1);
}
}
flock(fileno(fh), LOCK_EX);
char line[MAX_CONF_LINE_BUF];
while(fgets(line,sizeof(line), fh) != NULL)
{
chomp(line);
char *conf_item;
conf_item = strstr((char*)line,DELIMETER);
conf_item = conf_item + strlen(DELIMETER);
if (strstr(line, "DEVICE") !=0)
{
strlcpy(c->entropy_device, conf_item, FILENAME_MAX);
}
if (strstr(line, "BYTES") != 0)
{
strlcpy(c->read_bytes, conf_item, 5);
}
if (strstr(line, "INTERVAL") != 0)
{
strlcpy(c->sleep_seconds, conf_item, 3);
}
}
flock(fileno(fh),LOCK_UN);
fclose(fh);
}
int
main(int argc, char *argv[])
{
int ch;
conf_t config;
int c = 0;
int daemonize = 0;
struct pidfh *pfh;
pid_t spid;
while((ch = getopt(argc, argv, "hdc:")) != -1)
{
switch(ch)
{
case 'h':
usage();
break;
case 'd':
daemonize = 1;
break;
case 'c':
read_config(&config,optarg,daemonize);
c = 1;
break;
default:
usage();
}
}
if (c == 0)
read_config(&config, "/usr/local/etc/bsd-rngd.conf",daemonize);
uint32_t bytes = 0, sleepsec = 0;
const char *errstr;
bytes = (uint32_t)strtonum(config.read_bytes,8,4096,&errstr);
if (errstr != NULL)
{
if (daemonize == 0)
{
errx(1, "BYTES value is out of range (8, 4096): %u :%s", bytes, errstr);
exit(-1);
}
else
{
syslog(LOG_ERR, "Value for bytes to read is out of range (8,4096): %d: %s", bytes, errstr);
exit(-1);
}
}
if ((bytes % 8) != 0)
{
if (daemonize == 0)
{
fprintf(stderr,"Error: specified value for read_bytes %u is not a multiple of 8!\n", bytes);
exit(-1);
}
else
{
syslog(LOG_ERR, "specified value for read_bytes %u is not a multiple of 8!\n", bytes);
exit(-1);
}
}
sleepsec = (uint32_t)strtonum(config.sleep_seconds, 0,10,&errstr);
if (errstr != NULL)
{
if (daemonize == 0)
{
errx(1, "Sleep seconds is out of range (0,10): %u: %s", (uint32_t)sleepsec, errstr);
exit(-1);
}
else
{
syslog(LOG_ERR, "Specified value for sleep interval %u is out of range (0,10): %s", (uint32_t)sleepsec, errstr);
exit(-1);
}
}
pfh = pidfile_open(NULL, 0600, &spid);
if (pfh == NULL)
{
if (errno == EEXIST)
errx(EXIT_FAILURE, "Daemon already running, pid %d", spid);
warn("Cannot open or create pid file");
}
if ((daemonize == 1) && (daemon(0,0) == -1))
{
pidfile_remove(pfh);
err(EXIT_FAILURE, "Cannot daemonize");
}
(void)signal(SIGTERM, dodie);
pidfile_write(pfh);
/* get to doing work */
entropy_feed(config.entropy_device, bytes, sleepsec);
pidfile_remove(pfh);
return 0;
}