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.

syscall.c 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
  5. * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Linux for s390 port by D.J. Barrow
  8. * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
  9. * Copyright (c) 1999-2019 The strace developers.
  10. * All rights reserved.
  11. *
  12. * SPDX-License-Identifier: LGPL-2.1-or-later
  13. */
  14. #include "defs.h"
  15. #include "get_personality.h"
  16. #include "mmap_notify.h"
  17. #include "native_defs.h"
  18. #include "ptrace.h"
  19. #include "ptrace_syscall_info.h"
  20. #include "nsig.h"
  21. #include "number_set.h"
  22. #include "delay.h"
  23. #include "retval.h"
  24. #include <limits.h>
  25. /* for struct iovec */
  26. #include <sys/uio.h>
  27. /* for __X32_SYSCALL_BIT */
  28. #include "scno.h"
  29. #include "regs.h"
  30. #if defined(SPARC64)
  31. # undef PTRACE_GETREGS
  32. # define PTRACE_GETREGS PTRACE_GETREGS64
  33. # undef PTRACE_SETREGS
  34. # define PTRACE_SETREGS PTRACE_SETREGS64
  35. #endif
  36. #include "syscall.h"
  37. #include "xstring.h"
  38. #include "syscallent_base_nr.h"
  39. /* Define these shorthand notations to simplify the syscallent files. */
  40. #include "sysent_shorthand_defs.h"
  41. #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
  42. const struct_sysent sysent0[] = {
  43. #include "syscallent.h"
  44. };
  45. #if SUPPORTED_PERSONALITIES > 1
  46. # include PERSONALITY1_INCLUDE_FUNCS
  47. static const struct_sysent sysent1[] = {
  48. # include "syscallent1.h"
  49. };
  50. #endif
  51. #if SUPPORTED_PERSONALITIES > 2
  52. # include PERSONALITY2_INCLUDE_FUNCS
  53. static const struct_sysent sysent2[] = {
  54. # include "syscallent2.h"
  55. };
  56. #endif
  57. /* Now undef them since short defines cause wicked namespace pollution. */
  58. #include "sysent_shorthand_undefs.h"
  59. const char *const errnoent[] = {
  60. #include "errnoent.h"
  61. };
  62. const char *const signalent[] = {
  63. #include "signalent.h"
  64. };
  65. /*
  66. * `ioctlent[012].h' files are automatically generated by the auxiliary
  67. * program `ioctlsort', such that the list is sorted by the `code' field.
  68. * This has the side-effect of resolving the _IO.. macros into
  69. * plain integers, eliminating the need to include here everything
  70. * in "/usr/include".
  71. */
  72. const struct_ioctlent ioctlent0[] = {
  73. #include "ioctlent0.h"
  74. };
  75. #if SUPPORTED_PERSONALITIES > 1
  76. static const struct_ioctlent ioctlent1[] = {
  77. # include "ioctlent1.h"
  78. };
  79. # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
  80. static const struct_printers printers0 = {
  81. # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
  82. };
  83. # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
  84. static const struct_printers printers1 = {
  85. # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
  86. };
  87. #endif
  88. #if SUPPORTED_PERSONALITIES > 2
  89. static const struct_ioctlent ioctlent2[] = {
  90. # include "ioctlent2.h"
  91. };
  92. # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
  93. static const struct_printers printers2 = {
  94. # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
  95. };
  96. #endif
  97. enum {
  98. nsyscalls0 = ARRAY_SIZE(sysent0)
  99. #if SUPPORTED_PERSONALITIES > 1
  100. , nsyscalls1 = ARRAY_SIZE(sysent1)
  101. # if SUPPORTED_PERSONALITIES > 2
  102. , nsyscalls2 = ARRAY_SIZE(sysent2)
  103. # endif
  104. #endif
  105. };
  106. enum {
  107. nioctlents0 = ARRAY_SIZE(ioctlent0)
  108. #if SUPPORTED_PERSONALITIES > 1
  109. , nioctlents1 = ARRAY_SIZE(ioctlent1)
  110. # if SUPPORTED_PERSONALITIES > 2
  111. , nioctlents2 = ARRAY_SIZE(ioctlent2)
  112. # endif
  113. #endif
  114. };
  115. #if SUPPORTED_PERSONALITIES > 1
  116. const struct_sysent *sysent = sysent0;
  117. const struct_ioctlent *ioctlent = ioctlent0;
  118. const struct_printers *printers = &printers0;
  119. #endif
  120. const unsigned int nerrnos = ARRAY_SIZE(errnoent);
  121. const unsigned int nsignals = ARRAY_SIZE(signalent);
  122. unsigned nsyscalls = nsyscalls0;
  123. unsigned nioctlents = nioctlents0;
  124. const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
  125. nsyscalls0,
  126. #if SUPPORTED_PERSONALITIES > 1
  127. nsyscalls1,
  128. #endif
  129. #if SUPPORTED_PERSONALITIES > 2
  130. nsyscalls2,
  131. #endif
  132. };
  133. const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
  134. sysent0,
  135. #if SUPPORTED_PERSONALITIES > 1
  136. sysent1,
  137. #endif
  138. #if SUPPORTED_PERSONALITIES > 2
  139. sysent2,
  140. #endif
  141. };
  142. const char *const personality_names[] =
  143. #if defined X86_64
  144. {"64 bit", "32 bit", "x32"}
  145. #elif defined X32
  146. {"x32", "32 bit"}
  147. #elif SUPPORTED_PERSONALITIES == 2
  148. {"64 bit", "32 bit"}
  149. #else
  150. {STRINGIFY_VAL(__WORDSIZE) " bit"}
  151. #endif
  152. ;
  153. const char *const personality_designators[] =
  154. #if defined X86_64
  155. { "64", "32", "x32" }
  156. #elif defined X32
  157. { "x32", "32" }
  158. #elif SUPPORTED_PERSONALITIES == 2
  159. { "64", "32" }
  160. #else
  161. { STRINGIFY_VAL(__WORDSIZE) }
  162. #endif
  163. ;
  164. #if SUPPORTED_PERSONALITIES > 1
  165. unsigned current_personality;
  166. # ifndef current_wordsize
  167. unsigned current_wordsize = PERSONALITY0_WORDSIZE;
  168. static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
  169. PERSONALITY0_WORDSIZE,
  170. PERSONALITY1_WORDSIZE,
  171. # if SUPPORTED_PERSONALITIES > 2
  172. PERSONALITY2_WORDSIZE,
  173. # endif
  174. };
  175. # endif
  176. # ifndef current_klongsize
  177. unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
  178. static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
  179. PERSONALITY0_KLONGSIZE,
  180. PERSONALITY1_KLONGSIZE,
  181. # if SUPPORTED_PERSONALITIES > 2
  182. PERSONALITY2_KLONGSIZE,
  183. # endif
  184. };
  185. # endif
  186. void
  187. set_personality(unsigned int personality)
  188. {
  189. if (personality == current_personality)
  190. return;
  191. if (personality >= SUPPORTED_PERSONALITIES)
  192. error_msg_and_die("Requested switch to unsupported personality "
  193. "%u", personality);
  194. nsyscalls = nsyscall_vec[personality];
  195. sysent = sysent_vec[personality];
  196. switch (personality) {
  197. case 0:
  198. ioctlent = ioctlent0;
  199. nioctlents = nioctlents0;
  200. printers = &printers0;
  201. break;
  202. case 1:
  203. ioctlent = ioctlent1;
  204. nioctlents = nioctlents1;
  205. printers = &printers1;
  206. break;
  207. # if SUPPORTED_PERSONALITIES > 2
  208. case 2:
  209. ioctlent = ioctlent2;
  210. nioctlents = nioctlents2;
  211. printers = &printers2;
  212. break;
  213. # endif
  214. }
  215. current_personality = personality;
  216. # ifndef current_wordsize
  217. current_wordsize = personality_wordsize[personality];
  218. # endif
  219. # ifndef current_klongsize
  220. current_klongsize = personality_klongsize[personality];
  221. # endif
  222. }
  223. static void
  224. update_personality(struct tcb *tcp, unsigned int personality)
  225. {
  226. static bool need_mpers_warning[] =
  227. { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
  228. set_personality(personality);
  229. if (personality == tcp->currpers)
  230. return;
  231. tcp->currpers = personality;
  232. if (!qflag) {
  233. error_msg("[ Process PID=%d runs in %s mode. ]",
  234. tcp->pid, personality_names[personality]);
  235. }
  236. if (need_mpers_warning[personality]) {
  237. error_msg("WARNING: Proper structure decoding for this "
  238. "personality is not supported, please consider "
  239. "building strace with mpers support enabled.");
  240. need_mpers_warning[personality] = false;
  241. }
  242. }
  243. #endif
  244. #ifdef SYS_socket_subcall
  245. static void
  246. decode_socket_subcall(struct tcb *tcp)
  247. {
  248. const int call = tcp->u_arg[0];
  249. if (call < 1 || call >= SYS_socket_nsubcalls)
  250. return;
  251. const kernel_ulong_t scno = SYS_socket_subcall + call;
  252. const unsigned int nargs = sysent[scno].nargs;
  253. uint64_t buf[nargs];
  254. if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
  255. return;
  256. tcp->scno = scno;
  257. tcp->qual_flg = qual_flags(scno);
  258. tcp->s_ent = &sysent[scno];
  259. unsigned int i;
  260. for (i = 0; i < nargs; ++i)
  261. tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
  262. ? ((uint32_t *) (void *) buf)[i] : buf[i];
  263. }
  264. #endif /* SYS_socket_subcall */
  265. #ifdef SYS_ipc_subcall
  266. static void
  267. decode_ipc_subcall(struct tcb *tcp)
  268. {
  269. unsigned int call = tcp->u_arg[0];
  270. const unsigned int version = call >> 16;
  271. if (version) {
  272. # if defined S390 || defined S390X
  273. return;
  274. # else
  275. # ifdef SPARC64
  276. if (current_wordsize == 8)
  277. return;
  278. # endif
  279. set_tcb_priv_ulong(tcp, version);
  280. call &= 0xffff;
  281. # endif
  282. }
  283. switch (call) {
  284. case 1: case 2: case 3: case 4:
  285. case 11: case 12: case 13: case 14:
  286. case 21: case 22: case 23: case 24:
  287. break;
  288. default:
  289. return;
  290. }
  291. tcp->scno = SYS_ipc_subcall + call;
  292. tcp->qual_flg = qual_flags(tcp->scno);
  293. tcp->s_ent = &sysent[tcp->scno];
  294. const unsigned int n = n_args(tcp);
  295. unsigned int i;
  296. for (i = 0; i < n; i++)
  297. tcp->u_arg[i] = tcp->u_arg[i + 1];
  298. }
  299. #endif /* SYS_ipc_subcall */
  300. #ifdef SYS_syscall_subcall
  301. /* The implementation is architecture specific. */
  302. static void decode_syscall_subcall(struct tcb *);
  303. #endif /* SYS_syscall_subcall */
  304. static void
  305. dumpio(struct tcb *tcp)
  306. {
  307. int fd = tcp->u_arg[0];
  308. if (fd < 0)
  309. return;
  310. if (is_number_in_set(fd, write_set)) {
  311. switch (tcp_sysent(tcp)->sen) {
  312. case SEN_write:
  313. case SEN_pwrite:
  314. case SEN_send:
  315. case SEN_sendto:
  316. case SEN_mq_timedsend_time32:
  317. case SEN_mq_timedsend_time64:
  318. dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
  319. break;
  320. case SEN_writev:
  321. case SEN_pwritev:
  322. case SEN_pwritev2:
  323. case SEN_vmsplice:
  324. dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
  325. break;
  326. case SEN_sendmsg:
  327. dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
  328. break;
  329. case SEN_sendmmsg:
  330. dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
  331. break;
  332. }
  333. }
  334. if (syserror(tcp))
  335. return;
  336. if (is_number_in_set(fd, read_set)) {
  337. switch (tcp_sysent(tcp)->sen) {
  338. case SEN_read:
  339. case SEN_pread:
  340. case SEN_recv:
  341. case SEN_recvfrom:
  342. case SEN_mq_timedreceive_time32:
  343. case SEN_mq_timedreceive_time64:
  344. dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
  345. return;
  346. case SEN_readv:
  347. case SEN_preadv:
  348. case SEN_preadv2:
  349. dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
  350. tcp->u_rval);
  351. return;
  352. case SEN_recvmsg:
  353. dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
  354. return;
  355. case SEN_recvmmsg:
  356. case SEN_recvmmsg_time32:
  357. case SEN_recvmmsg_time64:
  358. dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
  359. return;
  360. }
  361. }
  362. }
  363. static const char *
  364. err_name(uint64_t err)
  365. {
  366. return err < nerrnos ? errnoent[err] : NULL;
  367. }
  368. void
  369. print_err(int64_t err, bool negated)
  370. {
  371. const char *str = err_name(negated ? -err : err);
  372. if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
  373. tprintf(negated ? "%" PRId64 : "%" PRIu64, err);
  374. if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
  375. return;
  376. (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
  377. ? tprintf : tprintf_comment)("%s%s",
  378. negated ? "-" : "", str);
  379. }
  380. static void
  381. print_err_ret(kernel_ulong_t ret, unsigned long u_error)
  382. {
  383. const char *u_error_str = err_name(u_error);
  384. if (u_error_str)
  385. tprintf("= %" PRI_kld " %s (%s)",
  386. ret, u_error_str, strerror(u_error));
  387. else
  388. tprintf("= %" PRI_kld " (errno %lu)", ret, u_error);
  389. }
  390. static long get_regs(struct tcb *);
  391. static int get_syscall_args(struct tcb *);
  392. static int get_syscall_result(struct tcb *);
  393. static void get_error(struct tcb *, bool);
  394. static void set_error(struct tcb *, unsigned long);
  395. static void set_success(struct tcb *, kernel_long_t);
  396. static int arch_get_scno(struct tcb *);
  397. static int arch_check_scno(struct tcb *);
  398. static int arch_set_scno(struct tcb *, kernel_ulong_t);
  399. static int arch_get_syscall_args(struct tcb *);
  400. static void arch_get_error(struct tcb *, bool);
  401. static int arch_set_error(struct tcb *);
  402. static int arch_set_success(struct tcb *);
  403. #if MAX_ARGS > 6
  404. static void arch_get_syscall_args_extra(struct tcb *, unsigned int);
  405. #endif
  406. struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
  407. static struct inject_opts *
  408. tcb_inject_opts(struct tcb *tcp)
  409. {
  410. return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
  411. ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
  412. }
  413. static long
  414. tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
  415. {
  416. if (!tcp->inject_vec[current_personality]) {
  417. tcp->inject_vec[current_personality] =
  418. xcalloc(nsyscalls, sizeof(**inject_vec));
  419. memcpy(tcp->inject_vec[current_personality],
  420. inject_vec[current_personality],
  421. nsyscalls * sizeof(**inject_vec));
  422. }
  423. struct inject_opts *opts = tcb_inject_opts(tcp);
  424. if (!opts || opts->first == 0)
  425. return 0;
  426. --opts->first;
  427. if (opts->first != 0)
  428. return 0;
  429. opts->first = opts->step;
  430. if (!recovering(tcp)) {
  431. if (opts->data.flags & INJECT_F_SIGNAL)
  432. *signo = opts->data.signo;
  433. if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL)) {
  434. kernel_long_t scno =
  435. (opts->data.flags & INJECT_F_SYSCALL)
  436. ? (kernel_long_t) shuffle_scno(opts->data.scno)
  437. : -1;
  438. if (!arch_set_scno(tcp, scno)) {
  439. tcp->flags |= TCB_TAMPERED;
  440. if (scno != -1)
  441. tcp->flags |= TCB_TAMPERED_NO_FAIL;
  442. #if ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING
  443. /*
  444. * So far it's just a workaround for hppa,
  445. * but let's pretend it could be used elsewhere.
  446. */
  447. else {
  448. kernel_long_t rval =
  449. (opts->data.flags & INJECT_F_RETVAL) ?
  450. ENOSYS : retval_get(opts->data.rval_idx);
  451. tcp->u_error = 0; /* force reset */
  452. set_error(tcp, rval);
  453. }
  454. #endif
  455. }
  456. }
  457. if (opts->data.flags & INJECT_F_DELAY_ENTER)
  458. delay_tcb(tcp, opts->data.delay_idx, true);
  459. if (opts->data.flags & INJECT_F_DELAY_EXIT)
  460. tcp->flags |= TCB_INJECT_DELAY_EXIT;
  461. }
  462. return 0;
  463. }
  464. static long
  465. tamper_with_syscall_exiting(struct tcb *tcp)
  466. {
  467. struct inject_opts *opts = tcb_inject_opts(tcp);
  468. if (!opts)
  469. return 0;
  470. if (inject_delay_exit(tcp))
  471. delay_tcb(tcp, opts->data.delay_idx, false);
  472. if (!syscall_tampered(tcp))
  473. return 0;
  474. if (!syserror(tcp) ^ !!syscall_tampered_nofail(tcp)) {
  475. error_msg("Failed to tamper with process %d: unexpectedly got"
  476. " %serror (return value %#" PRI_klx ", error %lu)",
  477. tcp->pid, syscall_tampered_nofail(tcp) ? "" : "no ",
  478. tcp->u_rval, tcp->u_error);
  479. return 1;
  480. }
  481. if (opts->data.flags & INJECT_F_RETVAL)
  482. set_success(tcp, retval_get(opts->data.rval_idx));
  483. else
  484. set_error(tcp, retval_get(opts->data.rval_idx));
  485. return 0;
  486. }
  487. /*
  488. * Returns:
  489. * 0: "ignore this ptrace stop", bail out silently.
  490. * 1: ok, decoded; call
  491. * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
  492. * other: error; call syscall_entering_finish(tcp, res), where res is the value
  493. * returned.
  494. */
  495. int
  496. syscall_entering_decode(struct tcb *tcp)
  497. {
  498. int res = get_scno(tcp);
  499. if (res == 0)
  500. return res;
  501. if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
  502. printleader(tcp);
  503. tprintf("%s(", tcp_sysent(tcp)->sys_name);
  504. /*
  505. * " <unavailable>" will be added later by the code which
  506. * detects ptrace errors.
  507. */
  508. return res;
  509. }
  510. #ifdef SYS_syscall_subcall
  511. if (tcp_sysent(tcp)->sen == SEN_syscall)
  512. decode_syscall_subcall(tcp);
  513. #endif
  514. #if defined SYS_ipc_subcall \
  515. || defined SYS_socket_subcall
  516. switch (tcp_sysent(tcp)->sen) {
  517. # ifdef SYS_ipc_subcall
  518. case SEN_ipc:
  519. decode_ipc_subcall(tcp);
  520. break;
  521. # endif
  522. # ifdef SYS_socket_subcall
  523. case SEN_socketcall:
  524. decode_socket_subcall(tcp);
  525. break;
  526. # endif
  527. }
  528. #endif
  529. return 1;
  530. }
  531. int
  532. syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
  533. {
  534. if (hide_log(tcp)) {
  535. /*
  536. * Restrain from fault injection
  537. * while the trace executes strace code.
  538. */
  539. tcp->qual_flg &= ~QUAL_INJECT;
  540. switch (tcp_sysent(tcp)->sen) {
  541. case SEN_execve:
  542. case SEN_execveat:
  543. case SEN_execv:
  544. /*
  545. * First exec* syscall makes the log visible.
  546. */
  547. tcp->flags &= ~TCB_HIDE_LOG;
  548. /*
  549. * Check whether this exec* syscall succeeds.
  550. */
  551. tcp->flags |= TCB_CHECK_EXEC_SYSCALL;
  552. break;
  553. }
  554. }
  555. if (hide_log(tcp) || !traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
  556. tcp->flags |= TCB_FILTERED;
  557. return 0;
  558. }
  559. tcp->flags &= ~TCB_FILTERED;
  560. if (inject(tcp))
  561. tamper_with_syscall_entering(tcp, sig);
  562. if (cflag == CFLAG_ONLY_STATS) {
  563. return 0;
  564. }
  565. #ifdef ENABLE_STACKTRACE
  566. if (stack_trace_enabled) {
  567. if (tcp_sysent(tcp)->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
  568. unwind_tcb_capture(tcp);
  569. }
  570. #endif
  571. if (!is_complete_set(status_set, NUMBER_OF_STATUSES))
  572. strace_open_memstream(tcp);
  573. printleader(tcp);
  574. tprintf("%s(", tcp_sysent(tcp)->sys_name);
  575. int res = raw(tcp) ? printargs(tcp) : tcp_sysent(tcp)->sys_func(tcp);
  576. fflush(tcp->outf);
  577. return res;
  578. }
  579. void
  580. syscall_entering_finish(struct tcb *tcp, int res)
  581. {
  582. tcp->flags |= TCB_INSYSCALL;
  583. tcp->sys_func_rval = res;
  584. /* Measure the entrance time as late as possible to avoid errors. */
  585. if ((Tflag || cflag) && !filtered(tcp))
  586. clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
  587. /* Start tracking system time */
  588. if (cflag) {
  589. if (debug_flag) {
  590. struct timespec dt;
  591. ts_sub(&dt, &tcp->stime, &tcp->ltime);
  592. if (ts_nz(&dt))
  593. debug_func_msg("pid %d: %.9f seconds of system "
  594. "time spent since the last "
  595. "syscall exit",
  596. tcp->pid, ts_float(&dt));
  597. }
  598. tcp->ltime = tcp->stime;
  599. }
  600. }
  601. /* Returns:
  602. * 0: "bail out".
  603. * 1: ok.
  604. * -1: error in one of ptrace ops.
  605. *
  606. * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
  607. * value. Anyway, call syscall_exiting_finish(tcp) then.
  608. */
  609. int
  610. syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
  611. {
  612. /* Measure the exit time as early as possible to avoid errors. */
  613. if ((Tflag || cflag) && !filtered(tcp))
  614. clock_gettime(CLOCK_MONOTONIC, pts);
  615. if (tcp_sysent(tcp)->sys_flags & MEMORY_MAPPING_CHANGE)
  616. mmap_notify_report(tcp);
  617. if (filtered(tcp))
  618. return 0;
  619. if (check_exec_syscall(tcp)) {
  620. /* The check failed, hide the log. */
  621. tcp->flags |= TCB_HIDE_LOG;
  622. }
  623. #if SUPPORTED_PERSONALITIES > 1
  624. update_personality(tcp, tcp->currpers);
  625. #endif
  626. return get_syscall_result(tcp);
  627. }
  628. void
  629. print_syscall_resume(struct tcb *tcp)
  630. {
  631. /* If not in -ff mode, and printing_tcp != tcp,
  632. * then the log currently does not end with output
  633. * of _our syscall entry_, but with something else.
  634. * We need to say which syscall's return is this.
  635. *
  636. * Forced reprinting via TCB_REPRINT is used only by
  637. * "strace -ff -oLOG test/threaded_execve" corner case.
  638. * It's the only case when -ff mode needs reprinting.
  639. */
  640. if ((followfork < 2 && printing_tcp != tcp && !tcp->staged_output_data)
  641. || (tcp->flags & TCB_REPRINT)) {
  642. tcp->flags &= ~TCB_REPRINT;
  643. printleader(tcp);
  644. tprintf("<... %s resumed>", tcp_sysent(tcp)->sys_name);
  645. }
  646. }
  647. int
  648. syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
  649. {
  650. if (syscall_tampered(tcp) || inject_delay_exit(tcp))
  651. tamper_with_syscall_exiting(tcp);
  652. if (cflag) {
  653. count_syscall(tcp, ts);
  654. if (cflag == CFLAG_ONLY_STATS) {
  655. return 0;
  656. }
  657. }
  658. print_syscall_resume(tcp);
  659. printing_tcp = tcp;
  660. tcp->s_prev_ent = NULL;
  661. if (res != 1) {
  662. /* There was error in one of prior ptrace ops */
  663. tprints(") ");
  664. tabto();
  665. tprints("= ? <unavailable>\n");
  666. if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
  667. bool publish = is_number_in_set(STATUS_UNAVAILABLE,
  668. status_set);
  669. strace_close_memstream(tcp, publish);
  670. }
  671. line_ended();
  672. return res;
  673. }
  674. tcp->s_prev_ent = tcp->s_ent;
  675. int sys_res = 0;
  676. if (raw(tcp)) {
  677. /* sys_res = printargs(tcp); - but it's nop on sysexit */
  678. } else {
  679. if (tcp->sys_func_rval & RVAL_DECODED)
  680. sys_res = tcp->sys_func_rval;
  681. else
  682. sys_res = tcp_sysent(tcp)->sys_func(tcp);
  683. }
  684. if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
  685. bool publish = syserror(tcp)
  686. && is_number_in_set(STATUS_FAILED, status_set);
  687. publish |= !syserror(tcp)
  688. && is_number_in_set(STATUS_SUCCESSFUL, status_set);
  689. strace_close_memstream(tcp, publish);
  690. if (!publish) {
  691. line_ended();
  692. return 0;
  693. }
  694. }
  695. tprints(") ");
  696. tabto();
  697. if (raw(tcp)) {
  698. if (tcp->u_error)
  699. print_err_ret(tcp->u_rval, tcp->u_error);
  700. else
  701. tprintf("= %#" PRI_klx, tcp->u_rval);
  702. if (syscall_tampered(tcp))
  703. tprints(" (INJECTED)");
  704. } else if (!(sys_res & RVAL_NONE) && tcp->u_error) {
  705. switch (tcp->u_error) {
  706. /* Blocked signals do not interrupt any syscalls.
  707. * In this case syscalls don't return ERESTARTfoo codes.
  708. *
  709. * Deadly signals set to SIG_DFL interrupt syscalls
  710. * and kill the process regardless of which of the codes below
  711. * is returned by the interrupted syscall.
  712. * In some cases, kernel forces a kernel-generated deadly
  713. * signal to be unblocked and set to SIG_DFL (and thus cause
  714. * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
  715. * or SIGILL. (The alternative is to leave process spinning
  716. * forever on the faulty instruction - not useful).
  717. *
  718. * SIG_IGNed signals and non-deadly signals set to SIG_DFL
  719. * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
  720. * but kernel will always restart them.
  721. */
  722. case ERESTARTSYS:
  723. /* Most common type of signal-interrupted syscall exit code.
  724. * The system call will be restarted with the same arguments
  725. * if SA_RESTART is set; otherwise, it will fail with EINTR.
  726. */
  727. tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
  728. break;
  729. case ERESTARTNOINTR:
  730. /* Rare. For example, fork() returns this if interrupted.
  731. * SA_RESTART is ignored (assumed set): the restart is unconditional.
  732. */
  733. tprints("= ? ERESTARTNOINTR (To be restarted)");
  734. break;
  735. case ERESTARTNOHAND:
  736. /* pause(), rt_sigsuspend() etc use this code.
  737. * SA_RESTART is ignored (assumed not set):
  738. * syscall won't restart (will return EINTR instead)
  739. * even after signal with SA_RESTART set. However,
  740. * after SIG_IGN or SIG_DFL signal it will restart
  741. * (thus the name "restart only if has no handler").
  742. */
  743. tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
  744. break;
  745. case ERESTART_RESTARTBLOCK:
  746. /* Syscalls like nanosleep(), poll() which can't be
  747. * restarted with their original arguments use this
  748. * code. Kernel will execute restart_syscall() instead,
  749. * which changes arguments before restarting syscall.
  750. * SA_RESTART is ignored (assumed not set) similarly
  751. * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
  752. * since restart data is saved in "restart block"
  753. * in task struct, and if signal handler uses a syscall
  754. * which in turn saves another such restart block,
  755. * old data is lost and restart becomes impossible)
  756. */
  757. tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
  758. break;
  759. default:
  760. print_err_ret(tcp->u_rval, tcp->u_error);
  761. break;
  762. }
  763. if (syscall_tampered(tcp))
  764. tprints(" (INJECTED)");
  765. if ((sys_res & RVAL_STR) && tcp->auxstr)
  766. tprintf(" (%s)", tcp->auxstr);
  767. } else {
  768. if (sys_res & RVAL_NONE)
  769. tprints("= ?");
  770. else {
  771. switch (sys_res & RVAL_MASK) {
  772. case RVAL_HEX:
  773. #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
  774. if (current_klongsize < sizeof(tcp->u_rval)) {
  775. tprintf("= %#x",
  776. (unsigned int) tcp->u_rval);
  777. } else
  778. #endif
  779. {
  780. tprintf("= %#" PRI_klx, tcp->u_rval);
  781. }
  782. break;
  783. case RVAL_OCTAL:
  784. tprints("= ");
  785. print_numeric_long_umask(tcp->u_rval);
  786. break;
  787. case RVAL_UDECIMAL:
  788. #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
  789. if (current_klongsize < sizeof(tcp->u_rval)) {
  790. tprintf("= %u",
  791. (unsigned int) tcp->u_rval);
  792. } else
  793. #endif
  794. {
  795. tprintf("= %" PRI_klu, tcp->u_rval);
  796. }
  797. break;
  798. case RVAL_FD:
  799. if (show_fd_path) {
  800. tprints("= ");
  801. printfd(tcp, tcp->u_rval);
  802. } else
  803. tprintf("= %" PRI_kld, tcp->u_rval);
  804. break;
  805. default:
  806. error_msg("invalid rval format");
  807. break;
  808. }
  809. }
  810. if ((sys_res & RVAL_STR) && tcp->auxstr)
  811. tprintf(" (%s)", tcp->auxstr);
  812. if (syscall_tampered(tcp))
  813. tprints(" (INJECTED)");
  814. }
  815. if (Tflag) {
  816. ts_sub(ts, ts, &tcp->etime);
  817. tprintf(" <%ld.%06ld>",
  818. (long) ts->tv_sec, (long) ts->tv_nsec / 1000);
  819. }
  820. tprints("\n");
  821. dumpio(tcp);
  822. line_ended();
  823. #ifdef ENABLE_STACKTRACE
  824. if (stack_trace_enabled)
  825. unwind_tcb_print(tcp);
  826. #endif
  827. return 0;
  828. }
  829. void
  830. syscall_exiting_finish(struct tcb *tcp)
  831. {
  832. tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
  833. tcp->sys_func_rval = 0;
  834. free_tcb_priv_data(tcp);
  835. if (cflag)
  836. tcp->ltime = tcp->stime;
  837. }
  838. bool
  839. is_erestart(struct tcb *tcp)
  840. {
  841. switch (tcp->u_error) {
  842. case ERESTARTSYS:
  843. case ERESTARTNOINTR:
  844. case ERESTARTNOHAND:
  845. case ERESTART_RESTARTBLOCK:
  846. return true;
  847. default:
  848. return false;
  849. }
  850. }
  851. static unsigned long saved_u_error;
  852. void
  853. temporarily_clear_syserror(struct tcb *tcp)
  854. {
  855. saved_u_error = tcp->u_error;
  856. tcp->u_error = 0;
  857. }
  858. void
  859. restore_cleared_syserror(struct tcb *tcp)
  860. {
  861. tcp->u_error = saved_u_error;
  862. }
  863. static struct ptrace_syscall_info ptrace_sci;
  864. static bool
  865. ptrace_syscall_info_is_valid(void)
  866. {
  867. return ptrace_get_syscall_info_supported &&
  868. ptrace_sci.op <= PTRACE_SYSCALL_INFO_SECCOMP;
  869. }
  870. #define XLAT_MACROS_ONLY
  871. #include "xlat/nt_descriptor_types.h"
  872. #undef XLAT_MACROS_ONLY
  873. #define ARCH_MIGHT_USE_SET_REGS 1
  874. #include "arch_regs.c"
  875. #if HAVE_ARCH_GETRVAL2
  876. # include "arch_getrval2.c"
  877. #endif
  878. #include "getregs_old.h"
  879. #ifdef HAVE_GETREGS_OLD
  880. /* Either getregs_old() or set_regs() */
  881. # undef ARCH_MIGHT_USE_SET_REGS
  882. # define ARCH_MIGHT_USE_SET_REGS 0
  883. #endif
  884. #undef ptrace_getregset_or_getregs
  885. #undef ptrace_setregset_or_setregs
  886. #ifdef ARCH_REGS_FOR_GETREGSET
  887. # define ptrace_getregset_or_getregs ptrace_getregset
  888. static long
  889. ptrace_getregset(pid_t pid)
  890. {
  891. # ifdef ARCH_IOVEC_FOR_GETREGSET
  892. /* variable iovec */
  893. ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
  894. return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
  895. &ARCH_IOVEC_FOR_GETREGSET);
  896. # else
  897. /* constant iovec */
  898. static struct iovec io = {
  899. .iov_base = &ARCH_REGS_FOR_GETREGSET,
  900. .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
  901. };
  902. return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
  903. # endif
  904. }
  905. # if ARCH_MIGHT_USE_SET_REGS
  906. # define ptrace_setregset_or_setregs ptrace_setregset
  907. static int
  908. ptrace_setregset(pid_t pid)
  909. {
  910. # ifdef ARCH_IOVEC_FOR_GETREGSET
  911. /* variable iovec */
  912. return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
  913. &ARCH_IOVEC_FOR_GETREGSET);
  914. # else
  915. /* constant iovec */
  916. static struct iovec io = {
  917. .iov_base = &ARCH_REGS_FOR_GETREGSET,
  918. .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
  919. };
  920. return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
  921. # endif
  922. }
  923. # endif /* ARCH_MIGHT_USE_SET_REGS */
  924. #elif defined ARCH_REGS_FOR_GETREGS
  925. # define ptrace_getregset_or_getregs ptrace_getregs
  926. static long
  927. ptrace_getregs(pid_t pid)
  928. {
  929. # if defined SPARC || defined SPARC64
  930. /* SPARC systems have the meaning of data and addr reversed */
  931. return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
  932. # else
  933. return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
  934. # endif
  935. }
  936. # if ARCH_MIGHT_USE_SET_REGS
  937. # define ptrace_setregset_or_setregs ptrace_setregs
  938. static int
  939. ptrace_setregs(pid_t pid)
  940. {
  941. # if defined SPARC || defined SPARC64
  942. /* SPARC systems have the meaning of data and addr reversed */
  943. return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
  944. # else
  945. return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
  946. # endif
  947. }
  948. # endif /* ARCH_MIGHT_USE_SET_REGS */
  949. #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
  950. static long get_regs_error = -1;
  951. void
  952. clear_regs(struct tcb *tcp)
  953. {
  954. ptrace_sci.op = 0xff;
  955. get_regs_error = -1;
  956. }
  957. static long
  958. get_regs(struct tcb *const tcp)
  959. {
  960. #ifdef ptrace_getregset_or_getregs
  961. if (get_regs_error != -1)
  962. return get_regs_error;
  963. # ifdef HAVE_GETREGS_OLD
  964. /*
  965. * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
  966. * fallback to getregs_old.
  967. */
  968. static int use_getregs_old;
  969. if (use_getregs_old < 0) {
  970. return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
  971. } else if (use_getregs_old == 0) {
  972. get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
  973. if (get_regs_error >= 0) {
  974. use_getregs_old = -1;
  975. return get_regs_error;
  976. }
  977. if (errno == EPERM || errno == ESRCH)
  978. return get_regs_error;
  979. use_getregs_old = 1;
  980. }
  981. return get_regs_error = getregs_old(tcp);
  982. # else /* !HAVE_GETREGS_OLD */
  983. /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
  984. get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
  985. # if defined ARCH_PERSONALITY_0_IOV_SIZE
  986. if (get_regs_error)
  987. return get_regs_error;
  988. switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
  989. case ARCH_PERSONALITY_0_IOV_SIZE:
  990. update_personality(tcp, 0);
  991. break;
  992. case ARCH_PERSONALITY_1_IOV_SIZE:
  993. update_personality(tcp, 1);
  994. break;
  995. default: {
  996. static bool printed = false;
  997. if (!printed) {
  998. error_msg("Unsupported regset size returned by "
  999. "PTRACE_GETREGSET: %zu",
  1000. ARCH_IOVEC_FOR_GETREGSET.iov_len);
  1001. printed = true;
  1002. }
  1003. update_personality(tcp, 0);
  1004. }
  1005. }
  1006. # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
  1007. return get_regs_error;
  1008. # endif /* !HAVE_GETREGS_OLD */
  1009. #else /* !ptrace_getregset_or_getregs */
  1010. # warning get_regs is not implemented for this architecture yet
  1011. return 0;
  1012. #endif /* !ptrace_getregset_or_getregs */
  1013. }
  1014. #ifdef ptrace_setregset_or_setregs
  1015. static int
  1016. set_regs(pid_t pid)
  1017. {
  1018. return ptrace_setregset_or_setregs(pid);
  1019. }
  1020. #endif /* ptrace_setregset_or_setregs */
  1021. struct sysent_buf {
  1022. struct tcb *tcp;
  1023. struct_sysent ent;
  1024. char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
  1025. };
  1026. static void
  1027. free_sysent_buf(void *ptr)
  1028. {
  1029. struct sysent_buf *s = ptr;
  1030. s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
  1031. free(ptr);
  1032. }
  1033. static bool
  1034. strace_get_syscall_info(struct tcb *tcp)
  1035. {
  1036. /*
  1037. * ptrace_get_syscall_info_supported should have been checked
  1038. * by the caller.
  1039. */
  1040. if (ptrace_sci.op == 0xff) {
  1041. const size_t size = sizeof(ptrace_sci);
  1042. if (ptrace(PTRACE_GET_SYSCALL_INFO, tcp->pid,
  1043. (void *) size, &ptrace_sci) < 0) {
  1044. get_regs_error = -2;
  1045. return false;
  1046. }
  1047. #if SUPPORTED_PERSONALITIES > 1
  1048. int newpers = get_personality_from_syscall_info(&ptrace_sci);
  1049. if (newpers >= 0)
  1050. update_personality(tcp, newpers);
  1051. #endif
  1052. }
  1053. if (entering(tcp)) {
  1054. if (ptrace_sci.op == PTRACE_SYSCALL_INFO_EXIT) {
  1055. error_msg("pid %d: entering"
  1056. ", ptrace_syscall_info.op == %u",
  1057. tcp->pid, ptrace_sci.op);
  1058. /* TODO: handle this. */
  1059. }
  1060. } else {
  1061. if (ptrace_sci.op == PTRACE_SYSCALL_INFO_ENTRY) {
  1062. error_msg("pid %d: exiting"
  1063. ", ptrace_syscall_info.op == %u",
  1064. tcp->pid, ptrace_sci.op);
  1065. /* TODO: handle this. */
  1066. }
  1067. }
  1068. return true;
  1069. }
  1070. bool
  1071. get_instruction_pointer(struct tcb *tcp, kernel_ulong_t *ip)
  1072. {
  1073. if (get_regs_error < -1)
  1074. return false;
  1075. if (ptrace_get_syscall_info_supported) {
  1076. if (!strace_get_syscall_info(tcp))
  1077. return false;
  1078. *ip = (kernel_ulong_t) ptrace_sci.instruction_pointer;
  1079. return true;
  1080. }
  1081. #if defined ARCH_PC_REG
  1082. if (get_regs(tcp) < 0)
  1083. return false;
  1084. *ip = (kernel_ulong_t) ARCH_PC_REG;
  1085. return true;
  1086. #elif defined ARCH_PC_PEEK_ADDR
  1087. if (upeek(tcp, ARCH_PC_PEEK_ADDR, ip) < 0)
  1088. return false;
  1089. return true;
  1090. #else
  1091. # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
  1092. #endif
  1093. }
  1094. bool
  1095. get_stack_pointer(struct tcb *tcp, kernel_ulong_t *sp)
  1096. {
  1097. if (get_regs_error < -1)
  1098. return false;
  1099. if (ptrace_get_syscall_info_supported) {
  1100. if (!strace_get_syscall_info(tcp))
  1101. return false;
  1102. *sp = (kernel_ulong_t) ptrace_sci.stack_pointer;
  1103. return true;
  1104. }
  1105. #if defined ARCH_SP_REG
  1106. if (get_regs(tcp) < 0)
  1107. return false;
  1108. *sp = (kernel_ulong_t) ARCH_SP_REG;
  1109. return true;
  1110. #elif defined ARCH_SP_PEEK_ADDR
  1111. if (upeek(tcp, ARCH_SP_PEEK_ADDR, sp) < 0)
  1112. return false;
  1113. return true;
  1114. #else
  1115. return false;
  1116. #endif
  1117. }
  1118. static int
  1119. get_syscall_regs(struct tcb *tcp)
  1120. {
  1121. if (get_regs_error != -1)
  1122. return get_regs_error;
  1123. if (ptrace_get_syscall_info_supported)
  1124. return strace_get_syscall_info(tcp) ? 0 : get_regs_error;
  1125. return get_regs(tcp);
  1126. }
  1127. const struct_sysent stub_sysent = {
  1128. .nargs = MAX_ARGS,
  1129. .sys_flags = MEMORY_MAPPING_CHANGE,
  1130. .sen = SEN_printargs,
  1131. .sys_func = printargs,
  1132. .sys_name = "????",
  1133. };
  1134. /*
  1135. * Returns:
  1136. * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
  1137. * out silently" code.
  1138. * 1: ok, continue in syscall_entering_decode().
  1139. * other: error, syscall_entering_decode() should print error indicator
  1140. * ("????" etc) and return an appropriate code.
  1141. */
  1142. int
  1143. get_scno(struct tcb *tcp)
  1144. {
  1145. tcp->scno = -1;
  1146. tcp->s_ent = NULL;
  1147. tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
  1148. if (get_syscall_regs(tcp) < 0)
  1149. return -1;
  1150. if (ptrace_syscall_info_is_valid()) {
  1151. /*
  1152. * So far it's just a workaround for x32,
  1153. * but let's pretend it could be used elsewhere.
  1154. */
  1155. int rc = arch_check_scno(tcp);
  1156. if (rc != 1)
  1157. return rc;
  1158. tcp->scno = ptrace_sci.entry.nr;
  1159. } else {
  1160. int rc = arch_get_scno(tcp);
  1161. if (rc != 1)
  1162. return rc;
  1163. }
  1164. tcp->scno = shuffle_scno(tcp->scno);
  1165. if (scno_is_valid(tcp->scno)) {
  1166. tcp->s_ent = &sysent[tcp->scno];
  1167. tcp->qual_flg = qual_flags(tcp->scno);
  1168. } else {
  1169. struct sysent_buf *s = xzalloc(sizeof(*s));
  1170. s->tcp = tcp;
  1171. s->ent = stub_sysent;
  1172. s->ent.sys_name = s->buf;
  1173. xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
  1174. tcp->s_ent = &s->ent;
  1175. set_tcb_priv_data(tcp, s, free_sysent_buf);
  1176. debug_msg("pid %d invalid syscall %#" PRI_klx,
  1177. tcp->pid, shuffle_scno(tcp->scno));
  1178. }
  1179. /*
  1180. * We refrain from argument decoding during recovering
  1181. * as tracee memory mappings has changed and the registers
  1182. * are very likely pointing to garbage already.
  1183. */
  1184. if (recovering(tcp))
  1185. tcp->qual_flg |= QUAL_RAW;
  1186. return 1;
  1187. }
  1188. static int
  1189. get_syscall_args(struct tcb *tcp)
  1190. {
  1191. if (ptrace_syscall_info_is_valid()) {
  1192. const unsigned int n =
  1193. MIN(ARRAY_SIZE(tcp->u_arg),
  1194. ARRAY_SIZE(ptrace_sci.entry.args));
  1195. for (unsigned int i = 0; i < n; ++i)
  1196. tcp->u_arg[i] = ptrace_sci.entry.args[i];
  1197. #if SUPPORTED_PERSONALITIES > 1
  1198. if (tcp_sysent(tcp)->sys_flags & COMPAT_SYSCALL_TYPES) {
  1199. for (unsigned int i = 0; i < n; ++i)
  1200. tcp->u_arg[i] = (uint32_t) tcp->u_arg[i];
  1201. }
  1202. #endif
  1203. /*
  1204. * So far it's just a workaround for mips o32,
  1205. * but let's pretend it could be used elsewhere.
  1206. */
  1207. #if MAX_ARGS > 6
  1208. arch_get_syscall_args_extra(tcp, n);
  1209. #endif
  1210. return 1;
  1211. }
  1212. return arch_get_syscall_args(tcp);
  1213. }
  1214. #ifdef ptrace_getregset_or_getregs
  1215. # define get_syscall_result_regs get_syscall_regs
  1216. #else
  1217. static int get_syscall_result_regs(struct tcb *);
  1218. #endif
  1219. /* Returns:
  1220. * 1: ok, continue in syscall_exiting_trace().
  1221. * -1: error, syscall_exiting_trace() should print error indicator
  1222. * ("????" etc) and bail out.
  1223. */
  1224. static int
  1225. get_syscall_result(struct tcb *tcp)
  1226. {
  1227. if (get_syscall_result_regs(tcp) < 0)
  1228. return -1;
  1229. get_error(tcp,
  1230. (!(tcp_sysent(tcp)->sys_flags & SYSCALL_NEVER_FAILS)
  1231. || syscall_tampered(tcp))
  1232. && !syscall_tampered_nofail(tcp));
  1233. return 1;
  1234. }
  1235. static void
  1236. get_error(struct tcb *tcp, const bool check_errno)
  1237. {
  1238. if (ptrace_syscall_info_is_valid()) {
  1239. if (ptrace_sci.exit.is_error) {
  1240. tcp->u_rval = -1;
  1241. tcp->u_error = -ptrace_sci.exit.rval;
  1242. } else {
  1243. tcp->u_error = 0;
  1244. tcp->u_rval = ptrace_sci.exit.rval;
  1245. }
  1246. } else {
  1247. tcp->u_error = 0;
  1248. arch_get_error(tcp, check_errno);
  1249. }
  1250. }
  1251. static void
  1252. set_error(struct tcb *tcp, unsigned long new_error)
  1253. {
  1254. const unsigned long old_error = tcp->u_error;
  1255. if (new_error == old_error || new_error > MAX_ERRNO_VALUE)
  1256. return;
  1257. #ifdef ptrace_setregset_or_setregs
  1258. /* if we are going to invoke set_regs, call get_regs first */
  1259. if (get_regs(tcp) < 0)
  1260. return;
  1261. #endif
  1262. tcp->u_error = new_error;
  1263. if (arch_set_error(tcp)) {
  1264. tcp->u_error = old_error;
  1265. /* arch_set_error does not update u_rval */
  1266. } else {
  1267. if (ptrace_syscall_info_is_valid())
  1268. tcp->u_rval = -1;
  1269. else
  1270. get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
  1271. SYSCALL_NEVER_FAILS));
  1272. }
  1273. }
  1274. static void
  1275. set_success(struct tcb *tcp, kernel_long_t new_rval)
  1276. {
  1277. const kernel_long_t old_rval = tcp->u_rval;
  1278. #ifdef ptrace_setregset_or_setregs
  1279. /* if we are going to invoke set_regs, call get_regs first */
  1280. if (get_regs(tcp) < 0)
  1281. return;
  1282. #endif
  1283. tcp->u_rval = new_rval;
  1284. if (arch_set_success(tcp)) {
  1285. tcp->u_rval = old_rval;
  1286. /* arch_set_success does not update u_error */
  1287. } else {
  1288. if (ptrace_syscall_info_is_valid())
  1289. tcp->u_error = 0;
  1290. else
  1291. get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
  1292. SYSCALL_NEVER_FAILS));
  1293. }
  1294. }
  1295. #include "get_scno.c"
  1296. #include "check_scno.c"
  1297. #include "set_scno.c"
  1298. #include "get_syscall_args.c"
  1299. #ifndef ptrace_getregset_or_getregs
  1300. # include "get_syscall_result.c"
  1301. #endif
  1302. #include "get_error.c"
  1303. #include "set_error.c"
  1304. #ifdef HAVE_GETREGS_OLD
  1305. # include "getregs_old.c"
  1306. #endif
  1307. #include "shuffle_scno.c"
  1308. const char *
  1309. syscall_name(kernel_ulong_t scno)
  1310. {
  1311. return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
  1312. }