VirtualFileSystem.cpp 33 KB

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