VirtualFileSystem.cpp 30 KB

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