• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2000 PocketPenguins Inc.  Linux for Hitachi SuperH
7  *                    port by Greg Banks <gbanks@pocketpenguins.com>
8  * Copyright (c) 1999-2018 The strace developers.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "defs.h"
35 #include <linux/mman.h>
36 #include <sys/mman.h>
37 
38 unsigned long
get_pagesize(void)39 get_pagesize(void)
40 {
41 	static unsigned long pagesize;
42 
43 	if (!pagesize) {
44 		long ret = sysconf(_SC_PAGESIZE);
45 
46 		if (ret < 0)
47 			perror_func_msg_and_die("sysconf(_SC_PAGESIZE)");
48 		if (ret == 0)
49 			error_func_msg_and_die("sysconf(_SC_PAGESIZE) "
50 					       "returned 0");
51 
52 		pagesize = (unsigned long) ret;
53 	}
54 
55 	return pagesize;
56 }
57 
SYS_FUNC(brk)58 SYS_FUNC(brk)
59 {
60 	printaddr(tcp->u_arg[0]);
61 
62 	return RVAL_DECODED | RVAL_HEX;
63 }
64 
65 #include "xlat/mmap_prot.h"
66 #include "xlat/mmap_flags.h"
67 
68 #ifndef MAP_HUGE_SHIFT
69 # define MAP_HUGE_SHIFT 26
70 #endif
71 
72 #ifndef MAP_HUGE_MASK
73 # define MAP_HUGE_MASK 0x3f
74 #endif
75 
76 static void
print_mmap_flags(kernel_ulong_t flags)77 print_mmap_flags(kernel_ulong_t flags)
78 {
79 	printxval64(mmap_flags, flags & MAP_TYPE, "MAP_???");
80 	flags &= ~MAP_TYPE;
81 
82 	const unsigned int mask = MAP_HUGE_MASK << MAP_HUGE_SHIFT;
83 	const unsigned int hugetlb_value = flags & mask;
84 
85 	flags &= ~mask;
86 	if (flags) {
87 		tprints("|");
88 		printflags64(mmap_flags, flags, NULL);
89 	}
90 
91 	if (hugetlb_value)
92 		tprintf("|%u<<MAP_HUGE_SHIFT",
93 			hugetlb_value >> MAP_HUGE_SHIFT);
94 }
95 
96 static void
print_mmap(struct tcb * tcp,kernel_ulong_t * u_arg,unsigned long long offset)97 print_mmap(struct tcb *tcp, kernel_ulong_t *u_arg, unsigned long long offset)
98 {
99 	const kernel_ulong_t addr = u_arg[0];
100 	const kernel_ulong_t len = u_arg[1];
101 	const kernel_ulong_t prot = u_arg[2];
102 	const kernel_ulong_t flags = u_arg[3];
103 	const int fd = u_arg[4];
104 
105 	printaddr(addr);
106 	tprintf(", %" PRI_klu ", ", len);
107 	printflags64(mmap_prot, prot, "PROT_???");
108 	tprints(", ");
109 	print_mmap_flags(flags);
110 	tprints(", ");
111 	printfd(tcp, fd);
112 	tprintf(", %#llx", offset);
113 }
114 
115 /* Syscall name<->function correspondence is messed up on many arches.
116  * For example:
117  * i386 has __NR_mmap == 90, and it is "old mmap", and
118  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
119  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
120  * Confused? Me too!
121  */
122 
123 #if HAVE_ARCH_OLD_MMAP
124 /* Params are pointed to by u_arg[0], offset is in bytes */
SYS_FUNC(old_mmap)125 SYS_FUNC(old_mmap)
126 {
127 	kernel_ulong_t *args =
128 		fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
129 
130 	if (args)
131 		print_mmap(tcp, args, args[5]);
132 	else
133 		printaddr(tcp->u_arg[0]);
134 
135 	return RVAL_DECODED | RVAL_HEX;
136 }
137 
138 # if HAVE_ARCH_OLD_MMAP_PGOFF
139 /* Params are pointed to by u_arg[0], offset is in pages */
SYS_FUNC(old_mmap_pgoff)140 SYS_FUNC(old_mmap_pgoff)
141 {
142 	kernel_ulong_t *args =
143 		fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
144 
145 	if (args) {
146 		unsigned long long offset;
147 
148 		offset = args[5];
149 		offset *= get_pagesize();
150 
151 		print_mmap(tcp, args, offset);
152 	} else {
153 		printaddr(tcp->u_arg[0]);
154 	}
155 
156 	return RVAL_DECODED | RVAL_HEX;
157 }
158 # endif /* HAVE_ARCH_OLD_MMAP_PGOFF */
159 #endif /* HAVE_ARCH_OLD_MMAP */
160 
161 /* Params are passed directly, offset is in bytes */
SYS_FUNC(mmap)162 SYS_FUNC(mmap)
163 {
164 	/* Example of kernel-side handling of this variety of mmap:
165 	 * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
166 	 * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
167 	 * since the above code converts off to pages.
168 	 */
169 	print_mmap(tcp, tcp->u_arg, tcp->u_arg[5]);
170 
171 	return RVAL_DECODED | RVAL_HEX;
172 }
173 
174 /* Params are passed directly, offset is in pages */
SYS_FUNC(mmap_pgoff)175 SYS_FUNC(mmap_pgoff)
176 {
177 	/* Try test/mmap_offset_decode.c */
178 	unsigned long long offset;
179 	offset = tcp->u_arg[5];
180 	offset *= get_pagesize();
181 	print_mmap(tcp, tcp->u_arg, offset);
182 
183 	return RVAL_DECODED | RVAL_HEX;
184 }
185 
186 /* Params are passed directly, offset is in 4k units */
SYS_FUNC(mmap_4koff)187 SYS_FUNC(mmap_4koff)
188 {
189 	unsigned long long offset;
190 	offset = tcp->u_arg[5];
191 	offset <<= 12;
192 	print_mmap(tcp, tcp->u_arg, offset);
193 
194 	return RVAL_DECODED | RVAL_HEX;
195 }
196 
SYS_FUNC(munmap)197 SYS_FUNC(munmap)
198 {
199 	printaddr(tcp->u_arg[0]);
200 	tprintf(", %" PRI_klu, tcp->u_arg[1]);
201 
202 	return RVAL_DECODED;
203 }
204 
205 static int
do_mprotect(struct tcb * tcp,bool has_pkey)206 do_mprotect(struct tcb *tcp, bool has_pkey)
207 {
208 	printaddr(tcp->u_arg[0]);
209 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
210 	printflags64(mmap_prot, tcp->u_arg[2], "PROT_???");
211 
212 	if (has_pkey)
213 		tprintf(", %d", (int) tcp->u_arg[3]);
214 
215 	return RVAL_DECODED;
216 }
217 
SYS_FUNC(mprotect)218 SYS_FUNC(mprotect)
219 {
220 	return do_mprotect(tcp, false);
221 }
222 
SYS_FUNC(pkey_mprotect)223 SYS_FUNC(pkey_mprotect)
224 {
225 	return do_mprotect(tcp, true);
226 }
227 
228 #include "xlat/mremap_flags.h"
229 
SYS_FUNC(mremap)230 SYS_FUNC(mremap)
231 {
232 	printaddr(tcp->u_arg[0]);
233 	tprintf(", %" PRI_klu ", %" PRI_klu ", ", tcp->u_arg[1], tcp->u_arg[2]);
234 	printflags64(mremap_flags, tcp->u_arg[3], "MREMAP_???");
235 #ifdef MREMAP_FIXED
236 	if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
237 	    (MREMAP_MAYMOVE | MREMAP_FIXED)) {
238 		tprints(", ");
239 		printaddr(tcp->u_arg[4]);
240 	}
241 #endif
242 	return RVAL_DECODED | RVAL_HEX;
243 }
244 
245 #include "xlat/madvise_cmds.h"
246 
SYS_FUNC(madvise)247 SYS_FUNC(madvise)
248 {
249 	printaddr(tcp->u_arg[0]);
250 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
251 	printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
252 
253 	return RVAL_DECODED;
254 }
255 
256 #include "xlat/mlockall_flags.h"
257 
SYS_FUNC(mlockall)258 SYS_FUNC(mlockall)
259 {
260 	printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
261 
262 	return RVAL_DECODED;
263 }
264 
265 #include "xlat/mctl_sync.h"
266 
SYS_FUNC(msync)267 SYS_FUNC(msync)
268 {
269 	/* addr */
270 	printaddr(tcp->u_arg[0]);
271 	/* len */
272 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
273 	/* flags */
274 	printflags(mctl_sync, tcp->u_arg[2], "MS_???");
275 
276 	return RVAL_DECODED;
277 }
278 
279 #include "xlat/mlock_flags.h"
280 
SYS_FUNC(mlock2)281 SYS_FUNC(mlock2)
282 {
283 	printaddr(tcp->u_arg[0]);
284 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
285 	printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
286 
287 	return RVAL_DECODED;
288 }
289 
SYS_FUNC(mincore)290 SYS_FUNC(mincore)
291 {
292 	if (entering(tcp)) {
293 		printaddr(tcp->u_arg[0]);
294 		tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
295 	} else {
296 		const unsigned long page_size = get_pagesize();
297 		const unsigned long page_mask = page_size - 1;
298 		unsigned long len = tcp->u_arg[1];
299 		unsigned char *vec = NULL;
300 
301 		len = len / page_size + (len & page_mask ? 1 : 0);
302 		if (syserror(tcp) || !verbose(tcp) ||
303 		    !tcp->u_arg[2] || !(vec = malloc(len)) ||
304 		    umoven(tcp, tcp->u_arg[2], len, vec) < 0)
305 			printaddr(tcp->u_arg[2]);
306 		else {
307 			unsigned long i;
308 			tprints("[");
309 			for (i = 0; i < len; i++) {
310 				if (i)
311 					tprints(", ");
312 				if (abbrev(tcp) && i >= max_strlen) {
313 					tprints("...");
314 					break;
315 				}
316 				tprints((vec[i] & 1) ? "1" : "0");
317 			}
318 			tprints("]");
319 		}
320 		free(vec);
321 	}
322 	return 0;
323 }
324 
SYS_FUNC(remap_file_pages)325 SYS_FUNC(remap_file_pages)
326 {
327 	const kernel_ulong_t addr = tcp->u_arg[0];
328 	const kernel_ulong_t size = tcp->u_arg[1];
329 	const kernel_ulong_t prot = tcp->u_arg[2];
330 	const kernel_ulong_t pgoff = tcp->u_arg[3];
331 	const kernel_ulong_t flags = tcp->u_arg[4];
332 
333 	printaddr(addr);
334 	tprintf(", %" PRI_klu ", ", size);
335 	printflags64(mmap_prot, prot, "PROT_???");
336 	tprintf(", %" PRI_klu ", ", pgoff);
337 	print_mmap_flags(flags);
338 
339 	return RVAL_DECODED;
340 }
341 
342 #if defined(POWERPC)
343 static bool
print_protmap_entry(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)344 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
345 {
346 	tprintf("%#08x", *(unsigned int *) elem_buf);
347 
348 	return true;
349 }
350 
SYS_FUNC(subpage_prot)351 SYS_FUNC(subpage_prot)
352 {
353 	kernel_ulong_t addr = tcp->u_arg[0];
354 	kernel_ulong_t len = tcp->u_arg[1];
355 	kernel_ulong_t nmemb = len >> 16;
356 	kernel_ulong_t map = tcp->u_arg[2];
357 
358 	printaddr(addr);
359 	tprintf(", %" PRI_klu ", ", len);
360 
361 	unsigned int entry;
362 	print_array(tcp, map, nmemb, &entry, sizeof(entry),
363 		    tfetch_mem, print_protmap_entry, 0);
364 
365 	return RVAL_DECODED;
366 }
367 #endif
368