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