unveil.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/StringView.h>
  9. #include <Kernel/FileSystem/Custody.h>
  10. #include <Kernel/FileSystem/VirtualFileSystem.h>
  11. #include <Kernel/Process.h>
  12. namespace Kernel {
  13. static void update_intermediate_node_permissions(UnveilNode& root_node, UnveilAccess new_permissions)
  14. {
  15. for (auto& entry : root_node.children()) {
  16. auto& node = static_cast<UnveilNode&>(*entry.value);
  17. if (node.was_explicitly_unveiled())
  18. continue;
  19. node.set_metadata({ node.path(), new_permissions, node.was_explicitly_unveiled() });
  20. update_intermediate_node_permissions(node, new_permissions);
  21. }
  22. }
  23. KResultOr<FlatPtr> Process::sys$unveil(Userspace<const Syscall::SC_unveil_params*> user_params)
  24. {
  25. Syscall::SC_unveil_params params;
  26. if (!copy_from_user(&params, user_params))
  27. return EFAULT;
  28. if (!params.path.characters && !params.permissions.characters) {
  29. m_veil_state = VeilState::Locked;
  30. return 0;
  31. }
  32. if (m_veil_state == VeilState::Locked)
  33. return EPERM;
  34. if (!params.path.characters || !params.permissions.characters)
  35. return EINVAL;
  36. if (params.permissions.length > 5)
  37. return EINVAL;
  38. auto path_or_error = get_syscall_path_argument(params.path);
  39. if (path_or_error.is_error())
  40. return path_or_error.error();
  41. auto& path = *path_or_error.value();
  42. if (path.is_empty() || !path.view().starts_with('/'))
  43. return EINVAL;
  44. auto permissions = copy_string_from_user(params.permissions);
  45. if (permissions.is_null())
  46. return EFAULT;
  47. // Let's work out permissions first...
  48. unsigned new_permissions = 0;
  49. for (const char permission : permissions) {
  50. switch (permission) {
  51. case 'r':
  52. new_permissions |= UnveilAccess::Read;
  53. break;
  54. case 'w':
  55. new_permissions |= UnveilAccess::Write;
  56. break;
  57. case 'x':
  58. new_permissions |= UnveilAccess::Execute;
  59. break;
  60. case 'c':
  61. new_permissions |= UnveilAccess::CreateOrRemove;
  62. break;
  63. case 'b':
  64. new_permissions |= UnveilAccess::Browse;
  65. break;
  66. default:
  67. return EINVAL;
  68. }
  69. }
  70. // Now, let's try and resolve the path and obtain custody of the inode on the disk, and if not, bail out with
  71. // the error from resolve_path_without_veil()
  72. // However, if the user specified unveil() with "c" permissions, we don't set errno if ENOENT is encountered,
  73. // because they most likely intend the program to create the file for them later on.
  74. // If this case is encountered, the parent node of the path is returned and the custody of that inode is used instead.
  75. RefPtr<Custody> parent_custody; // Parent inode in case of ENOENT
  76. String new_unveiled_path;
  77. auto custody_or_error = VFS::the().resolve_path_without_veil(path.view(), root_directory(), &parent_custody);
  78. if (!custody_or_error.is_error()) {
  79. new_unveiled_path = custody_or_error.value()->absolute_path();
  80. } else if (custody_or_error.error() == -ENOENT && parent_custody && (new_permissions & UnveilAccess::CreateOrRemove)) {
  81. String basename = LexicalPath(path.view()).basename();
  82. new_unveiled_path = String::formatted("{}/{}", parent_custody->absolute_path(), basename);
  83. } else {
  84. // FIXME Should this be EINVAL?
  85. return custody_or_error.error();
  86. }
  87. LexicalPath lexical_path(new_unveiled_path);
  88. auto it = lexical_path.parts().begin();
  89. auto& matching_node = m_unveiled_paths.traverse_until_last_accessible_node(it, lexical_path.parts().end());
  90. if (it.is_end()) {
  91. // If the path has already been explicitly unveiled, do not allow elevating its permissions.
  92. if (matching_node.was_explicitly_unveiled()) {
  93. if (new_permissions & ~matching_node.permissions())
  94. return EPERM;
  95. }
  96. // It is possible that nodes that are "grandchildren" of the matching node have already been unveiled.
  97. // This means that there may be intermediate nodes between this one and the unveiled "grandchildren"
  98. // that inherited the current node's previous permissions. Those nodes now need their permissions
  99. // updated to match the current node.
  100. if (matching_node.permissions() != new_permissions)
  101. update_intermediate_node_permissions(matching_node, (UnveilAccess)new_permissions);
  102. matching_node.set_metadata({ matching_node.path(), (UnveilAccess)new_permissions, true });
  103. m_veil_state = VeilState::Dropped;
  104. return 0;
  105. }
  106. matching_node.insert(
  107. it,
  108. lexical_path.parts().end(),
  109. { new_unveiled_path, (UnveilAccess)new_permissions, true },
  110. [](auto& parent, auto& it) -> Optional<UnveilMetadata> {
  111. auto path = LexicalPath::join(parent.path(), *it).string();
  112. return UnveilMetadata { path, parent.permissions(), false };
  113. });
  114. VERIFY(m_veil_state != VeilState::Locked);
  115. m_veil_state = VeilState::Dropped;
  116. return 0;
  117. }
  118. }