Browse Source

Implement getrandom syscall decoding

* getrandom.c: New file.
* Makefile.am (strace_SOURCES): Add it.
* linux/dummy.h (sys_getrandom): Remove.
* linux/syscall.h (sys_getrandom): New prototype.
* xlat/getrandom_flags.in: New file.
* tests/getrandom.c: New file.
* tests/getrandom.awk: New file.
* tests/getrandom.test: New test.
* tests/Makefile.am (check_PROGRAMS): Add getrandom.
(TESTS): Add getrandom.test.
(EXTRA_DIST): Add getrandom.awk.
* tests/.gitignore: Add getrandom.
Dmitry V. Levin 5 years ago
parent
commit
fef09070d6
10 changed files with 106 additions and 4 deletions
  1. 1
    0
      Makefile.am
  2. 16
    0
      getrandom.c
  3. 0
    1
      linux/dummy.h
  4. 1
    0
      linux/syscall.h
  5. 1
    0
      tests/.gitignore
  6. 5
    3
      tests/Makefile.am
  7. 26
    0
      tests/getrandom.awk
  8. 21
    0
      tests/getrandom.c
  9. 23
    0
      tests/getrandom.test
  10. 12
    0
      xlat/getrandom_flags.in

+ 1
- 0
Makefile.am View File

@@ -50,6 +50,7 @@ strace_SOURCES =	\
50 50
 	get_robust_list.c \
51 51
 	getcpu.c	\
52 52
 	getcwd.c	\
53
+	getrandom.c	\
53 54
 	hostname.c	\
54 55
 	inotify.c	\
55 56
 	io.c		\

+ 16
- 0
getrandom.c View File

@@ -0,0 +1,16 @@
1
+#include "defs.h"
2
+#include "xlat/getrandom_flags.h"
3
+
4
+int
5
+sys_getrandom(struct tcb *tcp)
6
+{
7
+	if (exiting(tcp)) {
8
+		if (syserror(tcp))
9
+			tprintf("%#lx", tcp->u_arg[0]);
10
+		else
11
+			printstr(tcp, tcp->u_arg[0], tcp->u_rval);
12
+		tprintf(", %lu, ", tcp->u_arg[1]);
13
+		printflags(getrandom_flags, tcp->u_arg[2], "GRND_???");
14
+	}
15
+	return 0;
16
+}

+ 0
- 1
linux/dummy.h View File

@@ -34,7 +34,6 @@
34 34
 /* still unfinished */
35 35
 #define	sys_bpf			printargs
36 36
 #define	sys_execveat		printargs
37
-#define	sys_getrandom		printargs
38 37
 #define	sys_ioperm		printargs
39 38
 #define	sys_iopl		printargs
40 39
 #define	sys_kcmp		printargs

+ 1
- 0
linux/syscall.h View File

@@ -104,6 +104,7 @@ int sys_gethostname();
104 104
 int sys_getitimer();
105 105
 int sys_getpmsg(); /* TODO: non-Linux, remove? */
106 106
 int sys_getpriority();
107
+int sys_getrandom();
107 108
 int sys_getresuid();
108 109
 int sys_getrlimit();
109 110
 int sys_getrusage();

+ 1
- 0
tests/.gitignore View File

@@ -1,5 +1,6 @@
1 1
 caps
2 2
 fanotify_mark
3
+getrandom
3 4
 inet-accept-connect-send-recv
4 5
 ioctl
5 6
 ipc_msg

+ 5
- 3
tests/Makefile.am View File

@@ -3,13 +3,14 @@
3 3
 AM_CFLAGS = $(WARN_CFLAGS)
4 4
 
5 5
 check_PROGRAMS = \
6
+	caps \
7
+	fanotify_mark \
8
+	getrandom \
6 9
 	inet-accept-connect-send-recv \
7 10
 	ioctl \
8 11
 	ipc_msg \
