]> granicus.if.org Git - strace/commitdiff
Add support for /dev/[u]random ioctls
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>
Mon, 5 Nov 2018 17:29:00 +0000 (17:29 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 5 Nov 2018 17:40:36 +0000 (17:40 +0000)
* random_ioctl.c: New file.
* Makefile.am (strace_SOURCES): Add it.
* defs.h (DECL_IOCTL): Add random.
* ioctl.c (ioctl_decode): Add 'R' case.
* xlat/random_ioctl_cmds.in: New file.
* tests/ioctl_random.c: New file.
* tests/.gitignore: Add ioctl_random.
* tests/pure_executables.list: Likewise.
* tests/gen_tests.in (ioctl_random): New entry.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Makefile.am
defs.h
ioctl.c
random_ioctl.c [new file with mode: 0644]
tests/.gitignore
tests/gen_tests.in
tests/ioctl_random.c [new file with mode: 0644]
tests/pure_executables.list
xlat/random_ioctl_cmds.in [new file with mode: 0644]

index db16b3aac7aa4e3fa7353266d8273af56b70065a..350bc51a9e108504409cb7d660d1ebed7ccc68b7 100644 (file)
@@ -267,6 +267,7 @@ strace_SOURCES =    \
        ptp.c           \
        ptrace.h        \
        quota.c         \
+       random_ioctl.c  \
        readahead.c     \
        readlink.c      \
        reboot.c        \
diff --git a/defs.h b/defs.h
index 5ba95f53d788952a0060c3a0d32f13813b4da3c6..b8d561aab61cccd54c6334321ec3c4db43b46c7f 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -973,6 +973,7 @@ DECL_IOCTL(kvm);
 DECL_IOCTL(nbd);
 DECL_IOCTL(nsfs);
 DECL_IOCTL(ptp);
+DECL_IOCTL(random);
 DECL_IOCTL(scsi);
 DECL_IOCTL(term);
 DECL_IOCTL(ubi);
diff --git a/ioctl.c b/ioctl.c
index 4c9e7db313d1bb14fd082a8fee8d0a0b266dd439..e7dd4c4ec72fcf16f701ce0c16f880d58b649dea 100644 (file)
--- a/ioctl.c
+++ b/ioctl.c
@@ -329,6 +329,8 @@ ioctl_decode(struct tcb *tcp)
                return inotify_ioctl(tcp, code, arg);
        case 0xab:
                return nbd_ioctl(tcp, code, arg);
+       case 'R':
+               return random_ioctl(tcp, code, arg);
        default:
                break;
        }
diff --git a/random_ioctl.c b/random_ioctl.c
new file mode 100644 (file)
index 0000000..6eaf6da
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 "defs.h"
+#include "print_fields.h"
+
+#include <linux/types.h>
+#include <linux/random.h>
+
+#define XLAT_MACROS_ONLY
+# include "xlat/random_ioctl_cmds.h"
+#undef XLAT_MACROS_ONLY
+
+/*
+ * RNDGETPOOL was removed in 2.6.9, so non-ancient kernels always
+ * return -EINVAL for that.
+ */
+
+int
+random_ioctl(struct tcb *const tcp, const unsigned int code,
+          const kernel_ulong_t arg)
+{
+       struct rand_pool_info info;
+       kernel_ulong_t buf;
+
+       switch (code) {
+       case RNDGETENTCNT:
+               if (entering(tcp))
+                       return 0;
+               ATTRIBUTE_FALLTHROUGH;
+       case RNDADDTOENTCNT:
+               tprints(", ");
+               printnum_int(tcp, arg, "%d");
+               break;
+
+       case RNDADDENTROPY:
+               tprints(", ");
+               if (!umove_or_printaddr(tcp, arg, &info)) {
+                       PRINT_FIELD_D("{", info, entropy_count);
+                       PRINT_FIELD_D(", ", info, buf_size);
+                       tprints(", buf=");
+                       buf = arg + offsetof(struct rand_pool_info, buf);
+                       printstrn(tcp, buf, info.buf_size);
+                       tprints("}");
+               }
+               break;
+
+       /* ioctls with no parameters */
+       case RNDZAPENTCNT:
+       case RNDCLEARPOOL:
+       case RNDRESEEDCRNG:
+               break;
+       default:
+               return RVAL_DECODED;
+       }
+       return RVAL_IOCTL_DECODED;
+}
index 577a1316da0630bd90aa3421dca4d9ad4e387634..297319eff9421702c6ece2b6759cfb88ce7c224e 100644 (file)
@@ -158,6 +158,7 @@ ioctl_nsfs
 ioctl_perf
 ioctl_perf-success
 ioctl_ptp
