VirtualFileSystem.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  270. // Turn it into a regular file. (This feels rather hackish.)
  271. mode |= 0100000;
  272. }
  273. auto& parent_inode = parent_custody.inode();
  274. if (!parent_inode.metadata().may_write(*Process::current))
  275. return KResult(-EACCES);
  276. FileSystemPath p(path);
  277. #ifdef VFS_DEBUG
  278. dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
  279. #endif
  280. uid_t uid = owner.has_value() ? owner.value().uid : Process::current->uid();
  281. gid_t gid = owner.has_value() ? owner.value().gid : Process::current->gid();
  282. auto inode_or_error = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, uid, gid);
  283. if (inode_or_error.is_error())
  284. return inode_or_error.error();
  285. auto new_custody = Custody::create(&parent_custody, p.basename(), inode_or_error.value(), parent_custody.mount_flags());
  286. auto description = FileDescription::create(*new_custody);
  287. description->set_rw_mode(options);
  288. description->set_file_flags(options);
  289. return description;
  290. }
  291. KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
  292. {
  293. // Unlike in basically every other case, where it's only the last
  294. // path component (the one being created) that is allowed not to
  295. // exist, POSIX allows mkdir'ed path to have trailing slashes.
  296. // Let's handle that case by trimming any trailing slashes.
  297. while (path.length() > 1 && path.ends_with("/"))
  298. path = path.substring_view(0, path.length() - 1);
  299. RefPtr<Custody> parent_custody;
  300. auto result = resolve_path(path, base, &parent_custody);
  301. if (!result.is_error())
  302. return KResult(-EEXIST);
  303. if (!parent_custody)
  304. return KResult(-ENOENT);
  305. if (result.error() != -ENOENT)
  306. return result.error();
  307. auto& parent_inode = parent_custody->inode();
  308. if (!parent_inode.metadata().may_write(*Process::current))
  309. return KResult(-EACCES);
  310. FileSystemPath p(path);
  311. #ifdef VFS_DEBUG
  312. dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
  313. #endif
  314. return parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, Process::current->uid(), Process::current->gid());
  315. }
  316. KResult VFS::access(StringView path, int mode, Custody& base)
  317. {
  318. auto custody_or_error = resolve_path(path, base);
  319. if (custody_or_error.is_error())
  320. return custody_or_error.error();
  321. auto& custody = *custody_or_error.value();
  322. auto& inode = custody.inode();
  323. auto metadata = inode.metadata();
  324. if (mode & R_OK) {
  325. if (!metadata.may_read(*Process::current))
  326. return KResult(-EACCES);
  327. }
  328. if (mode & W_OK) {
  329. if (!metadata.may_write(*Process::current))
  330. return KResult(-EACCES);
  331. }
  332. if (mode & X_OK) {
  333. if (!metadata.may_execute(*Process::current))
  334. return KResult(-EACCES);
  335. }
  336. return KSuccess;
  337. }
  338. KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
  339. {
  340. auto inode_or_error = resolve_path(path, base);
  341. if (inode_or_error.is_error())
  342. return inode_or_error.error();
  343. auto& custody = *inode_or_error.value();
  344. auto& inode = custody.inode();
  345. if (!inode.is_directory())
  346. return KResult(-ENOTDIR);
  347. if (!inode.metadata().may_execute(*Process::current))
  348. return KResult(-EACCES);
  349. return custody;
  350. }
  351. KResult VFS::chmod(Inode& inode, mode_t mode)
  352. {
  353. if (inode.fs().is_readonly())
  354. return KResult(-EROFS);
  355. if (Process::current->euid() != inode.metadata().uid && !Process::current->is_superuser())
  356. return KResult(-EPERM);
  357. // Only change the permission bits.
  358. mode = (inode.mode() & ~04777u) | (mode & 04777u);
  359. return inode.chmod(mode);
  360. }
  361. KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
  362. {
  363. auto custody_or_error = resolve_path(path, base);
  364. if (custody_or_error.is_error())
  365. return custody_or_error.error();
  366. auto& custody = *custody_or_error.value();
  367. auto& inode = custody.inode();
  368. return chmod(inode, mode);
  369. }
  370. KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
  371. {
  372. RefPtr<Custody> old_parent_custody;
  373. auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody);
  374. if (old_custody_or_error.is_error())
  375. return old_custody_or_error.error();
  376. auto& old_custody = *old_custody_or_error.value();
  377. auto& old_inode = old_custody.inode();
  378. RefPtr<Custody> new_parent_custody;
  379. auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
  380. if (new_custody_or_error.is_error()) {
  381. if (new_custody_or_error.error() != -ENOENT || !new_parent_custody)
  382. return new_custody_or_error.error();
  383. }
  384. auto& old_parent_inode = old_parent_custody->inode();
  385. auto& new_parent_inode = new_parent_custody->inode();
  386. if (&old_parent_inode.fs() != &new_parent_inode.fs())
  387. return KResult(-EXDEV);
  388. if (!new_parent_inode.metadata().may_write(*Process::current))
  389. return KResult(-EACCES);
  390. if (!old_parent_inode.metadata().may_write(*Process::current))
  391. return KResult(-EACCES);
  392. if (old_parent_inode.metadata().is_sticky()) {
  393. if (!Process::current->is_superuser() && old_inode.metadata().uid != Process::current->euid())
  394. return KResult(-EACCES);
  395. }
  396. auto new_basename = FileSystemPath(new_path).basename();
  397. if (!new_custody_or_error.is_error()) {
  398. auto& new_custody = *new_custody_or_error.value();
  399. auto& new_inode = new_custody.inode();
  400. // FIXME: Is this really correct? Check what other systems do.
  401. if (&new_inode == &old_inode)
  402. return KSuccess;
  403. if (new_parent_inode.metadata().is_sticky()) {
  404. if (!Process::current->is_superuser() && new_inode.metadata().uid != Process::current->euid())
  405. return KResult(-EACCES);
  406. }
  407. if (new_inode.is_directory() && !old_inode.is_directory())
  408. return KResult(-EISDIR);
  409. auto result = new_parent_inode.remove_child(new_basename);
  410. if (result.is_error())
  411. return result;
  412. }
  413. auto result = new_parent_inode.add_child(old_inode.identifier(), new_basename, old_inode.mode());
  414. if (result.is_error())
  415. return result;
  416. result = old_parent_inode.remove_child(FileSystemPath(old_path).basename());
  417. if (result.is_error())
  418. return result;
  419. return KSuccess;
  420. }
  421. KResult VFS::chown(Inode& inode, uid_t a_uid, gid_t a_gid)
  422. {
  423. if (inode.fs().is_readonly())
  424. return KResult(-EROFS);
  425. auto metadata = inode.metadata();
  426. if (Process::current->euid() != metadata.uid && !Process::current->is_superuser())
  427. return KResult(-EPERM);
  428. uid_t new_uid = metadata.uid;
  429. gid_t new_gid = metadata.gid;
  430. if (a_uid != (uid_t)-1) {
  431. if (Process::current->euid() != a_uid && !Process::current->is_superuser())
  432. return KResult(-EPERM);
  433. new_uid = a_uid;
  434. }
  435. if (a_gid != (gid_t)-1) {
  436. if (!Process::current->in_group(a_gid) && !Process::current->is_superuser())
  437. return KResult(-EPERM);
  438. new_gid = a_gid;
  439. }
  440. dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
  441. return inode.chown(new_uid, new_gid);
  442. }
  443. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
  444. {
  445. auto custody_or_error = resolve_path(path, base);
  446. if (custody_or_error.is_error())
  447. return custody_or_error.error();
  448. auto& custody = *custody_or_error.value();
  449. auto& inode = custody.inode();
  450. return chown(inode, a_uid, a_gid);
  451. }
  452. KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
  453. {
  454. auto old_custody_or_error = resolve_path(old_path, base);
  455. if (old_custody_or_error.is_error())
  456. return old_custody_or_error.error();
  457. auto& old_custody = *old_custody_or_error.value();
  458. auto& old_inode = old_custody.inode();
  459. RefPtr<Custody> parent_custody;
  460. auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
  461. if (!new_custody_or_error.is_error())
  462. return KResult(-EEXIST);
  463. if (!parent_custody)
  464. return KResult(-ENOENT);
  465. auto& parent_inode = parent_custody->inode();
  466. if (parent_inode.fsid() != old_inode.fsid())
  467. return KResult(-EXDEV);
  468. if (parent_inode.fs().is_readonly())
  469. return KResult(-EROFS);
  470. if (!parent_inode.metadata().may_write(*Process::current))
  471. return KResult(-EACCES);
  472. if (old_inode.is_directory())
  473. return KResult(-EPERM);
  474. return parent_inode.add_child(old_inode.identifier(), FileSystemPath(new_path).basename(), old_inode.mode());
  475. }
  476. KResult VFS::unlink(StringView path, Custody& base)
  477. {
  478. RefPtr<Custody> parent_custody;
  479. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR | O_UNLINK_INTERNAL);
  480. if (custody_or_error.is_error())
  481. return custody_or_error.error();
  482. auto& custody = *custody_or_error.value();
  483. auto& inode = custody.inode();
  484. if (inode.is_directory())
  485. return KResult(-EISDIR);
  486. auto& parent_inode = parent_custody->inode();
  487. if (!parent_inode.metadata().may_write(*Process::current))
  488. return KResult(-EACCES);
  489. if (parent_inode.metadata().is_sticky()) {
  490. if (!Process::current->is_superuser() && inode.metadata().uid != Process::current->euid())
  491. return KResult(-EACCES);
  492. }
  493. auto result = parent_inode.remove_child(FileSystemPath(path).basename());
  494. if (result.is_error())
  495. return result;
  496. return KSuccess;
  497. }
  498. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  499. {
  500. RefPtr<Custody> parent_custody;
  501. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  502. if (!existing_custody_or_error.is_error())
  503. return KResult(-EEXIST);
  504. if (!parent_custody)
  505. return KResult(-ENOENT);
  506. if (existing_custody_or_error.error() != -ENOENT)
  507. return existing_custody_or_error.error();
  508. auto& parent_inode = parent_custody->inode();
  509. if (!parent_inode.metadata().may_write(*Process::current))
  510. return KResult(-EACCES);
  511. FileSystemPath p(linkpath);
  512. dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
  513. auto inode_or_error = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, Process::current->uid(), Process::current->gid());
  514. if (inode_or_error.is_error())
  515. return inode_or_error.error();
  516. auto& inode = inode_or_error.value();
  517. ssize_t nwritten = inode->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
  518. if (nwritten < 0)
  519. return KResult(nwritten);
  520. return KSuccess;
  521. }
  522. KResult VFS::rmdir(StringView path, Custody& base)
  523. {
  524. RefPtr<Custody> parent_custody;
  525. auto custody_or_error = resolve_path(path, base, &parent_custody);
  526. if (custody_or_error.is_error())
  527. return KResult(custody_or_error.error());
  528. auto& custody = *custody_or_error.value();
  529. auto& inode = custody.inode();
  530. if (inode.fs().is_readonly())
  531. return KResult(-EROFS);
  532. // FIXME: We should return EINVAL if the last component of the path is "."
  533. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  534. if (!inode.is_directory())
  535. return KResult(-ENOTDIR);
  536. auto& parent_inode = parent_custody->inode();
  537. if (!parent_inode.metadata().may_write(*Process::current))
  538. return KResult(-EACCES);
  539. if (inode.directory_entry_count() != 2)
  540. return KResult(-ENOTEMPTY);
  541. auto result = inode.remove_child(".");
  542. if (result.is_error())
  543. return result;
  544. result = inode.remove_child("..");
  545. if (result.is_error())
  546. return result;
  547. return parent_inode.remove_child(FileSystemPath(path).basename());
  548. }
  549. RefPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
  550. {
  551. if (!inode_id.is_valid())
  552. return nullptr;
  553. return inode_id.fs()->get_inode(inode_id);
  554. }
  555. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  556. : m_guest(guest_fs.root_inode())
  557. , m_guest_fs(guest_fs)
  558. , m_host_custody(host_custody)
  559. , m_flags(flags)
  560. {
  561. }
  562. VFS::Mount::Mount(Inode& source, Custody& host_custody, int flags)
  563. : m_guest(source.identifier())
  564. , m_guest_fs(source.fs())
  565. , m_host_custody(host_custody)
  566. , m_flags(flags)
  567. {
  568. }
  569. String VFS::Mount::absolute_path() const
  570. {
  571. if (!m_host_custody)
  572. return "/";
  573. return m_host_custody->absolute_path();
  574. }
  575. InodeIdentifier VFS::Mount::host() const
  576. {
  577. if (!m_host_custody)
  578. return {};
  579. return m_host_custody->inode().identifier();
  580. }
  581. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  582. {
  583. for (auto& mount : m_mounts) {
  584. callback(mount);
  585. }
  586. }
  587. void VFS::sync()
  588. {
  589. FS::sync();
  590. }
  591. Custody& VFS::root_custody()
  592. {
  593. if (!m_root_custody)
  594. m_root_custody = Custody::create(nullptr, "", *m_root_inode, MS_NODEV | MS_NOSUID);
  595. return *m_root_custody;
  596. }
  597. const UnveiledPath* VFS::find_matching_unveiled_path(StringView path)
  598. {
  599. for (auto& unveiled_path : Process::current->unveiled_paths()) {
  600. if (path == unveiled_path.path)
  601. return &unveiled_path;
  602. if (path.starts_with(unveiled_path.path) && path.length() > unveiled_path.path.length() && path[unveiled_path.path.length()] == '/')
  603. return &unveiled_path;
  604. }
  605. return nullptr;
  606. }
  607. KResult VFS::validate_path_against_process_veil(StringView path, int options)
  608. {
  609. if (Process::current->veil_state() == VeilState::None)
  610. return KSuccess;
  611. // FIXME: Figure out a nicer way to do this.
  612. if (String(path).contains("/.."))
  613. return KResult(-EINVAL);
  614. auto* unveiled_path = find_matching_unveiled_path(path);
  615. if (!unveiled_path) {
  616. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled.";
  617. dump_backtrace();
  618. return KResult(-ENOENT);
  619. }
  620. if (options & O_CREAT) {
  621. if (!(unveiled_path->permissions & UnveiledPath::Access::CreateOrRemove)) {
  622. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'c' permission.";
  623. dump_backtrace();
  624. return KResult(-EACCES);
  625. }
  626. }
  627. if (options & O_UNLINK_INTERNAL) {
  628. if (!(unveiled_path->permissions & UnveiledPath::Access::CreateOrRemove)) {
  629. dbg() << "Rejecting path '" << path << "' for unlink since it hasn't been unveiled with 'c' permission.";
  630. dump_backtrace();
  631. return KResult(-EACCES);
  632. }
  633. return KSuccess;
  634. }
  635. if (options & O_RDONLY) {
  636. if (!(unveiled_path->permissions & UnveiledPath::Access::Read)) {
  637. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'r' permission.";
  638. dump_backtrace();
  639. return KResult(-EACCES);
  640. }
  641. }
  642. if (options & O_WRONLY) {
  643. if (!(unveiled_path->permissions & UnveiledPath::Access::Write)) {
  644. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'w' permission.";
  645. dump_backtrace();
  646. return KResult(-EACCES);
  647. }
  648. }
  649. if (options & O_EXEC) {
  650. if (!(unveiled_path->permissions & UnveiledPath::Access::Execute)) {
  651. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'x' permission.";
  652. dump_backtrace();
  653. return KResult(-EACCES);
  654. }
  655. }
  656. return KSuccess;
  657. }
  658. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  659. {
  660. auto result = validate_path_against_process_veil(path, options);
  661. if (result.is_error())
  662. return result;
  663. if (symlink_recursion_level >= symlink_recursion_limit)
  664. return KResult(-ELOOP);
  665. if (path.is_empty())
  666. return KResult(-EINVAL);
  667. auto parts = path.split_view('/', true);
  668. auto& current_root = Process::current->root_directory();
  669. NonnullRefPtr<Custody> custody = path[0] == '/' ? current_root : base;
  670. for (size_t i = 0; i < parts.size(); ++i) {
  671. Custody& parent = custody;
  672. auto parent_metadata = parent.inode().metadata();
  673. if (!parent_metadata.is_directory())
  674. return KResult(-ENOTDIR);
  675. // Ensure the current user is allowed to resolve paths inside this directory.
  676. if (!parent_metadata.may_execute(*Process::current))
  677. return KResult(-EACCES);
  678. auto& part = parts[i];
  679. bool have_more_parts = i + 1 < parts.size();
  680. if (part == "..") {
  681. // If we encounter a "..", take a step back, but don't go beyond the root.
  682. if (custody->parent())
  683. custody = *custody->parent();
  684. continue;
  685. } else if (part == "." || part.is_empty()) {
  686. continue;
  687. }
  688. // Okay, let's look up this part.
  689. auto child_inode = parent.inode().lookup(part);
  690. if (!child_inode) {
  691. if (out_parent) {
  692. // ENOENT with a non-null parent custody signals to caller that
  693. // we found the immediate parent of the file, but the file itself
  694. // does not exist yet.
  695. *out_parent = have_more_parts ? nullptr : &parent;
  696. }
  697. return KResult(-ENOENT);
  698. }
  699. int mount_flags_for_child = parent.mount_flags();
  700. // See if there's something mounted on the child; in that case
  701. // we would need to return the guest inode, not the host inode.
  702. if (auto mount = find_mount_for_host(child_inode->identifier())) {
  703. child_inode = get_inode(mount->guest());
  704. mount_flags_for_child = mount->flags();
  705. }
  706. custody = Custody::create(&parent, part, *child_inode, mount_flags_for_child);
  707. if (child_inode->metadata().is_symlink()) {
  708. if (!have_more_parts) {
  709. if (options & O_NOFOLLOW)
  710. return KResult(-ELOOP);
  711. if (options & O_NOFOLLOW_NOERROR)
  712. break;
  713. }
  714. auto symlink_target = child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1);
  715. if (symlink_target.is_error() || !have_more_parts)
  716. return symlink_target;
  717. // Now, resolve the remaining path relative to the symlink target.
  718. // We prepend a "." to it to ensure that it's not empty and that
  719. // any initial slashes it might have get interpreted properly.
  720. StringBuilder remaining_path;
  721. remaining_path.append('.');
  722. remaining_path.append(path.substring_view_starting_after_substring(part));
  723. return resolve_path(remaining_path.to_string(), *symlink_target.value(), out_parent, options, symlink_recursion_level + 1);
  724. }
  725. }
  726. if (out_parent)
  727. *out_parent = custody->parent();
  728. return custody;
  729. }
  730. }