]> granicus.if.org Git - strace/commitdiff
tests: check injection of return values into a "never fails" syscall
authorEugene Syromyatnikov <evgsyr@gmail.com>
Sun, 11 Feb 2018 23:45:37 +0000 (00:45 +0100)
committerDmitry V. Levin <ldv@altlinux.org>
Fri, 23 Mar 2018 13:43:13 +0000 (13:43 +0000)
* tests/inject-nf.c: New file.
* tests/inject-nf.test: New test.
* tests/.gitignore: Add inject-nf.
* tests/Makefile.am (check_PROGRAMS): Likewise.
(MISC_TESTS): Add inject-nf.test.

Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
tests/.gitignore
tests/Makefile.am
tests/inject-nf.c [new file with mode: 0644]
tests/inject-nf.test [new file with mode: 0755]

index d7a08d3e464b8142f91a842aa0b0699b94516ceb..aec35145c5c02a6c0f39369834f9ea531af475f3 100644 (file)
@@ -123,6 +123,7 @@ getxxid
 group_req
 inet-cmsg
 init_module
+inject-nf
 inotify
 inotify_init1
 int_0x80
index c448070054a61b9c723826a6a79c251642bd9801..352d2b26ddf9579c705284d6b4373797b42f9a4b 100644 (file)
@@ -106,6 +106,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
        getpid  \
        getppid \
        gettid \
+       inject-nf \
        int_0x80 \
        ioctl_dm-v \
        ioctl_evdev-v \
@@ -282,6 +283,7 @@ MISC_TESTS = \
        filtering_syscall-syntax.test \
        fflush.test \
        get_regs.test \
+       inject-nf.test \
        interactive_block.test \
        ksysent.test \
        opipe.test \
diff --git a/tests/inject-nf.c b/tests/inject-nf.c
new file mode 100644 (file)
index 0000000..91aee80
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Check decoding of return values injected into a syscall that "never fails".
+ *
+ * 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 <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <asm/unistd.h>
+
+#include "raw_syscall.h"
+
+#ifdef __alpha__
+/* alpha has no getpid */
+# define SC_NR __NR_getpgrp
+# define SC_NAME "getpgrp"
+# define getpid getpgrp
+#else
+# define SC_NR __NR_getpid
+# define SC_NAME "getpid"
+#endif
+
+#ifdef raw_syscall_0
+# define INVOKE_SC(err) raw_syscall_0(SC_NR, &err)
+#else
+/* No raw_syscall_0, let's use getpid() and hope for the best.  */
+# define INVOKE_SC(err) getpid()
+#endif
+
+/*
+ * This prototype is intentionally different
+ * from the prototype provided by <unistd.h>.
+ */
+extern kernel_ulong_t getpid(void);
+
+int
+main(int ac, char **av)
+{
+       assert(ac == 1 || ac == 2);
+
+       kernel_ulong_t expected =
+               (ac == 1) ? getpid() : strtoull(av[1], NULL, 0);
+       kernel_ulong_t err = 0;
+       kernel_ulong_t rc = INVOKE_SC(err);
+
+       if (err || rc != expected)
+               error_msg_and_fail("expected %#llx, got rval=%#llx err=%#llx",
+                                  (unsigned long long) expected,
+                                  (unsigned long long) rc,
+                                  (unsigned long long) err);
+
+       if (ac == 2) {
+               printf("%s() = %lld (INJECTED)\n",
+                      SC_NAME, (unsigned long long) rc);
+
+               puts("+++ exited with 0 +++");
+       }
+
+       return 0;
+}
diff --git a/tests/inject-nf.test b/tests/inject-nf.test
new file mode 100755 (executable)
index 0000000..7e3eda4
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/sh -efu
+
+# Check decoding of return values injected into a syscall that "never fails".
+
+. "${srcdir=.}/scno_tampering.sh"
+
+case "$STRACE_ARCH" in
+alpha)
+       SYSCALL=getpgrp
+       ;;
+*)
+       SYSCALL=getpid
+       ;;
+esac
+
+run_prog
+prog="$args"
+fault_args="-a9 -e trace=${SYSCALL} -e inject=${SYSCALL}:retval="
+
+test_rval()
+{
+       local rval
+       rval="$1"; shift
+
+       run_strace $fault_args$rval $prog $rval > "$EXP"
+       match_diff "$LOG" "$EXP"
+}
+
+test_rval 0
+test_rval 1
+test_rval 2147483647 # 0x7fffffff
+
+case "$SIZEOF_KERNEL_LONG_T" in
+8)
+       test_rval 2147483648 # 0x80000000
+       test_rval 4294963200 # 0xfffff000
+       test_rval 4294967294 # 0xfffffffe
+       test_rval 4294967295 # 0xffffffff
+       test_rval 4294967296 # 0x100000000
+       test_rval 9223372036854775807 # 0x7fffffffffffffff
+       ;;
+esac