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