VirtualFileSystem.cpp 32 KB

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