1 /*
2  * Check decoding of finit_module syscall.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * Copyright (c) 2016-2017 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 
33 #include <asm/unistd.h>
34 #include "scno.h"
35 
36 #if defined(__NR_finit_module)
37 
38 # include <stdio.h>
39 # include <unistd.h>
40 
41 # include "init_delete_module.h"
42 
43 int
44 main(void)
45 {
46 	static const kernel_ulong_t bogus_fd =
47 		(kernel_ulong_t) 0xdeb0d1edbeeff00dULL;
48 
49 	static const struct {
50 		kernel_ulong_t val;
51 		const char *str;
52 	} flags[] = {
53 		{ ARG_STR(0) },
54 		{ (kernel_ulong_t) 0xffffffff00000002ULL,
55 			"MODULE_INIT_IGNORE_VERMAGIC" },
56 		{ (kernel_ulong_t) 0xbadc0deddefaced0ULL,
57 			"0xdefaced0 /* MODULE_INIT_??? */" },
58 		{ (kernel_ulong_t) 0xfacef157dec0ded1ULL,
59 			"MODULE_INIT_IGNORE_MODVERSIONS|0xdec0ded0" },
60 		{ -1LL, "MODULE_INIT_IGNORE_MODVERSIONS|"
61 			"MODULE_INIT_IGNORE_VERMAGIC|0xfffffffc" },
62 	};
63 
64 	long rc;
65 	char *bogus_param1 = tail_alloc(PARAM1_LEN);
66 	char *bogus_param2 = tail_alloc(PARAM2_LEN);
67 	const char *errstr;
68 
69 	fill_memory_ex(bogus_param1, PARAM1_LEN, PARAM1_BASE, PARAM1_LEN);
70 	fill_memory_ex(bogus_param2, PARAM2_LEN, PARAM2_BASE, PARAM2_LEN);
71 
72 	rc = syscall(__NR_finit_module, F8ILL_KULONG_MASK, NULL,
73 		     F8ILL_KULONG_MASK);
74 	printf("finit_module(0, NULL, 0) = %s\n", sprintrc(rc));
75 
76 	rc = syscall(__NR_finit_module, bogus_fd, bogus_param1, flags[0].val);
77 	errstr = sprintrc(rc);
78 
79 	printf("finit_module(%d, \"", (int) bogus_fd);
80 	print_str(PARAM1_BASE, MAX_STRLEN, false);
81 	printf("\"..., %s) = %s\n", flags[0].str, errstr);
82 
83 	bogus_param1[PARAM1_LEN - 1] = '\0';
84 
85 	rc = syscall(__NR_finit_module, bogus_fd, bogus_param1, flags[1].val);
86 	errstr = sprintrc(rc);
87 
88 	printf("finit_module(%d, \"", (int) bogus_fd);
89 	print_str(PARAM1_BASE, MAX_STRLEN, false);
90 	printf("\", %s) = %s\n", flags[1].str, errstr);
91 
92 	rc = syscall(__NR_finit_module, bogus_fd, bogus_param2 + PARAM2_LEN,
93 		flags[2].val);
94 	printf("finit_module(%d, %p, %s) = %s\n",
95 	       (int) bogus_fd, bogus_param2 + PARAM2_LEN, flags[2].str,
96 	       sprintrc(rc));
97 
98 	rc = syscall(__NR_finit_module, bogus_fd, bogus_param2, flags[3].val);
99 	printf("finit_module(%d, %p, %s) = %s\n",
100 	       (int) bogus_fd, bogus_param2, flags[3].str, sprintrc(rc));
101 
102 	bogus_param2[PARAM2_LEN - 1] = '\0';
103 
104 	rc = syscall(__NR_finit_module, bogus_fd, bogus_param2, flags[4].val);
105 	errstr = sprintrc(rc);
106 
107 	printf("finit_module(%d, \"", (int) bogus_fd);
108 	print_str(PARAM2_BASE, PARAM2_LEN - 1, true);
109 	printf("\", %s) = %s\n", flags[4].str, errstr);
110 
111 	puts("+++ exited with 0 +++");
112 
113 	return 0;
114 }
115 
116 #else
117 
118 SKIP_MAIN_UNDEFINED("__NR_finit_module");
119 
120 #endif
121