env_chroot.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #if !defined(OS_WIN)
  7. #include <string>
  8. #include "env/fs_remap.h"
  9. #include "rocksdb/file_system.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. class ChrootFileSystem : public RemapFileSystem {
  12. public:
  13. ChrootFileSystem(const std::shared_ptr<FileSystem>& base,
  14. const std::string& chroot_dir);
  15. static const char* kClassName() { return "ChrootFS"; }
  16. const char* Name() const override { return kClassName(); }
  17. IOStatus GetTestDirectory(const IOOptions& options, std::string* path,
  18. IODebugContext* dbg) override;
  19. Status PrepareOptions(const ConfigOptions& options) override;
  20. protected:
  21. // Returns status and expanded absolute path including the chroot directory.
  22. // Checks whether the provided path breaks out of the chroot. If it returns
  23. // non-OK status, the returned path should not be used.
  24. std::pair<IOStatus, std::string> EncodePath(const std::string& path) override;
  25. // Similar to EncodePath() except assumes the basename in the path hasn't been
  26. // created yet.
  27. std::pair<IOStatus, std::string> EncodePathWithNewBasename(
  28. const std::string& path) override;
  29. private:
  30. std::string chroot_dir_;
  31. };
  32. // Returns an Env that translates paths such that the root directory appears to
  33. // be chroot_dir. chroot_dir should refer to an existing directory.
  34. //
  35. // This class has not been fully analyzed for providing strong security
  36. // guarantees.
  37. Env* NewChrootEnv(Env* base_env, const std::string& chroot_dir);
  38. std::shared_ptr<FileSystem> NewChrootFileSystem(
  39. const std::shared_ptr<FileSystem>& base, const std::string& chroot_dir);
  40. } // namespace ROCKSDB_NAMESPACE
  41. #endif // !defined(OS_WIN)