• 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-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  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifdef STRACE_UID_SIZE
33 # if STRACE_UID_SIZE != 16
34 #  error invalid STRACE_UID_SIZE
35 # endif
36 
37 # define SIZEIFY(x)		SIZEIFY_(x, STRACE_UID_SIZE)
38 # define SIZEIFY_(x, size)	SIZEIFY__(x, size)
39 # define SIZEIFY__(x, size)	x ## size
40 
41 # define printuid	SIZEIFY(printuid)
42 # define sys_chown	SIZEIFY(sys_chown)
43 # define sys_fchown	SIZEIFY(sys_fchown)
44 # define sys_getgroups	SIZEIFY(sys_getgroups)
45 # define sys_getresuid	SIZEIFY(sys_getresuid)
46 # define sys_getuid	SIZEIFY(sys_getuid)
47 # define sys_setfsuid	SIZEIFY(sys_setfsuid)
48 # define sys_setgroups	SIZEIFY(sys_setgroups)
49 # define sys_setresuid	SIZEIFY(sys_setresuid)
50 # define sys_setreuid	SIZEIFY(sys_setreuid)
51 # define sys_setuid	SIZEIFY(sys_setuid)
52 #endif /* STRACE_UID_SIZE */
53 
54 #include "defs.h"
55 
56 #ifdef STRACE_UID_SIZE
57 # if !NEED_UID16_PARSERS
58 #  undef STRACE_UID_SIZE
59 # endif
60 #else
61 # define STRACE_UID_SIZE 32
62 #endif
63 
64 #ifdef STRACE_UID_SIZE
65 
66 # undef uid_t
67 # define uid_t		uid_t_(STRACE_UID_SIZE)
68 # define uid_t_(size)	uid_t__(size)
69 # define uid_t__(size)	uint ## size ## _t
70 
SYS_FUNC(getuid)71 SYS_FUNC(getuid)
72 {
73 	return RVAL_UDECIMAL | RVAL_DECODED;
74 }
75 
SYS_FUNC(setfsuid)76 SYS_FUNC(setfsuid)
77 {
78 	printuid("", tcp->u_arg[0]);
79 
80 	return RVAL_UDECIMAL | RVAL_DECODED;
81 }
82 
SYS_FUNC(setuid)83 SYS_FUNC(setuid)
84 {
85 	printuid("", tcp->u_arg[0]);
86 
87 	return RVAL_DECODED;
88 }
89 
90 static void
get_print_uid(struct tcb * const tcp,const char * const prefix,const kernel_ulong_t addr)91 get_print_uid(struct tcb *const tcp, const char *const prefix,
92 	      const kernel_ulong_t addr)
93 {
94 	uid_t uid;
95 
96 	tprints(prefix);
97 	if (!umove_or_printaddr(tcp, addr, &uid)) {
98 		printuid("[", uid);
99 		tprints("]");
100 	}
101 }
102 
SYS_FUNC(getresuid)103 SYS_FUNC(getresuid)
104 {
105 	if (entering(tcp))
106 		return 0;
107 
108 	get_print_uid(tcp, "", tcp->u_arg[0]);
109 	get_print_uid(tcp, ", ", tcp->u_arg[1]);
110 	get_print_uid(tcp, ", ", tcp->u_arg[2]);
111 
112 	return 0;
113 }
114 
SYS_FUNC(setreuid)115 SYS_FUNC(setreuid)
116 {
117 	printuid("", tcp->u_arg[0]);
118 	printuid(", ", tcp->u_arg[1]);
119 
120 	return RVAL_DECODED;
121 }
122 
SYS_FUNC(setresuid)123 SYS_FUNC(setresuid)
124 {
125 	printuid("", tcp->u_arg[0]);
126 	printuid(", ", tcp->u_arg[1]);
127 	printuid(", ", tcp->u_arg[2]);
128 
129 	return RVAL_DECODED;
130 }
131 
SYS_FUNC(chown)132 SYS_FUNC(chown)
133 {
134 	printpath(tcp, tcp->u_arg[0]);
135 	printuid(", ", tcp->u_arg[1]);
136 	printuid(", ", tcp->u_arg[2]);
137 
138 	return RVAL_DECODED;
139 }
140 
SYS_FUNC(fchown)141 SYS_FUNC(fchown)
142 {
143 	printfd(tcp, tcp->u_arg[0]);
144 	printuid(", ", tcp->u_arg[1]);
145 	printuid(", ", tcp->u_arg[2]);
146 
147 	return RVAL_DECODED;
148 }
149 
150 void
printuid(const char * text,const unsigned int uid)151 printuid(const char *text, const unsigned int uid)
152 {
153 	if ((uid_t) -1U == (uid_t) uid)
154 		tprintf("%s-1", text);
155 	else
156 		tprintf("%s%u", text, (uid_t) uid);
157 }
158 
159 static bool
print_gid(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)160 print_gid(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
161 {
162 	printuid("", (*(uid_t *) elem_buf));
163 
164 	return true;
165 }
166 
167 static void
print_groups(struct tcb * const tcp,const unsigned int len,const kernel_ulong_t addr)168 print_groups(struct tcb *const tcp, const unsigned int len,
169 	     const kernel_ulong_t addr)
170 {
171 	static unsigned long ngroups_max;
172 	if (!ngroups_max)
173 		ngroups_max = sysconf(_SC_NGROUPS_MAX);
174 
175 	if (len > ngroups_max) {
176 		printaddr(addr);
177 		return;
178 	}
179 
180 	uid_t gid;
181 	print_array(tcp, addr, len, &gid, sizeof(gid),
182 		    umoven_or_printaddr, print_gid, 0);
183 }
184 
SYS_FUNC(setgroups)185 SYS_FUNC(setgroups)
186 {
187 	const int len = tcp->u_arg[0];
188 
189 	tprintf("%d, ", len);
190 	print_groups(tcp, len, tcp->u_arg[1]);
191 	return RVAL_DECODED;
192 }
193 
SYS_FUNC(getgroups)194 SYS_FUNC(getgroups)
195 {
196 	if (entering(tcp))
197 		tprintf("%d, ", (int) tcp->u_arg[0]);
198 	else
199 		print_groups(tcp, tcp->u_rval, tcp->u_arg[1]);
200 	return 0;
201 }
202 
203 #endif /* STRACE_UID_SIZE */
204