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 34KB

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