]> granicus.if.org Git - strace/blob - prctl.c
Include "kernel_types.h" in defs.h and tests/tests.h
[strace] / prctl.c
1 /*
2  * Copyright (c) 1994-1996 Rick Sladkey <jrs@world.std.com>
3  * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
4  * Copyright (c) 2005-2007 Roland McGrath <roland@redhat.com>
5  * Copyright (c) 2008-2015 Dmitry V. Levin <ldv@altlinux.org>
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 "defs.h"
32
33 #include <linux/prctl.h>
34
35 #include "xlat/prctl_options.h"
36 #include "xlat/pr_cap_ambient.h"
37 #include "xlat/pr_fp_mode.h"
38 #include "xlat/pr_mce_kill.h"
39 #include "xlat/pr_mce_kill_policy.h"
40 #include "xlat/pr_set_mm.h"
41 #include "xlat/pr_tsc.h"
42 #include "xlat/pr_unalign_flags.h"
43
44 #ifndef TASK_COMM_LEN
45 # define TASK_COMM_LEN 16
46 #endif
47
48 #ifdef HAVE_LINUX_SECCOMP_H
49 # include <linux/seccomp.h>
50 #endif
51 #include "xlat/seccomp_mode.h"
52
53 #ifdef HAVE_LINUX_SECUREBITS_H
54 # include <linux/securebits.h>
55 #endif
56 #include "xlat/secbits.h"
57
58 /* these constants are the same as in <linux/capability.h> */
59 enum {
60 #include "caps0.h"
61 #include "caps1.h"
62 };
63
64 #include "xlat/cap.h"
65
66 static void
67 print_prctl_args(struct tcb *tcp, const unsigned int first)
68 {
69         unsigned int i;
70
71         for (i = first; i < tcp->s_ent->nargs; ++i)
72                 tprintf(", %#llx", getarg_ull(tcp, i));
73 }
74
75 SYS_FUNC(prctl)
76 {
77         const unsigned int option = tcp->u_arg[0];
78         const unsigned long long arg2 = getarg_ull(tcp, 1);
79         const unsigned long long arg3 = getarg_ull(tcp, 2);
80         /*
81          * PR_SET_VMA is the only command which actually uses these arguments
82          * currently, and it is available only on Android for now.
83          */
84 #ifdef __ANDROID__
85         const unsigned long long arg4 = getarg_ull(tcp, 3);
86         const unsigned long long arg5 = getarg_ull(tcp, 4);
87 #endif
88         unsigned int i;
89
90         if (entering(tcp))
91                 printxval(prctl_options, option, "PR_???");
92
93         switch (option) {
94         case PR_GET_DUMPABLE:
95         case PR_GET_KEEPCAPS:
96         case PR_GET_SECCOMP:
97         case PR_GET_TIMERSLACK:
98         case PR_GET_TIMING:
99                 return RVAL_DECODED;
100
101         case PR_GET_CHILD_SUBREAPER:
102         case PR_GET_ENDIAN:
103         case PR_GET_FPEMU:
104         case PR_GET_FPEXC:
105                 if (entering(tcp))
106                         tprints(", ");
107                 else
108                         printnum_int(tcp, arg2, "%u");
109                 break;
110
111         case PR_GET_NAME:
112                 if (entering(tcp)) {
113                         tprints(", ");
114                 } else {
115                         if (syserror(tcp))
116                                 printaddr(arg2);
117                         else
118                                 printstr_ex(tcp, arg2, TASK_COMM_LEN,
119                                             QUOTE_0_TERMINATED);
120                 }
121                 break;
122
123         case PR_GET_PDEATHSIG:
124                 if (entering(tcp)) {
125                         tprints(", ");
126                 } else if (!umove_or_printaddr(tcp, arg2, &i)) {
127                         tprints("[");
128                         tprints(signame(i));
129                         tprints("]");
130                 }
131                 break;
132
133         case PR_GET_SECUREBITS:
134                 if (entering(tcp))
135                         break;
136                 if (syserror(tcp) || tcp->u_rval == 0)
137                         return 0;
138                 tcp->auxstr = sprintflags("", secbits,
139                                           (unsigned long) tcp->u_rval);
140                 return RVAL_STR;
141
142         case PR_GET_TID_ADDRESS:
143                 if (entering(tcp))
144                         tprints(", ");
145                 else
146                         printnum_ptr(tcp, arg2);
147                 break;
148
149         case PR_GET_TSC:
150                 if (entering(tcp)) {
151                         tprints(", ");
152                 } else if (!umove_or_printaddr(tcp, arg2, &i)) {
153                         tprints("[");
154                         printxval(pr_tsc, i, "PR_TSC_???");
155                         tprints("]");
156                 }
157                 break;
158
159         case PR_GET_UNALIGN:
160                 if (entering(tcp)) {
161                         tprints(", ");
162                 } else if (!umove_or_printaddr(tcp, arg2, &i)) {
163                         tprints("[");
164                         printflags(pr_unalign_flags, i, "PR_UNALIGN_???");
165                         tprints("]");
166                 }
167                 break;
168
169         case PR_GET_FP_MODE:
170                 if (entering(tcp))
171                         break;
172                 if (syserror(tcp) || tcp->u_rval == 0)
173                         return 0;
174                 tcp->auxstr = sprintflags("", pr_fp_mode,
175                                           (unsigned long) tcp->u_rval);
176                 return RVAL_STR;
177
178         /* PR_TASK_PERF_EVENTS_* take no arguments. */
179         case PR_TASK_PERF_EVENTS_DISABLE:
180         case PR_TASK_PERF_EVENTS_ENABLE:
181                 return RVAL_DECODED;
182
183         case PR_SET_CHILD_SUBREAPER:
184         case PR_SET_DUMPABLE:
185         case PR_SET_ENDIAN:
186         case PR_SET_FPEMU:
187         case PR_SET_FPEXC:
188         case PR_SET_KEEPCAPS:
189         case PR_SET_TIMING:
190                 tprintf(", %llu", arg2);
191                 return RVAL_DECODED;
192
193         case PR_CAPBSET_DROP:
194         case PR_CAPBSET_READ:
195                 tprints(", ");
196                 printxval64(cap, arg2, "CAP_???");
197                 return RVAL_DECODED;
198
199         case PR_CAP_AMBIENT:
200                 tprints(", ");
201                 printxval64(pr_cap_ambient, arg2,
202                                "PR_CAP_AMBIENT_???");
203                 switch (arg2) {
204                 case PR_CAP_AMBIENT_RAISE:
205                 case PR_CAP_AMBIENT_LOWER:
206                 case PR_CAP_AMBIENT_IS_SET:
207                         tprints(", ");
208                         printxval64(cap, arg3, "CAP_???");
209                         print_prctl_args(tcp, 3);
210                         break;
211                 default:
212                         print_prctl_args(tcp, 2);
213                         break;
214                 }
215                 return RVAL_DECODED;
216
217         case PR_MCE_KILL:
218                 tprints(", ");
219                 printxval64(pr_mce_kill, arg2, "PR_MCE_KILL_???");
220                 tprints(", ");
221                 if (PR_MCE_KILL_SET == arg2)
222                         printxval64(pr_mce_kill_policy, arg3,
223                                     "PR_MCE_KILL_???");
224                 else
225                         tprintf("%#llx", arg3);
226                 print_prctl_args(tcp, 3);
227                 return RVAL_DECODED;
228
229         case PR_SET_NAME:
230                 tprints(", ");
231                 printstr_ex(tcp, arg2, TASK_COMM_LEN - 1,
232                             QUOTE_0_TERMINATED);
233                 return RVAL_DECODED;
234
235 #ifdef __ANDROID__
236 # ifndef PR_SET_VMA_ANON_NAME
237 #  define PR_SET_VMA_ANON_NAME    0
238 # endif
239         case PR_SET_VMA:
240                 if (arg2 == PR_SET_VMA_ANON_NAME) {
241                         tprintf(", PR_SET_VMA_ANON_NAME, %#llx", arg3);
242                         tprintf(", %llu, ", arg4);
243                         printstr(tcp, arg5, -1);
244                 } else {
245                         /* There are no other sub-options now, but there
246                          * might be in future... */
247                         print_prctl_args(tcp, 1);
248                 }
249                 return RVAL_DECODED;
250 #endif
251
252         case PR_SET_MM:
253                 tprints(", ");
254                 printxval(pr_set_mm, arg2, "PR_SET_MM_???");
255                 print_prctl_args(tcp, 2);
256                 return RVAL_DECODED;
257
258         case PR_SET_PDEATHSIG:
259                 tprints(", ");
260                 if (arg2 > 128)
261                         tprintf("%llu", arg2);
262                 else
263                         tprints(signame(arg2));
264                 return RVAL_DECODED;
265
266         case PR_SET_PTRACER:
267                 tprints(", ");
268                 if ((int) arg2 == -1)
269                         tprints("PR_SET_PTRACER_ANY");
270                 else
271                         tprintf("%llu", arg2);
272                 return RVAL_DECODED;
273
274         case PR_SET_SECCOMP:
275                 tprints(", ");
276                 printxval64(seccomp_mode, arg2,
277                             "SECCOMP_MODE_???");
278                 if (SECCOMP_MODE_STRICT == arg2)
279                         return RVAL_DECODED;
280                 if (SECCOMP_MODE_FILTER == arg2) {
281                         tprints(", ");
282                         print_seccomp_filter(tcp, arg3);
283                         return RVAL_DECODED;
284                 }
285                 print_prctl_args(tcp, 2);
286                 return RVAL_DECODED;
287
288         case PR_SET_SECUREBITS:
289                 tprints(", ");
290                 printflags64(secbits, arg2, "SECBIT_???");
291                 return RVAL_DECODED;
292
293         case PR_SET_TIMERSLACK:
294                 tprintf(", %lld", arg2);
295                 return RVAL_DECODED;
296
297         case PR_SET_TSC:
298                 tprints(", ");
299                 printxval(pr_tsc, arg2, "PR_TSC_???");
300                 return RVAL_DECODED;
301
302         case PR_SET_UNALIGN:
303                 tprints(", ");
304                 printflags(pr_unalign_flags, arg2, "PR_UNALIGN_???");
305                 return RVAL_DECODED;
306
307         case PR_SET_NO_NEW_PRIVS:
308         case PR_SET_THP_DISABLE:
309                 tprintf(", %llu", arg2);
310                 print_prctl_args(tcp, 2);
311                 return RVAL_DECODED;
312
313         case PR_MCE_KILL_GET:
314                 if (entering(tcp)) {
315                         print_prctl_args(tcp, 1);
316                         return 0;
317                 }
318                 if (syserror(tcp))
319                         return 0;
320                 tcp->auxstr = xlookup(pr_mce_kill_policy,
321                                       (unsigned long) tcp->u_rval);
322                 return tcp->auxstr ? RVAL_STR : RVAL_UDECIMAL;
323
324         case PR_SET_FP_MODE:
325                 tprints(", ");
326                 printflags(pr_fp_mode, arg2, "PR_FP_MODE_???");
327                 return RVAL_DECODED;
328
329         case PR_GET_NO_NEW_PRIVS:
330         case PR_GET_THP_DISABLE:
331         case PR_MPX_DISABLE_MANAGEMENT:
332         case PR_MPX_ENABLE_MANAGEMENT:
333         default:
334                 print_prctl_args(tcp, 1);
335                 return RVAL_DECODED;
336         }
337         return 0;
338 }
339
340 #if defined X86_64 || defined X32
341 # include <asm/prctl.h>
342 # include "xlat/archvals.h"
343
344 SYS_FUNC(arch_prctl)
345 {
346         const unsigned int option = tcp->u_arg[0];
347         const unsigned long long addr = getarg_ull(tcp, 1);
348
349         if (entering(tcp))
350                 printxval(archvals, option, "ARCH_???");
351
352         switch (option) {
353         case ARCH_GET_GS:
354         case ARCH_GET_FS:
355                 if (entering(tcp))
356                         tprints(", ");
357                 else
358                         printnum_ptr(tcp, addr);
359                 return 0;
360         }
361
362         tprintf(", %#llx", addr);
363         return RVAL_DECODED;
364 }
365 #endif /* X86_64 || X32 */