+ioctl_random
 ioctl_rtc
 ioctl_rtc-v
 ioctl_scsi
index cd63788d8893d0d7d31fcc62348444f844a25933..f36cb62f99ff72d112e7687659c0dba7b7b42af7 100644 (file)
@@ -150,6 +150,7 @@ ioctl_nbd   +ioctl.test -y
 ioctl_nsfs     +ioctl.test -esignal=none
 ioctl_perf     +ioctl.test
 ioctl_ptp      +ioctl.test
+ioctl_random   +ioctl.test
 ioctl_rtc      +ioctl.test
 ioctl_rtc-v    +ioctl.test -v
 ioctl_scsi     +ioctl.test
diff --git a/tests/ioctl_random.c b/tests/ioctl_random.c
new file mode 100644 (file)
index 0000000..06545e8
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Check decoding of RND* commands of ioctl syscall.
+ *
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 "tests.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <linux/types.h>
+#include <linux/random.h>
+
+#define XLAT_MACROS_ONLY
+# include "xlat/random_ioctl_cmds.h"
+#undef XLAT_MACROS_ONLY
+
+#define RVAL_EBADF " = -1 EBADF (%m)\n"
+
+int
+main(void)
+{
+       union {
+               char c[sizeof(struct rand_pool_info) + 8];
+               struct rand_pool_info info;
+       } u;
+       struct rand_pool_info *info = &u.info;
+       int cnt = 6;
+
+       memcpy(info->buf, "12345678", 8);
+       info->buf_size = 8;
+       info->entropy_count = 3;
+
+       ioctl(-1, RNDGETENTCNT, &cnt);
+       printf("ioctl(-1, RNDGETENTCNT, %p)" RVAL_EBADF, &cnt);
+       ioctl(-1, RNDADDTOENTCNT, &cnt);
+       printf("ioctl(-1, RNDADDTOENTCNT, [6])" RVAL_EBADF);
+       ioctl(-1, RNDADDENTROPY, info);
+       printf("ioctl(-1, RNDADDENTROPY, {entropy_count=3, buf_size=8, buf=\"12345678\"})" RVAL_EBADF);
+
+       ioctl(-1, RNDZAPENTCNT);
+       printf("ioctl(-1, RNDZAPENTCNT)" RVAL_EBADF);
+       ioctl(-1, RNDCLEARPOOL);
+       printf("ioctl(-1, RNDCLEARPOOL)" RVAL_EBADF);
+       ioctl(-1, RNDRESEEDCRNG);
+       printf("ioctl(-1, RNDRESEEDCRNG)" RVAL_EBADF);
+
+       puts("+++ exited with 0 +++");
+       return 0;
+}
index 69097bfb0955038ae24b7efdc2720fca0e2afa3a..d787f78e46fa176d34d65529a729c723a2d0e224 100755 (executable)
@@ -121,6 +121,7 @@ ioctl_mtd
 ioctl_nbd
 ioctl_perf
 ioctl_ptp
+ioctl_random
 ioctl_rtc
 ioctl_scsi
 ioctl_sg_io_v3
diff --git a/xlat/random_ioctl_cmds.in b/xlat/random_ioctl_cmds.in
new file mode 100644 (file)
index 0000000..1b31dc5
--- /dev/null
@@ -0,0 +1,7 @@
+RNDGETENTCNT           _IOR( 'R', 0x00, int )
+RNDADDTOENTCNT         _IOW( 'R', 0x01, int )
+RNDGETPOOL             _IOR( 'R', 0x02, int [2] )
+RNDADDENTROPY          _IOW( 'R', 0x03, int [2] )
+RNDZAPENTCNT           _IO( 'R', 0x04 )
+RNDCLEARPOOL           _IO( 'R', 0x06 )
+RNDRESEEDCRNG          _IO( 'R', 0x07 )