Skip to content

Commit eacbc5b

Browse files
author
deraadt
committed
Add new getentropy() system call. Code and pressure from matthew.
I accepted that he's right (again) to seperate this out from heavy sysctl API and this will simply a variety of things. Functionname is not used by anyone in the ports tree, so we guess we can use it. Shocking that no application has a function called this. ok matthew & others who pushed him to start this early on
1 parent 5fb0c78 commit eacbc5b

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

include/unistd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: unistd.h,v 1.86 2014/05/16 21:28:15 tedu Exp $ */
1+
/* $OpenBSD: unistd.h,v 1.87 2014/06/13 08:26:10 deraadt Exp $ */
22
/* $NetBSD: unistd.h,v 1.26.4.1 1996/05/28 02:31:51 mrg Exp $ */
33

44
/*-
@@ -538,6 +538,7 @@ void setusershell(void);
538538
int strtofflags(char **, u_int32_t *, u_int32_t *);
539539
int swapctl(int cmd, const void *arg, int misc);
540540
int syscall(int, ...);
541+
ssize_t getentropy(void *, size_t);
541542
pid_t __tfork_thread(const struct __tfork *, size_t, void (*)(void *),
542543
void *);
543544
#endif /* __BSD_VISIBLE */

lib/libc/sys/Makefile.inc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $OpenBSD: Makefile.inc,v 1.117 2014/03/18 22:36:30 miod Exp $
1+
# $OpenBSD: Makefile.inc,v 1.118 2014/06/13 08:26:10 deraadt Exp $
22
# $NetBSD: Makefile.inc,v 1.35 1995/10/16 23:49:07 jtc Exp $
33
# @(#)Makefile.inc 8.1 (Berkeley) 6/17/93
44

@@ -33,7 +33,7 @@ ASM= __get_tcb.o __getcwd.o __semctl.o __set_tcb.o __syscall.o \
3333
fchdir.o fchflags.o fchmod.o fchmodat.o fchown.o \
3434
fchownat.o fcntl.o fhopen.o fhstat.o fhstatfs.o \
3535
flock.o fpathconf.o fstat.o fstatat.o fstatfs.o \
36-
fsync.o futimens.o futimes.o getdents.o getdtablecount.o \
36+
fsync.o futimens.o futimes.o getentropy.o getdents.o getdtablecount.o \
3737
getegid.o geteuid.o getfh.o getfsstat.o getgid.o \
3838
getgroups.o getitimer.o getpeername.o getpgid.o getpgrp.o \
3939
getpid.o getppid.o getpriority.o getresgid.o getresuid.o \
@@ -200,10 +200,10 @@ MAN+= __get_tcb.2 __thrsigdivert.2 __thrsleep.2 _exit.2 accept.2 \
200200
access.2 acct.2 adjfreq.2 adjtime.2 bind.2 brk.2 chdir.2 \
201201
chflags.2 chmod.2 chown.2 chroot.2 clock_gettime.2 close.2 \
202202
closefrom.2 connect.2 dup.2 execve.2 fcntl.2 fhopen.2 flock.2 \
203-
fork.2 fsync.2 getdents.2 getdtablecount.2 getfh.2 getfsstat.2 \
204-
getgid.2 getgroups.2 getitimer.2 getlogin.2 getpeername.2 \
205-
getpgrp.2 getpid.2 getpriority.2 getrlimit.2 getrtable.2 \
206-
getrusage.2 getsid.2 getsockname.2 getsockopt.2 \
203+
fork.2 fsync.2 getentropy.2 getdents.2 getdtablecount.2 \
204+
getfh.2 getfsstat.2 getgid.2 getgroups.2 getitimer.2 getlogin.2 \
205+
getpeername.2 getpgrp.2 getpid.2 getpriority.2 getrlimit.2 \
206+
getrtable.2 getrusage.2 getsid.2 getsockname.2 getsockopt.2 \
207207
gettimeofday.2 getuid.2 intro.2 ioctl.2 issetugid.2 \
208208
kill.2 kqueue.2 ktrace.2 link.2 listen.2 lseek.2 madvise.2 \
209209
mincore.2 minherit.2 mkdir.2 mkfifo.2 mknod.2 mlock.2 \

