syscall_dispatcher.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2020 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_SYSCALL_BROKER_SYSCALL_DISPATCHER_H_
  5. #define SANDBOX_LINUX_SYSCALL_BROKER_SYSCALL_DISPATCHER_H_
  6. #include <sys/stat.h>
  7. #include <cstddef>
  8. #include "sandbox/linux/system_headers/linux_seccomp.h"
  9. #include "sandbox/linux/system_headers/linux_stat.h"
  10. #include "sandbox/sandbox_export.h"
  11. namespace sandbox {
  12. namespace syscall_broker {
  13. // An abstract class that defines all the system calls we perform for the
  14. // sandboxed process.
  15. class SANDBOX_EXPORT SyscallDispatcher {
  16. public:
  17. // Emulates access()/faccessat().
  18. // X_OK will always return an error in practice since the broker process
  19. // doesn't support execute permissions.
  20. virtual int Access(const char* pathname, int mode) const = 0;
  21. // Emulates mkdir()/mkdirat.
  22. virtual int Mkdir(const char* path, int mode) const = 0;
  23. // Emulates open()/openat().
  24. // The implementation only supports certain white listed flags and will
  25. // return -EPERM on other flags.
  26. virtual int Open(const char* pathname, int flags) const = 0;
  27. // Emulates readlink()/readlinkat().
  28. virtual int Readlink(const char* path, char* buf, size_t bufsize) const = 0;
  29. // Emulates rename()/renameat()/renameat2().
  30. virtual int Rename(const char* oldpath, const char* newpath) const = 0;
  31. // Emulates rmdir().
  32. virtual int Rmdir(const char* path) const = 0;
  33. // Emulates stat()/stat64()/lstat()/lstat64()/fstatat()/newfstatat().
  34. // Stat64 is only available on 32-bit systems.
  35. virtual int Stat(const char* pathname,
  36. bool follow_links,
  37. struct kernel_stat* sb) const = 0;
  38. virtual int Stat64(const char* pathname,
  39. bool follow_links,
  40. struct kernel_stat64* sb) const = 0;
  41. // Emulates unlink()/unlinkat().
  42. virtual int Unlink(const char* unlink) const = 0;
  43. // Different architectures use a different syscall from the stat family by
  44. // default in glibc. E.g. 32-bit systems use *stat*64() and fill out struct
  45. // kernel_stat64, whereas 64-bit systems use *stat*() and fill out struct
  46. // kernel_stat. Some tests want to call the SyscallDispatcher directly, and
  47. // should be using the default stat in order to test against glibc.
  48. int DefaultStatForTesting(const char* pathname,
  49. bool follow_links,
  50. default_stat_struct* sb);
  51. // Validates the args passed to a *statat*() syscall and performs the syscall
  52. // using Stat(), or on 32-bit systems it uses Stat64() for the *statat64()
  53. // syscalls.
  54. int PerformStatat(const arch_seccomp_data& args, bool stat64);
  55. // Validates the args passed to an unlinkat() syscall and performs the syscall
  56. // using either Unlink() or Rmdir().
  57. int PerformUnlinkat(const arch_seccomp_data& args);
  58. // Reads the syscall number and arguments, imposes some policy (e.g. the *at()
  59. // system calls must only allow AT_FDCWD as the first argument), and
  60. // dispatches to the correct method from above.
  61. // Async-signal-safe since this might be called in a signal handler.
  62. int DispatchSyscall(const arch_seccomp_data& args);
  63. protected:
  64. virtual ~SyscallDispatcher() = default;
  65. };
  66. } // namespace syscall_broker
  67. } // namespace sandbox
  68. #endif // SANDBOX_LINUX_SYSCALL_BROKER_SYSCALL_DISPATCHER_H_