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