Inode.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  4. * Copyright (c) 2021-2023, Liav A. <liavalb@hotmail.co.il>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <Kernel/FileSystem/ProcFS/Inode.h>
  9. #include <Kernel/Process.h>
  10. #include <Kernel/Time/TimeManagement.h>
  11. namespace Kernel {
  12. ProcFSInode::~ProcFSInode() = default;
  13. static mode_t determine_procfs_process_inode_mode(u32 subdirectory, u32 property)
  14. {
  15. if (subdirectory == process_fd_subdirectory_root_entry.subdirectory)
  16. return S_IFLNK | 0400;
  17. if (subdirectory == process_stacks_subdirectory_root_entry.subdirectory)
  18. return S_IFREG | 0400;
  19. if (subdirectory == process_children_subdirectory_root_entry.subdirectory)
  20. return S_IFLNK | 0400;
  21. VERIFY(subdirectory == main_process_directory_root_entry.subdirectory);
  22. if (property == process_exe_symlink_entry.property)
  23. return S_IFLNK | 0777;
  24. if (property == process_cwd_symlink_entry.property)
  25. return S_IFLNK | 0777;
  26. return S_IFREG | 0400;
  27. }
  28. static u16 extract_subdirectory_index_from_inode_index(InodeIndex inode_index)
  29. {
  30. return (inode_index.value() >> 20) & 0xFFFF;
  31. }
  32. static u32 extract_property_index_from_inode_index(InodeIndex inode_index)
  33. {
  34. return inode_index.value() & 0xFFFFF;
  35. }
  36. InodeIndex ProcFSInode::create_index_from_global_directory_entry(segmented_global_inode_index entry)
  37. {
  38. u64 inode_index = 0;
  39. VERIFY(entry.primary < 0x10000000);
  40. u64 tmp = entry.primary;
  41. inode_index |= tmp << 36;
  42. // NOTE: The sub-directory part is already limited to 0xFFFF, so no need to VERIFY it.
  43. tmp = entry.subdirectory;
  44. inode_index |= tmp << 20;
  45. VERIFY(entry.property < 0x100000);
  46. inode_index |= entry.property;
  47. return inode_index;
  48. }
  49. InodeIndex ProcFSInode::create_index_from_process_directory_entry(ProcessID pid, segmented_process_directory_entry entry)
  50. {
  51. u64 inode_index = 0;
  52. // NOTE: We use 0xFFFFFFF because PID part (bits 64-36) as 0 is reserved for global inodes.
  53. VERIFY(pid.value() < 0xFFFFFFF);
  54. u64 tmp = (pid.value() + 1);
  55. inode_index |= tmp << 36;
  56. // NOTE: The sub-directory part is already limited to 0xFFFF, so no need to VERIFY it.
  57. tmp = entry.subdirectory;
  58. inode_index |= tmp << 20;
  59. VERIFY(entry.property < 0x100000);
  60. inode_index |= entry.property;
  61. return inode_index;
  62. }
  63. static Optional<ProcessID> extract_possible_pid_from_inode_index(InodeIndex inode_index)
  64. {
  65. auto pid_part = inode_index.value() >> 36;
  66. // NOTE: pid_part is set to 0 for global inodes.
  67. if (pid_part == 0)
  68. return {};
  69. return pid_part - 1;
  70. }
  71. ProcFSInode::ProcFSInode(ProcFS const& procfs_instance, InodeIndex inode_index)
  72. : Inode(const_cast<ProcFS&>(procfs_instance), inode_index)
  73. , m_associated_pid(extract_possible_pid_from_inode_index(inode_index))
  74. , m_subdirectory(extract_subdirectory_index_from_inode_index(inode_index))
  75. , m_property(extract_property_index_from_inode_index(inode_index))
  76. {
  77. if (inode_index == 1) {
  78. m_type = Type::RootDirectory;
  79. return;
  80. }
  81. if (inode_index == 2) {
  82. m_type = Type::SelfProcessLink;
  83. return;
  84. }
  85. if (m_property == 0) {
  86. if (m_subdirectory > 0)
  87. m_type = Type::ProcessSubdirectory;
  88. else
  89. m_type = Type::ProcessDirectory;
  90. return;
  91. }
  92. m_type = Type::ProcessProperty;
  93. }
  94. ErrorOr<void> ProcFSInode::traverse_as_root_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  95. {
  96. TRY(callback({ "."sv, { fsid(), 1 }, 0 }));
  97. TRY(callback({ ".."sv, { fsid(), 0 }, 0 }));
  98. TRY(callback({ "self"sv, { fsid(), 2 }, 0 }));
  99. return Process::for_each_in_same_jail([&](Process& process) -> ErrorOr<void> {
  100. VERIFY(!(process.pid() < 0));
  101. u64 process_id = (u64)process.pid().value();
  102. InodeIdentifier identifier = { fsid(), static_cast<InodeIndex>(process_id << 36) };
  103. auto process_id_string = TRY(KString::formatted("{:d}", process_id));
  104. TRY(callback({ process_id_string->view(), identifier, 0 }));
  105. return {};
  106. });
  107. }
  108. ErrorOr<void> ProcFSInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  109. {
  110. MutexLocker locker(procfs().m_lock);
  111. if (m_type == Type::ProcessSubdirectory) {
  112. VERIFY(m_associated_pid.has_value());
  113. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  114. if (!process)
  115. return EINVAL;
  116. switch (m_subdirectory) {
  117. case process_fd_subdirectory_root_entry.subdirectory:
  118. return process->traverse_file_descriptions_directory(procfs().fsid(), move(callback));
  119. case process_stacks_subdirectory_root_entry.subdirectory:
  120. return process->traverse_stacks_directory(procfs().fsid(), move(callback));
  121. case process_children_subdirectory_root_entry.subdirectory:
  122. return process->traverse_children_directory(procfs().fsid(), move(callback));
  123. default:
  124. VERIFY_NOT_REACHED();
  125. }
  126. VERIFY_NOT_REACHED();
  127. }
  128. if (m_type == Type::RootDirectory) {
  129. return traverse_as_root_directory(move(callback));
  130. }
  131. VERIFY(m_type == Type::ProcessDirectory);
  132. VERIFY(m_associated_pid.has_value());
  133. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  134. if (!process)
  135. return EINVAL;
  136. return process->traverse_as_directory(procfs().fsid(), move(callback));
  137. }
  138. ErrorOr<NonnullRefPtr<Inode>> ProcFSInode::lookup_as_root_directory(StringView name)
  139. {
  140. if (name == "self"sv)
  141. return procfs().get_inode({ fsid(), 2 });
  142. auto pid = name.to_uint<unsigned>();
  143. if (!pid.has_value())
  144. return ESRCH;
  145. auto actual_pid = pid.value();
  146. if (auto maybe_process = Process::from_pid_in_same_jail(actual_pid)) {
  147. InodeIndex id = (static_cast<u64>(maybe_process->pid().value()) + 1) << 36;
  148. return procfs().get_inode({ fsid(), id });
  149. }
  150. return ENOENT;
  151. }
  152. ErrorOr<NonnullRefPtr<Inode>> ProcFSInode::lookup(StringView name)
  153. {
  154. MutexLocker locker(procfs().m_lock);
  155. if (m_type == Type::ProcessSubdirectory) {
  156. VERIFY(m_associated_pid.has_value());
  157. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  158. if (!process)
  159. return ESRCH;
  160. switch (m_subdirectory) {
  161. case process_fd_subdirectory_root_entry.subdirectory:
  162. return process->lookup_file_descriptions_directory(procfs(), name);
  163. case process_stacks_subdirectory_root_entry.subdirectory:
  164. return process->lookup_stacks_directory(procfs(), name);
  165. case process_children_subdirectory_root_entry.subdirectory:
  166. return process->lookup_children_directory(procfs(), name);
  167. default:
  168. VERIFY_NOT_REACHED();
  169. }
  170. VERIFY_NOT_REACHED();
  171. }
  172. if (m_type == Type::RootDirectory) {
  173. return lookup_as_root_directory(name);
  174. }
  175. VERIFY(m_type == Type::ProcessDirectory);
  176. VERIFY(m_associated_pid.has_value());
  177. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  178. if (!process)
  179. return ESRCH;
  180. return process->lookup_as_directory(procfs(), name);
  181. }
  182. ErrorOr<void> ProcFSInode::attach(OpenFileDescription& description)
  183. {
  184. if (m_type == Type::RootDirectory || m_type == Type::SelfProcessLink || m_type == Type::ProcessDirectory || m_type == Type::ProcessSubdirectory)
  185. return {};
  186. VERIFY(m_type == Type::ProcessProperty);
  187. return refresh_process_property_data(description);
  188. }
  189. void ProcFSInode::did_seek(OpenFileDescription& description, off_t offset)
  190. {
  191. if (m_type == Type::SelfProcessLink) {
  192. return;
  193. }
  194. VERIFY(m_type == Type::ProcessProperty);
  195. if (offset != 0)
  196. return;
  197. (void)refresh_process_property_data(description);
  198. }
  199. ErrorOr<size_t> ProcFSInode::read_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
  200. {
  201. dbgln_if(PROCFS_DEBUG, "ProcFSInode: read_bytes_locked offset: {} count: {}", offset, count);
  202. VERIFY(offset >= 0);
  203. VERIFY(buffer.user_or_kernel_ptr());
  204. if (m_type == Type::SelfProcessLink) {
  205. auto builder = TRY(KBufferBuilder::try_create());
  206. TRY(builder.appendff("{}", Process::current().pid().value()));
  207. auto data_buffer = builder.build();
  208. if (!data_buffer)
  209. return Error::from_errno(EFAULT);
  210. if ((size_t)offset >= data_buffer->size())
  211. return 0;
  212. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  213. TRY(buffer.write(data_buffer->data() + offset, nread));
  214. return nread;
  215. }
  216. VERIFY(m_type == Type::ProcessProperty);
  217. if (!description) {
  218. auto builder = TRY(KBufferBuilder::try_create());
  219. VERIFY(m_associated_pid.has_value());
  220. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  221. if (!process)
  222. return Error::from_errno(ESRCH);
  223. TRY(try_fetch_process_property_data(*process, builder));
  224. auto data_buffer = builder.build();
  225. if (!data_buffer)
  226. return Error::from_errno(EFAULT);
  227. if ((size_t)offset >= data_buffer->size())
  228. return 0;
  229. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  230. TRY(buffer.write(data_buffer->data() + offset, nread));
  231. return nread;
  232. }
  233. if (!description->data()) {
  234. dbgln("ProcFS Process Information: Do not have cached data!");
  235. return Error::from_errno(EIO);
  236. }
  237. MutexLocker locker(m_refresh_lock);
  238. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  239. auto& data_buffer = typed_cached_data.buffer;
  240. if (!data_buffer || (size_t)offset >= data_buffer->size())
  241. return 0;
  242. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  243. TRY(buffer.write(data_buffer->data() + offset, nread));
  244. return nread;
  245. }
  246. static ErrorOr<void> build_from_cached_data(KBufferBuilder& builder, ProcFSInodeData& cached_data)
  247. {
  248. cached_data.buffer = builder.build();
  249. if (!cached_data.buffer)
  250. return ENOMEM;
  251. return {};
  252. }
  253. ErrorOr<void> ProcFSInode::try_fetch_process_property_data(NonnullRefPtr<Process> process, KBufferBuilder& builder) const
  254. {
  255. VERIFY(m_type == Type::ProcessProperty);
  256. if (m_subdirectory == process_fd_subdirectory_root_entry.subdirectory) {
  257. // NOTE: All property numbers should start from 1 as 0 is reserved for the directory itself.
  258. // Therefore subtract 1 to get the actual correct fd number.
  259. TRY(process->procfs_get_file_description_link(m_property - 1, builder));
  260. return {};
  261. }
  262. if (m_subdirectory == process_stacks_subdirectory_root_entry.subdirectory) {
  263. // NOTE: All property numbers should start from 1 as 0 is reserved for the directory itself.
  264. // Therefore subtract 1 to get the actual correct thread stack number.
  265. TRY(process->procfs_get_thread_stack(m_property - 1, builder));
  266. return {};
  267. }
  268. if (m_subdirectory == process_children_subdirectory_root_entry.subdirectory) {
  269. // NOTE: All property numbers should start from 1 as 0 is reserved for the directory itself.
  270. // Therefore subtract 1 to get the actual correct child process index number for a correct symlink.
  271. TRY(process->procfs_get_child_process_link(m_property - 1, builder));
  272. return {};
  273. }
  274. VERIFY(m_subdirectory == main_process_directory_root_entry.subdirectory);
  275. switch (m_property) {
  276. case process_unveil_list_entry.property:
  277. return process->procfs_get_unveil_stats(builder);
  278. case process_pledge_list_entry.property:
  279. return process->procfs_get_pledge_stats(builder);
  280. case process_fds_list_entry.property:
  281. return process->procfs_get_fds_stats(builder);
  282. case process_exe_symlink_entry.property:
  283. return process->procfs_get_binary_link(builder);
  284. case process_cwd_symlink_entry.property:
  285. return process->procfs_get_current_work_directory_link(builder);
  286. case process_perf_events_entry.property:
  287. return process->procfs_get_perf_events(builder);
  288. case process_vm_entry.property:
  289. return process->procfs_get_virtual_memory_stats(builder);
  290. case process_cmdline_entry.property:
  291. return process->procfs_get_command_line(builder);
  292. default:
  293. VERIFY_NOT_REACHED();
  294. }
  295. }
  296. ErrorOr<void> ProcFSInode::refresh_process_property_data(OpenFileDescription& description)
  297. {
  298. // For process-specific inodes, hold the process's ptrace lock across refresh
  299. // and refuse to load data if the process is not dumpable.
  300. // Without this, files opened before a process went non-dumpable could still be used for dumping.
  301. VERIFY(m_type == Type::ProcessProperty);
  302. VERIFY(m_associated_pid.has_value());
  303. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  304. if (!process)
  305. return Error::from_errno(ESRCH);
  306. process->ptrace_lock().lock();
  307. if (!process->is_dumpable()) {
  308. process->ptrace_lock().unlock();
  309. return EPERM;
  310. }
  311. ScopeGuard guard = [&] {
  312. process->ptrace_lock().unlock();
  313. };
  314. MutexLocker locker(m_refresh_lock);
  315. auto& cached_data = description.data();
  316. if (!cached_data) {
  317. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  318. if (!cached_data)
  319. return ENOMEM;
  320. }
  321. auto builder = TRY(KBufferBuilder::try_create());
  322. TRY(try_fetch_process_property_data(*process, builder));
  323. return build_from_cached_data(builder, static_cast<ProcFSInodeData&>(*cached_data));
  324. }
  325. InodeMetadata ProcFSInode::metadata() const
  326. {
  327. InodeMetadata metadata;
  328. switch (m_type) {
  329. case Type::SelfProcessLink: {
  330. metadata.inode = { fsid(), 2 };
  331. metadata.mode = S_IFLNK | 0777;
  332. metadata.uid = 0;
  333. metadata.gid = 0;
  334. metadata.size = 0;
  335. metadata.mtime = TimeManagement::boot_time();
  336. break;
  337. }
  338. case Type::RootDirectory: {
  339. metadata.inode = { fsid(), 1 };
  340. metadata.mode = S_IFDIR | 0555;
  341. metadata.uid = 0;
  342. metadata.gid = 0;
  343. metadata.size = 0;
  344. metadata.mtime = TimeManagement::boot_time();
  345. break;
  346. }
  347. case Type::ProcessProperty: {
  348. VERIFY(m_associated_pid.has_value());
  349. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  350. if (!process)
  351. return {};
  352. metadata.inode = identifier();
  353. metadata.mode = determine_procfs_process_inode_mode(m_subdirectory, m_property);
  354. auto credentials = process->credentials();
  355. metadata.uid = credentials->uid();
  356. metadata.gid = credentials->gid();
  357. metadata.size = 0;
  358. metadata.mtime = TimeManagement::now();
  359. break;
  360. }
  361. case Type::ProcessDirectory: {
  362. VERIFY(m_associated_pid.has_value());
  363. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  364. if (!process)
  365. return {};
  366. metadata.inode = identifier();
  367. metadata.mode = S_IFDIR | 0555;
  368. auto credentials = process->credentials();
  369. metadata.uid = credentials->uid();
  370. metadata.gid = credentials->gid();
  371. metadata.size = 0;
  372. metadata.mtime = TimeManagement::now();
  373. break;
  374. }
  375. case Type::ProcessSubdirectory: {
  376. VERIFY(m_associated_pid.has_value());
  377. auto process = Process::from_pid_in_same_jail(m_associated_pid.value());
  378. if (!process)
  379. return {};
  380. metadata.inode = identifier();
  381. metadata.mode = S_IFDIR | 0555;
  382. auto credentials = process->credentials();
  383. metadata.uid = credentials->uid();
  384. metadata.gid = credentials->gid();
  385. metadata.size = 0;
  386. metadata.mtime = TimeManagement::now();
  387. break;
  388. }
  389. }
  390. return metadata;
  391. }
  392. }