linux-sandbox-fix-fstatat-crash.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. From 60d5e803ef2a4874d29799b638754152285e0ed9 Mon Sep 17 00:00:00 2001
  2. From: Matthew Denton <mpdenton@chromium.org>
  3. Date: Wed, 21 Jul 2021 12:55:11 +0000
  4. Subject: [PATCH] Linux sandbox: fix fstatat() crash
  5. This is a reland of https://crrev.com/c/2801873.
  6. Glibc has started rewriting fstat(fd, stat_buf) to
  7. fstatat(fd, "", stat_buf, AT_EMPTY_PATH). This works because when
  8. AT_EMPTY_PATH is specified, and the second argument is an empty string,
  9. then fstatat just performs an fstat on fd like normal.
  10. Unfortunately, fstatat() also allows stat-ing arbitrary pathnames like
  11. with fstatat(AT_FDCWD, "/i/am/a/file", stat_buf, 0);
  12. The baseline policy needs to prevent this usage of fstatat() since it
  13. doesn't allow access to arbitrary pathnames.
  14. Sadly, if the second argument is not an empty string, AT_EMPTY_PATH is
  15. simply ignored by current kernels.
  16. This means fstatat() is completely unsandboxable with seccomp, since
  17. we *need* to verify that the second argument is the empty string, but
  18. we can't dereference pointers in seccomp (due to limitations of BPF,
  19. and the difficulty of addressing these limitations due to TOCTOU
  20. issues).
  21. So, this CL Traps (raises a SIGSYS via seccomp) on any fstatat syscall.
  22. The signal handler, which runs in the sandboxed process, checks for
  23. AT_EMPTY_PATH and the empty string, and then rewrites any applicable
  24. fstatat() back into the old-style fstat().
  25. Bug: 1164975
  26. Change-Id: I3df6c04c0d781eb1f181d707ccaaead779337291
  27. Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3042179
  28. Reviewed-by: Robert Sesek <rsesek@chromium.org>
  29. Commit-Queue: Matthew Denton <mpdenton@chromium.org>
  30. Cr-Commit-Position: refs/heads/master@{#903873}
  31. ---
  32. .../seccomp-bpf-helpers/baseline_policy.cc | 8 ++++++
  33. .../baseline_policy_unittest.cc | 17 ++++++++++++-
  34. .../seccomp-bpf-helpers/sigsys_handlers.cc | 25 +++++++++++++++++++
  35. .../seccomp-bpf-helpers/sigsys_handlers.h | 14 +++++++++++
  36. .../linux/syscall_broker/broker_process.cc | 21 ++++++++++------
  37. .../syscall_broker/broker_process_unittest.cc | 18 ++++++-------
  38. sandbox/linux/system_headers/linux_stat.h | 4 +++
  39. 7 files changed, 89 insertions(+), 18 deletions(-)
  40. diff --git a/sandbox/linux/seccomp-bpf-helpers/baseline_policy.cc b/sandbox/linux/seccomp-bpf-helpers/baseline_policy.cc
  41. index f2a60bb4d7..9df0d2dbd3 100644
  42. --- a/sandbox/linux/seccomp-bpf-helpers/baseline_policy.cc
  43. +++ b/sandbox/linux/seccomp-bpf-helpers/baseline_policy.cc
  44. @@ -20,6 +20,7 @@
  45. #include "sandbox/linux/seccomp-bpf-helpers/syscall_sets.h"
  46. #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
  47. #include "sandbox/linux/services/syscall_wrappers.h"
  48. +#include "sandbox/linux/system_headers/linux_stat.h"
  49. #include "sandbox/linux/system_headers/linux_syscalls.h"
  50. #if !defined(SO_PEEK_OFF)
  51. @@ -304,6 +305,13 @@ ResultExpr EvaluateSyscallImpl(int fs_denied_errno,
  52. return Allow();
  53. }
  54. + // The fstatat syscalls are file system syscalls, which will be denied below
  55. + // with fs_denied_errno. However some allowed fstat syscalls are rewritten by
  56. + // libc implementations to fstatat syscalls, and we need to rewrite them back.
  57. + if (sysno == __NR_fstatat_default) {
  58. + return RewriteFstatatSIGSYS(fs_denied_errno);
  59. + }
  60. +
  61. if (SyscallSets::IsFileSystem(sysno) ||
  62. SyscallSets::IsCurrentDirectory(sysno)) {
  63. return Error(fs_denied_errno);
  64. diff --git a/sandbox/linux/seccomp-bpf-helpers/baseline_policy_unittest.cc b/sandbox/linux/seccomp-bpf-helpers/baseline_policy_unittest.cc
  65. index 68c29b564b..57d307e09d 100644
  66. --- a/sandbox/linux/seccomp-bpf-helpers/baseline_policy_unittest.cc
  67. +++ b/sandbox/linux/seccomp-bpf-helpers/baseline_policy_unittest.cc
  68. @@ -51,7 +51,8 @@ namespace sandbox {
  69. namespace {
  70. -// This also tests that read(), write() and fstat() are allowed.
  71. +// This also tests that read(), write(), fstat(), and fstatat(.., "", ..,
  72. +// AT_EMPTY_PATH) are allowed.
  73. void TestPipeOrSocketPair(base::ScopedFD read_end, base::ScopedFD write_end) {
  74. BPF_ASSERT_LE(0, read_end.get());
  75. BPF_ASSERT_LE(0, write_end.get());
  76. @@ -60,6 +61,20 @@ void TestPipeOrSocketPair(base::ScopedFD read_end, base::ScopedFD write_end) {
  77. BPF_ASSERT_EQ(0, sys_ret);
  78. BPF_ASSERT(S_ISFIFO(stat_buf.st_mode) || S_ISSOCK(stat_buf.st_mode));
  79. + sys_ret = fstatat(read_end.get(), "", &stat_buf, AT_EMPTY_PATH);
  80. + BPF_ASSERT_EQ(0, sys_ret);
  81. + BPF_ASSERT(S_ISFIFO(stat_buf.st_mode) || S_ISSOCK(stat_buf.st_mode));
  82. +
  83. + // Make sure fstatat with anything other than an empty string is denied.
  84. + sys_ret = fstatat(read_end.get(), "/", &stat_buf, AT_EMPTY_PATH);
  85. + BPF_ASSERT_EQ(sys_ret, -1);
  86. + BPF_ASSERT_EQ(EPERM, errno);
  87. +
  88. + // Make sure fstatat without AT_EMPTY_PATH is denied.
  89. + sys_ret = fstatat(read_end.get(), "", &stat_buf, 0);
  90. + BPF_ASSERT_EQ(sys_ret, -1);
  91. + BPF_ASSERT_EQ(EPERM, errno);
  92. +
  93. const ssize_t kTestTransferSize = 4;
  94. static const char kTestString[kTestTransferSize] = {'T', 'E', 'S', 'T'};
  95. ssize_t transfered = 0;
  96. diff --git a/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc b/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc
  97. index 64edbd68bd..71068a0452 100644
  98. --- a/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc
  99. +++ b/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc
  100. @@ -6,6 +6,7 @@
  101. #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h"
  102. +#include <fcntl.h>
  103. #include <stddef.h>
  104. #include <stdint.h>
  105. #include <string.h>
  106. @@ -22,6 +23,7 @@
  107. #include "sandbox/linux/seccomp-bpf/syscall.h"
  108. #include "sandbox/linux/services/syscall_wrappers.h"
  109. #include "sandbox/linux/system_headers/linux_seccomp.h"
  110. +#include "sandbox/linux/system_headers/linux_stat.h"
  111. #include "sandbox/linux/system_headers/linux_syscalls.h"
  112. #if defined(__mips__)
  113. @@ -355,6 +357,24 @@ intptr_t SIGSYSSchedHandler(const struct arch_seccomp_data& args,
  114. return -ENOSYS;
  115. }
  116. +intptr_t SIGSYSFstatatHandler(const struct arch_seccomp_data& args,
  117. + void* fs_denied_errno) {
  118. + if (args.nr == __NR_fstatat_default) {
  119. + if (*reinterpret_cast<const char*>(args.args[1]) == '\0' &&
  120. + args.args[3] == static_cast<uint64_t>(AT_EMPTY_PATH)) {
  121. + return syscall(__NR_fstat_default, static_cast<int>(args.args[0]),
  122. + reinterpret_cast<default_stat_struct*>(args.args[2]));
  123. + }
  124. + return -reinterpret_cast<intptr_t>(fs_denied_errno);
  125. + }
  126. +
  127. + CrashSIGSYS_Handler(args, fs_denied_errno);
  128. +
  129. + // Should never be reached.
  130. + RAW_CHECK(false);
  131. + return -ENOSYS;
  132. +}
  133. +
  134. bpf_dsl::ResultExpr CrashSIGSYS() {
  135. return bpf_dsl::Trap(CrashSIGSYS_Handler, NULL);
  136. }
  137. @@ -387,6 +407,11 @@ bpf_dsl::ResultExpr RewriteSchedSIGSYS() {
  138. return bpf_dsl::Trap(SIGSYSSchedHandler, NULL);
  139. }
  140. +bpf_dsl::ResultExpr RewriteFstatatSIGSYS(int fs_denied_errno) {
  141. + return bpf_dsl::Trap(SIGSYSFstatatHandler,
  142. + reinterpret_cast<void*>(fs_denied_errno));
  143. +}
  144. +
  145. void AllocateCrashKeys() {
  146. #if !defined(OS_NACL_NONSFI)
  147. if (seccomp_crash_key)
  148. diff --git a/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h b/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h
  149. index 7a958b93b2..8cd735ce15 100644
  150. --- a/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h
  151. +++ b/sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h
  152. @@ -62,6 +62,19 @@ SANDBOX_EXPORT intptr_t SIGSYSPtraceFailure(const arch_seccomp_data& args,
  153. // sched_setparam(), sched_setscheduler()
  154. SANDBOX_EXPORT intptr_t SIGSYSSchedHandler(const arch_seccomp_data& args,
  155. void* aux);
  156. +// If the fstatat() syscall is functionally equivalent to an fstat() syscall,
  157. +// then rewrite the syscall to the equivalent fstat() syscall which can be
  158. +// adequately sandboxed.
  159. +// If the fstatat() is not functionally equivalent to an fstat() syscall, we
  160. +// fail with -fs_denied_errno.
  161. +// If the syscall is not an fstatat() at all, crash in the same way as
  162. +// CrashSIGSYS_Handler.
  163. +// This is necessary because glibc and musl have started rewriting fstat(fd,
  164. +// stat_buf) as fstatat(fd, "", stat_buf, AT_EMPTY_PATH). We rewrite the latter
  165. +// back to the former, which is actually sandboxable.
  166. +SANDBOX_EXPORT intptr_t
  167. +SIGSYSFstatatHandler(const struct arch_seccomp_data& args,
  168. + void* fs_denied_errno);
  169. // Variants of the above functions for use with bpf_dsl.
  170. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYS();
  171. @@ -72,6 +85,7 @@ SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSKill();
  172. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSFutex();
  173. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSPtrace();
  174. SANDBOX_EXPORT bpf_dsl::ResultExpr RewriteSchedSIGSYS();
  175. +SANDBOX_EXPORT bpf_dsl::ResultExpr RewriteFstatatSIGSYS(int fs_denied_errno);
  176. // Allocates a crash key so that Seccomp information can be recorded.
  177. void AllocateCrashKeys();
  178. diff --git a/sandbox/linux/syscall_broker/broker_process.cc b/sandbox/linux/syscall_broker/broker_process.cc
  179. index c2176eb785..e9dad37485 100644
  180. --- a/sandbox/linux/syscall_broker/broker_process.cc
  181. +++ b/sandbox/linux/syscall_broker/broker_process.cc
  182. @@ -113,44 +113,49 @@ bool BrokerProcess::IsSyscallAllowed(int sysno) const {
  183. }
  184. bool BrokerProcess::IsSyscallBrokerable(int sysno, bool fast_check) const {
  185. + // The syscalls unavailable on aarch64 are all blocked by Android's default
  186. + // seccomp policy, even on non-aarch64 architectures. I.e., the syscalls XX()
  187. + // with a corresponding XXat() versions are typically unavailable in aarch64
  188. + // and are default disabled in Android. So, we should refuse to broker them
  189. + // to be consistent with the platform's restrictions.
  190. switch (sysno) {
  191. -#if !defined(__aarch64__)
  192. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  193. case __NR_access:
  194. #endif
  195. case __NR_faccessat:
  196. return !fast_check || allowed_command_set_.test(COMMAND_ACCESS);
  197. -#if !defined(__aarch64__)
  198. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  199. case __NR_mkdir:
  200. #endif
  201. case __NR_mkdirat:
  202. return !fast_check || allowed_command_set_.test(COMMAND_MKDIR);
  203. -#if !defined(__aarch64__)
  204. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  205. case __NR_open:
  206. #endif
  207. case __NR_openat:
  208. return !fast_check || allowed_command_set_.test(COMMAND_OPEN);
  209. -#if !defined(__aarch64__)
  210. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  211. case __NR_readlink:
  212. #endif
  213. case __NR_readlinkat:
  214. return !fast_check || allowed_command_set_.test(COMMAND_READLINK);
  215. -#if !defined(__aarch64__)
  216. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  217. case __NR_rename:
  218. #endif
  219. case __NR_renameat:
  220. case __NR_renameat2:
  221. return !fast_check || allowed_command_set_.test(COMMAND_RENAME);
  222. -#if !defined(__aarch64__)
  223. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  224. case __NR_rmdir:
  225. return !fast_check || allowed_command_set_.test(COMMAND_RMDIR);
  226. #endif
  227. -#if !defined(__aarch64__)
  228. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  229. case __NR_stat:
  230. case __NR_lstat:
  231. #endif
  232. @@ -175,7 +180,7 @@ bool BrokerProcess::IsSyscallBrokerable(int sysno, bool fast_check) const {
  233. return !fast_check || allowed_command_set_.test(COMMAND_STAT);
  234. #endif
  235. -#if !defined(__aarch64__)
  236. +#if !defined(__aarch64__) && !defined(OS_ANDROID)
  237. case __NR_unlink:
  238. return !fast_check || allowed_command_set_.test(COMMAND_UNLINK);
  239. #endif
  240. diff --git a/sandbox/linux/syscall_broker/broker_process_unittest.cc b/sandbox/linux/syscall_broker/broker_process_unittest.cc
  241. index c65f25a78a..f0db08d84e 100644
  242. --- a/sandbox/linux/syscall_broker/broker_process_unittest.cc
  243. +++ b/sandbox/linux/syscall_broker/broker_process_unittest.cc
  244. @@ -1596,52 +1596,52 @@ TEST(BrokerProcess, IsSyscallAllowed) {
  245. const base::flat_map<BrokerCommand, base::flat_set<int>> kSysnosForCommand = {
  246. {COMMAND_ACCESS,
  247. {__NR_faccessat,
  248. -#if defined(__NR_access)
  249. +#if defined(__NR_access) && !defined(OS_ANDROID)
  250. __NR_access
  251. #endif
  252. }},
  253. {COMMAND_MKDIR,
  254. {__NR_mkdirat,
  255. -#if defined(__NR_mkdir)
  256. +#if defined(__NR_mkdir) && !defined(OS_ANDROID)
  257. __NR_mkdir
  258. #endif
  259. }},
  260. {COMMAND_OPEN,
  261. {__NR_openat,
  262. -#if defined(__NR_open)
  263. +#if defined(__NR_open) && !defined(OS_ANDROID)
  264. __NR_open
  265. #endif
  266. }},
  267. {COMMAND_READLINK,
  268. {__NR_readlinkat,
  269. -#if defined(__NR_readlink)
  270. +#if defined(__NR_readlink) && !defined(OS_ANDROID)
  271. __NR_readlink
  272. #endif
  273. }},
  274. {COMMAND_RENAME,
  275. {__NR_renameat,
  276. -#if defined(__NR_rename)
  277. +#if defined(__NR_rename) && !defined(OS_ANDROID)
  278. __NR_rename
  279. #endif
  280. }},
  281. {COMMAND_UNLINK,
  282. {__NR_unlinkat,
  283. -#if defined(__NR_unlink)
  284. +#if defined(__NR_unlink) && !defined(OS_ANDROID)
  285. __NR_unlink
  286. #endif
  287. }},
  288. {COMMAND_RMDIR,
  289. {__NR_unlinkat,
  290. -#if defined(__NR_rmdir)
  291. +#if defined(__NR_rmdir) && !defined(OS_ANDROID)
  292. __NR_rmdir
  293. #endif
  294. }},
  295. {COMMAND_STAT,
  296. {
  297. -#if defined(__NR_stat)
  298. +#if defined(__NR_stat) && !defined(OS_ANDROID)
  299. __NR_stat,
  300. #endif
  301. -#if defined(__NR_lstat)
  302. +#if defined(__NR_lstat) && !defined(OS_ANDROID)
  303. __NR_lstat,
  304. #endif
  305. #if defined(__NR_fstatat)
  306. diff --git a/sandbox/linux/system_headers/linux_stat.h b/sandbox/linux/system_headers/linux_stat.h
  307. index 35788eb22a..83b89efc75 100644
  308. --- a/sandbox/linux/system_headers/linux_stat.h
  309. +++ b/sandbox/linux/system_headers/linux_stat.h
  310. @@ -157,6 +157,10 @@ struct kernel_stat {
  311. };
  312. #endif
  313. +#if !defined(AT_EMPTY_PATH)
  314. +#define AT_EMPTY_PATH 0x1000
  315. +#endif
  316. +
  317. // On 32-bit systems, we default to the 64-bit stat struct like libc
  318. // implementations do. Otherwise we default to the normal stat struct which is
  319. // already 64-bit.