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.

clone.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 1999-2000 Wichert Akkerman <wichert@cistron.nl>
  3. * Copyright (c) 2002-2005 Roland McGrath <roland@redhat.com>
  4. * Copyright (c) 2008 Jan Kratochvil <jan.kratochvil@redhat.com>
  5. * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk@redhat.com>
  6. * Copyright (c) 2006-2015 Dmitry V. Levin <ldv@altlinux.org>
  7. * Copyright (c) 2014-2018 The strace developers.
  8. * All rights reserved.
  9. *
  10. * SPDX-License-Identifier: LGPL-2.1-or-later
  11. */
  12. #include "defs.h"
  13. #include <sched.h>
  14. #include <asm/unistd.h>
  15. #ifndef CSIGNAL
  16. # define CSIGNAL 0x000000ff
  17. #endif
  18. #include "xlat/clone_flags.h"
  19. #include "xlat/setns_types.h"
  20. #include "xlat/unshare_flags.h"
  21. #if defined IA64
  22. # define ARG_FLAGS 0
  23. # define ARG_STACK 1
  24. # define ARG_STACKSIZE (tcp->scno == __NR_clone2 ? 2 : -1)
  25. # define ARG_PTID (tcp->scno == __NR_clone2 ? 3 : 2)
  26. # define ARG_CTID (tcp->scno == __NR_clone2 ? 4 : 3)
  27. # define ARG_TLS (tcp->scno == __NR_clone2 ? 5 : 4)
  28. #elif defined S390 || defined S390X
  29. # define ARG_STACK 0
  30. # define ARG_FLAGS 1
  31. # define ARG_PTID 2
  32. # define ARG_CTID 3
  33. # define ARG_TLS 4
  34. #elif defined X86_64 || defined X32
  35. /* x86 personality processes have the last two arguments flipped. */
  36. # define ARG_FLAGS 0
  37. # define ARG_STACK 1
  38. # define ARG_PTID 2
  39. # define ARG_CTID ((current_personality != 1) ? 3 : 4)
  40. # define ARG_TLS ((current_personality != 1) ? 4 : 3)
  41. #elif defined ALPHA || defined TILE || defined OR1K || defined RISCV
  42. # define ARG_FLAGS 0
  43. # define ARG_STACK 1
  44. # define ARG_PTID 2
  45. # define ARG_CTID 3
  46. # define ARG_TLS 4
  47. #else
  48. # define ARG_FLAGS 0
  49. # define ARG_STACK 1
  50. # define ARG_PTID 2
  51. # define ARG_TLS 3
  52. # define ARG_CTID 4
  53. #endif
  54. static void
  55. print_tls_arg(struct tcb *const tcp, const kernel_ulong_t addr)
  56. {
  57. #ifdef HAVE_STRUCT_USER_DESC
  58. # if SUPPORTED_PERSONALITIES > 1
  59. if (current_personality == 1)
  60. # endif
  61. {
  62. print_user_desc(tcp, tcp->u_arg[ARG_TLS], USER_DESC_BOTH);
  63. }
  64. # if SUPPORTED_PERSONALITIES > 1
  65. else
  66. # endif
  67. #endif /* HAVE_STRUCT_USER_DESC */
  68. {
  69. printaddr(tcp->u_arg[ARG_TLS]);
  70. }
  71. }
  72. SYS_FUNC(clone)
  73. {
  74. if (exiting(tcp)) {
  75. const char *sep = "|";
  76. kernel_ulong_t flags = tcp->u_arg[ARG_FLAGS];
  77. tprints("child_stack=");
  78. printaddr(tcp->u_arg[ARG_STACK]);
  79. tprints(", ");
  80. #ifdef ARG_STACKSIZE
  81. if (ARG_STACKSIZE != -1)
  82. tprintf("stack_size=%#" PRI_klx ", ",
  83. tcp->u_arg[ARG_STACKSIZE]);
  84. #endif
  85. tprints("flags=");
  86. if (!printflags64(clone_flags, flags & ~CSIGNAL, NULL))
  87. sep = "";
  88. if ((flags & CSIGNAL) != 0) {
  89. tprints(sep);
  90. printsignal(flags & CSIGNAL);
  91. }
  92. if ((flags & (CLONE_PARENT_SETTID|CLONE_CHILD_SETTID
  93. |CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0)
  94. return 0;
  95. if (flags & CLONE_PARENT_SETTID) {
  96. tprints(", parent_tidptr=");
  97. printaddr(tcp->u_arg[ARG_PTID]);
  98. }
  99. if (flags & CLONE_SETTLS) {
  100. tprints(", tls=");
  101. print_tls_arg(tcp, tcp->u_arg[ARG_TLS]);
  102. }
  103. if (flags & (CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) {
  104. tprints(", child_tidptr=");
  105. printaddr(tcp->u_arg[ARG_CTID]);
  106. }
  107. }
  108. /* TODO on syscall entry:
  109. * We can clear CLONE_PTRACE here since it is an ancient hack
  110. * to allow us to catch children, and we use another hack for that.
  111. * But CLONE_PTRACE can conceivably be used by malicious programs
  112. * to subvert us. By clearing this bit, we can defend against it:
  113. * in untraced execution, CLONE_PTRACE should have no effect.
  114. *
  115. * We can also clear CLONE_UNTRACED, since it allows to start
  116. * children outside of our control. At the moment
  117. * I'm trying to figure out whether there is a *legitimate*
  118. * use of this flag which we should respect.
  119. */
  120. return 0;
  121. }
  122. SYS_FUNC(setns)
  123. {
  124. printfd(tcp, tcp->u_arg[0]);
  125. tprints(", ");
  126. printxval(setns_types, tcp->u_arg[1], "CLONE_NEW???");
  127. return RVAL_DECODED;
  128. }
  129. SYS_FUNC(unshare)
  130. {
  131. printflags64(unshare_flags, tcp->u_arg[0], "CLONE_???");
  132. return RVAL_DECODED;
  133. }
  134. SYS_FUNC(fork)
  135. {
  136. return RVAL_DECODED;
  137. }