sigsys_handlers.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SANDBOX_LINUX_SECCOMP_BPF_HELPERS_SIGSYS_HANDLERS_H_
  5. #define SANDBOX_LINUX_SECCOMP_BPF_HELPERS_SIGSYS_HANDLERS_H_
  6. #include <stdint.h>
  7. #include "build/build_config.h"
  8. #include "sandbox/linux/bpf_dsl/bpf_dsl_forward.h"
  9. #include "sandbox/sandbox_export.h"
  10. // The handlers are suitable for use in Trap() error codes. They are
  11. // guaranteed to be async-signal safe.
  12. // See sandbox/linux/seccomp-bpf/trap.h to see how they work.
  13. struct arch_seccomp_data;
  14. namespace sandbox {
  15. // This handler will crash the currently running process. The crashing address
  16. // will be the number of the current system call, extracted from |args|.
  17. // This handler will also print to stderr the number of the crashing syscall.
  18. SANDBOX_EXPORT intptr_t CrashSIGSYS_Handler(const arch_seccomp_data& args,
  19. void* aux);
  20. // The following seven handlers are suitable to report failures for specific
  21. // system calls with additional information.
  22. // The crashing address will be (clone_flags & 0xFFFFFF), where clone_flags is
  23. // the clone(2) argument, extracted from |args|.
  24. SANDBOX_EXPORT intptr_t SIGSYSCloneFailure(const arch_seccomp_data& args,
  25. void* aux);
  26. // The crashing address will be (option & 0xFFF), where option is the prctl(2)
  27. // argument.
  28. SANDBOX_EXPORT intptr_t SIGSYSPrctlFailure(const arch_seccomp_data& args,
  29. void* aux);
  30. // The crashing address will be request & 0xFFFF, where request is the ioctl(2)
  31. // argument.
  32. SANDBOX_EXPORT intptr_t SIGSYSIoctlFailure(const arch_seccomp_data& args,
  33. void* aux);
  34. // The crashing address will be (pid & 0xFFF), where pid is the first
  35. // argument (and can be a tid).
  36. SANDBOX_EXPORT intptr_t SIGSYSKillFailure(const arch_seccomp_data& args,
  37. void* aux);
  38. // The crashing address will be (op & 0xFFF), where op is the second
  39. // argument.
  40. SANDBOX_EXPORT intptr_t SIGSYSFutexFailure(const arch_seccomp_data& args,
  41. void* aux);
  42. // The crashing address will be (op & 0xFFF), where op is the second
  43. // argument.
  44. SANDBOX_EXPORT intptr_t SIGSYSPtraceFailure(const arch_seccomp_data& args,
  45. void* aux);
  46. // If the syscall is not being called on the current tid, crashes in the same
  47. // way as CrashSIGSYS_Handler. Otherwise, returns the result of calling the
  48. // syscall with the pid argument set to 0 (which for these calls means the
  49. // current thread). The following syscalls are supported:
  50. //
  51. // sched_getaffinity(), sched_getattr(), sched_getparam(), sched_getscheduler(),
  52. // sched_rr_get_interval(), sched_setaffinity(), sched_setattr(),
  53. // sched_setparam(), sched_setscheduler()
  54. SANDBOX_EXPORT intptr_t SIGSYSSchedHandler(const arch_seccomp_data& args,
  55. void* aux);
  56. // If the fstatat() syscall is functionally equivalent to an fstat() syscall,
  57. // then rewrite the syscall to the equivalent fstat() syscall which can be
  58. // adequately sandboxed.
  59. // If the fstatat() is not functionally equivalent to an fstat() syscall, we
  60. // fail with -fs_denied_errno.
  61. // If the syscall is not an fstatat() at all, crash in the same way as
  62. // CrashSIGSYS_Handler.
  63. // This is necessary because glibc and musl have started rewriting fstat(fd,
  64. // stat_buf) as fstatat(fd, "", stat_buf, AT_EMPTY_PATH). We rewrite the latter
  65. // back to the former, which is actually sandboxable.
  66. SANDBOX_EXPORT intptr_t
  67. SIGSYSFstatatHandler(const struct arch_seccomp_data& args,
  68. void* fs_denied_errno);
  69. // Variants of the above functions for use with bpf_dsl.
  70. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYS();
  71. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSClone();
  72. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSPrctl();
  73. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSIoctl();
  74. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSKill();
  75. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSFutex();
  76. SANDBOX_EXPORT bpf_dsl::ResultExpr CrashSIGSYSPtrace();
  77. SANDBOX_EXPORT bpf_dsl::ResultExpr RewriteSchedSIGSYS();
  78. SANDBOX_EXPORT bpf_dsl::ResultExpr RewriteFstatatSIGSYS(int fs_denied_errno);
  79. // Allocates a crash key so that Seccomp information can be recorded.
  80. void AllocateCrashKeys();
  81. // Following four functions return substrings of error messages used
  82. // in the above four functions. They are useful in death tests.
  83. SANDBOX_EXPORT const char* GetErrorMessageContentForTests();
  84. SANDBOX_EXPORT const char* GetCloneErrorMessageContentForTests();
  85. SANDBOX_EXPORT const char* GetPrctlErrorMessageContentForTests();
  86. SANDBOX_EXPORT const char* GetIoctlErrorMessageContentForTests();
  87. SANDBOX_EXPORT const char* GetKillErrorMessageContentForTests();
  88. SANDBOX_EXPORT const char* GetFutexErrorMessageContentForTests();
  89. SANDBOX_EXPORT const char* GetPtraceErrorMessageContentForTests();
  90. } // namespace sandbox.
  91. #endif // SANDBOX_LINUX_SECCOMP_BPF_HELPERS_SIGSYS_HANDLERS_H_