VirtualFileSystem.cpp 31 KB

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