Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uid.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifdef STRACE_UID_SIZE
  13. # if STRACE_UID_SIZE != 16
  14. # error invalid STRACE_UID_SIZE
  15. # endif
  16. # define SIZEIFY(x) SIZEIFY_(x, STRACE_UID_SIZE)
  17. # define SIZEIFY_(x, size) SIZEIFY__(x, size)
  18. # define SIZEIFY__(x, size) x ## size
  19. # define printuid SIZEIFY(printuid)
  20. # define sys_chown SIZEIFY(sys_chown)
  21. # define sys_fchown SIZEIFY(sys_fchown)
  22. # define sys_getgroups SIZEIFY(sys_getgroups)
  23. # define sys_getresuid SIZEIFY(sys_getresuid)
  24. # define sys_getuid SIZEIFY(sys_getuid)
  25. # define sys_setfsuid SIZEIFY(sys_setfsuid)
  26. # define sys_setgroups SIZEIFY(sys_setgroups)
  27. # define sys_setresuid SIZEIFY(sys_setresuid)
  28. # define sys_setreuid SIZEIFY(sys_setreuid)
  29. # define sys_setuid SIZEIFY(sys_setuid)
  30. #endif /* STRACE_UID_SIZE */
  31. #include "defs.h"
  32. #ifdef STRACE_UID_SIZE
  33. # if !HAVE_ARCH_UID16_SYSCALLS
  34. # undef STRACE_UID_SIZE
  35. # endif
  36. #else
  37. # define STRACE_UID_SIZE 32
  38. #endif
  39. #ifdef STRACE_UID_SIZE
  40. # undef uid_t
  41. # define uid_t uid_t_(STRACE_UID_SIZE)
  42. # define uid_t_(size) uid_t__(size)
  43. # define uid_t__(size) uint ## size ## _t
  44. SYS_FUNC(getuid)
  45. {
  46. return RVAL_DECODED;
  47. }
  48. SYS_FUNC(setfsuid)
  49. {
  50. printuid("", tcp->u_arg[0]);
  51. return RVAL_DECODED;
  52. }
  53. SYS_FUNC(setuid)
  54. {
  55. printuid("", tcp->u_arg[0]);
  56. return RVAL_DECODED;
  57. }
  58. static void
  59. get_print_uid(struct tcb *const tcp, const char *const prefix,
  60. const kernel_ulong_t addr)
  61. {
  62. uid_t uid;
  63. tprints(prefix);
  64. if (!umove_or_printaddr(tcp, addr, &uid)) {
  65. printuid("[", uid);
  66. tprints("]");
  67. }
  68. }
  69. SYS_FUNC(getresuid)
  70. {
  71. if (entering(tcp))
  72. return 0;
  73. get_print_uid(tcp, "", tcp->u_arg[0]);
  74. get_print_uid(tcp, ", ", tcp->u_arg[1]);
  75. get_print_uid(tcp, ", ", tcp->u_arg[2]);
  76. return 0;
  77. }
  78. SYS_FUNC(setreuid)
  79. {
  80. printuid("", tcp->u_arg[0]);
  81. printuid(", ", tcp->u_arg[1]);
  82. return RVAL_DECODED;
  83. }
  84. SYS_FUNC(setresuid)
  85. {
  86. printuid("", tcp->u_arg[0]);
  87. printuid(", ", tcp->u_arg[1]);
  88. printuid(", ", tcp->u_arg[2]);
  89. return RVAL_DECODED;
  90. }
  91. SYS_FUNC(chown)
  92. {
  93. printpath(tcp, tcp->u_arg[0]);
  94. printuid(", ", tcp->u_arg[1]);
  95. printuid(", ", tcp->u_arg[2]);
  96. return RVAL_DECODED;
  97. }
  98. SYS_FUNC(fchown)
  99. {
  100. printfd(tcp, tcp->u_arg[0]);
  101. printuid(", ", tcp->u_arg[1]);
  102. printuid(", ", tcp->u_arg[2]);
  103. return RVAL_DECODED;
  104. }
  105. void
  106. printuid(const char *text, const unsigned int uid)
  107. {
  108. if ((uid_t) -1U == (uid_t) uid)
  109. tprintf("%s-1", text);
  110. else
  111. tprintf("%s%u", text, (uid_t) uid);
  112. }
  113. static bool
  114. print_gid(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
  115. {
  116. printuid("", (*(uid_t *) elem_buf));
  117. return true;
  118. }
  119. static void
  120. print_groups(struct tcb *const tcp, const unsigned int len,
  121. const kernel_ulong_t addr)
  122. {
  123. static unsigned long ngroups_max;
  124. if (!ngroups_max)
  125. ngroups_max = sysconf(_SC_NGROUPS_MAX);
  126. if (len > ngroups_max) {
  127. printaddr(addr);
  128. return;
  129. }
  130. uid_t gid;
  131. print_array(tcp, addr, len, &gid, sizeof(gid),
  132. tfetch_mem, print_gid, 0);
  133. }
  134. SYS_FUNC(setgroups)
  135. {
  136. const int len = tcp->u_arg[0];
  137. tprintf("%d, ", len);
  138. print_groups(tcp, len, tcp->u_arg[1]);
  139. return RVAL_DECODED;
  140. }
  141. SYS_FUNC(getgroups)
  142. {
  143. if (entering(tcp))
  144. tprintf("%d, ", (int) tcp->u_arg[0]);
  145. else
  146. print_groups(tcp, tcp->u_rval, tcp->u_arg[1]);
  147. return 0;
  148. }
  149. #endif /* STRACE_UID_SIZE */