broker_client.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2014 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_BROKER_CLIENT_H_
  5. #define SANDBOX_LINUX_SYSCALL_BROKER_BROKER_CLIENT_H_
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include "sandbox/linux/syscall_broker/broker_channel.h"
  10. #include "sandbox/linux/syscall_broker/broker_command.h"
  11. #include "sandbox/linux/syscall_broker/broker_sandbox_config.h"
  12. #include "sandbox/linux/syscall_broker/syscall_dispatcher.h"
  13. #include "sandbox/sandbox_export.h"
  14. namespace sandbox {
  15. namespace syscall_broker {
  16. // This class can be embedded in a sandboxed process and can be
  17. // used to perform certain system calls in another, presumably
  18. // non-sandboxed process (which embeds BrokerHost).
  19. // A key feature of this class is the ability to use some of its methods in a
  20. // thread-safe and async-signal safe way. The goal is to be able to use it to
  21. // replace the open() or access() system calls happening anywhere in a process
  22. // (as allowed for instance by seccomp-bpf's SIGSYS mechanism).
  23. class SANDBOX_EXPORT BrokerClient : public SyscallDispatcher {
  24. public:
  25. // Handler to be used with a bpf_dsl Trap() function to forward system calls
  26. // to the methods below.
  27. static intptr_t SIGSYS_Handler(const arch_seccomp_data& args,
  28. void* aux_broker_process);
  29. // |policy| needs to match the policy used by BrokerHost. This allows to
  30. // predict some of the requests which will be denied and save an IPC round
  31. // trip.
  32. // |ipc_channel| needs to be a suitable SOCK_SEQPACKET unix socket.
  33. // |fast_check_in_client| should be set to true and
  34. BrokerClient(const BrokerSandboxConfig& policy,
  35. BrokerChannel::EndPoint ipc_channel,
  36. bool fast_check_in_client);
  37. BrokerClient(const BrokerClient&) = delete;
  38. BrokerClient& operator=(const BrokerClient&) = delete;
  39. ~BrokerClient() override;
  40. // Get the file descriptor used for IPC.
  41. int GetIPCDescriptorForTesting() const { return ipc_channel_.get(); }
  42. // The following public methods can be used in place of the equivalently
  43. // name system calls. They all return -errno on errors. They are all async
  44. // signal safe so they may be called from a SIGSYS trap handler.
  45. // SyscallDispatcher implementation:
  46. int Access(const char* pathname, int mode) const override;
  47. int Mkdir(const char* path, int mode) const override;
  48. int Open(const char* pathname, int flags) const override;
  49. int Readlink(const char* path, char* buf, size_t bufsize) const override;
  50. int Rename(const char* oldpath, const char* newpath) const override;
  51. int Rmdir(const char* path) const override;
  52. int Stat(const char* pathname,
  53. bool follow_links,
  54. struct kernel_stat* sb) const override;
  55. int Stat64(const char* pathname,
  56. bool follow_links,
  57. struct kernel_stat64* sb) const override;
  58. int Unlink(const char* unlink) const override;
  59. const BrokerSandboxConfig& policy() const { return policy_; }
  60. private:
  61. int PathOnlySyscall(BrokerCommand syscall_type, const char* pathname) const;
  62. int PathAndFlagsSyscall(BrokerCommand syscall_type,
  63. const char* pathname,
  64. int flags) const;
  65. int PathAndFlagsSyscallReturningFD(BrokerCommand syscall_type,
  66. const char* pathname,
  67. int flags) const;
  68. int StatFamilySyscall(BrokerCommand syscall_type,
  69. const char* pathname,
  70. bool follow_links,
  71. void* result_ptr,
  72. size_t expected_result_size) const;
  73. const BrokerSandboxConfig& policy_;
  74. const BrokerChannel::EndPoint ipc_channel_;
  75. const bool fast_check_in_client_; // Whether to forward a request that we
  76. // know will be denied to the broker. (Used
  77. // for tests).
  78. };
  79. } // namespace syscall_broker
  80. } // namespace sandbox
  81. #endif // SANDBOX_LINUX_SYSCALL_BROKER_BROKER_CLIENT_H_