VirtualFileSystem.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. return true;
  110. }
  111. auto VFS::find_mount_for_host(Inode& inode) -> Mount*
  112. {
  113. for (auto& mount : m_mounts) {
  114. if (mount.host() == &inode)
  115. return &mount;
  116. }
  117. return nullptr;
  118. }
  119. auto VFS::find_mount_for_host(InodeIdentifier id) -> Mount*
  120. {
  121. for (auto& mount : m_mounts) {
  122. if (mount.host() && mount.host()->identifier() == id)
  123. return &mount;
  124. }
  125. return nullptr;
  126. }
  127. auto VFS::find_mount_for_guest(Inode& inode) -> Mount*
  128. {
  129. for (auto& mount : m_mounts) {
  130. if (&mount.guest() == &inode)
  131. return &mount;
  132. }
  133. return nullptr;
  134. }
  135. auto VFS::find_mount_for_guest(InodeIdentifier id) -> Mount*
  136. {
  137. for (auto& mount : m_mounts) {
  138. if (mount.guest().identifier() == id)
  139. return &mount;
  140. }
  141. return nullptr;
  142. }
  143. bool VFS::is_vfs_root(InodeIdentifier inode) const
  144. {
  145. return inode == root_inode_id();
  146. }
  147. KResult VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntryView&)> callback)
  148. {
  149. return dir_inode.traverse_as_directory([&](auto& entry) {
  150. InodeIdentifier resolved_inode;
  151. if (auto mount = find_mount_for_host(entry.inode))
  152. resolved_inode = mount->guest().identifier();
  153. else
  154. resolved_inode = entry.inode;
  155. // FIXME: This is now broken considering chroot and bind mounts.
  156. bool is_root_inode = dir_inode.identifier() == dir_inode.fs().root_inode()->identifier();
  157. if (is_root_inode && !is_vfs_root(dir_inode.identifier()) && entry.name == "..") {
  158. auto mount = find_mount_for_guest(dir_inode);
  159. VERIFY(mount);
  160. VERIFY(mount->host());
  161. resolved_inode = mount->host()->identifier();
  162. }
  163. callback({ entry.name, resolved_inode, entry.file_type });
  164. return true;
  165. });
  166. }
  167. KResult VFS::utime(StringView path, Custody& base, time_t atime, time_t mtime)
  168. {
  169. auto custody_or_error = VFS::the().resolve_path(move(path), base);
  170. if (custody_or_error.is_error())
  171. return custody_or_error.error();
  172. auto& custody = *custody_or_error.value();
  173. auto& inode = custody.inode();
  174. auto current_process = Process::current();
  175. if (!current_process->is_superuser() && inode.metadata().uid != current_process->euid())
  176. return EACCES;
  177. if (custody.is_readonly())
  178. return EROFS;
  179. if (auto result = inode.set_atime(atime); result.is_error())
  180. return result;
  181. if (auto result = inode.set_mtime(mtime); result.is_error())
  182. return result;
  183. return KSuccess;
  184. }
  185. KResultOr<InodeMetadata> VFS::lookup_metadata(StringView path, Custody& base, int options)
  186. {
  187. auto custody_or_error = resolve_path(path, base, nullptr, options);
  188. if (custody_or_error.is_error())
  189. return custody_or_error.error();
  190. return custody_or_error.value()->inode().metadata();
  191. }
  192. KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> owner)
  193. {
  194. if ((options & O_CREAT) && (options & O_DIRECTORY))
  195. return EINVAL;
  196. RefPtr<Custody> parent_custody;
  197. auto custody_or_error = resolve_path(path, base, &parent_custody, options);
  198. if (options & O_CREAT) {
  199. if (!parent_custody)
  200. return ENOENT;
  201. if (custody_or_error.is_error()) {
  202. if (custody_or_error.error() != -ENOENT)
  203. return custody_or_error.error();
  204. return create(path, options, mode, *parent_custody, move(owner));
  205. }
  206. if (options & O_EXCL)
  207. return EEXIST;
  208. }
  209. if (custody_or_error.is_error())
  210. return custody_or_error.error();
  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 = Custody::create(&parent_custody, p.basename(), inode_or_error.value(), parent_custody.mount_flags());
  330. auto description = FileDescription::create(*new_custody);
  331. if (!description.is_error()) {
  332. description.value()->set_rw_mode(options);
  333. description.value()->set_file_flags(options);
  334. }
  335. return description;
  336. }
  337. KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
  338. {
  339. // Unlike in basically every other case, where it's only the last
  340. // path component (the one being created) that is allowed not to
  341. // exist, POSIX allows mkdir'ed path to have trailing slashes.
  342. // Let's handle that case by trimming any trailing slashes.
  343. while (path.length() > 1 && path.ends_with("/"))
  344. path = path.substring_view(0, path.length() - 1);
  345. RefPtr<Custody> parent_custody;
  346. if (auto result = resolve_path(path, base, &parent_custody); !result.is_error())
  347. return EEXIST;
  348. else if (!parent_custody)
  349. return ENOENT;
  350. else if (result.error() != -ENOENT)
  351. return result.error();
  352. auto& parent_inode = parent_custody->inode();
  353. auto current_process = Process::current();
  354. if (!parent_inode.metadata().may_write(*current_process))
  355. return EACCES;
  356. if (parent_custody->is_readonly())
  357. return EROFS;
  358. LexicalPath p(path);
  359. dbgln_if(VFS_DEBUG, "VFS::mkdir: '{}' in {}", p.basename(), parent_inode.identifier());
  360. return parent_inode.create_child(p.basename(), S_IFDIR | mode, 0, current_process->euid(), current_process->egid()).result();
  361. }
  362. KResult VFS::access(StringView path, int mode, Custody& base)
  363. {
  364. auto custody_or_error = resolve_path(path, base);
  365. if (custody_or_error.is_error())
  366. return custody_or_error.error();
  367. auto& custody = *custody_or_error.value();
  368. auto& inode = custody.inode();
  369. auto metadata = inode.metadata();
  370. auto current_process = Process::current();
  371. if (mode & R_OK) {
  372. if (!metadata.may_read(*current_process))
  373. return EACCES;
  374. }
  375. if (mode & W_OK) {
  376. if (!metadata.may_write(*current_process))
  377. return EACCES;
  378. if (custody.is_readonly())
  379. return EROFS;
  380. }
  381. if (mode & X_OK) {
  382. if (!metadata.may_execute(*current_process))
  383. return EACCES;
  384. }
  385. return KSuccess;
  386. }
  387. KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
  388. {
  389. auto inode_or_error = resolve_path(path, base);
  390. if (inode_or_error.is_error())
  391. return inode_or_error.error();
  392. auto& custody = *inode_or_error.value();
  393. auto& inode = custody.inode();
  394. if (!inode.is_directory())
  395. return ENOTDIR;
  396. if (!inode.metadata().may_execute(*Process::current()))
  397. return EACCES;
  398. return custody;
  399. }
  400. KResult VFS::chmod(Custody& custody, mode_t mode)
  401. {
  402. auto& inode = custody.inode();
  403. auto current_process = Process::current();
  404. if (current_process->euid() != inode.metadata().uid && !current_process->is_superuser())
  405. return EPERM;
  406. if (custody.is_readonly())
  407. return EROFS;
  408. // Only change the permission bits.
  409. mode = (inode.mode() & ~07777u) | (mode & 07777u);
  410. return inode.chmod(mode);
  411. }
  412. KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
  413. {
  414. auto custody_or_error = resolve_path(path, base);
  415. if (custody_or_error.is_error())
  416. return custody_or_error.error();
  417. auto& custody = *custody_or_error.value();
  418. return chmod(custody, mode);
  419. }
  420. KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
  421. {
  422. RefPtr<Custody> old_parent_custody;
  423. auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody, O_NOFOLLOW_NOERROR);
  424. if (old_custody_or_error.is_error())
  425. return old_custody_or_error.error();
  426. auto& old_custody = *old_custody_or_error.value();
  427. auto& old_inode = old_custody.inode();
  428. RefPtr<Custody> new_parent_custody;
  429. auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
  430. if (new_custody_or_error.is_error()) {
  431. if (new_custody_or_error.error() != -ENOENT || !new_parent_custody)
  432. return new_custody_or_error.error();
  433. }
  434. if (!old_parent_custody || !new_parent_custody) {
  435. return EPERM;
  436. }
  437. auto& old_parent_inode = old_parent_custody->inode();
  438. auto& new_parent_inode = new_parent_custody->inode();
  439. if (&old_parent_inode.fs() != &new_parent_inode.fs())
  440. return EXDEV;
  441. for (auto* new_ancestor = new_parent_custody.ptr(); new_ancestor; new_ancestor = new_ancestor->parent()) {
  442. if (&old_inode == &new_ancestor->inode())
  443. return EDIRINTOSELF;
  444. }
  445. auto current_process = Process::current();
  446. if (!new_parent_inode.metadata().may_write(*current_process))
  447. return EACCES;
  448. if (!old_parent_inode.metadata().may_write(*current_process))
  449. return EACCES;
  450. if (old_parent_inode.metadata().is_sticky()) {
  451. if (!current_process->is_superuser() && old_inode.metadata().uid != current_process->euid())
  452. return EACCES;
  453. }
  454. if (old_parent_custody->is_readonly() || new_parent_custody->is_readonly())
  455. return EROFS;
  456. auto new_basename = LexicalPath(new_path).basename();
  457. if (!new_custody_or_error.is_error()) {
  458. auto& new_custody = *new_custody_or_error.value();
  459. auto& new_inode = new_custody.inode();
  460. // FIXME: Is this really correct? Check what other systems do.
  461. if (&new_inode == &old_inode)
  462. return KSuccess;
  463. if (new_parent_inode.metadata().is_sticky()) {
  464. if (!current_process->is_superuser() && new_inode.metadata().uid != current_process->euid())
  465. return EACCES;
  466. }
  467. if (new_inode.is_directory() && !old_inode.is_directory())
  468. return EISDIR;
  469. if (auto result = new_parent_inode.remove_child(new_basename); result.is_error())
  470. return result;
  471. }
  472. if (auto result = new_parent_inode.add_child(old_inode, new_basename, old_inode.mode()); result.is_error())
  473. return result;
  474. if (auto result = old_parent_inode.remove_child(LexicalPath(old_path).basename()); result.is_error())
  475. return result;
  476. return KSuccess;
  477. }
  478. KResult VFS::chown(Custody& custody, uid_t a_uid, gid_t a_gid)
  479. {
  480. auto& inode = custody.inode();
  481. auto metadata = inode.metadata();
  482. auto current_process = Process::current();
  483. if (current_process->euid() != metadata.uid && !current_process->is_superuser())
  484. return EPERM;
  485. uid_t new_uid = metadata.uid;
  486. gid_t new_gid = metadata.gid;
  487. if (a_uid != (uid_t)-1) {
  488. if (current_process->euid() != a_uid && !current_process->is_superuser())
  489. return EPERM;
  490. new_uid = a_uid;
  491. }
  492. if (a_gid != (gid_t)-1) {
  493. if (!current_process->in_group(a_gid) && !current_process->is_superuser())
  494. return EPERM;
  495. new_gid = a_gid;
  496. }
  497. if (custody.is_readonly())
  498. return EROFS;
  499. dbgln_if(VFS_DEBUG, "VFS::chown(): inode {} <- uid={} gid={}", inode.identifier(), new_uid, new_gid);
  500. if (metadata.is_setuid() || metadata.is_setgid()) {
  501. dbgln_if(VFS_DEBUG, "VFS::chown(): Stripping SUID/SGID bits from {}", inode.identifier());
  502. if (auto result = inode.chmod(metadata.mode & ~(04000 | 02000)); result.is_error())
  503. return result;
  504. }
  505. return inode.chown(new_uid, new_gid);
  506. }
  507. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
  508. {
  509. auto custody_or_error = resolve_path(path, base);
  510. if (custody_or_error.is_error())
  511. return custody_or_error.error();
  512. auto& custody = *custody_or_error.value();
  513. return chown(custody, a_uid, a_gid);
  514. }
  515. static bool hard_link_allowed(const Inode& inode)
  516. {
  517. auto metadata = inode.metadata();
  518. if (Process::current()->euid() == metadata.uid)
  519. return true;
  520. if (metadata.is_regular_file()
  521. && !metadata.is_setuid()
  522. && !(metadata.is_setgid() && metadata.mode & S_IXGRP)
  523. && metadata.may_write(*Process::current())) {
  524. return true;
  525. }
  526. return false;
  527. }
  528. KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
  529. {
  530. auto old_custody_or_error = resolve_path(old_path, base);
  531. if (old_custody_or_error.is_error())
  532. return old_custody_or_error.error();
  533. auto& old_custody = *old_custody_or_error.value();
  534. auto& old_inode = old_custody.inode();
  535. RefPtr<Custody> parent_custody;
  536. auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
  537. if (!new_custody_or_error.is_error())
  538. return EEXIST;
  539. if (!parent_custody)
  540. return ENOENT;
  541. auto& parent_inode = parent_custody->inode();
  542. if (parent_inode.fsid() != old_inode.fsid())
  543. return EXDEV;
  544. if (!parent_inode.metadata().may_write(*Process::current()))
  545. return EACCES;
  546. if (old_inode.is_directory())
  547. return EPERM;
  548. if (parent_custody->is_readonly())
  549. return EROFS;
  550. if (!hard_link_allowed(old_inode))
  551. return EPERM;
  552. return parent_inode.add_child(old_inode, LexicalPath(new_path).basename(), old_inode.mode());
  553. }
  554. KResult VFS::unlink(StringView path, Custody& base)
  555. {
  556. RefPtr<Custody> parent_custody;
  557. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR | O_UNLINK_INTERNAL);
  558. if (custody_or_error.is_error())
  559. return custody_or_error.error();
  560. auto& custody = *custody_or_error.value();
  561. auto& inode = custody.inode();
  562. if (inode.is_directory())
  563. return EISDIR;
  564. // We have just checked that the inode is not a directory, and thus it's not
  565. // the root. So it should have a parent. Note that this would be invalidated
  566. // if we were to support bind-mounting regular files on top of the root.
  567. VERIFY(parent_custody);
  568. auto& parent_inode = parent_custody->inode();
  569. auto current_process = Process::current();
  570. if (!parent_inode.metadata().may_write(*current_process))
  571. return EACCES;
  572. if (parent_inode.metadata().is_sticky()) {
  573. if (!current_process->is_superuser() && inode.metadata().uid != current_process->euid())
  574. return EACCES;
  575. }
  576. if (parent_custody->is_readonly())
  577. return EROFS;
  578. if (auto result = parent_inode.remove_child(LexicalPath(path).basename()); result.is_error())
  579. return result;
  580. return KSuccess;
  581. }
  582. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  583. {
  584. RefPtr<Custody> parent_custody;
  585. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  586. if (!existing_custody_or_error.is_error())
  587. return EEXIST;
  588. if (!parent_custody)
  589. return ENOENT;
  590. if (existing_custody_or_error.error() != -ENOENT)
  591. return existing_custody_or_error.error();
  592. auto& parent_inode = parent_custody->inode();
  593. auto current_process = Process::current();
  594. if (!parent_inode.metadata().may_write(*current_process))
  595. return EACCES;
  596. if (parent_custody->is_readonly())
  597. return EROFS;
  598. LexicalPath p(linkpath);
  599. dbgln_if(VFS_DEBUG, "VFS::symlink: '{}' (-> '{}') in {}", p.basename(), target, parent_inode.identifier());
  600. auto inode_or_error = parent_inode.create_child(p.basename(), S_IFLNK | 0644, 0, current_process->euid(), current_process->egid());
  601. if (inode_or_error.is_error())
  602. return inode_or_error.error();
  603. auto& inode = inode_or_error.value();
  604. auto target_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)target.characters_without_null_termination()));
  605. auto result = inode->write_bytes(0, target.length(), target_buffer, nullptr);
  606. if (result.is_error())
  607. return result.error();
  608. return KSuccess;
  609. }
  610. KResult VFS::rmdir(StringView path, Custody& base)
  611. {
  612. RefPtr<Custody> parent_custody;
  613. auto custody_or_error = resolve_path(path, base, &parent_custody);
  614. if (custody_or_error.is_error())
  615. return KResult(custody_or_error.error());
  616. auto& custody = *custody_or_error.value();
  617. auto& inode = custody.inode();
  618. // FIXME: We should return EINVAL if the last component of the path is "."
  619. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  620. if (!inode.is_directory())
  621. return ENOTDIR;
  622. if (!parent_custody)
  623. return EBUSY;
  624. auto& parent_inode = parent_custody->inode();
  625. auto parent_metadata = parent_inode.metadata();
  626. if (!parent_metadata.may_write(*Process::current()))
  627. return EACCES;
  628. if (parent_metadata.is_sticky()) {
  629. if (!Process::current()->is_superuser() && inode.metadata().uid != Process::current()->euid())
  630. return EACCES;
  631. }
  632. KResultOr<size_t> dir_count_result = inode.directory_entry_count();
  633. if (dir_count_result.is_error())
  634. return dir_count_result.result();
  635. if (dir_count_result.value() != 2)
  636. return ENOTEMPTY;
  637. if (custody.is_readonly())
  638. return EROFS;
  639. if (auto result = inode.remove_child("."); result.is_error())
  640. return result;
  641. if (auto result = inode.remove_child(".."); result.is_error())
  642. return result;
  643. return parent_inode.remove_child(LexicalPath(path).basename());
  644. }
  645. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  646. : m_guest(guest_fs.root_inode())
  647. , m_guest_fs(guest_fs)
  648. , m_host_custody(host_custody)
  649. , m_flags(flags)
  650. {
  651. }
  652. VFS::Mount::Mount(Inode& source, Custody& host_custody, int flags)
  653. : m_guest(source)
  654. , m_guest_fs(source.fs())
  655. , m_host_custody(host_custody)
  656. , m_flags(flags)
  657. {
  658. }
  659. String VFS::Mount::absolute_path() const
  660. {
  661. if (!m_host_custody)
  662. return "/";
  663. return m_host_custody->absolute_path();
  664. }
  665. Inode* VFS::Mount::host()
  666. {
  667. if (!m_host_custody)
  668. return nullptr;
  669. return &m_host_custody->inode();
  670. }
  671. const Inode* VFS::Mount::host() const
  672. {
  673. if (!m_host_custody)
  674. return nullptr;
  675. return &m_host_custody->inode();
  676. }
  677. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  678. {
  679. for (auto& mount : m_mounts) {
  680. callback(mount);
  681. }
  682. }
  683. void VFS::sync()
  684. {
  685. FS::sync();
  686. }
  687. Custody& VFS::root_custody()
  688. {
  689. if (!m_root_custody)
  690. m_root_custody = Custody::create(nullptr, "", *m_root_inode, root_mount_flags);
  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) {
  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. auto parts = path.split_view('/', true);
  792. auto current_process = Process::current();
  793. auto& current_root = current_process->root_directory();
  794. NonnullRefPtr<Custody> custody = path[0] == '/' ? current_root : base;
  795. for (size_t i = 0; i < parts.size(); ++i) {
  796. Custody& parent = custody;
  797. auto parent_metadata = parent.inode().metadata();
  798. if (!parent_metadata.is_directory())
  799. return ENOTDIR;
  800. // Ensure the current user is allowed to resolve paths inside this directory.
  801. if (!parent_metadata.may_execute(*current_process))
  802. return EACCES;
  803. auto& part = parts[i];
  804. bool have_more_parts = i + 1 < parts.size();
  805. if (part == "..") {
  806. // If we encounter a "..", take a step back, but don't go beyond the root.
  807. if (custody->parent())
  808. custody = *custody->parent();
  809. continue;
  810. } else if (part == "." || part.is_empty()) {
  811. continue;
  812. }
  813. // Okay, let's look up this part.
  814. auto child_inode = parent.inode().lookup(part);
  815. if (!child_inode) {
  816. if (out_parent) {
  817. // ENOENT with a non-null parent custody signals to caller that
  818. // we found the immediate parent of the file, but the file itself
  819. // does not exist yet.
  820. *out_parent = have_more_parts ? nullptr : &parent;
  821. }
  822. return ENOENT;
  823. }
  824. int mount_flags_for_child = parent.mount_flags();
  825. // See if there's something mounted on the child; in that case
  826. // we would need to return the guest inode, not the host inode.
  827. if (auto mount = find_mount_for_host(*child_inode)) {
  828. child_inode = mount->guest();
  829. mount_flags_for_child = mount->flags();
  830. }
  831. custody = Custody::create(&parent, part, *child_inode, mount_flags_for_child);
  832. if (child_inode->metadata().is_symlink()) {
  833. if (!have_more_parts) {
  834. if (options & O_NOFOLLOW)
  835. return ELOOP;
  836. if (options & O_NOFOLLOW_NOERROR)
  837. break;
  838. }
  839. if (!safe_to_follow_symlink(*child_inode, parent_metadata))
  840. return EACCES;
  841. if (auto result = validate_path_against_process_veil(custody->absolute_path(), options); result.is_error())
  842. return result;
  843. auto symlink_target = child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1);
  844. if (symlink_target.is_error() || !have_more_parts)
  845. return symlink_target;
  846. // Now, resolve the remaining path relative to the symlink target.
  847. // We prepend a "." to it to ensure that it's not empty and that
  848. // any initial slashes it might have get interpreted properly.
  849. StringBuilder remaining_path;
  850. remaining_path.append('.');
  851. remaining_path.append(path.substring_view_starting_after_substring(part));
  852. return resolve_path_without_veil(remaining_path.to_string(), *symlink_target.value(), out_parent, options, symlink_recursion_level + 1);
  853. }
  854. }
  855. if (out_parent)
  856. *out_parent = custody->parent();
  857. return custody;
  858. }
  859. }