VirtualFileSystem.cpp 32 KB

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