VirtualFileSystem.cpp 30 KB

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