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.

prctl.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. * Copyright (c) 2014-2018 The strace developers.
  7. * All rights reserved.
  8. *
  9. * SPDX-License-Identifier: LGPL-2.1-or-later
  10. */
  11. #include "defs.h"
  12. #include <linux/prctl.h>
  13. #include "xstring.h"
  14. #include "xlat/prctl_options.h"
  15. #include "xlat/pr_cap_ambient.h"
  16. #include "xlat/pr_dumpable.h"
  17. #include "xlat/pr_fp_mode.h"
  18. #include "xlat/pr_mce_kill.h"
  19. #include "xlat/pr_mce_kill_policy.h"
  20. #include "xlat/pr_set_mm.h"
  21. #include "xlat/pr_spec_cmds.h"
  22. #include "xlat/pr_spec_get_store_bypass_flags.h"
  23. #include "xlat/pr_spec_set_store_bypass_flags.h"
  24. #include "xlat/pr_sve_vl_flags.h"
  25. #include "xlat/pr_tsc.h"
  26. #include "xlat/pr_unalign_flags.h"
  27. #ifndef TASK_COMM_LEN
  28. # define TASK_COMM_LEN 16
  29. #endif
  30. #ifdef HAVE_LINUX_SECCOMP_H
  31. # include <linux/seccomp.h>
  32. #endif
  33. #include "xlat/seccomp_mode.h"
  34. #ifdef HAVE_LINUX_SECUREBITS_H
  35. # include <linux/securebits.h>
  36. #endif
  37. #include "xlat/secbits.h"
  38. /* these constants are the same as in <linux/capability.h> */
  39. enum {
  40. #include "caps0.h"
  41. #include "caps1.h"
  42. };
  43. #include "xlat/cap.h"
  44. #ifndef PR_SVE_VL_LEN_MASK
  45. # define PR_SVE_VL_LEN_MASK 0xffff
  46. #endif
  47. static void
  48. print_prctl_args(struct tcb *tcp, const unsigned int first)
  49. {
  50. unsigned int i;
  51. for (i = first; i < n_args(tcp); ++i)
  52. tprintf(", %#" PRI_klx, tcp->u_arg[i]);
  53. }
  54. static char *
  55. sprint_sve_val(kernel_ulong_t arg)
  56. {
  57. static char out[sizeof("PR_SVE_SET_VL_ONEXEC|PR_SVE_VL_INHERIT|0x") +
  58. sizeof(kernel_ulong_t) * 2];
  59. kernel_ulong_t vl = arg & PR_SVE_VL_LEN_MASK;
  60. kernel_ulong_t flags = arg & ~PR_SVE_VL_LEN_MASK;
  61. const char *flags_str = sprintflags("", pr_sve_vl_flags, flags);
  62. xsprintf(out, "%s%s%#" PRI_klx,
  63. flags_str ?: "", flags_str ? "|" : "", vl);
  64. return out;
  65. }
  66. SYS_FUNC(prctl)
  67. {
  68. const unsigned int option = tcp->u_arg[0];
  69. const kernel_ulong_t arg2 = tcp->u_arg[1];
  70. const kernel_ulong_t arg3 = tcp->u_arg[2];
  71. /*
  72. * PR_SET_VMA is the only command which actually uses these arguments
  73. * currently, and it is available only on Android for now.
  74. */
  75. #ifdef __ANDROID__
  76. const kernel_ulong_t arg4 = tcp->u_arg[3];
  77. const kernel_ulong_t arg5 = tcp->u_arg[4];
  78. #endif
  79. unsigned int i;
  80. if (entering(tcp))
  81. printxval(prctl_options, option, "PR_???");
  82. switch (option) {
  83. case PR_GET_KEEPCAPS:
  84. case PR_GET_SECCOMP:
  85. case PR_GET_TIMERSLACK:
  86. case PR_GET_TIMING:
  87. return RVAL_DECODED;
  88. case PR_GET_CHILD_SUBREAPER:
  89. case PR_GET_ENDIAN:
  90. case PR_GET_FPEMU:
  91. case PR_GET_FPEXC:
  92. if (entering(tcp))
  93. tprints(", ");
  94. else
  95. printnum_int(tcp, arg2, "%u");
  96. break;
  97. case PR_GET_DUMPABLE:
  98. if (entering(tcp))
  99. break;
  100. if (syserror(tcp))
  101. return 0;
  102. tcp->auxstr = xlookup(pr_dumpable, (kernel_ulong_t) tcp->u_rval);
  103. return RVAL_STR;
  104. case PR_GET_NAME:
  105. if (entering(tcp)) {
  106. tprints(", ");
  107. } else {
  108. if (syserror(tcp))
  109. printaddr(arg2);
  110. else
  111. printstr_ex(tcp, arg2, TASK_COMM_LEN,
  112. QUOTE_0_TERMINATED);
  113. }
  114. break;
  115. case PR_GET_PDEATHSIG:
  116. if (entering(tcp)) {
  117. tprints(", ");
  118. } else if (!umove_or_printaddr(tcp, arg2, &i)) {
  119. tprints("[");
  120. printsignal(i);
  121. tprints("]");
  122. }
  123. break;
  124. case PR_GET_SECUREBITS:
  125. if (entering(tcp))
  126. break;
  127. if (syserror(tcp) || tcp->u_rval == 0)
  128. return 0;
  129. tcp->auxstr = sprintflags("", secbits,
  130. (kernel_ulong_t) tcp->u_rval);
  131. return RVAL_STR;
  132. case PR_GET_TID_ADDRESS:
  133. if (entering(tcp))
  134. tprints(", ");
  135. else
  136. printnum_kptr(tcp, arg2);
  137. break;
  138. case PR_GET_TSC:
  139. if (entering(tcp)) {
  140. tprints(", ");
  141. } else if (!umove_or_printaddr(tcp, arg2, &i)) {
  142. tprints("[");
  143. printxval(pr_tsc, i, "PR_TSC_???");
  144. tprints("]");
  145. }
  146. break;
  147. case PR_GET_UNALIGN:
  148. if (entering(tcp)) {
  149. tprints(", ");
  150. } else if (!umove_or_printaddr(tcp, arg2, &i)) {
  151. tprints("[");
  152. printflags(pr_unalign_flags, i, "PR_UNALIGN_???");
  153. tprints("]");
  154. }
  155. break;
  156. case PR_GET_FP_MODE:
  157. if (entering(tcp))
  158. break;
  159. if (syserror(tcp) || tcp->u_rval == 0)
  160. return 0;
  161. tcp->auxstr = sprintflags("", pr_fp_mode,
  162. (kernel_ulong_t) tcp->u_rval);
  163. return RVAL_STR;
  164. case PR_SVE_SET_VL:
  165. if (entering(tcp)) {
  166. tprintf(", %s", sprint_sve_val(arg2));
  167. return 0;
  168. }
  169. ATTRIBUTE_FALLTHROUGH;
  170. case PR_SVE_GET_VL:
  171. if (entering(tcp))
  172. break;
  173. if (syserror(tcp) || tcp->u_rval == 0)
  174. return 0;
  175. tcp->auxstr = sprint_sve_val(tcp->u_rval);
  176. return RVAL_STR;
  177. case PR_GET_SPECULATION_CTRL:
  178. if (entering(tcp)) {
  179. tprints(", ");
  180. printxval64(pr_spec_cmds, arg2, "PR_SPEC_???");
  181. break;
  182. }
  183. if (syserror(tcp))
  184. return 0;
  185. switch (arg2) {
  186. case PR_SPEC_STORE_BYPASS:
  187. case PR_SPEC_INDIRECT_BRANCH:
  188. tcp->auxstr = sprintflags("",
  189. pr_spec_get_store_bypass_flags,
  190. (kernel_ulong_t) tcp->u_rval);
  191. break;
  192. }
  193. return RVAL_STR;
  194. /* PR_TASK_PERF_EVENTS_* take no arguments. */
  195. case PR_TASK_PERF_EVENTS_DISABLE:
  196. case PR_TASK_PERF_EVENTS_ENABLE:
  197. return RVAL_DECODED;
  198. case PR_SET_CHILD_SUBREAPER:
  199. case PR_SET_ENDIAN:
  200. case PR_SET_FPEMU:
  201. case PR_SET_FPEXC:
  202. case PR_SET_KEEPCAPS:
  203. case PR_SET_TIMING:
  204. tprintf(", %" PRI_klu, arg2);
  205. return RVAL_DECODED;
  206. case PR_SET_DUMPABLE:
  207. tprints(", ");
  208. printxval64(pr_dumpable, arg2, "SUID_DUMP_???");
  209. return RVAL_DECODED;
  210. case PR_CAPBSET_DROP:
  211. case PR_CAPBSET_READ:
  212. tprints(", ");
  213. printxval64(cap, arg2, "CAP_???");
  214. return RVAL_DECODED;
  215. case PR_CAP_AMBIENT:
  216. tprints(", ");
  217. printxval64(pr_cap_ambient, arg2,
  218. "PR_CAP_AMBIENT_???");
  219. switch (arg2) {
  220. case PR_CAP_AMBIENT_RAISE:
  221. case PR_CAP_AMBIENT_LOWER:
  222. case PR_CAP_AMBIENT_IS_SET:
  223. tprints(", ");
  224. printxval64(cap, arg3, "CAP_???");
  225. print_prctl_args(tcp, 3);
  226. break;
  227. default:
  228. print_prctl_args(tcp, 2);
  229. break;
  230. }
  231. return RVAL_DECODED;
  232. case PR_MCE_KILL:
  233. tprints(", ");
  234. printxval64(pr_mce_kill, arg2, "PR_MCE_KILL_???");
  235. tprints(", ");
  236. if (PR_MCE_KILL_SET == arg2)
  237. printxval64(pr_mce_kill_policy, arg3,
  238. "PR_MCE_KILL_???");
  239. else
  240. tprintf("%#" PRI_klx, arg3);
  241. print_prctl_args(tcp, 3);
  242. return RVAL_DECODED;
  243. case PR_SET_NAME:
  244. tprints(", ");
  245. printstr_ex(tcp, arg2, TASK_COMM_LEN - 1,
  246. QUOTE_0_TERMINATED);
  247. return RVAL_DECODED;
  248. #ifdef __ANDROID__
  249. # ifndef PR_SET_VMA_ANON_NAME
  250. # define PR_SET_VMA_ANON_NAME 0
  251. # endif
  252. case PR_SET_VMA:
  253. if (arg2 == PR_SET_VMA_ANON_NAME) {
  254. tprintf(", PR_SET_VMA_ANON_NAME, %#" PRI_klx, arg3);
  255. tprintf(", %" PRI_klu ", ", arg4);
  256. printstr(tcp, arg5);
  257. } else {
  258. /* There are no other sub-options now, but there
  259. * might be in future... */
  260. print_prctl_args(tcp, 1);
  261. }
  262. return RVAL_DECODED;
  263. #endif
  264. case PR_SET_MM:
  265. tprints(", ");
  266. printxval(pr_set_mm, arg2, "PR_SET_MM_???");
  267. print_prctl_args(tcp, 2);
  268. return RVAL_DECODED;
  269. case PR_SET_PDEATHSIG:
  270. tprints(", ");
  271. if (arg2 > 128)
  272. tprintf("%" PRI_klu, arg2);
  273. else
  274. printsignal(arg2);
  275. return RVAL_DECODED;
  276. case PR_SET_PTRACER:
  277. tprints(", ");
  278. if ((int) arg2 == -1) {
  279. print_xlat_ex(arg2, "PR_SET_PTRACER_ANY",
  280. XLAT_STYLE_DEFAULT);
  281. } else {
  282. tprintf("%" PRI_klu, arg2);
  283. }
  284. return RVAL_DECODED;
  285. case PR_SET_SECCOMP:
  286. tprints(", ");
  287. printxval64(seccomp_mode, arg2,
  288. "SECCOMP_MODE_???");
  289. if (SECCOMP_MODE_STRICT == arg2)
  290. return RVAL_DECODED;
  291. if (SECCOMP_MODE_FILTER == arg2) {
  292. tprints(", ");
  293. decode_seccomp_fprog(tcp, arg3);
  294. return RVAL_DECODED;
  295. }
  296. print_prctl_args(tcp, 2);
  297. return RVAL_DECODED;
  298. case PR_SET_SECUREBITS:
  299. tprints(", ");
  300. printflags64(secbits, arg2, "SECBIT_???");
  301. return RVAL_DECODED;
  302. case PR_SET_TIMERSLACK:
  303. tprintf(", %" PRI_kld, arg2);
  304. return RVAL_DECODED;
  305. case PR_SET_TSC:
  306. tprints(", ");
  307. printxval(pr_tsc, arg2, "PR_TSC_???");
  308. return RVAL_DECODED;
  309. case PR_SET_UNALIGN:
  310. tprints(", ");
  311. printflags(pr_unalign_flags, arg2, "PR_UNALIGN_???");
  312. return RVAL_DECODED;
  313. case PR_SET_NO_NEW_PRIVS:
  314. case PR_SET_THP_DISABLE:
  315. tprintf(", %" PRI_klu, arg2);
  316. print_prctl_args(tcp, 2);
  317. return RVAL_DECODED;
  318. case PR_MCE_KILL_GET:
  319. if (entering(tcp)) {
  320. print_prctl_args(tcp, 1);
  321. return 0;
  322. }
  323. if (syserror(tcp))
  324. return 0;
  325. tcp->auxstr = xlookup(pr_mce_kill_policy,
  326. (kernel_ulong_t) tcp->u_rval);
  327. return RVAL_STR;
  328. case PR_SET_FP_MODE:
  329. tprints(", ");
  330. printflags(pr_fp_mode, arg2, "PR_FP_MODE_???");
  331. return RVAL_DECODED;
  332. case PR_SET_SPECULATION_CTRL:
  333. tprints(", ");
  334. printxval64(pr_spec_cmds, arg2, "PR_SPEC_???");
  335. tprints(", ");
  336. switch (arg2) {
  337. case PR_SPEC_STORE_BYPASS:
  338. case PR_SPEC_INDIRECT_BRANCH:
  339. printxval64(pr_spec_set_store_bypass_flags, arg3,
  340. "PR_SPEC_???");
  341. break;
  342. default:
  343. tprintf("%#" PRI_klx, arg3);
  344. }
  345. return RVAL_DECODED;
  346. case PR_GET_NO_NEW_PRIVS:
  347. case PR_GET_THP_DISABLE:
  348. case PR_MPX_DISABLE_MANAGEMENT:
  349. case PR_MPX_ENABLE_MANAGEMENT:
  350. default:
  351. print_prctl_args(tcp, 1);
  352. return RVAL_DECODED;
  353. }
  354. return 0;
  355. }
  356. #if defined X86_64 || defined X32 || defined I386
  357. # include "xlat/archvals.h"
  358. SYS_FUNC(arch_prctl)
  359. {
  360. const unsigned int option = tcp->u_arg[0];
  361. const kernel_ulong_t addr = tcp->u_arg[1];
  362. if (entering(tcp))
  363. printxval(archvals, option, "ARCH_???");
  364. switch (option) {
  365. case ARCH_GET_GS:
  366. case ARCH_GET_FS:
  367. if (entering(tcp))
  368. tprints(", ");
  369. else
  370. printnum_ptr(tcp, addr);
  371. return 0;
  372. }
  373. tprintf(", %#" PRI_klx, addr);
  374. return RVAL_DECODED;
  375. }
  376. #endif /* X86_64 || X32 || I386 */