1 /*
2  * Check decoding of delete_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 
35 #if defined(__NR_delete_module)
36 
37 # include <fcntl.h>
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 struct {
47 		kernel_ulong_t val;
48 		const char *str;
49 		unsigned int val_prefix, val_suffix;
50 	} flags[] = {
51 		{ ARG_STR(0), 0, 0 },
52 		{ F8ILL_KULONG_MASK | O_NONBLOCK, "O_NONBLOCK", 0, 0 },
53 		{ (kernel_ulong_t) 0xbadc0dedfacef157ULL & ~(O_NONBLOCK | O_TRUNC),
54 			" /* O_??? */", 0xfacef157U & ~(O_NONBLOCK | O_TRUNC), 0},
55 		{ (kernel_ulong_t) (0xfacef157deade71cULL & ~O_NONBLOCK) | O_TRUNC,
56 			"O_TRUNC", 0, 0xdeade71c & ~(O_NONBLOCK | O_TRUNC)},
57 		{ -1LL, "O_NONBLOCK|O_TRUNC", 0, -1U & ~(O_NONBLOCK | O_TRUNC)},
58 	};
59 
60 	long rc;
61 	char *bogus_param1 = tail_alloc(PARAM1_LEN);
62 	char *bogus_param2 = tail_alloc(PARAM2_LEN);
63 	const char *errstr;
64 
65 	fill_memory_ex(bogus_param1, PARAM1_LEN, PARAM1_BASE, PARAM1_LEN);
66 	fill_memory_ex(bogus_param2, PARAM2_LEN, PARAM2_BASE, PARAM2_LEN);
67 
68 	rc = syscall(__NR_delete_module, NULL, F8ILL_KULONG_MASK);
69 	printf("delete_module(NULL, 0) = %s\n", sprintrc(rc));
70 
71 	rc = syscall(__NR_delete_module, bogus_param1, flags[0].val);
72 	errstr = sprintrc(rc);
73 
74 	printf("delete_module(\"");
75 	print_str(PARAM1_BASE, MAX_STRLEN, false);
76 	printf("\"..., %s) = %s\n", flags[0].str, errstr);
77 
78 	bogus_param1[PARAM1_LEN - 1] = '\0';
79 
80 	rc = syscall(__NR_delete_module, bogus_param1, flags[1].val);
81 	errstr = sprintrc(rc);
82 
83 	printf("delete_module(\"");
84 	print_str(PARAM1_BASE, MAX_STRLEN, false);
85 	printf("\", %s) = %s\n", flags[1].str, errstr);
86 
87 	rc = syscall(__NR_delete_module, bogus_param2 + PARAM2_LEN,
88 		flags[2].val);
89 	printf("delete_module(%p, %#x%s) = %s\n",
90 	       bogus_param2 + PARAM2_LEN, flags[2].val_prefix,
91 	       flags[2].str, sprintrc(rc));
92 
93 	rc = syscall(__NR_delete_module, bogus_param2, flags[3].val);
94 	printf("delete_module(%p, %s|%#x) = %s\n",
95 	       bogus_param2, flags[3].str, flags[3].val_suffix, sprintrc(rc));
96 
97 	bogus_param2[PARAM2_LEN - 1] = '\0';
98 
99 	rc = syscall(__NR_delete_module, bogus_param2, flags[4].val);
100 	errstr = sprintrc(rc);
101 
102 	printf("delete_module(\"");
103 	print_str(PARAM2_BASE, PARAM2_LEN - 1, true);
104 	printf("\", %s|%#x) = %s\n", flags[4].str, flags[4].val_suffix, errstr);
105 
106 	puts("+++ exited with 0 +++");
107 
108 	return 0;
109 }
110 
111 #else
112 
113 SKIP_MAIN_UNDEFINED("__NR_delete_module");
114 
115 #endif
116