lib/libc/sys/getentropy.2

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.\" $OpenBSD: getentropy.2,v 1.1 2014/06/13 08:26:10 deraadt Exp $
2+
.\"
3+
.\" Copyright (c) 2014 Theo de Raadt
4+
.\"
5+
.\" Permission to use, copy, modify, and distribute this software for any
6+
.\" purpose with or without fee is hereby granted, provided that the above
7+
.\" copyright notice and this permission notice appear in all copies.
8+
.\"
9+
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
.\"
17+
.Dd $Mdocdate: June 13 2014 $
18+
.Dt GETENTROPY 2
19+
.Os
20+
.Sh NAME
21+
.Nm getentropy
22+
.Nd get entropy
23+
.Sh SYNOPSIS
24+
.Fd #include <unistd.h>
25+
.Ft ssize_t
26+
.Fn getentropy "char *buf" "size_t buflen"
27+
.Sh DESCRIPTION
28+
.Nm
29+
returns a buffer of high-quality seed-grade entropy.
30+
.Pp
31+
This is typically used to seed a process-context generator
32+
like
33+
.Xr arc4random 3 .
34+
.Sh ERRORS
35+
.Nm
36+
.Fn setlogin
37+
will succeed unless:
38+
.Bl -tag -width Er
39+
.It Bq Er EFAULT
40+
The
41+
.Fa name
42+
parameter points to an
43+
invalid address.
44+
.El
45+
.Sh SEE ALSO
46+
.Xr arc4random 3
47+
.Sh HISTORY
48+
The
49+
.Nm
50+
function appeared in
51+
.Ox 5.6 .

sys/dev/rnd.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: rnd.c,v 1.155 2014/02/05 05:54:58 tedu Exp $ */
1+
/* $OpenBSD: rnd.c,v 1.156 2014/06/13 08:26:09 deraadt Exp $ */
22

33
/*
44
* Copyright (c) 2011 Theo de Raadt.
@@ -123,6 +123,8 @@
123123
#include <sys/mutex.h>
124124
#include <sys/task.h>
125125
#include <sys/msgbuf.h>
126+
#include <sys/mount.h>
127+
#include <sys/syscallargs.h>
126128

127129
#include <crypto/md5.h>
128130

@@ -928,3 +930,26 @@ randomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
928930
}
929931
return 0;
930932
}
933+
934+
int
935+
sys_getentropy(struct proc *p, void *v, register_t *retval)
936+
{
937+
struct sys_getentropy_args /* {
938+
syscallarg(void *) buf;
939+
syscallarg(size_t) nbyte;
940+
} */ *uap = v;
941+
char buf[256];
942+
int error;
943+
size_t nbyte;
944+
945+
nbyte = SCARG(uap, nbyte);
946+
if (nbyte > sizeof(buf))
947+
nbyte = sizeof(buf);
948+
949+
arc4random_buf(buf, nbyte);
950+
if ((error = copyout(buf, SCARG(uap, buf), nbyte)) != 0)
951+
return (error);
952+
953+
retval[0] = nbyte;
954+
return (0);
955+
}

sys/kern/syscalls.master

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; $OpenBSD: syscalls.master,v 1.138 2014/02/09 10:04:42 guenther Exp $
1+
; $OpenBSD: syscalls.master,v 1.139 2014/06/13 08:26:10 deraadt Exp $
22
; $NetBSD: syscalls.master,v 1.32 1996/04/23 10:24:21 mycroft Exp $
33

44
; @(#)syscalls.master 8.2 (Berkeley) 1/13/94
@@ -57,7 +57,7 @@
5757
5 STD { int sys_open(const char *path, \
5858
int flags, ... mode_t mode); }
5959
6 STD { int sys_close(int fd); }
60-
7 OBSOL t32_wait4
60+
7 STD { ssize_t sys_getentropy(void *buf, size_t nbyte); }
6161
8 STD { int sys___tfork(const struct __tfork *param, \
6262
size_t psize); }
6363
9 STD { int sys_link(const char *path, const char *link); }

0 commit comments

Comments
 (0)