]> granicus.if.org Git - strace/blob - uid.c
Remove XLAT_END
[strace] / uid.c
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-1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2003-2016 Dmitry V. Levin <ldv@altlinux.org>
7  * Copyright (c) 2014-2018 The strace developers.
8  * All rights reserved.
9  *
10  * SPDX-License-Identifier: LGPL-2.1-or-later
11  */
12
13 #ifdef STRACE_UID_SIZE
14 # if STRACE_UID_SIZE != 16
15 #  error invalid STRACE_UID_SIZE
16 # endif
17
18 # define SIZEIFY(x)             SIZEIFY_(x, STRACE_UID_SIZE)
19 # define SIZEIFY_(x, size)      SIZEIFY__(x, size)
20 # define SIZEIFY__(x, size)     x ## size
21
22 # define printuid       SIZEIFY(printuid)
23 # define sys_chown      SIZEIFY(sys_chown)
24 # define sys_fchown     SIZEIFY(sys_fchown)
25 # define sys_getgroups  SIZEIFY(sys_getgroups)
26 # define sys_getresuid  SIZEIFY(sys_getresuid)
27 # define sys_getuid     SIZEIFY(sys_getuid)
28 # define sys_setfsuid   SIZEIFY(sys_setfsuid)
29 # define sys_setgroups  SIZEIFY(sys_setgroups)
30 # define sys_setresuid  SIZEIFY(sys_setresuid)
31 # define sys_setreuid   SIZEIFY(sys_setreuid)
32 # define sys_setuid     SIZEIFY(sys_setuid)
33 #endif /* STRACE_UID_SIZE */
34
35 #include "defs.h"
36
37 #ifdef STRACE_UID_SIZE
38 # if !HAVE_ARCH_UID16_SYSCALLS
39 #  undef STRACE_UID_SIZE
40 # endif
41 #else
42 # define STRACE_UID_SIZE 32
43 #endif
44
45 #ifdef STRACE_UID_SIZE
46
47 # undef uid_t
48 # define uid_t          uid_t_(STRACE_UID_SIZE)
49 # define uid_t_(size)   uid_t__(size)
50 # define uid_t__(size)  uint ## size ## _t
51
52 SYS_FUNC(getuid)
53 {
54         return RVAL_DECODED;
55 }
56
57 SYS_FUNC(setfsuid)
58 {
59         printuid("", tcp->u_arg[0]);
60
61         return RVAL_DECODED;
62 }
63
64 SYS_FUNC(setuid)
65 {
66         printuid("", tcp->u_arg[0]);
67
68         return RVAL_DECODED;
69 }
70
71 static void
72 get_print_uid(struct tcb *const tcp, const char *const prefix,
73               const kernel_ulong_t addr)
74 {
75         uid_t uid;
76
77         tprints(prefix);
78         if (!umove_or_printaddr(tcp, addr, &uid)) {
79                 printuid("[", uid);
80                 tprints("]");
81         }
82 }
83
84 SYS_FUNC(getresuid)
85 {
86         if (entering(tcp))
87                 return 0;
88
89         get_print_uid(tcp, "", tcp->u_arg[0]);
90         get_print_uid(tcp, ", ", tcp->u_arg[1]);
91         get_print_uid(tcp, ", ", tcp->u_arg[2]);
92
93         return 0;
94 }
95
96 SYS_FUNC(setreuid)
97 {
98         printuid("", tcp->u_arg[0]);
99         printuid(", ", tcp->u_arg[1]);
100
101         return RVAL_DECODED;
102 }
103
104 SYS_FUNC(setresuid)
105 {
106         printuid("", tcp->u_arg[0]);
107         printuid(", ", tcp->u_arg[1]);
108         printuid(", ", tcp->u_arg[2]);
109
110         return RVAL_DECODED;
111 }
112
113 SYS_FUNC(chown)
114 {
115         printpath(tcp, tcp->u_arg[0]);
116         printuid(", ", tcp->u_arg[1]);
117         printuid(", ", tcp->u_arg[2]);
118
119         return RVAL_DECODED;
120 }
121
122 SYS_FUNC(fchown)
123 {
124         printfd(tcp, tcp->u_arg[0]);
125         printuid(", ", tcp->u_arg[1]);
126         printuid(", ", tcp->u_arg[2]);
127
128         return RVAL_DECODED;
129 }
130
131 void
132 printuid(const char *text, const unsigned int uid)
133 {
134         if ((uid_t) -1U == (uid_t) uid)
135                 tprintf("%s-1", text);
136         else
137                 tprintf("%s%u", text, (uid_t) uid);
138 }
139
140 static bool
141 print_gid(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
142 {
143         printuid("", (*(uid_t *) elem_buf));
144
145         return true;
146 }
147
148 static void
149 print_groups(struct tcb *const tcp, const unsigned int len,
150              const kernel_ulong_t addr)
151 {
152         static unsigned long ngroups_max;
153         if (!ngroups_max)
154                 ngroups_max = sysconf(_SC_NGROUPS_MAX);
155
156         if (len > ngroups_max) {
157                 printaddr(addr);
158                 return;
159         }
160
161         uid_t gid;
162         print_array(tcp, addr, len, &gid, sizeof(gid),
163                     tfetch_mem, print_gid, 0);
164 }
165
166 SYS_FUNC(setgroups)
167 {
168         const int len = tcp->u_arg[0];
169
170         tprintf("%d, ", len);
171         print_groups(tcp, len, tcp->u_arg[1]);
172         return RVAL_DECODED;
173 }
174
175 SYS_FUNC(getgroups)
176 {
177         if (entering(tcp))
178                 tprintf("%d, ", (int) tcp->u_arg[0]);
179         else
180                 print_groups(tcp, tcp->u_rval, tcp->u_arg[1]);
181         return 0;
182 }
183
184 #endif /* STRACE_UID_SIZE */