broker_command.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2017 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. #include <unistd.h>
  5. #include "sandbox/linux/syscall_broker/broker_command.h"
  6. #include "sandbox/linux/syscall_broker/broker_permission_list.h"
  7. namespace sandbox {
  8. namespace syscall_broker {
  9. bool CommandAccessIsSafe(const BrokerCommandSet& command_set,
  10. const BrokerPermissionList& policy,
  11. const char* requested_filename,
  12. int requested_mode,
  13. const char** filename_to_use) {
  14. return command_set.test(COMMAND_ACCESS) &&
  15. policy.GetFileNameIfAllowedToAccess(requested_filename, requested_mode,
  16. filename_to_use);
  17. }
  18. bool CommandMkdirIsSafe(const BrokerCommandSet& command_set,
  19. const BrokerPermissionList& policy,
  20. const char* requested_filename,
  21. const char** filename_to_use) {
  22. return command_set.test(COMMAND_MKDIR) &&
  23. policy.GetFileNameIfAllowedToOpen(requested_filename,
  24. O_RDWR | O_CREAT | O_EXCL,
  25. filename_to_use, nullptr);
  26. }
  27. bool CommandOpenIsSafe(const BrokerCommandSet& command_set,
  28. const BrokerPermissionList& policy,
  29. const char* requested_filename,
  30. int requested_flags,
  31. const char** filename_to_use,
  32. bool* unlink_after_open) {
  33. return command_set.test(COMMAND_OPEN) &&
  34. policy.GetFileNameIfAllowedToOpen(
  35. requested_filename,
  36. requested_flags & ~kCurrentProcessOpenFlagsMask, filename_to_use,
  37. unlink_after_open);
  38. }
  39. bool CommandReadlinkIsSafe(const BrokerCommandSet& command_set,
  40. const BrokerPermissionList& policy,
  41. const char* requested_filename,
  42. const char** filename_to_use) {
  43. return command_set.test(COMMAND_READLINK) &&
  44. policy.GetFileNameIfAllowedToOpen(requested_filename, O_RDONLY,
  45. filename_to_use, nullptr);
  46. }
  47. bool CommandRenameIsSafe(const BrokerCommandSet& command_set,
  48. const BrokerPermissionList& policy,
  49. const char* old_filename,
  50. const char* new_filename,
  51. const char** old_filename_to_use,
  52. const char** new_filename_to_use) {
  53. return command_set.test(COMMAND_RENAME) &&
  54. policy.GetFileNameIfAllowedToOpen(old_filename,
  55. O_RDWR | O_CREAT | O_EXCL,
  56. old_filename_to_use, nullptr) &&
  57. policy.GetFileNameIfAllowedToOpen(new_filename,
  58. O_RDWR | O_CREAT | O_EXCL,
  59. new_filename_to_use, nullptr);
  60. }
  61. bool CommandRmdirIsSafe(const BrokerCommandSet& command_set,
  62. const BrokerPermissionList& policy,
  63. const char* requested_filename,
  64. const char** filename_to_use) {
  65. return command_set.test(COMMAND_RMDIR) &&
  66. policy.GetFileNameIfAllowedToOpen(requested_filename,
  67. O_RDWR | O_CREAT | O_EXCL,
  68. filename_to_use, nullptr);
  69. }
  70. bool CommandStatIsSafe(const BrokerCommandSet& command_set,
  71. const BrokerPermissionList& policy,
  72. const char* requested_filename,
  73. const char** filename_to_use) {
  74. return command_set.test(COMMAND_STAT) &&
  75. policy.GetFileNameIfAllowedToStat(requested_filename, filename_to_use);
  76. }
  77. bool CommandUnlinkIsSafe(const BrokerCommandSet& command_set,
  78. const BrokerPermissionList& policy,
  79. const char* requested_filename,
  80. const char** filename_to_use) {
  81. return command_set.test(COMMAND_UNLINK) &&
  82. policy.GetFileNameIfAllowedToOpen(requested_filename,
  83. O_RDWR | O_CREAT | O_EXCL,
  84. filename_to_use, nullptr);
  85. }
  86. } // namespace syscall_broker
  87. } // namespace sandbox