VirtualFileSystem.cpp 33 KB

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