rseq.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * rseq.c
  4. *
  5. * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; only
  10. * version 2.1 of the License.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #define _GNU_SOURCE
  18. #include <errno.h>
  19. #include <sched.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <syscall.h>
  25. #include <assert.h>
  26. #include <signal.h>
  27. #include <limits.h>
  28. #include <dlfcn.h>
  29. #include <stddef.h>
  30. #include <sys/auxv.h>
  31. #include <linux/auxvec.h>
  32. #include <linux/compiler.h>
  33. #include "kselftest.h"
  34. #include "rseq.h"
  35. /*
  36. * Define weak versions to play nice with binaries that are statically linked
  37. * against a libc that doesn't support registering its own rseq.
  38. */
  39. extern __weak ptrdiff_t __rseq_offset;
  40. extern __weak unsigned int __rseq_size;
  41. extern __weak unsigned int __rseq_flags;
  42. static const ptrdiff_t *libc_rseq_offset_p = &__rseq_offset;
  43. static const unsigned int *libc_rseq_size_p = &__rseq_size;
  44. static const unsigned int *libc_rseq_flags_p = &__rseq_flags;
  45. /* Offset from the thread pointer to the rseq area. */
  46. ptrdiff_t rseq_offset;
  47. /*
  48. * Size of the registered rseq area. 0 if the registration was
  49. * unsuccessful.
  50. */
  51. unsigned int rseq_size = -1U;
  52. /* Flags used during rseq registration. */
  53. unsigned int rseq_flags;
  54. static int rseq_ownership;
  55. /* Allocate a large area for the TLS. */
  56. #define RSEQ_THREAD_AREA_ALLOC_SIZE 1024
  57. /* Original struct rseq feature size is 20 bytes. */
  58. #define ORIG_RSEQ_FEATURE_SIZE 20
  59. /* Original struct rseq allocation size is 32 bytes. */
  60. #define ORIG_RSEQ_ALLOC_SIZE 32
  61. /*
  62. * Use a union to ensure we allocate a TLS area of 1024 bytes to accomodate an
  63. * rseq registration that is larger than the current rseq ABI.
  64. */
  65. union rseq_tls {
  66. struct rseq_abi abi;
  67. char dummy[RSEQ_THREAD_AREA_ALLOC_SIZE];
  68. };
  69. static
  70. __thread union rseq_tls __rseq __attribute__((tls_model("initial-exec"))) = {
  71. .abi = {
  72. .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED,
  73. },
  74. };
  75. static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len,
  76. int flags, uint32_t sig)
  77. {
  78. return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
  79. }
  80. static int sys_getcpu(unsigned *cpu, unsigned *node)
  81. {
  82. return syscall(__NR_getcpu, cpu, node, NULL);
  83. }
  84. bool rseq_available(void)
  85. {
  86. int rc;
  87. rc = sys_rseq(NULL, 0, 0, 0);
  88. if (rc != -1)
  89. abort();
  90. switch (errno) {
  91. case ENOSYS:
  92. return false;
  93. case EINVAL:
  94. return true;
  95. default:
  96. abort();
  97. }
  98. }
  99. /* The rseq areas need to be at least 32 bytes. */
  100. static
  101. unsigned int get_rseq_min_alloc_size(void)
  102. {
  103. unsigned int alloc_size = rseq_size;
  104. if (alloc_size < ORIG_RSEQ_ALLOC_SIZE)
  105. alloc_size = ORIG_RSEQ_ALLOC_SIZE;
  106. return alloc_size;
  107. }
  108. /*
  109. * Return the feature size supported by the kernel.
  110. *
  111. * Depending on the value returned by getauxval(AT_RSEQ_FEATURE_SIZE):
  112. *
  113. * 0: Return ORIG_RSEQ_FEATURE_SIZE (20)
  114. * > 0: Return the value from getauxval(AT_RSEQ_FEATURE_SIZE).
  115. *
  116. * It should never return a value below ORIG_RSEQ_FEATURE_SIZE.
  117. */
  118. static
  119. unsigned int get_rseq_kernel_feature_size(void)
  120. {
  121. unsigned long auxv_rseq_feature_size, auxv_rseq_align;
  122. auxv_rseq_align = getauxval(AT_RSEQ_ALIGN);
  123. assert(!auxv_rseq_align || auxv_rseq_align <= RSEQ_THREAD_AREA_ALLOC_SIZE);
  124. auxv_rseq_feature_size = getauxval(AT_RSEQ_FEATURE_SIZE);
  125. assert(!auxv_rseq_feature_size || auxv_rseq_feature_size <= RSEQ_THREAD_AREA_ALLOC_SIZE);
  126. if (auxv_rseq_feature_size)
  127. return auxv_rseq_feature_size;
  128. else
  129. return ORIG_RSEQ_FEATURE_SIZE;
  130. }
  131. int rseq_register_current_thread(void)
  132. {
  133. int rc;
  134. if (!rseq_ownership) {
  135. /* Treat libc's ownership as a successful registration. */
  136. return 0;
  137. }
  138. rc = sys_rseq(&__rseq.abi, get_rseq_min_alloc_size(), 0, RSEQ_SIG);
  139. if (rc) {
  140. /*
  141. * After at least one thread has registered successfully
  142. * (rseq_size > 0), the registration of other threads should
  143. * never fail.
  144. */
  145. if (RSEQ_READ_ONCE(rseq_size) > 0) {
  146. /* Incoherent success/failure within process. */
  147. abort();
  148. }
  149. return -1;
  150. }
  151. assert(rseq_current_cpu_raw() >= 0);
  152. /*
  153. * The first thread to register sets the rseq_size to mimic the libc
  154. * behavior.
  155. */
  156. if (RSEQ_READ_ONCE(rseq_size) == 0) {
  157. RSEQ_WRITE_ONCE(rseq_size, get_rseq_kernel_feature_size());
  158. }
  159. return 0;
  160. }
  161. int rseq_unregister_current_thread(void)
  162. {
  163. int rc;
  164. if (!rseq_ownership) {
  165. /* Treat libc's ownership as a successful unregistration. */
  166. return 0;
  167. }
  168. rc = sys_rseq(&__rseq.abi, get_rseq_min_alloc_size(), RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG);
  169. if (rc)
  170. return -1;
  171. return 0;
  172. }
  173. static __attribute__((constructor))
  174. void rseq_init(void)
  175. {
  176. /*
  177. * If the libc's registered rseq size isn't already valid, it may be
  178. * because the binary is dynamically linked and not necessarily due to
  179. * libc not having registered a restartable sequence. Try to find the
  180. * symbols if that's the case.
  181. */
  182. if (!libc_rseq_size_p || !*libc_rseq_size_p) {
  183. libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
  184. libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
  185. libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");
  186. }
  187. if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p &&
  188. *libc_rseq_size_p != 0) {
  189. unsigned int libc_rseq_size;
  190. /* rseq registration owned by glibc */
  191. rseq_offset = *libc_rseq_offset_p;
  192. libc_rseq_size = *libc_rseq_size_p;
  193. rseq_flags = *libc_rseq_flags_p;
  194. /*
  195. * Previous versions of glibc expose the value
  196. * 32 even though the kernel only supported 20
  197. * bytes initially. Therefore treat 32 as a
  198. * special-case. glibc 2.40 exposes a 20 bytes
  199. * __rseq_size without using getauxval(3) to
  200. * query the supported size, while still allocating a 32
  201. * bytes area. Also treat 20 as a special-case.
  202. *
  203. * Special-cases are handled by using the following
  204. * value as active feature set size:
  205. *
  206. * rseq_size = min(32, get_rseq_kernel_feature_size())
  207. */
  208. switch (libc_rseq_size) {
  209. case ORIG_RSEQ_FEATURE_SIZE:
  210. fallthrough;
  211. case ORIG_RSEQ_ALLOC_SIZE:
  212. {
  213. unsigned int rseq_kernel_feature_size = get_rseq_kernel_feature_size();
  214. if (rseq_kernel_feature_size < ORIG_RSEQ_ALLOC_SIZE)
  215. rseq_size = rseq_kernel_feature_size;
  216. else
  217. rseq_size = ORIG_RSEQ_ALLOC_SIZE;
  218. break;
  219. }
  220. default:
  221. /* Otherwise just use the __rseq_size from libc as rseq_size. */
  222. rseq_size = libc_rseq_size;
  223. break;
  224. }
  225. return;
  226. }
  227. rseq_ownership = 1;
  228. /* Calculate the offset of the rseq area from the thread pointer. */
  229. rseq_offset = (void *)&__rseq.abi - rseq_thread_pointer();
  230. /* rseq flags are deprecated, always set to 0. */
  231. rseq_flags = 0;
  232. /*
  233. * Set the size to 0 until at least one thread registers to mimic the
  234. * libc behavior.
  235. */
  236. rseq_size = 0;
  237. }
  238. static __attribute__((destructor))
  239. void rseq_exit(void)
  240. {
  241. if (!rseq_ownership)
  242. return;
  243. rseq_offset = 0;
  244. rseq_size = -1U;
  245. rseq_ownership = 0;
  246. }
  247. int32_t rseq_fallback_current_cpu(void)
  248. {
  249. int32_t cpu;
  250. cpu = sched_getcpu();
  251. if (cpu < 0) {
  252. perror("sched_getcpu()");
  253. abort();
  254. }
  255. return cpu;
  256. }
  257. int32_t rseq_fallback_current_node(void)
  258. {
  259. uint32_t cpu_id, node_id;
  260. int ret;
  261. ret = sys_getcpu(&cpu_id, &node_id);
  262. if (ret) {
  263. perror("sys_getcpu()");
  264. return ret;
  265. }
  266. return (int32_t) node_id;
  267. }