VirtualFileSystem.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StringBuilder.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/Devices/BlockDevice.h>
  11. #include <Kernel/FileSystem/Custody.h>
  12. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  13. #include <Kernel/FileSystem/FileDescription.h>
  14. #include <Kernel/FileSystem/FileSystem.h>
  15. #include <Kernel/FileSystem/VirtualFileSystem.h>
  16. #include <Kernel/KSyms.h>
  17. #include <Kernel/Process.h>
  18. #include <LibC/errno_numbers.h>
  19. namespace Kernel {
  20. static AK::Singleton<VFS> s_the;
  21. static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
  22. static constexpr int root_mount_flags = MS_NODEV | MS_NOSUID | MS_RDONLY;
  23. UNMAP_AFTER_INIT void VFS::initialize()
  24. {
  25. s_the.ensure_instance();
  26. }
  27. VFS& VFS::the()
  28. {
  29. return *s_the;
  30. }
  31. UNMAP_AFTER_INIT VFS::VFS()
  32. {
  33. }
  34. UNMAP_AFTER_INIT VFS::~VFS()
  35. {
  36. }
  37. InodeIdentifier VFS::root_inode_id() const
  38. {
  39. VERIFY(m_root_inode);
  40. return m_root_inode->identifier();
  41. }
  42. KResult VFS::mount(FS& file_system, Custody& mount_point, int flags)
  43. {
  44. Locker locker(m_lock);
  45. auto& inode = mount_point.inode();
  46. dbgln("VFS: Mounting {} at {} (inode: {}) with flags {}",
  47. file_system.class_name(),
  48. mount_point.absolute_path(),
  49. inode.identifier(),
  50. flags);
  51. // FIXME: check that this is not already a mount point
  52. Mount mount { file_system, &mount_point, flags };
  53. m_mounts.append(move(mount));
  54. return KSuccess;
  55. }
  56. KResult VFS::bind_mount(Custody& source, Custody& mount_point, int flags)
  57. {
  58. Locker locker(m_lock);
  59. dbgln("VFS: Bind-mounting {} at {}", source.absolute_path(), mount_point.absolute_path());
  60. // FIXME: check that this is not already a mount point
  61. Mount mount { source.inode(), mount_point, flags };
  62. m_mounts.append(move(mount));
  63. return KSuccess;
  64. }
  65. KResult VFS::remount(Custody& mount_point, int new_flags)
  66. {
  67. Locker locker(m_lock);
  68. dbgln("VFS: Remounting {}", mount_point.absolute_path());
  69. Mount* mount = find_mount_for_guest(mount_point.inode());
  70. if (!mount)
  71. return ENODEV;
  72. mount->set_flags(new_flags);
  73. return KSuccess;
  74. }
  75. KResult VFS::unmount(Inode& guest_inode)
  76. {
  77. Locker locker(m_lock);
  78. dbgln("VFS: unmount called with inode {}", guest_inode.identifier());
  79. for (size_t i = 0; i < m_mounts.size(); ++i) {
  80. auto& mount = m_mounts.at(i);
  81. if (&mount.guest() == &guest_inode) {
  82. if (auto result = mount.guest_fs().prepare_to_unmount(); result.is_error()) {
  83. dbgln("VFS: Failed to unmount!");
  84. return result;
  85. }
  86. dbgln("VFS: found fs {} at mount index {}! Unmounting...", mount.guest_fs().fsid(), i);
  87. m_mounts.unstable_take(i);
  88. return KSuccess;
  89. }
  90. }
  91. dbgln("VFS: Nothing mounted on inode {}", guest_inode.identifier());
  92. return ENODEV;
  93. }
  94. bool VFS::mount_root(FS& file_system)
  95. {
  96. if (m_root_inode) {
  97. dmesgln("VFS: mount_root can't mount another root");
  98. return false;
  99. }
  100. Mount mount { file_system, nullptr, root_mount_flags };
  101. auto root_inode = file_system.root_inode();
  102. if (!root_inode->is_directory()) {
  103. dmesgln("VFS: root inode ({}) for / is not a directory :(", root_inode->identifier());
  104. return false;
  105. }
  106. m_root_inode = move(root_inode);
  107. dmesgln("VFS: mounted root from {} ({})", file_system.class_name(), static_cast<FileBackedFS&>(file_system).file_description().absolute_path());
  108. m_mounts.append(move(mount));
  109. auto custody_or_error = Custody::try_create(nullptr, "", *m_root_inode, root_mount_flags);
  110. if (custody_or_error.is_error())
  111. return false;
  112. m_root_custody = custody_or_error.release_value();
  113. return true;
  114. }
  115. auto VFS::find_mount_for_host(Inode& inode) -> Mount*
  116. {
  117. for (auto& mount : m_mounts) {
  118. if (mount.host() == &inode)
  119. return &mount;
  120. }
  121. return nullptr;
  122. }
  123. auto VFS::find_mount_for_host(InodeIdentifier id) -> Mount*
  124. {
  125. for (auto& mount : m_mounts) {
  126. if (mount.host() && mount.host()->identifier() == id)
  127. return &mount;
  128. }
  129. return nullptr;
  130. }
  131. auto VFS::find_mount_for_guest(Inode& inode) -> Mount*
  132. {
  133. for (auto& mount : m_mounts) {
  134. if (&mount.guest() == &inode)
  135. return &mount;
  136. }
  137. return nullptr;
  138. }
  139. auto VFS::find_mount_for_guest(InodeIdentifier id) -> Mount*
  140. {
  141. for (auto& mount : m_mounts) {
  142. if (mount.guest().identifier() == id)
  143. return &mount;
  144. }
  145. return nullptr;
  146. }
  147. bool VFS::is_vfs_root(InodeIdentifier inode) const
  148. {
  149. return inode == root_inode_id();
  150. }
  151. KResult VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntryView&)> callback)
  152. {
  153. return dir_inode.traverse_as_directory([&](auto& entry) {
  154. InodeIdentifier resolved_inode;
  155. if (auto mount = find_mount_for_host(entry.inode))
  156. resolved_inode = mount->guest().identifier();
  157. else
  158. resolved_inode = entry.inode;
  159. // FIXME: This is now broken considering chroot and bind mounts.
  160. bool is_root_inode = dir_inode.identifier() == dir_inode.fs().root_inode()->identifier();
  161. if (is_root_inode && !is_vfs_root(dir_inode.identifier()) && entry.name == "..") {
  162. auto mount = find_mount_for_guest(dir_inode);
  163. VERIFY(mount);
  164. VERIFY(mount->host());
  165. resolved_inode = mount->host()->identifier();
  166. }
  167. callback({ entry.name, resolved_inode, entry.file_type });
  168. return true;
  169. });
  170. }
  171. KResult VFS::utime(StringView path, Custody& base, time_t atime, time_t mtime)
  172. {
  173. auto custody_or_error = VFS::the().resolve_path(move(path), base);
  174. if (custody_or_error.is_error())
  175. return custody_or_error.error();
  176. auto& custody = *custody_or_error.value();
  177. auto& inode = custody.inode();
  178. auto current_process = Process::current();
  179. if (!current_process->is_superuser() && inode.metadata().uid != current_process->euid())
  180. return EACCES;
  181. if (custody.is_readonly())
  182. return EROFS;
  183. if (auto result = inode.set_atime(atime); result.is_error())
  184. return result;
  185. if (auto result = inode.set_mtime(mtime); result.is_error())
  186. return result;
  187. return KSuccess;
  188. }
  189. KResultOr<InodeMetadata> VFS::lookup_metadata(StringView path, Custody& base, int options)
  190. {
  191. auto custody_or_error = resolve_path(path, base, nullptr, options);
  192. if (custody_or_error.is_error())
  193. return custody_or_error.error();
  194. return custody_or_error.value()->inode().metadata();
  195. }
  196. KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> owner)
  197. {
  198. if ((options & O_CREAT) && (options & O_DIRECTORY))
  199. return EINVAL;
  200. RefPtr<Custody> parent_custody;
  201. auto custody_or_error = resolve_path(path, base, &parent_custody, options);
  202. if (custody_or_error.is_error()) {
  203. // NOTE: ENOENT with a non-null parent custody signals us that the immediate parent
  204. // of the file exists, but the file itself does not.
  205. if ((options & O_CREAT) && custody_or_error.error() == -ENOENT && parent_custody)
  206. return create(path, options, mode, *parent_custody, move(owner));
  207. return custody_or_error.error();
  208. }
  209. if ((options & O_CREAT) && (options & O_EXCL))
  210. return EEXIST;
  211. auto& custody = *custody_or_error.value();
  212. auto& inode = custody.inode();
  213. auto metadata = inode.metadata();
  214. if ((options & O_DIRECTORY) && !metadata.is_directory())
  215. return ENOTDIR;
  216. bool should_truncate_file = false;
  217. auto current_process = Process::current();
  218. if ((options & O_RDONLY) && !metadata.may_read(*current_process))
  219. return EACCES;
  220. if (options & O_WRONLY) {
  221. if (!metadata.may_write(*current_process))
  222. return EACCES;
  223. if (metadata.is_directory())
  224. return EISDIR;
  225. should_truncate_file = options & O_TRUNC;
  226. }
  227. if (options & O_EXEC) {
  228. if (!metadata.may_execute(*current_process) || (custody.mount_flags() & MS_NOEXEC))
  229. return EACCES;
  230. }
  231. if (auto preopen_fd = inode.preopen_fd())
  232. return *preopen_fd;
  233. if (metadata.is_fifo()) {
  234. auto fifo = inode.fifo();
  235. if (options & O_WRONLY) {
  236. auto open_result = fifo->open_direction_blocking(FIFO::Direction::Writer);
  237. if (open_result.is_error())
  238. return open_result.error();
  239. auto& description = open_result.value();
  240. description->set_rw_mode(options);
  241. description->set_file_flags(options);
  242. description->set_original_inode({}, inode);
  243. return description;
  244. } else if (options & O_RDONLY) {
  245. auto open_result = fifo->open_direction_blocking(FIFO::Direction::Reader);
  246. if (open_result.is_error())
  247. return open_result.error();
  248. auto& description = open_result.value();
  249. description->set_rw_mode(options);
  250. description->set_file_flags(options);
  251. description->set_original_inode({}, inode);
  252. return description;
  253. }
  254. return EINVAL;
  255. }
  256. if (metadata.is_device()) {
  257. if (custody.mount_flags() & MS_NODEV)
  258. return EACCES;
  259. auto device = Device::get_device(metadata.major_device, metadata.minor_device);
  260. if (device == nullptr) {
  261. return ENODEV;
  262. }
  263. auto descriptor_or_error = device->open(options);
  264. if (descriptor_or_error.is_error())
  265. return descriptor_or_error.error();
  266. descriptor_or_error.value()->set_original_inode({}, inode);
  267. return descriptor_or_error;
  268. }
  269. // Check for read-only FS. Do this after handling preopen FD and devices,
  270. // but before modifying the inode in any way.
  271. if ((options & O_WRONLY) && custody.is_readonly())
  272. return EROFS;
  273. if (should_truncate_file) {
  274. if (auto result = inode.truncate(0); result.is_error())
  275. return result;
  276. if (auto result = inode.set_mtime(kgettimeofday().to_truncated_seconds()); result.is_error())
  277. return result;
  278. }
  279. auto description = FileDescription::create(custody);
  280. if (!description.is_error()) {
  281. description.value()->set_rw_mode(options);
  282. description.value()->set_file_flags(options);
  283. }
  284. return description;
  285. }
  286. KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
  287. {
  288. if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
  289. return EINVAL;
  290. RefPtr<Custody> parent_custody;
  291. auto existing_file_or_error = resolve_path(path, base, &parent_custody);
  292. if (!existing_file_or_error.is_error())
  293. return EEXIST;
  294. if (!parent_custody)
  295. return ENOENT;
  296. if (existing_file_or_error.error() != -ENOENT)
  297. return existing_file_or_error.error();
  298. auto& parent_inode = parent_custody->inode();
  299. auto current_process = Process::current();
  300. if (!parent_inode.metadata().may_write(*current_process))
  301. return EACCES;
  302. if (parent_custody->is_readonly())
  303. return EROFS;
  304. LexicalPath p(path);
  305. dbgln("VFS::mknod: '{}' mode={} dev={} in {}", p.basename(), mode, dev, parent_inode.identifier());
  306. return parent_inode.create_child(p.basename(), mode, dev, current_process->euid(), current_process->egid()).result();
  307. }
  308. KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
  309. {
  310. LexicalPath p(path);
  311. if (auto result = validate_path_against_process_veil(String::formatted("{}/{}", parent_custody.absolute_path(), p.basename()), options); result.is_error())
  312. return result;
  313. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  314. // Turn it into a regular file. (This feels rather hackish.)
  315. mode |= 0100000;
  316. }
  317. auto& parent_inode = parent_custody.inode();
  318. auto current_process = Process::current();
  319. if (!parent_inode.metadata().may_write(*current_process))
  320. return EACCES;
  321. if (parent_custody.is_readonly())
  322. return EROFS;
  323. dbgln_if(VFS_DEBUG, "VFS::create: '{}' in {}", p.basename(), parent_inode.identifier());
  324. uid_t uid = owner.has_value() ? owner.value().uid : current_process->euid();
  325. gid_t gid = owner.has_value() ? owner.value().gid : current_process->egid();
  326. auto inode_or_error = parent_inode.create_child(p.basename(), mode, 0, uid, gid);
  327. if (inode_or_error.is_error())
  328. return inode_or_error.error();
  329. auto new_custody_or_error = Custody::try_create(&parent_custody, p.basename(), inode_or_error.value(), parent_custody.mount_flags());
  330. if (new_custody_or_error.is_error())
  331. return new_custody_or_error.error();
  332. auto description = FileDescription::create(*new_custody_or_error.release_value());
  333. if (!description.is_error()) {
  334. description.value()->set_rw_mode(options);
  335. description.value()->set_file_flags(options);
  336. }
  337. return description;
  338. }
  339. KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
  340. {
  341. // Unlike in basically every other case, where it's only the last
  342. // path component (the one being created) that is allowed not to
  343. // exist, POSIX allows mkdir'ed path to have trailing slashes.
  344. // Let's handle that case by trimming any trailing slashes.
  345. while (path.length() > 1 && path.ends_with("/"))
  346. path = path.substring_view(0, path.length() - 1);
  347. RefPtr<Custody> parent_custody;
  348. if (auto result = resolve_path(path, base, &parent_custody); !result.is_error())
  349. return EEXIST;
  350. else if (!parent_custody)
  351. return ENOENT;
  352. else if (result.error() != -ENOENT)
  353. return result.error();
  354. auto& parent_inode = parent_custody->inode();
  355. auto current_process = Process::current();
  356. if (!parent_inode.metadata().may_write(*current_process))
  357. return EACCES;
  358. if (parent_custody->is_readonly())
  359. return EROFS;
  360. LexicalPath p(path);
  361. dbgln_if(VFS_DEBUG, "VFS::mkdir: '{}' in {}", p.basename(), parent_inode.identifier());
  362. return parent_inode.create_child(p.basename(), S_IFDIR | mode, 0, current_process->euid(), current_process->egid()).result();
  363. }
  364. KResult VFS::access(StringView path, int mode, Custody& base)
  365. {
  366. auto custody_or_error = resolve_path(path, base);
  367. if (custody_or_error.is_error())
  368. return custody_or_error.error();
  369. auto& custody = *custody_or_error.value();
  370. auto& inode = custody.inode();
  371. auto metadata = inode.metadata();
  372. auto current_process = Process::current();
  373. if (mode & R_OK) {
  374. if (!metadata.may_read(*current_process))
  375. return EACCES;
  376. }
  377. if (mode & W_OK) {
  378. if (!metadata.may_write(*current_process))
  379. return EACCES;
  380. if (custody.is_readonly())
  381. return EROFS;
  382. }
  383. if (mode & X_OK) {
  384. if (!metadata.may_execute(*current_process))
  385. return EACCES;
  386. }
  387. return KSuccess;
  388. }
  389. KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
  390. {
  391. auto inode_or_error = resolve_path(path, base);
  392. if (inode_or_error.is_error())
  393. return inode_or_error.error();
  394. auto& custody = *inode_or_error.value();
  395. auto& inode = custody.inode();
  396. if (!inode.is_directory())
  397. return ENOTDIR;
  398. if (!inode.metadata().may_execute(*Process::current()))
  399. return EACCES;
  400. return custody;
  401. }
  402. KResult VFS::chmod(Custody& custody, mode_t mode)
  403. {
  404. auto& inode = custody.inode();
  405. auto current_process = Process::current();
  406. if (current_process->euid() != inode.metadata().uid && !current_process->is_superuser())
  407. return EPERM;
  408. if (custody.is_readonly())
  409. return EROFS;
  410. // Only change the permission bits.
  411. mode = (inode.mode() & ~07777u) | (mode & 07777u);
  412. return inode.chmod(mode);
  413. }
  414. KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
  415. {
  416. auto custody_or_error = resolve_path(path, base);
  417. if (custody_or_error.is_error())
  418. return custody_or_error.error();
  419. auto& custody = *custody_or_error.value();
  420. return chmod(custody, mode);
  421. }
  422. KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
  423. {
  424. RefPtr<Custody> old_parent_custody;
  425. auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody, O_NOFOLLOW_NOERROR);
  426. if (old_custody_or_error.is_error())
  427. return old_custody_or_error.error();
  428. auto& old_custody = *old_custody_or_error.value();
  429. auto& old_inode = old_custody.inode();
  430. RefPtr<Custody> new_parent_custody;
  431. auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
  432. if (new_custody_or_error.is_error()) {
  433. if (new_custody_or_error.error() != -ENOENT || !new_parent_custody)
  434. return new_custody_or_error.error();
  435. }
  436. if (!old_parent_custody || !new_parent_custody) {
  437. return EPERM;
  438. }
  439. auto& old_parent_inode = old_parent_custody->inode();
  440. auto& new_parent_inode = new_parent_custody->inode();
  441. if (&old_parent_inode.fs() != &new_parent_inode.fs())
  442. return EXDEV;
  443. for (auto* new_ancestor = new_parent_custody.ptr(); new_ancestor; new_ancestor = new_ancestor->parent()) {
  444. if (&old_inode == &new_ancestor->inode())
  445. return EDIRINTOSELF;
  446. }
  447. auto current_process = Process::current();
  448. if (!new_parent_inode.metadata().may_write(*current_process))
  449. return EACCES;
  450. if (!old_parent_inode.metadata().may_write(*current_process))
  451. return EACCES;
  452. if (old_parent_inode.metadata().is_sticky()) {
  453. if (!current_process->is_superuser() && old_inode.metadata().uid != current_process->euid())
  454. return EACCES;
  455. }
  456. if (old_parent_custody->is_readonly() || new_parent_custody->is_readonly())
  457. return EROFS;
  458. auto new_basename = LexicalPath(new_path).basename();
  459. if (!new_custody_or_error.is_error()) {
  460. auto& new_custody = *new_custody_or_error.value();
  461. auto& new_inode = new_custody.inode();
  462. // FIXME: Is this really correct? Check what other systems do.
  463. if (&new_inode == &old_inode)
  464. return KSuccess;
  465. if (new_parent_inode.metadata().is_sticky()) {
  466. if (!current_process->is_superuser() && new_inode.metadata().uid != current_process->euid())
  467. return EACCES;
  468. }
  469. if (new_inode.is_directory() && !old_inode.is_directory())
  470. return EISDIR;
  471. if (auto result = new_parent_inode.remove_child(new_basename); result.is_error())
  472. return result;
  473. }
  474. if (auto result = new_parent_inode.add_child(old_inode, new_basename, old_inode.mode()); result.is_error())
  475. return result;
  476. if (auto result = old_parent_inode.remove_child(LexicalPath(old_path).basename()); result.is_error())
  477. return result;
  478. return KSuccess;
  479. }
  480. KResult VFS::chown(Custody& custody, uid_t a_uid, gid_t a_gid)
  481. {
  482. auto& inode = custody.inode();
  483. auto metadata = inode.metadata();
  484. auto current_process = Process::current();
  485. if (current_process->euid() != metadata.uid && !current_process->is_superuser())
  486. return EPERM;
  487. uid_t new_uid = metadata.uid;
  488. gid_t new_gid = metadata.gid;
  489. if (a_uid != (uid_t)-1) {
  490. if (current_process->euid() != a_uid && !current_process->is_superuser())
  491. return EPERM;
  492. new_uid = a_uid;
  493. }
  494. if (a_gid != (gid_t)-1) {
  495. if (!current_process->in_group(a_gid) && !current_process->is_superuser())
  496. return EPERM;
  497. new_gid = a_gid;
  498. }
  499. if (custody.is_readonly())
  500. return EROFS;
  501. dbgln_if(VFS_DEBUG, "VFS::chown(): inode {} <- uid={} gid={}", inode.identifier(), new_uid, new_gid);
  502. if (metadata.is_setuid() || metadata.is_setgid()) {
  503. dbgln_if(VFS_DEBUG, "VFS::chown(): Stripping SUID/SGID bits from {}", inode.identifier());
  504. if (auto result = inode.chmod(metadata.mode & ~(04000 | 02000)); result.is_error())
  505. return result;
  506. }
  507. return inode.chown(new_uid, new_gid);
  508. }
  509. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
  510. {
  511. auto custody_or_error = resolve_path(path, base);
  512. if (custody_or_error.is_error())
  513. return custody_or_error.error();
  514. auto& custody = *custody_or_error.value();
  515. return chown(custody, a_uid, a_gid);
  516. }
  517. static bool hard_link_allowed(const Inode& inode)
  518. {
  519. auto metadata = inode.metadata();
  520. if (Process::current()->euid() == metadata.uid)
  521. return true;
  522. if (metadata.is_regular_file()
  523. && !metadata.is_setuid()
  524. && !(metadata.is_setgid() && metadata.mode & S_IXGRP)
  525. && metadata.may_write(*Process::current())) {
  526. return true;
  527. }
  528. return false;
  529. }
  530. KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
  531. {
  532. auto old_custody_or_error = resolve_path(old_path, base);
  533. if (old_custody_or_error.is_error())
  534. return old_custody_or_error.error();
  535. auto& old_custody = *old_custody_or_error.value();
  536. auto& old_inode = old_custody.inode();
  537. RefPtr<Custody> parent_custody;
  538. auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
  539. if (!new_custody_or_error.is_error())
  540. return EEXIST;
  541. if (!parent_custody)
  542. return ENOENT;
  543. auto& parent_inode = parent_custody->inode();
  544. if (parent_inode.fsid() != old_inode.fsid())
  545. return EXDEV;
  546. if (!parent_inode.metadata().may_write(*Process::current()))
  547. return EACCES;
  548. if (old_inode.is_directory())
  549. return EPERM;
  550. if (parent_custody->is_readonly())
  551. return EROFS;
  552. if (!hard_link_allowed(old_inode))
  553. return EPERM;
  554. return parent_inode.add_child(old_inode, LexicalPath(new_path).basename(), old_inode.mode());
  555. }
  556. KResult VFS::unlink(StringView path, Custody& base)
  557. {
  558. RefPtr<Custody> parent_custody;
  559. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR | O_UNLINK_INTERNAL);
  560. if (custody_or_error.is_error())
  561. return custody_or_error.error();
  562. auto& custody = *custody_or_error.value();
  563. auto& inode = custody.inode();
  564. if (inode.is_directory())
  565. return EISDIR;
  566. // We have just checked that the inode is not a directory, and thus it's not
  567. // the root. So it should have a parent. Note that this would be invalidated
  568. // if we were to support bind-mounting regular files on top of the root.
  569. VERIFY(parent_custody);
  570. auto& parent_inode = parent_custody->inode();
  571. auto current_process = Process::current();
  572. if (!parent_inode.metadata().may_write(*current_process))
  573. return EACCES;
  574. if (parent_inode.metadata().is_sticky()) {
  575. if (!current_process->is_superuser() && inode.metadata().uid != current_process->euid())
  576. return EACCES;
  577. }
  578. if (parent_custody->is_readonly())
  579. return EROFS;
  580. if (auto result = parent_inode.remove_child(LexicalPath(path).basename()); result.is_error())
  581. return result;
  582. return KSuccess;
  583. }
  584. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  585. {
  586. RefPtr<Custody> parent_custody;
  587. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  588. if (!existing_custody_or_error.is_error())
  589. return EEXIST;
  590. if (!parent_custody)
  591. return ENOENT;
  592. if (existing_custody_or_error.error() != -ENOENT)
  593. return existing_custody_or_error.error();
  594. auto& parent_inode = parent_custody->inode();
  595. auto current_process = Process::current();
  596. if (!parent_inode.metadata().may_write(*current_process))
  597. return EACCES;
  598. if (parent_custody->is_readonly())
  599. return EROFS;
  600. LexicalPath p(linkpath);
  601. dbgln_if(VFS_DEBUG, "VFS::symlink: '{}' (-> '{}') in {}", p.basename(), target, parent_inode.identifier());
  602. auto inode_or_error = parent_inode.create_child(p.basename(), S_IFLNK | 0644, 0, current_process->euid(), current_process->egid());
  603. if (inode_or_error.is_error())
  604. return inode_or_error.error();
  605. auto& inode = inode_or_error.value();
  606. auto target_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)target.characters_without_null_termination()));
  607. auto result = inode->write_bytes(0, target.length(), target_buffer, nullptr);
  608. if (result.is_error())
  609. return result.error();
  610. return KSuccess;
  611. }
  612. KResult VFS::rmdir(StringView path, Custody& base)
  613. {
  614. RefPtr<Custody> parent_custody;
  615. auto custody_or_error = resolve_path(path, base, &parent_custody);
  616. if (custody_or_error.is_error())
  617. return KResult(custody_or_error.error());
  618. auto& custody = *custody_or_error.value();
  619. auto& inode = custody.inode();
  620. // FIXME: We should return EINVAL if the last component of the path is "."
  621. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  622. if (!inode.is_directory())
  623. return ENOTDIR;
  624. if (!parent_custody)
  625. return EBUSY;
  626. auto& parent_inode = parent_custody->inode();
  627. auto parent_metadata = parent_inode.metadata();
  628. if (!parent_metadata.may_write(*Process::current()))
  629. return EACCES;
  630. if (parent_metadata.is_sticky()) {
  631. if (!Process::current()->is_superuser() && inode.metadata().uid != Process::current()->euid())
  632. return EACCES;
  633. }
  634. KResultOr<size_t> dir_count_result = inode.directory_entry_count();
  635. if (dir_count_result.is_error())
  636. return dir_count_result.result();
  637. if (dir_count_result.value() != 2)
  638. return ENOTEMPTY;
  639. if (custody.is_readonly())
  640. return EROFS;
  641. if (auto result = inode.remove_child("."); result.is_error())
  642. return result;
  643. if (auto result = inode.remove_child(".."); result.is_error())
  644. return result;
  645. return parent_inode.remove_child(LexicalPath(path).basename());
  646. }
  647. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  648. : m_guest(guest_fs.root_inode())
  649. , m_guest_fs(guest_fs)
  650. , m_host_custody(host_custody)
  651. , m_flags(flags)
  652. {
  653. }
  654. VFS::Mount::Mount(Inode& source, Custody& host_custody, int flags)
  655. : m_guest(source)
  656. , m_guest_fs(source.fs())
  657. , m_host_custody(host_custody)
  658. , m_flags(flags)
  659. {
  660. }
  661. String VFS::Mount::absolute_path() const
  662. {
  663. if (!m_host_custody)
  664. return "/";
  665. return m_host_custody->absolute_path();
  666. }
  667. Inode* VFS::Mount::host()
  668. {
  669. if (!m_host_custody)
  670. return nullptr;
  671. return &m_host_custody->inode();
  672. }
  673. const Inode* VFS::Mount::host() const
  674. {
  675. if (!m_host_custody)
  676. return nullptr;
  677. return &m_host_custody->inode();
  678. }
  679. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  680. {
  681. for (auto& mount : m_mounts) {
  682. callback(mount);
  683. }
  684. }
  685. void VFS::sync()
  686. {
  687. FS::sync();
  688. }
  689. Custody& VFS::root_custody()
  690. {
  691. return *m_root_custody;
  692. }
  693. const UnveilNode* VFS::find_matching_unveiled_path(StringView path)
  694. {
  695. auto& unveil_root = Process::current()->unveiled_paths();
  696. if (unveil_root.is_empty())
  697. return nullptr;
  698. LexicalPath lexical_path { path };
  699. auto& path_parts = lexical_path.parts();
  700. auto& last_matching_node = unveil_root.traverse_until_last_accessible_node(path_parts.begin(), path_parts.end());
  701. return &last_matching_node;
  702. }
  703. KResult VFS::validate_path_against_process_veil(StringView path, int options)
  704. {
  705. if (Process::current()->veil_state() == VeilState::None)
  706. return KSuccess;
  707. if (path == "/usr/lib/Loader.so")
  708. return KSuccess;
  709. // FIXME: Figure out a nicer way to do this.
  710. if (String(path).contains("/.."))
  711. return EINVAL;
  712. auto* unveiled_path = find_matching_unveiled_path(path);
  713. if (!unveiled_path || unveiled_path->permissions() == UnveilAccess::None) {
  714. dbgln("Rejecting path '{}' since it hasn't been unveiled.", path);
  715. dump_backtrace();
  716. return ENOENT;
  717. }
  718. if (options & O_CREAT) {
  719. if (!(unveiled_path->permissions() & UnveilAccess::CreateOrRemove)) {
  720. dbgln("Rejecting path '{}' since it hasn't been unveiled with 'c' permission.", path);
  721. dump_backtrace();
  722. return EACCES;
  723. }
  724. }
  725. if (options & O_UNLINK_INTERNAL) {
  726. if (!(unveiled_path->permissions() & UnveilAccess::CreateOrRemove)) {
  727. dbgln("Rejecting path '{}' for unlink since it hasn't been unveiled with 'c' permission.", path);
  728. dump_backtrace();
  729. return EACCES;
  730. }
  731. return KSuccess;
  732. }
  733. if (options & O_RDONLY) {
  734. if (options & O_DIRECTORY) {
  735. if (!(unveiled_path->permissions() & (UnveilAccess::Read | UnveilAccess::Browse))) {
  736. dbgln("Rejecting path '{}' since it hasn't been unveiled with 'r' or 'b' permissions.", path);
  737. dump_backtrace();
  738. return EACCES;
  739. }
  740. } else {
  741. if (!(unveiled_path->permissions() & UnveilAccess::Read)) {
  742. dbgln("Rejecting path '{}' since it hasn't been unveiled with 'r' permission.", path);
  743. dump_backtrace();
  744. return EACCES;
  745. }
  746. }
  747. }
  748. if (options & O_WRONLY) {
  749. if (!(unveiled_path->permissions() & UnveilAccess::Write)) {
  750. dbgln("Rejecting path '{}' since it hasn't been unveiled with 'w' permission.", path);
  751. dump_backtrace();
  752. return EACCES;
  753. }
  754. }
  755. if (options & O_EXEC) {
  756. if (!(unveiled_path->permissions() & UnveilAccess::Execute)) {
  757. dbgln("Rejecting path '{}' since it hasn't been unveiled with 'x' permission.", path);
  758. dump_backtrace();
  759. return EACCES;
  760. }
  761. }
  762. return KSuccess;
  763. }
  764. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  765. {
  766. auto custody_or_error = resolve_path_without_veil(path, base, out_parent, options, symlink_recursion_level);
  767. if (custody_or_error.is_error())
  768. return custody_or_error.error();
  769. auto& custody = custody_or_error.value();
  770. if (auto result = validate_path_against_process_veil(custody->absolute_path(), options); result.is_error())
  771. return result;
  772. return custody;
  773. }
  774. static bool safe_to_follow_symlink(const Inode& inode, const InodeMetadata& parent_metadata)
  775. {
  776. auto metadata = inode.metadata();
  777. if (Process::current()->euid() == metadata.uid)
  778. return true;
  779. if (!(parent_metadata.is_sticky() && parent_metadata.mode & S_IWOTH))
  780. return true;
  781. if (metadata.uid == parent_metadata.uid)
  782. return true;
  783. return false;
  784. }
  785. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  786. {
  787. if (symlink_recursion_level >= symlink_recursion_limit)
  788. return ELOOP;
  789. if (path.is_empty())
  790. return EINVAL;
  791. GenericLexer path_lexer(path);
  792. auto current_process = Process::current();
  793. auto& current_root = current_process->root_directory();
  794. NonnullRefPtr<Custody> custody = path[0] == '/' ? current_root : base;
  795. bool extra_iteration = path[path.length() - 1] == '/';
  796. while (!path_lexer.is_eof() || extra_iteration) {
  797. if (path_lexer.is_eof())
  798. extra_iteration = false;
  799. auto part = path_lexer.consume_until('/');
  800. path_lexer.consume_specific('/');
  801. Custody& parent = custody;
  802. auto parent_metadata = parent.inode().metadata();
  803. if (!parent_metadata.is_directory())
  804. return ENOTDIR;
  805. // Ensure the current user is allowed to resolve paths inside this directory.
  806. if (!parent_metadata.may_execute(*current_process))
  807. return EACCES;
  808. bool have_more_parts = !path_lexer.is_eof() || extra_iteration;
  809. if (part == "..") {
  810. // If we encounter a "..", take a step back, but don't go beyond the root.
  811. if (custody->parent())
  812. custody = *custody->parent();
  813. continue;
  814. } else if (part == "." || part.is_empty()) {
  815. continue;
  816. }
  817. // Okay, let's look up this part.
  818. auto child_inode = parent.inode().lookup(part);
  819. if (!child_inode) {
  820. if (out_parent) {
  821. // ENOENT with a non-null parent custody signals to caller that
  822. // we found the immediate parent of the file, but the file itself
  823. // does not exist yet.
  824. *out_parent = have_more_parts ? nullptr : &parent;
  825. }
  826. return ENOENT;
  827. }
  828. int mount_flags_for_child = parent.mount_flags();
  829. // See if there's something mounted on the child; in that case
  830. // we would need to return the guest inode, not the host inode.
  831. if (auto mount = find_mount_for_host(*child_inode)) {
  832. child_inode = mount->guest();
  833. mount_flags_for_child = mount->flags();
  834. }
  835. auto new_custody_or_error = Custody::try_create(&parent, part, *child_inode, mount_flags_for_child);
  836. if (new_custody_or_error.is_error())
  837. return new_custody_or_error.error();
  838. custody = new_custody_or_error.release_value();
  839. if (child_inode->metadata().is_symlink()) {
  840. if (!have_more_parts) {
  841. if (options & O_NOFOLLOW)
  842. return ELOOP;
  843. if (options & O_NOFOLLOW_NOERROR)
  844. break;
  845. }
  846. if (!safe_to_follow_symlink(*child_inode, parent_metadata))
  847. return EACCES;
  848. if (auto result = validate_path_against_process_veil(custody->absolute_path(), options); result.is_error())
  849. return result;
  850. auto symlink_target = child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1);
  851. if (symlink_target.is_error() || !have_more_parts)
  852. return symlink_target;
  853. // Now, resolve the remaining path relative to the symlink target.
  854. // We prepend a "." to it to ensure that it's not empty and that
  855. // any initial slashes it might have get interpreted properly.
  856. StringBuilder remaining_path;
  857. remaining_path.append('.');
  858. remaining_path.append(path.substring_view_starting_after_substring(part));
  859. return resolve_path_without_veil(remaining_path.to_string(), *symlink_target.value(), out_parent, options, symlink_recursion_level + 1);
  860. }
  861. }
  862. if (out_parent)
  863. *out_parent = custody->parent();
  864. return custody;
  865. }
  866. }