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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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-2019 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. case PR_GET_TAGGED_ADDR_CTRL:
  88. return RVAL_DECODED;
  89. case PR_GET_CHILD_SUBREAPER:
  90. case PR_GET_ENDIAN:
  91. case PR_GET_FPEMU:
  92. case PR_GET_FPEXC:
  93. if (entering(tcp))
  94. tprints(", ");
  95. else
  96. printnum_int(tcp, arg2, "%u");
  97. break;
  98. case PR_GET_DUMPABLE:
  99. if (entering(tcp))
  100. break;
  101. if (syserror(tcp))
  102. return 0;
  103. tcp->auxstr = xlookup(pr_dumpable, (kernel_ulong_t) tcp->u_rval);
  104. return RVAL_STR;
  105. case PR_GET_NAME:
  106. if (entering(tcp)) {
  107. tprints(", ");
  108. } else {
  109. if (syserror(tcp))
  110. printaddr(arg2);
  111. else
  112. printstr_ex(tcp, arg2, TASK_COMM_LEN,
  113. QUOTE_0_TERMINATED);
  114. }
  115. break;
  116. case PR_GET_PDEATHSIG:
  117. if (entering(tcp)) {
  118. tprints(", ");
  119. } else if (!umove_or_printaddr(tcp, arg2, &i)) {
  120. tprints("[");
  121. printsignal(i);
  122. tprints("]");
  123. }
  124. break;
  125. case PR_GET_SECUREBITS:
  126. if (entering(tcp))
  127. break;
  128. if (syserror(tcp) || tcp->u_rval == 0)
  129. return 0;
  130. tcp->auxstr = sprintflags("", secbits,
  131. (kernel_ulong_t) tcp->u_rval);
  132. return RVAL_STR;
  133. case PR_GET_TID_ADDRESS:
  134. if (entering(tcp))
  135. tprints(", ");
  136. else
  137. printnum_kptr(tcp, arg2);
  138. break;
  139. case PR_GET_TSC:
  140. if (entering(tcp)) {
  141. tprints(", ");
  142. } else if (!umove_or_printaddr(tcp, arg2, &i)) {
  143. tprints("[");
  144. printxval(pr_tsc, i, "PR_TSC_???");
  145. tprints("]");
  146. }
  147. break;
  148. case PR_GET_UNALIGN:
  149. if (entering(tcp)) {
  150. tprints(", ");
  151. } else if (!umove_or_printaddr(tcp, arg2, &i)) {
  152. tprints("[");
  153. printflags(pr_unalign_flags, i, "PR_UNALIGN_???");
  154. tprints("]");
  155. }
  156. break;
  157. case PR_GET_FP_MODE:
  158. if (entering(tcp))
  159. break;
  160. if (syserror(tcp) || tcp->u_rval == 0)
  161. return 0;
  162. tcp->auxstr = sprintflags("", pr_fp_mode,
  163. (kernel_ulong_t) tcp->u_rval);
  164. return RVAL_STR;
  165. case PR_SVE_SET_VL:
  166. if (entering(tcp)) {
  167. tprintf(", %s", sprint_sve_val(arg2));
  168. return 0;
  169. }
  170. ATTRIBUTE_FALLTHROUGH;
  171. case PR_SVE_GET_VL:
  172. if (entering(tcp))
  173. break;
  174. if (syserror(tcp) || tcp->u_rval == 0)
  175. return 0;
  176. tcp->auxstr = sprint_sve_val(tcp->u_rval);
  177. return RVAL_STR;
  178. case PR_GET_SPECULATION_CTRL:
  179. if (entering(tcp)) {
  180. tprints(", ");
  181. printxval64(pr_spec_cmds, arg2, "PR_SPEC_???");
  182. break;
  183. }
  184. if (syserror(tcp))
  185. return 0;
  186. switch (arg2) {
  187. case PR_SPEC_STORE_BYPASS:
  188. case PR_SPEC_INDIRECT_BRANCH:
  189. tcp->auxstr = sprintflags("",
  190. pr_spec_get_store_bypass_flags,
  191. (kernel_ulong_t) tcp->u_rval);
  192. break;
  193. }
  194. return RVAL_STR;
  195. /* PR_TASK_PERF_EVENTS_* take no arguments. */
  196. case PR_TASK_PERF_EVENTS_DISABLE:
  197. case PR_TASK_PERF_EVENTS_ENABLE:
  198. return RVAL_DECODED;
  199. case PR_SET_CHILD_SUBREAPER:
  200. case PR_SET_ENDIAN:
  201. case PR_SET_FPEMU:
  202. case PR_SET_FPEXC:
  203. case PR_SET_KEEPCAPS:
  204. case PR_SET_TIMING:
  205. case PR_SET_TAGGED_ADDR_CTRL:
  206. tprintf(", %" PRI_klu, arg2);
  207. return RVAL_DECODED;
  208. case PR_SET_DUMPABLE:
  209. tprints(", ");
  210. printxval64(pr_dumpable, arg2, "SUID_DUMP_???");
  211. return RVAL_DECODED;
  212. case PR_CAPBSET_DROP:
  213. case PR_CAPBSET_READ:
  214. tprints(", ");
  215. printxval64(cap, arg2, "CAP_???");
  216. return RVAL_DECODED;
  217. case PR_CAP_AMBIENT:
  218. tprints(", ");
  219. printxval64(pr_cap_ambient, arg2,
  220. "PR_CAP_AMBIENT_???");
  221. switch (arg2) {
  222. case PR_CAP_AMBIENT_RAISE:
  223. case PR_CAP_AMBIENT_LOWER:
  224. case PR_CAP_AMBIENT_IS_SET:
  225. tprints(", ");
  226. printxval64(cap, arg3, "CAP_???");
  227. print_prctl_args(tcp, 3);
  228. break;
  229. default:
  230. print_prctl_args(tcp, 2);
  231. break;
  232. }
  233. return RVAL_DECODED;
  234. case PR_MCE_KILL:
  235. tprints(", ");
  236. printxval64(pr_mce_kill, arg2, "PR_MCE_KILL_???");
  237. tprints(", ");
  238. if (PR_MCE_KILL_SET == arg2)
  239. printxval64(pr_mce_kill_policy, arg3,
  240. "PR_MCE_KILL_???");
  241. else
  242. tprintf("%#" PRI_klx, arg3);
  243. print_prctl_args(tcp, 3);
  244. return RVAL_DECODED;
  245. case PR_SET_NAME:
  246. tprints(", ");
  247. printstr_ex(tcp, arg2, TASK_COMM_LEN - 1,
  248. QUOTE_0_TERMINATED);
  249. return RVAL_DECODED;
  250. #ifdef __ANDROID__
  251. # ifndef PR_SET_VMA_ANON_NAME
  252. # define PR_SET_VMA_ANON_NAME 0
  253. # endif
  254. case PR_SET_VMA:
  255. if (arg2 == PR_SET_VMA_ANON_NAME) {
  256. tprintf(", PR_SET_VMA_ANON_NAME, %#" PRI_klx, arg3);
  257. tprintf(", %" PRI_klu ", ", arg4);
  258. printstr(tcp, arg5);
  259. } else {
  260. /* There are no other sub-options now, but there
  261. * might be in future... */
  262. print_prctl_args(tcp, 1);
  263. }
  264. return RVAL_DECODED;
  265. #endif
  266. case PR_SET_MM:
  267. tprints(", ");
  268. printxval(pr_set_mm, arg2, "PR_SET_MM_???");
  269. print_prctl_args(tcp, 2);
  270. return RVAL_DECODED;
  271. case PR_SET_PDEATHSIG:
  272. tprints(", ");
  273. if (arg2 > 128)
  274. tprintf("%" PRI_klu, arg2);
  275. else
  276. printsignal(arg2);
  277. return RVAL_DECODED;
  278. case PR_SET_PTRACER:
  279. tprints(", ");
  280. if ((int) arg2 == -1) {
  281. print_xlat_ex(arg2, "PR_SET_PTRACER_ANY",
  282. XLAT_STYLE_DEFAULT);
  283. } else {
  284. tprintf("%" PRI_klu, arg2);
  285. }
  286. return RVAL_DECODED;
  287. case PR_SET_SECCOMP:
  288. tprints(", ");
  289. printxval64(seccomp_mode, arg2,
  290. "SECCOMP_MODE_???");
  291. if (SECCOMP_MODE_STRICT == arg2)
  292. return RVAL_DECODED;
  293. if (SECCOMP_MODE_FILTER == arg2) {
  294. tprints(", ");
  295. decode_seccomp_fprog(tcp, arg3);
  296. return RVAL_DECODED;
  297. }
  298. print_prctl_args(tcp, 2);
  299. return RVAL_DECODED;
  300. case PR_SET_SECUREBITS:
  301. tprints(", ");
  302. printflags64(secbits, arg2, "SECBIT_???");
  303. return RVAL_DECODED;
  304. case PR_SET_TIMERSLACK:
  305. tprintf(", %" PRI_kld, arg2);
  306. return RVAL_DECODED;
  307. case PR_SET_TSC:
  308. tprints(", ");
  309. printxval(pr_tsc, arg2, "PR_TSC_???");
  310. return RVAL_DECODED;
  311. case PR_SET_UNALIGN:
  312. tprints(", ");
  313. printflags(pr_unalign_flags, arg2, "PR_UNALIGN_???");
  314. return RVAL_DECODED;
  315. case PR_SET_NO_NEW_PRIVS:
  316. case PR_SET_THP_DISABLE:
  317. tprintf(", %" PRI_klu, arg2);
  318. print_prctl_args(tcp, 2);
  319. return RVAL_DECODED;
  320. case PR_MCE_KILL_GET:
  321. if (entering(tcp)) {
  322. print_prctl_args(tcp, 1);
  323. return 0;
  324. }
  325. if (syserror(tcp))
  326. return 0;
  327. tcp->auxstr = xlookup(pr_mce_kill_policy,
  328. (kernel_ulong_t) tcp->u_rval);
  329. return RVAL_STR;
  330. case PR_SET_FP_MODE:
  331. tprints(", ");
  332. printflags(pr_fp_mode, arg2, "PR_FP_MODE_???");
  333. return RVAL_DECODED;
  334. case PR_SET_SPECULATION_CTRL:
  335. tprints(", ");
  336. printxval64(pr_spec_cmds, arg2, "PR_SPEC_???");
  337. tprints(", ");
  338. switch (arg2) {
  339. case PR_SPEC_STORE_BYPASS:
  340. case PR_SPEC_INDIRECT_BRANCH:
  341. printxval64(pr_spec_set_store_bypass_flags, arg3,
  342. "PR_SPEC_???");
  343. break;
  344. default:
  345. tprintf("%#" PRI_klx, arg3);
  346. }
  347. return RVAL_DECODED;
  348. case PR_GET_NO_NEW_PRIVS:
  349. case PR_GET_THP_DISABLE:
  350. case PR_MPX_DISABLE_MANAGEMENT:
  351. case PR_MPX_ENABLE_MANAGEMENT:
  352. default:
  353. print_prctl_args(tcp, 1);
  354. return RVAL_DECODED;
  355. }
  356. return 0;
  357. }
  358. #if defined X86_64 || defined X32 || defined I386
  359. # include "xlat/archvals.h"
  360. SYS_FUNC(arch_prctl)
  361. {
  362. const unsigned int option = tcp->u_arg[0];
  363. const kernel_ulong_t addr = tcp->u_arg[1];
  364. if (entering(tcp))
  365. printxval(archvals, option, "ARCH_???");
  366. switch (option) {
  367. case ARCH_GET_GS:
  368. case ARCH_GET_FS:
  369. if (entering(tcp))
  370. tprints(", ");
  371. else
  372. printnum_ptr(tcp, addr);
  373. return 0;
  374. }
  375. tprintf(", %#" PRI_klx, addr);
  376. return RVAL_DECODED;
  377. }
  378. #endif /* X86_64 || X32 || I386 */