VirtualFileSystem.cpp 30 KB

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