9
-	ipc_shm \
10 12
 	ipc_sem \
11
-	caps \
12
-	fanotify_mark \
13
+	ipc_shm \
13 14
 	mmsg \
14 15
 	net-accept-connect \
15 16
 	netlink_inet_diag \
@@ -42,6 +43,7 @@ TESTS = \
42 43
 	dumpio.test \
43 44
 	fanotify_mark.test \
44 45
 	getdents.test \
46
+	getrandom.test \
45 47
 	ioctl.test \
46 48
 	ipc_msg.test \
47 49
 	ipc_shm.test \

+ 26
- 0
tests/getrandom.awk View File

@@ -0,0 +1,26 @@
1
+BEGIN {
2
+	r[1] = "^getrandom\\(\"(\\\\x[0-9a-f][0-9a-f]){3}\", 3, 0\\) += 3$"
3
+	r[2] = "^getrandom\\(\"(\\\\x[0-9a-f][0-9a-f]){3}\"\\.\\.\\., 4, GRND_NONBLOCK\\) += 4$"
4
+	r[3] = "^getrandom\\(0x[[0-9a-f]+, 4, GRND_NONBLOCK\\|GRND_RANDOM\\|0x3000\\) += -1 "
5
+	r[4] = "^\\+\\+\\+ exited with 0 \\+\\+\\+$"
6
+	lines = 4
7
+	fail = 0
8
+}
9
+
10
+NR > lines { exit 1 }
11
+
12
+{
13
+	if (match($0, r[NR]))
14
+		next
15
+
16
+	print "Line " NR " does not match."
17
+	fail = 1
18
+}
19
+
20
+END {
21
+	if (fail == 0 && NR != lines) {
22
+		fail = 1
23
+		print "Expected " lines " lines, found " NR " line(s)."
24
+	}
25
+	exit fail
26
+}

+ 21
- 0
tests/getrandom.c View File

@@ -0,0 +1,21 @@
1
+#include <unistd.h>
2
+#include <sys/syscall.h>
3
+
4
+int
5
+main(void)
6
+{
7
+#ifdef __NR_getrandom
8
+	char buf[4];
9
+
10
+	if (syscall(__NR_getrandom, buf, sizeof(buf) - 1, 0) != sizeof(buf) - 1)
11
+		return 77;
12
+	if (syscall(__NR_getrandom, buf, sizeof(buf), 1) != sizeof(buf))
13
+		return 77;
14
+	if (syscall(__NR_getrandom, buf, sizeof(buf), 0x3003) != -1)
15
+		return 77;
16
+
17
+	return 0;
18
+#else
19
+	return 77;
20
+#endif
21
+}

+ 23
- 0
tests/getrandom.test View File

@@ -0,0 +1,23 @@
1
+#!/bin/sh
2
+
3
+# Check getrandom syscall decoding.
4
+
5
+. "${srcdir=.}/init.sh"
6
+
7
+check_prog awk
8
+
9
+./getrandom ||
10
+	framework_skip_ 'getrandom is not available'
11
+
12
+args="-e getrandom -xx -s3 ./getrandom"
13
+$STRACE -o "$LOG" $args || {
14
+	cat "$LOG"
15
+	fail_ "$STRACE $args failed"
16
+}
17
+
18
+awk -f "$srcdir"/getrandom.awk "$LOG" || {
19
+	cat "$LOG"
20
+	fail_ 'unexpected output'
21
+}
22
+
23
+exit 0

+ 12
- 0
xlat/getrandom_flags.in View File

@@ -0,0 +1,12 @@
1
+#unconditional
2
+
3
+#ifndef GRND_NONBLOCK
4
+# define GRND_NONBLOCK 1
5
+#endif
6
+
7
+#ifndef GRND_RANDOM
8
+# define GRND_RANDOM 2
9
+#endif
10
+
11
+GRND_NONBLOCK
12
+GRND_RANDOM

Loading…
Cancel
Save