VirtualFileSystem.cpp 29 KB

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