VirtualFileSystem.cpp 35 KB

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