VirtualFileSystem.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StringBuilder.h>
  29. #include <Kernel/Devices/BlockDevice.h>
  30. #include <Kernel/FileSystem/Custody.h>
  31. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  32. #include <Kernel/FileSystem/FileDescription.h>
  33. #include <Kernel/FileSystem/FileSystem.h>
  34. #include <Kernel/FileSystem/VirtualFileSystem.h>
  35. #include <Kernel/KSyms.h>
  36. #include <Kernel/Process.h>
  37. #include <LibC/errno_numbers.h>
  38. //#define VFS_DEBUG
  39. namespace Kernel {
  40. static AK::Singleton<VFS> s_the;
  41. static constexpr int symlink_recursion_limit { 5 }; // FIXME: increase?
  42. static constexpr int root_mount_flags = MS_NODEV | MS_NOSUID | MS_RDONLY;
  43. void VFS::initialize()
  44. {
  45. s_the.ensure_instance();
  46. }
  47. VFS& VFS::the()
  48. {
  49. return *s_the;
  50. }
  51. VFS::VFS()
  52. {
  53. #ifdef VFS_DEBUG
  54. klog() << "VFS: Constructing VFS";
  55. #endif
  56. }
  57. VFS::~VFS()
  58. {
  59. }
  60. InodeIdentifier VFS::root_inode_id() const
  61. {
  62. ASSERT(m_root_inode);
  63. return m_root_inode->identifier();
  64. }
  65. KResult VFS::mount(FS& file_system, Custody& mount_point, int flags)
  66. {
  67. LOCKER(m_lock);
  68. auto& inode = mount_point.inode();
  69. dbg() << "VFS: Mounting " << file_system.class_name() << " at " << mount_point.absolute_path() << " (inode: " << inode.identifier() << ") with flags " << flags;
  70. // FIXME: check that this is not already a mount point
  71. Mount mount { file_system, &mount_point, flags };
  72. m_mounts.append(move(mount));
  73. return KSuccess;
  74. }
  75. KResult VFS::bind_mount(Custody& source, Custody& mount_point, int flags)
  76. {
  77. LOCKER(m_lock);
  78. dbg() << "VFS: Bind-mounting " << source.absolute_path() << " at " << mount_point.absolute_path();
  79. // FIXME: check that this is not already a mount point
  80. Mount mount { source.inode(), mount_point, flags };
  81. m_mounts.append(move(mount));
  82. return KSuccess;
  83. }
  84. KResult VFS::remount(Custody& mount_point, int new_flags)
  85. {
  86. LOCKER(m_lock);
  87. dbg() << "VFS: Remounting " << mount_point.absolute_path();
  88. Mount* mount = find_mount_for_guest(mount_point.inode());
  89. if (!mount)
  90. return KResult(-ENODEV);
  91. mount->set_flags(new_flags);
  92. return KSuccess;
  93. }
  94. KResult VFS::unmount(Inode& guest_inode)
  95. {
  96. LOCKER(m_lock);
  97. dbg() << "VFS: unmount called with inode " << guest_inode.identifier();
  98. for (size_t i = 0; i < m_mounts.size(); ++i) {
  99. auto& mount = m_mounts.at(i);
  100. if (&mount.guest() == &guest_inode) {
  101. auto result = mount.guest_fs().prepare_to_unmount();
  102. if (result.is_error()) {
  103. dbg() << "VFS: Failed to unmount!";
  104. return result;
  105. }
  106. dbg() << "VFS: found fs " << mount.guest_fs().fsid() << " at mount index " << i << "! Unmounting...";
  107. m_mounts.unstable_take(i);
  108. return KSuccess;
  109. }
  110. }
  111. dbg() << "VFS: Nothing mounted on inode " << guest_inode.identifier();
  112. return KResult(-ENODEV);
  113. }
  114. bool VFS::mount_root(FS& file_system)
  115. {
  116. if (m_root_inode) {
  117. klog() << "VFS: mount_root can't mount another root";
  118. return false;
  119. }
  120. Mount mount { file_system, nullptr, root_mount_flags };
  121. auto root_inode = file_system.root_inode();
  122. if (!root_inode->is_directory()) {
  123. klog() << "VFS: root inode (" << String::format("%02u", file_system.fsid()) << ":" << String::format("%08u", root_inode->index()) << ") for / is not a directory :(";
  124. return false;
  125. }
  126. m_root_inode = move(root_inode);
  127. klog() << "VFS: mounted root from " << file_system.class_name() << " (" << static_cast<FileBackedFS&>(file_system).file_description().absolute_path() << ")";
  128. m_mounts.append(move(mount));
  129. return true;
  130. }
  131. auto VFS::find_mount_for_host(Inode& inode) -> Mount*
  132. {
  133. for (auto& mount : m_mounts) {
  134. if (mount.host() == &inode)
  135. return &mount;
  136. }
  137. return nullptr;
  138. }
  139. auto VFS::find_mount_for_host(InodeIdentifier id) -> Mount*
  140. {
  141. for (auto& mount : m_mounts) {
  142. if (mount.host() && mount.host()->identifier() == id)
  143. return &mount;
  144. }
  145. return nullptr;
  146. }
  147. auto VFS::find_mount_for_guest(Inode& inode) -> Mount*
  148. {
  149. for (auto& mount : m_mounts) {
  150. if (&mount.guest() == &inode)
  151. return &mount;
  152. }
  153. return nullptr;
  154. }
  155. auto VFS::find_mount_for_guest(InodeIdentifier id) -> Mount*
  156. {
  157. for (auto& mount : m_mounts) {
  158. if (mount.guest().identifier() == id)
  159. return &mount;
  160. }
  161. return nullptr;
  162. }
  163. bool VFS::is_vfs_root(InodeIdentifier inode) const
  164. {
  165. return inode == root_inode_id();
  166. }
  167. KResult VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntryView&)> callback)
  168. {
  169. return dir_inode.traverse_as_directory([&](auto& entry) {
  170. InodeIdentifier resolved_inode;
  171. if (auto mount = find_mount_for_host(entry.inode))
  172. resolved_inode = mount->guest().identifier();
  173. else
  174. resolved_inode = entry.inode;
  175. // FIXME: This is now broken considering chroot and bind mounts.
  176. bool is_root_inode = dir_inode.identifier() == dir_inode.fs().root_inode()->identifier();
  177. if (is_root_inode && !is_vfs_root(dir_inode.identifier()) && entry.name == "..") {
  178. auto mount = find_mount_for_guest(dir_inode);
  179. ASSERT(mount);
  180. ASSERT(mount->host());
  181. resolved_inode = mount->host()->identifier();
  182. }
  183. callback({ entry.name, resolved_inode, entry.file_type });
  184. return true;
  185. });
  186. }
  187. KResult VFS::utime(StringView path, Custody& base, time_t atime, time_t mtime)
  188. {
  189. auto custody_or_error = VFS::the().resolve_path(move(path), base);
  190. if (custody_or_error.is_error())
  191. return custody_or_error.error();
  192. auto& custody = *custody_or_error.value();
  193. auto& inode = custody.inode();
  194. auto current_process = Process::current();
  195. if (!current_process->is_superuser() && inode.metadata().uid != current_process->euid())
  196. return KResult(-EACCES);
  197. if (custody.is_readonly())
  198. return KResult(-EROFS);
  199. int error = inode.set_atime(atime);
  200. if (error)
  201. return KResult(error);
  202. error = inode.set_mtime(mtime);
  203. if (error)
  204. return KResult(error);
  205. return KSuccess;
  206. }
  207. KResultOr<InodeMetadata> VFS::lookup_metadata(StringView path, Custody& base, int options)
  208. {
  209. auto custody_or_error = resolve_path(path, base, nullptr, options);
  210. if (custody_or_error.is_error())
  211. return custody_or_error.error();
  212. return custody_or_error.value()->inode().metadata();
  213. }
  214. KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options, mode_t mode, Custody& base, Optional<UidAndGid> owner)
  215. {
  216. if ((options & O_CREAT) && (options & O_DIRECTORY))
  217. return KResult(-EINVAL);
  218. RefPtr<Custody> parent_custody;
  219. auto custody_or_error = resolve_path(path, base, &parent_custody, options);
  220. if (options & O_CREAT) {
  221. if (!parent_custody)
  222. return KResult(-ENOENT);
  223. if (custody_or_error.is_error()) {
  224. if (custody_or_error.error() != -ENOENT)
  225. return custody_or_error.error();
  226. return create(path, options, mode, *parent_custody, move(owner));
  227. }
  228. if (options & O_EXCL)
  229. return KResult(-EEXIST);
  230. }
  231. if (custody_or_error.is_error())
  232. return custody_or_error.error();
  233. auto& custody = *custody_or_error.value();
  234. auto& inode = custody.inode();
  235. auto metadata = inode.metadata();
  236. if ((options & O_DIRECTORY) && !metadata.is_directory())
  237. return KResult(-ENOTDIR);
  238. bool should_truncate_file = false;
  239. auto current_process = Process::current();
  240. if ((options & O_RDONLY) && !metadata.may_read(*current_process))
  241. return KResult(-EACCES);
  242. if (options & O_WRONLY) {
  243. if (!metadata.may_write(*current_process))
  244. return KResult(-EACCES);
  245. if (metadata.is_directory())
  246. return KResult(-EISDIR);
  247. should_truncate_file = options & O_TRUNC;
  248. }
  249. if (options & O_EXEC) {
  250. if (!metadata.may_execute(*current_process) || (custody.mount_flags() & MS_NOEXEC))
  251. return KResult(-EACCES);
  252. }
  253. if (auto preopen_fd = inode.preopen_fd())
  254. return *preopen_fd;
  255. if (metadata.is_fifo()) {
  256. if (options & O_WRONLY) {
  257. auto description = inode.fifo().open_direction_blocking(FIFO::Direction::Writer);
  258. description->set_rw_mode(options);
  259. description->set_file_flags(options);
  260. description->set_original_inode({}, inode);
  261. return description;
  262. } else if (options & O_RDONLY) {
  263. auto description = inode.fifo().open_direction_blocking(FIFO::Direction::Reader);
  264. description->set_rw_mode(options);
  265. description->set_file_flags(options);
  266. description->set_original_inode({}, inode);
  267. return description;
  268. }
  269. return KResult(-EINVAL);
  270. }
  271. if (metadata.is_device()) {
  272. if (custody.mount_flags() & MS_NODEV)
  273. return KResult(-EACCES);
  274. auto device = Device::get_device(metadata.major_device, metadata.minor_device);
  275. if (device == nullptr) {
  276. return KResult(-ENODEV);
  277. }
  278. auto descriptor_or_error = device->open(options);
  279. if (descriptor_or_error.is_error())
  280. return descriptor_or_error.error();
  281. descriptor_or_error.value()->set_original_inode({}, inode);
  282. return descriptor_or_error;
  283. }
  284. // Check for read-only FS. Do this after handling preopen FD and devices,
  285. // but before modifying the inode in any way.
  286. if ((options & O_WRONLY) && custody.is_readonly())
  287. return KResult(-EROFS);
  288. if (should_truncate_file) {
  289. KResult result = inode.truncate(0);
  290. if (result.is_error())
  291. return result;
  292. inode.set_mtime(kgettimeofday().tv_sec);
  293. }
  294. auto description = FileDescription::create(custody);
  295. description->set_rw_mode(options);
  296. description->set_file_flags(options);
  297. return description;
  298. }
  299. KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
  300. {
  301. if (!is_regular_file(mode) && !is_block_device(mode) && !is_character_device(mode) && !is_fifo(mode) && !is_socket(mode))
  302. return KResult(-EINVAL);
  303. RefPtr<Custody> parent_custody;
  304. auto existing_file_or_error = resolve_path(path, base, &parent_custody);
  305. if (!existing_file_or_error.is_error())
  306. return KResult(-EEXIST);
  307. if (!parent_custody)
  308. return KResult(-ENOENT);
  309. if (existing_file_or_error.error() != -ENOENT)
  310. return existing_file_or_error.error();
  311. auto& parent_inode = parent_custody->inode();
  312. auto current_process = Process::current();
  313. if (!parent_inode.metadata().may_write(*current_process))
  314. return KResult(-EACCES);
  315. if (parent_custody->is_readonly())
  316. return KResult(-EROFS);
  317. LexicalPath p(path);
  318. dbg() << "VFS::mknod: '" << p.basename() << "' mode=" << mode << " dev=" << dev << " in " << parent_inode.identifier();
  319. return parent_inode.create_child(p.basename(), mode, dev, current_process->uid(), current_process->gid()).result();
  320. }
  321. KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
  322. {
  323. auto result = validate_path_against_process_veil(path, options);
  324. if (result.is_error())
  325. return result;
  326. if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
  327. // Turn it into a regular file. (This feels rather hackish.)
  328. mode |= 0100000;
  329. }
  330. auto& parent_inode = parent_custody.inode();
  331. auto current_process = Process::current();
  332. if (!parent_inode.metadata().may_write(*current_process))
  333. return KResult(-EACCES);
  334. if (parent_custody.is_readonly())
  335. return KResult(-EROFS);
  336. LexicalPath p(path);
  337. #ifdef VFS_DEBUG
  338. dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
  339. #endif
  340. uid_t uid = owner.has_value() ? owner.value().uid : current_process->uid();
  341. gid_t gid = owner.has_value() ? owner.value().gid : current_process->gid();
  342. auto inode_or_error = parent_inode.create_child(p.basename(), mode, 0, uid, gid);
  343. if (inode_or_error.is_error())
  344. return inode_or_error.error();
  345. auto new_custody = Custody::create(&parent_custody, p.basename(), inode_or_error.value(), parent_custody.mount_flags());
  346. auto description = FileDescription::create(*new_custody);
  347. description->set_rw_mode(options);
  348. description->set_file_flags(options);
  349. return description;
  350. }
  351. KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
  352. {
  353. // Unlike in basically every other case, where it's only the last
  354. // path component (the one being created) that is allowed not to
  355. // exist, POSIX allows mkdir'ed path to have trailing slashes.
  356. // Let's handle that case by trimming any trailing slashes.
  357. while (path.length() > 1 && path.ends_with("/"))
  358. path = path.substring_view(0, path.length() - 1);
  359. RefPtr<Custody> parent_custody;
  360. auto result = resolve_path(path, base, &parent_custody);
  361. if (!result.is_error())
  362. return KResult(-EEXIST);
  363. if (!parent_custody)
  364. return KResult(-ENOENT);
  365. if (result.error() != -ENOENT)
  366. return result.error();
  367. auto& parent_inode = parent_custody->inode();
  368. auto current_process = Process::current();
  369. if (!parent_inode.metadata().may_write(*current_process))
  370. return KResult(-EACCES);
  371. if (parent_custody->is_readonly())
  372. return KResult(-EROFS);
  373. LexicalPath p(path);
  374. #ifdef VFS_DEBUG
  375. dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
  376. #endif
  377. return parent_inode.create_child(p.basename(), S_IFDIR | mode, 0, current_process->uid(), current_process->gid()).result();
  378. }
  379. KResult VFS::access(StringView path, int mode, Custody& base)
  380. {
  381. auto custody_or_error = resolve_path(path, base);
  382. if (custody_or_error.is_error())
  383. return custody_or_error.error();
  384. auto& custody = *custody_or_error.value();
  385. auto& inode = custody.inode();
  386. auto metadata = inode.metadata();
  387. auto current_process = Process::current();
  388. if (mode & R_OK) {
  389. if (!metadata.may_read(*current_process))
  390. return KResult(-EACCES);
  391. }
  392. if (mode & W_OK) {
  393. if (!metadata.may_write(*current_process))
  394. return KResult(-EACCES);
  395. if (custody.is_readonly())
  396. return KResult(-EROFS);
  397. }
  398. if (mode & X_OK) {
  399. if (!metadata.may_execute(*current_process))
  400. return KResult(-EACCES);
  401. }
  402. return KSuccess;
  403. }
  404. KResultOr<NonnullRefPtr<Custody>> VFS::open_directory(StringView path, Custody& base)
  405. {
  406. auto inode_or_error = resolve_path(path, base);
  407. if (inode_or_error.is_error())
  408. return inode_or_error.error();
  409. auto& custody = *inode_or_error.value();
  410. auto& inode = custody.inode();
  411. if (!inode.is_directory())
  412. return KResult(-ENOTDIR);
  413. if (!inode.metadata().may_execute(*Process::current()))
  414. return KResult(-EACCES);
  415. return custody;
  416. }
  417. KResult VFS::chmod(Custody& custody, mode_t mode)
  418. {
  419. auto& inode = custody.inode();
  420. auto current_process = Process::current();
  421. if (current_process->euid() != inode.metadata().uid && !current_process->is_superuser())
  422. return KResult(-EPERM);
  423. if (custody.is_readonly())
  424. return KResult(-EROFS);
  425. // Only change the permission bits.
  426. mode = (inode.mode() & ~04777u) | (mode & 04777u);
  427. return inode.chmod(mode);
  428. }
  429. KResult VFS::chmod(StringView path, mode_t mode, Custody& base)
  430. {
  431. auto custody_or_error = resolve_path(path, base);
  432. if (custody_or_error.is_error())
  433. return custody_or_error.error();
  434. auto& custody = *custody_or_error.value();
  435. return chmod(custody, mode);
  436. }
  437. KResult VFS::rename(StringView old_path, StringView new_path, Custody& base)
  438. {
  439. RefPtr<Custody> old_parent_custody;
  440. auto old_custody_or_error = resolve_path(old_path, base, &old_parent_custody);
  441. if (old_custody_or_error.is_error())
  442. return old_custody_or_error.error();
  443. auto& old_custody = *old_custody_or_error.value();
  444. auto& old_inode = old_custody.inode();
  445. RefPtr<Custody> new_parent_custody;
  446. auto new_custody_or_error = resolve_path(new_path, base, &new_parent_custody);
  447. if (new_custody_or_error.is_error()) {
  448. if (new_custody_or_error.error() != -ENOENT || !new_parent_custody)
  449. return new_custody_or_error.error();
  450. }
  451. auto& old_parent_inode = old_parent_custody->inode();
  452. auto& new_parent_inode = new_parent_custody->inode();
  453. if (&old_parent_inode.fs() != &new_parent_inode.fs())
  454. return KResult(-EXDEV);
  455. auto current_process = Process::current();
  456. if (!new_parent_inode.metadata().may_write(*current_process))
  457. return KResult(-EACCES);
  458. if (!old_parent_inode.metadata().may_write(*current_process))
  459. return KResult(-EACCES);
  460. if (old_parent_inode.metadata().is_sticky()) {
  461. if (!current_process->is_superuser() && old_inode.metadata().uid != current_process->euid())
  462. return KResult(-EACCES);
  463. }
  464. if (old_parent_custody->is_readonly() || new_parent_custody->is_readonly())
  465. return KResult(-EROFS);
  466. auto new_basename = LexicalPath(new_path).basename();
  467. if (!new_custody_or_error.is_error()) {
  468. auto& new_custody = *new_custody_or_error.value();
  469. auto& new_inode = new_custody.inode();
  470. // FIXME: Is this really correct? Check what other systems do.
  471. if (&new_inode == &old_inode)
  472. return KSuccess;
  473. if (new_parent_inode.metadata().is_sticky()) {
  474. if (!current_process->is_superuser() && new_inode.metadata().uid != current_process->euid())
  475. return KResult(-EACCES);
  476. }
  477. if (new_inode.is_directory() && !old_inode.is_directory())
  478. return KResult(-EISDIR);
  479. auto result = new_parent_inode.remove_child(new_basename);
  480. if (result.is_error())
  481. return result;
  482. }
  483. auto result = new_parent_inode.add_child(old_inode, new_basename, old_inode.mode());
  484. if (result.is_error())
  485. return result;
  486. result = old_parent_inode.remove_child(LexicalPath(old_path).basename());
  487. if (result.is_error())
  488. return result;
  489. return KSuccess;
  490. }
  491. KResult VFS::chown(Custody& custody, uid_t a_uid, gid_t a_gid)
  492. {
  493. auto& inode = custody.inode();
  494. auto metadata = inode.metadata();
  495. auto current_process = Process::current();
  496. if (current_process->euid() != metadata.uid && !current_process->is_superuser())
  497. return KResult(-EPERM);
  498. uid_t new_uid = metadata.uid;
  499. gid_t new_gid = metadata.gid;
  500. if (a_uid != (uid_t)-1) {
  501. if (current_process->euid() != a_uid && !current_process->is_superuser())
  502. return KResult(-EPERM);
  503. new_uid = a_uid;
  504. }
  505. if (a_gid != (gid_t)-1) {
  506. if (!current_process->in_group(a_gid) && !current_process->is_superuser())
  507. return KResult(-EPERM);
  508. new_gid = a_gid;
  509. }
  510. if (custody.is_readonly())
  511. return KResult(-EROFS);
  512. dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
  513. if (metadata.is_setuid() || metadata.is_setgid()) {
  514. dbg() << "VFS::chown(): Stripping SUID/SGID bits from " << inode.identifier();
  515. auto result = inode.chmod(metadata.mode & ~(04000 | 02000));
  516. if (result.is_error())
  517. return result;
  518. }
  519. return inode.chown(new_uid, new_gid);
  520. }
  521. KResult VFS::chown(StringView path, uid_t a_uid, gid_t a_gid, Custody& base)
  522. {
  523. auto custody_or_error = resolve_path(path, base);
  524. if (custody_or_error.is_error())
  525. return custody_or_error.error();
  526. auto& custody = *custody_or_error.value();
  527. return chown(custody, a_uid, a_gid);
  528. }
  529. KResult VFS::link(StringView old_path, StringView new_path, Custody& base)
  530. {
  531. auto old_custody_or_error = resolve_path(old_path, base);
  532. if (old_custody_or_error.is_error())
  533. return old_custody_or_error.error();
  534. auto& old_custody = *old_custody_or_error.value();
  535. auto& old_inode = old_custody.inode();
  536. RefPtr<Custody> parent_custody;
  537. auto new_custody_or_error = resolve_path(new_path, base, &parent_custody);
  538. if (!new_custody_or_error.is_error())
  539. return KResult(-EEXIST);
  540. if (!parent_custody)
  541. return KResult(-ENOENT);
  542. auto& parent_inode = parent_custody->inode();
  543. if (parent_inode.fsid() != old_inode.fsid())
  544. return KResult(-EXDEV);
  545. if (!parent_inode.metadata().may_write(*Process::current()))
  546. return KResult(-EACCES);
  547. if (old_inode.is_directory())
  548. return KResult(-EPERM);
  549. if (parent_custody->is_readonly())
  550. return KResult(-EROFS);
  551. return parent_inode.add_child(old_inode, LexicalPath(new_path).basename(), old_inode.mode());
  552. }
  553. KResult VFS::unlink(StringView path, Custody& base)
  554. {
  555. RefPtr<Custody> parent_custody;
  556. auto custody_or_error = resolve_path(path, base, &parent_custody, O_NOFOLLOW_NOERROR | O_UNLINK_INTERNAL);
  557. if (custody_or_error.is_error())
  558. return custody_or_error.error();
  559. auto& custody = *custody_or_error.value();
  560. auto& inode = custody.inode();
  561. if (inode.is_directory())
  562. return KResult(-EISDIR);
  563. // We have just checked that the inode is not a directory, and thus it's not
  564. // the root. So it should have a parent. Note that this would be invalidated
  565. // if we were to support bind-mounting regular files on top of the root.
  566. ASSERT(parent_custody);
  567. auto& parent_inode = parent_custody->inode();
  568. auto current_process = Process::current();
  569. if (!parent_inode.metadata().may_write(*current_process))
  570. return KResult(-EACCES);
  571. if (parent_inode.metadata().is_sticky()) {
  572. if (!current_process->is_superuser() && inode.metadata().uid != current_process->euid())
  573. return KResult(-EACCES);
  574. }
  575. if (parent_custody->is_readonly())
  576. return KResult(-EROFS);
  577. auto result = parent_inode.remove_child(LexicalPath(path).basename());
  578. if (result.is_error())
  579. return result;
  580. return KSuccess;
  581. }
  582. KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
  583. {
  584. RefPtr<Custody> parent_custody;
  585. auto existing_custody_or_error = resolve_path(linkpath, base, &parent_custody);
  586. if (!existing_custody_or_error.is_error())
  587. return KResult(-EEXIST);
  588. if (!parent_custody)
  589. return KResult(-ENOENT);
  590. if (existing_custody_or_error.error() != -ENOENT)
  591. return existing_custody_or_error.error();
  592. auto& parent_inode = parent_custody->inode();
  593. auto current_process = Process::current();
  594. if (!parent_inode.metadata().may_write(*current_process))
  595. return KResult(-EACCES);
  596. if (parent_custody->is_readonly())
  597. return KResult(-EROFS);
  598. LexicalPath p(linkpath);
  599. dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
  600. auto inode_or_error = parent_inode.create_child(p.basename(), S_IFLNK | 0644, 0, current_process->uid(), current_process->gid());
  601. if (inode_or_error.is_error())
  602. return inode_or_error.error();
  603. auto& inode = inode_or_error.value();
  604. auto target_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>((const u8*)target.characters_without_null_termination()));
  605. ssize_t nwritten = inode->write_bytes(0, target.length(), target_buffer, nullptr);
  606. if (nwritten < 0)
  607. return KResult(nwritten);
  608. return KSuccess;
  609. }
  610. KResult VFS::rmdir(StringView path, Custody& base)
  611. {
  612. RefPtr<Custody> parent_custody;
  613. auto custody_or_error = resolve_path(path, base, &parent_custody);
  614. if (custody_or_error.is_error())
  615. return KResult(custody_or_error.error());
  616. auto& custody = *custody_or_error.value();
  617. auto& inode = custody.inode();
  618. // FIXME: We should return EINVAL if the last component of the path is "."
  619. // FIXME: We should return ENOTEMPTY if the last component of the path is ".."
  620. if (!inode.is_directory())
  621. return KResult(-ENOTDIR);
  622. if (!parent_custody)
  623. return KResult(-EBUSY);
  624. auto& parent_inode = parent_custody->inode();
  625. if (!parent_inode.metadata().may_write(*Process::current()))
  626. return KResult(-EACCES);
  627. KResultOr<size_t> dir_count_result = inode.directory_entry_count();
  628. if (dir_count_result.is_error())
  629. return dir_count_result.result();
  630. if (dir_count_result.value() != 2)
  631. return KResult(-ENOTEMPTY);
  632. if (custody.is_readonly())
  633. return KResult(-EROFS);
  634. auto result = inode.remove_child(".");
  635. if (result.is_error())
  636. return result;
  637. result = inode.remove_child("..");
  638. if (result.is_error())
  639. return result;
  640. return parent_inode.remove_child(LexicalPath(path).basename());
  641. }
  642. VFS::Mount::Mount(FS& guest_fs, Custody* host_custody, int flags)
  643. : m_guest(guest_fs.root_inode())
  644. , m_guest_fs(guest_fs)
  645. , m_host_custody(host_custody)
  646. , m_flags(flags)
  647. {
  648. }
  649. VFS::Mount::Mount(Inode& source, Custody& host_custody, int flags)
  650. : m_guest(source)
  651. , m_guest_fs(source.fs())
  652. , m_host_custody(host_custody)
  653. , m_flags(flags)
  654. {
  655. }
  656. String VFS::Mount::absolute_path() const
  657. {
  658. if (!m_host_custody)
  659. return "/";
  660. return m_host_custody->absolute_path();
  661. }
  662. Inode* VFS::Mount::host()
  663. {
  664. if (!m_host_custody)
  665. return nullptr;
  666. return &m_host_custody->inode();
  667. }
  668. const Inode* VFS::Mount::host() const
  669. {
  670. if (!m_host_custody)
  671. return nullptr;
  672. return &m_host_custody->inode();
  673. }
  674. void VFS::for_each_mount(Function<void(const Mount&)> callback) const
  675. {
  676. for (auto& mount : m_mounts) {
  677. callback(mount);
  678. }
  679. }
  680. void VFS::sync()
  681. {
  682. FS::sync();
  683. }
  684. Custody& VFS::root_custody()
  685. {
  686. if (!m_root_custody)
  687. m_root_custody = Custody::create(nullptr, "", *m_root_inode, root_mount_flags);
  688. return *m_root_custody;
  689. }
  690. const UnveiledPath* VFS::find_matching_unveiled_path(StringView path)
  691. {
  692. for (auto& unveiled_path : Process::current()->unveiled_paths()) {
  693. if (path == unveiled_path.path)
  694. return &unveiled_path;
  695. if (path.starts_with(unveiled_path.path) && path.length() > unveiled_path.path.length() && path[unveiled_path.path.length()] == '/')
  696. return &unveiled_path;
  697. }
  698. return nullptr;
  699. }
  700. KResult VFS::validate_path_against_process_veil(StringView path, int options)
  701. {
  702. if (Process::current()->veil_state() == VeilState::None)
  703. return KSuccess;
  704. // FIXME: Figure out a nicer way to do this.
  705. if (String(path).contains("/.."))
  706. return KResult(-EINVAL);
  707. auto* unveiled_path = find_matching_unveiled_path(path);
  708. if (!unveiled_path) {
  709. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled.";
  710. dump_backtrace();
  711. return KResult(-ENOENT);
  712. }
  713. if (options & O_CREAT) {
  714. if (!(unveiled_path->permissions & UnveiledPath::Access::CreateOrRemove)) {
  715. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'c' permission.";
  716. dump_backtrace();
  717. return KResult(-EACCES);
  718. }
  719. }
  720. if (options & O_UNLINK_INTERNAL) {
  721. if (!(unveiled_path->permissions & UnveiledPath::Access::CreateOrRemove)) {
  722. dbg() << "Rejecting path '" << path << "' for unlink since it hasn't been unveiled with 'c' permission.";
  723. dump_backtrace();
  724. return KResult(-EACCES);
  725. }
  726. return KSuccess;
  727. }
  728. if (options & O_RDONLY) {
  729. if (!(unveiled_path->permissions & UnveiledPath::Access::Read)) {
  730. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'r' permission.";
  731. dump_backtrace();
  732. return KResult(-EACCES);
  733. }
  734. }
  735. if (options & O_WRONLY) {
  736. if (!(unveiled_path->permissions & UnveiledPath::Access::Write)) {
  737. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'w' permission.";
  738. dump_backtrace();
  739. return KResult(-EACCES);
  740. }
  741. }
  742. if (options & O_EXEC) {
  743. if (!(unveiled_path->permissions & UnveiledPath::Access::Execute)) {
  744. dbg() << "Rejecting path '" << path << "' since it hasn't been unveiled with 'x' permission.";
  745. dump_backtrace();
  746. return KResult(-EACCES);
  747. }
  748. }
  749. return KSuccess;
  750. }
  751. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  752. {
  753. auto custody_or_error = resolve_path_without_veil(path, base, out_parent, options, symlink_recursion_level);
  754. if (custody_or_error.is_error())
  755. return custody_or_error.error();
  756. auto& custody = custody_or_error.value();
  757. auto result = validate_path_against_process_veil(custody->absolute_path(), options);
  758. if (result.is_error())
  759. return result;
  760. return custody;
  761. }
  762. KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path_without_veil(StringView path, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level)
  763. {
  764. if (symlink_recursion_level >= symlink_recursion_limit)
  765. return KResult(-ELOOP);
  766. if (path.is_empty())
  767. return KResult(-EINVAL);
  768. auto parts = path.split_view('/', true);
  769. auto current_process = Process::current();
  770. auto& current_root = current_process->root_directory();
  771. NonnullRefPtr<Custody> custody = path[0] == '/' ? current_root : base;
  772. for (size_t i = 0; i < parts.size(); ++i) {
  773. Custody& parent = custody;
  774. auto parent_metadata = parent.inode().metadata();
  775. if (!parent_metadata.is_directory())
  776. return KResult(-ENOTDIR);
  777. // Ensure the current user is allowed to resolve paths inside this directory.
  778. if (!parent_metadata.may_execute(*current_process))
  779. return KResult(-EACCES);
  780. auto& part = parts[i];
  781. bool have_more_parts = i + 1 < parts.size();
  782. if (part == "..") {
  783. // If we encounter a "..", take a step back, but don't go beyond the root.
  784. if (custody->parent())
  785. custody = *custody->parent();
  786. continue;
  787. } else if (part == "." || part.is_empty()) {
  788. continue;
  789. }
  790. // Okay, let's look up this part.
  791. auto child_inode = parent.inode().lookup(part);
  792. if (!child_inode) {
  793. if (out_parent) {
  794. // ENOENT with a non-null parent custody signals to caller that
  795. // we found the immediate parent of the file, but the file itself
  796. // does not exist yet.
  797. *out_parent = have_more_parts ? nullptr : &parent;
  798. }
  799. return KResult(-ENOENT);
  800. }
  801. int mount_flags_for_child = parent.mount_flags();
  802. // See if there's something mounted on the child; in that case
  803. // we would need to return the guest inode, not the host inode.
  804. if (auto mount = find_mount_for_host(*child_inode)) {
  805. child_inode = mount->guest();
  806. mount_flags_for_child = mount->flags();
  807. }
  808. custody = Custody::create(&parent, part, *child_inode, mount_flags_for_child);
  809. if (child_inode->metadata().is_symlink()) {
  810. if (!have_more_parts) {
  811. if (options & O_NOFOLLOW)
  812. return KResult(-ELOOP);
  813. if (options & O_NOFOLLOW_NOERROR)
  814. break;
  815. }
  816. auto symlink_target = child_inode->resolve_as_link(parent, out_parent, options, symlink_recursion_level + 1);
  817. if (symlink_target.is_error() || !have_more_parts)
  818. return symlink_target;
  819. // Now, resolve the remaining path relative to the symlink target.
  820. // We prepend a "." to it to ensure that it's not empty and that
  821. // any initial slashes it might have get interpreted properly.
  822. StringBuilder remaining_path;
  823. remaining_path.append('.');
  824. remaining_path.append(path.substring_view_starting_after_substring(part));
  825. return resolve_path_without_veil(remaining_path.to_string(), *symlink_target.value(), out_parent, options, symlink_recursion_level + 1);
  826. }
  827. }
  828. if (out_parent)
  829. *out_parent = custody->parent();
  830. return custody;
  831. }
  832. }