1 /*
2  * Check decoding of set_thread_area and get_thread_area syscalls on x86
3  * architecture.
4  *
5  * Copyright (c) 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 
33 #include <asm/unistd.h>
34 
35 #if defined __NR_get_thread_area && defined __NR_set_thread_area \
36  && defined HAVE_STRUCT_USER_DESC
37 
38 # include <assert.h>
39 # include <errno.h>
40 # include <stdbool.h>
41 # include <stdio.h>
42 # include <stdint.h>
43 # include <string.h>
44 # include <unistd.h>
45 
46 # include "print_user_desc.c"
47 
48 long errnum;
49 
50 static void
51 printptr(kernel_ulong_t ptr, const char *ptr_str)
52 {
53 	if (ptr_str)
54 		printf("%s", ptr_str);
55 	else
56 		printf("%#llx", zero_extend_signed_to_ull(ptr));
57 }
58 
59 /**
60  * Perform set_thread_area call along with printing the expected output.
61  *
62  * @param ptr_val Pointer to thread area argument.
63  * @param ptr_str Explicit string representation of the argument.
64  * @param valid   Whether argument points to the valid memory and its contents
65  *                should be decoded.
66  * @param entry_number_str explicit decoding of the entry_number field.
67  */
68 static long
69 set_thread_area(kernel_ulong_t ptr_val, const char *ptr_str, bool valid,
70 		const char *entry_number_str)
71 {
72 	struct user_desc *ptr = (struct user_desc *) (uintptr_t) ptr_val;
73 	long rc = -1;
74 	int saved_errno;
75 
76 	rc = syscall(__NR_set_thread_area, ptr_val);
77 	saved_errno = errno;
78 	printf("set_thread_area(");
79 
80 	if (valid)
81 		print_user_desc(ptr, entry_number_str);
82 	else
83 		printptr(ptr_val, ptr_str);
84 
85 	errno = saved_errno;
86 	printf(") = %s", sprintrc(rc));
87 	if (!rc)
88 		printf(" (entry_number=%u)", ptr->entry_number);
89 
90 	puts("");
91 
92 	return rc;
93 }
94 
95 /**
96  * Perform get_thread_are call along with printing the expected output and
97  * checking the result against the argument of the previous set_thread_area
98  * call, if it had place.
99  *
100  * @param ptr_val  Pointer to thread area argument.
101  * @param ptr_str  Explicit string representation of the argument.
102  * @param valid    Whether argument points to the valid memory and its contents
103  *                 should be decoded.
104  * @param set_rc   Return code of the previous set_thread_area call.
105  * @param expected The value of the argument passed to the previous
106  *                 set_thread_area call.
107  */
108 static void
109 get_thread_area(kernel_ulong_t ptr_val, const char *ptr_str, bool valid,
110 		long set_rc, kernel_ulong_t expected)
111 {
112 	struct user_desc *ptr = (struct user_desc *) (uintptr_t) ptr_val;
113 	struct user_desc *expected_ptr =
114 		(struct user_desc *) (uintptr_t) expected;
115 	int saved_errno;
116 	long rc;
117 
118 	rc = syscall(__NR_get_thread_area, ptr_val);
119 	saved_errno = errno;
120 
121 	printf("get_thread_area(");
122 
123 	if (valid && !rc) {
124 		if (!set_rc) {
125 			assert(ptr->entry_number == expected_ptr->entry_number);
126 			assert(ptr->base_addr    == expected_ptr->base_addr);
127 			assert(ptr->limit        == expected_ptr->limit);
128 			assert(ptr->seg_32bit    == expected_ptr->seg_32bit);
129 			assert(ptr->contents     == expected_ptr->contents);
130 			assert(ptr->read_exec_only ==
131 			       expected_ptr->read_exec_only);
132 			assert(ptr->limit_in_pages ==
133 			       expected_ptr->limit_in_pages);
134 			assert(ptr->seg_not_present ==
135 			       expected_ptr->seg_not_present);
136 			assert(ptr->useable      == expected_ptr->useable);
137 			/*
138 			 * We do not check lm as 32-bit processes ignore it, and
139 			 * only 32-bit processes can successfully execute
140 			 * get_thread_area.
141 			 */
142 		}
143 
144 		print_user_desc(ptr,
145 				(int) ptr->entry_number == -1 ? "-1" : NULL);
146 	} else {
147 		printptr(ptr_val, ptr_str);
148 	}
149 
150 	errno = saved_errno;
151 	printf(") = %s\n", sprintrc(rc));
152 }
153 
154 int main(void)
155 {
156 	struct user_desc *ta1 = tail_alloc(sizeof(*ta1));
157 	struct user_desc *ta2 = tail_alloc(sizeof(*ta2));
158 	unsigned *bogus_entry_number = tail_alloc(sizeof(*bogus_entry_number));
159 
160 	long set_rc = -1;
161 
162 	/*
163 	 * Let's do some weird syscall, it will mark the beginning of our
164 	 * expected output.
165 	 */
166 	syscall(__NR_reboot, 0, 0, 0, 0);
167 
168 	set_rc = set_thread_area((uintptr_t) ARG_STR(NULL), false, NULL);
169 	get_thread_area((uintptr_t) ARG_STR(NULL), false, set_rc,
170 			(uintptr_t) NULL);
171 
172 	set_rc = set_thread_area(-1, NULL, false, NULL);
173 	get_thread_area(-1, NULL, false, set_rc, -1);
174 
175 	fill_memory(ta1, sizeof(*ta1));
176 	fill_memory_ex(ta2, sizeof(*ta2), 0xA5, 0x5A);
177 
178 	set_thread_area((uintptr_t) (ta1 + 1), NULL, false, NULL);
179 
180 	set_thread_area((uintptr_t) bogus_entry_number, NULL, false, NULL);
181 
182 	set_thread_area((uintptr_t) ta1, NULL, true, NULL);
183 
184 	ta1->entry_number = -1;
185 	ta1->base_addr = 0;
186 	ta1->limit = 0;
187 	ta1->contents = 1;
188 	ta1->seg_32bit = 1;
189 	ta1->seg_not_present = 0;
190 
191 	set_rc = set_thread_area((uintptr_t) ta1, NULL, true, "-1");
192 
193 	*bogus_entry_number = 2718281828U;
194 	get_thread_area((uintptr_t) bogus_entry_number,
195 			"{entry_number=2718281828, ...}",
196 			false, set_rc, (uintptr_t) ta1);
197 
198 	/* That one should return -EFAULT on i386 */
199 	*bogus_entry_number = 12;
200 	get_thread_area((uintptr_t) bogus_entry_number,
201 			"{entry_number=12, ...}",
202 			false, set_rc, (uintptr_t) ta1);
203 
204 	ta2->entry_number = 3141592653U;
205 	get_thread_area((uintptr_t) ta2, "{entry_number=3141592653, ...}",
206 			false, set_rc, (uintptr_t) ta1);
207 
208 	ta2->entry_number = -1;
209 	get_thread_area((uintptr_t) ta2, "{entry_number=-1, ...}",
210 			false, set_rc, (uintptr_t) ta1);
211 
212 	ta2->entry_number = ta1->entry_number;
213 	assert(set_rc == 0 || (int) ta2->entry_number == -1);
214 	get_thread_area((uintptr_t) ta2, "{entry_number=-1, ...}",
215 			true, set_rc, (uintptr_t) ta1);
216 
217 	puts("+++ exited with 0 +++");
218 
219 	return 0;
220 }
221 
222 #else
223 
224 SKIP_MAIN_UNDEFINED("__NR_get_thread_area && __NR_set_thread_area"
225 		    " && HAVE_STRUCT_USER_DESC");
226 
227 #endif
228