Home | History | Annotate | Download | only in tests-m32
      1 /*
      2  * Check decoding of out-of-range syscalls.
      3  *
      4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv (at) altlinux.org>
      5  * Copyright (c) 2016-2018 The strace developers.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "tests.h"
     32 #include "sysent.h"
     33 #include <errno.h>
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <unistd.h>
     37 #include <asm/unistd.h>
     38 
     39 #include "sysent_shorthand_defs.h"
     40 
     41 static const struct_sysent syscallent[] = {
     42 #include "syscallent.h"
     43 };
     44 
     45 #include "sysent_shorthand_undefs.h"
     46 
     47 #ifndef DEBUG_PRINT
     48 # define DEBUG_PRINT 0
     49 #endif
     50 
     51 #if defined __X32_SYSCALL_BIT && defined __NR_read \
     52  && (__X32_SYSCALL_BIT & __NR_read) != 0
     53 # define SYSCALL_BIT __X32_SYSCALL_BIT
     54 #else
     55 # define SYSCALL_BIT 0
     56 #endif
     57 
     58 #if DEBUG_PRINT
     59 static const char *strace_name;
     60 static FILE *debug_out;
     61 #endif
     62 
     63 static void
     64 test_syscall(const unsigned long nr)
     65 {
     66 	static const kernel_ulong_t a[] = {
     67 		(kernel_ulong_t) 0xface0fedbadc0dedULL,
     68 		(kernel_ulong_t) 0xface1fedbadc1dedULL,
     69 		(kernel_ulong_t) 0xface2fedbadc2dedULL,
     70 		(kernel_ulong_t) 0xface3fedbadc3dedULL,
     71 		(kernel_ulong_t) 0xface4fedbadc4dedULL,
     72 		(kernel_ulong_t) 0xface5fedbadc5dedULL
     73 	};
     74 
     75 	long rc = syscall(nr | SYSCALL_BIT,
     76 			  a[0], a[1], a[2], a[3], a[4], a[5]);
     77 
     78 #if DEBUG_PRINT
     79 	fprintf(debug_out, "%s: pid %d invalid syscall %#lx\n",
     80 		strace_name, getpid(), nr | SYSCALL_BIT);
     81 #endif
     82 
     83 #ifdef LINUX_MIPSO32
     84 	printf("syscall(%#lx, %#lx, %#lx, %#lx, %#lx, %#lx, %#lx)"
     85 	       " = %ld ENOSYS (%m)\n", nr | SYSCALL_BIT,
     86 	       a[0], a[1], a[2], a[3], a[4], a[5], rc);
     87 #else
     88 	printf("syscall_%#lx(%#llx, %#llx, %#llx, %#llx, %#llx, %#llx)"
     89 	       " = %ld (errno %d)\n", nr | SYSCALL_BIT,
     90 	       (unsigned long long) a[0],
     91 	       (unsigned long long) a[1],
     92 	       (unsigned long long) a[2],
     93 	       (unsigned long long) a[3],
     94 	       (unsigned long long) a[4],
     95 	       (unsigned long long) a[5],
     96 	       rc, errno);
     97 #endif
     98 }
     99 
    100 int
    101 main(int argc, char *argv[])
    102 {
    103 #if DEBUG_PRINT
    104 	if (argc < 3)
    105 		error_msg_and_fail("Not enough arguments. "
    106 				   "Usage: %s STRACE_NAME DEBUG_OUT_FD",
    107 				   argv[0]);
    108 
    109 	strace_name = argv[1];
    110 
    111 	errno = 0;
    112 	int debug_out_fd = strtol(argv[2], NULL, 0);
    113 	if (errno)
    114 		error_msg_and_fail("Not a number: %s", argv[2]);
    115 
    116 	debug_out = fdopen(debug_out_fd, "a");
    117 	if (!debug_out)
    118 		perror_msg_and_fail("fdopen: %d", debug_out_fd);
    119 #endif
    120 
    121 	test_syscall(ARRAY_SIZE(syscallent));
    122 
    123 #ifdef SYS_socket_subcall
    124 	test_syscall(SYS_socket_subcall + 1);
    125 #endif
    126 
    127 #ifdef SYS_ipc_subcall
    128 	test_syscall(SYS_ipc_subcall + 1);
    129 #endif
    130 
    131 	puts("+++ exited with 0 +++");
    132 	return 0;
    133 }
    134