Kernel: Some improvements to the mount syscall

- You must now have superuser privileges to use mount().
- We now verify that the mount point is a valid path first, before
  trying to find a filesystem on the specified device.
- Convert some dbgprintf() to dbg().
This commit is contained in:
Andreas Kling 2019-08-02 19:03:50 +02:00
parent c76668644b
commit 31de5dee26
Notes: sideshowbarker 2024-07-19 12:56:16 +09:00
3 changed files with 33 additions and 19 deletions

View File

@ -36,21 +36,25 @@ InodeIdentifier VFS::root_inode_id() const
return m_root_inode->identifier();
}
bool VFS::mount(NonnullRefPtr<FS>&& file_system, StringView path)
KResult VFS::mount(NonnullRefPtr<FS>&& file_system, Custody& mount_point)
{
auto& inode = mount_point.inode();
dbg() << "VFS: Mounting " << file_system->class_name() << " at " << mount_point.absolute_path() << " (inode: " << inode.identifier() << ")";
// FIXME: check that this is not already a mount point
auto mount = make<Mount>(mount_point, move(file_system));
m_mounts.append(move(mount));
mount_point.did_mount_on({});
return KSuccess;
}
KResult VFS::mount(NonnullRefPtr<FS>&& file_system, StringView path)
{
auto result = resolve_path(path, root_custody());
if (result.is_error()) {
dbg() << "VFS: mount can't resolve mount point '" << path << "'";
return false;
return result.error();
}
auto& inode = result.value()->inode();
dbg() << "VFS: Mounting " << file_system->class_name() << " at " << path << " (inode: " << inode.identifier() << ")";
// FIXME: check that this is not already a mount point
auto mount = make<Mount>(*result.value(), move(file_system));
m_mounts.append(move(mount));
result.value()->did_mount_on({});
return true;
return mount(move(file_system), result.value());
}
bool VFS::mount_root(NonnullRefPtr<FS>&& file_system)

View File

@ -57,7 +57,8 @@ public:
~VFS();
bool mount_root(NonnullRefPtr<FS>&&);
bool mount(NonnullRefPtr<FS>&&, StringView path);
KResult mount(NonnullRefPtr<FS>&&, StringView path);
KResult mount(NonnullRefPtr<FS>&&, Custody& mount_point);
KResultOr<NonnullRefPtr<FileDescription>> open(RefPtr<Device>&&, int options);
KResultOr<NonnullRefPtr<FileDescription>> open(StringView path, int options, mode_t mode, Custody& base);

View File

@ -2729,16 +2729,26 @@ int Process::sys$reboot()
int Process::sys$mount(const char* device, const char* mountpoint)
{
dbgprintf("mount: device = %s mountpoint = %s\n", device, mountpoint);
if (!is_superuser())
return -EPERM;
if (!validate_read_str(device) || !validate_read_str(mountpoint))
return -EFAULT;
dbg() << "mount: device " << device << " @ " << mountpoint;
auto custody_or_error = VFS::the().resolve_path(mountpoint, current_directory());
if (custody_or_error.is_error())
return custody_or_error.error();
auto& mountpoint_custody = custody_or_error.value();
// Let's do a stat to get some information about the device..
// Let's use lstat so we can convert devname into a major/minor device pair!
struct stat st;
int rc = sys$stat(device, &st);
if (rc < 0) {
dbgprintf("mount: call to sys$stat failed!\n");
dbg() << "mount: call to sys$stat failed!";
return -ENODEV;
}
@ -2748,7 +2758,7 @@ int Process::sys$mount(const char* device, const char* mountpoint)
auto* dev = VFS::the().get_device(major, minor);
auto* disk_device = static_cast<DiskDevice*>(dev);
dbgprintf("mount: attempting to mount %d,%d to %s...\n", major, minor, mountpoint);
dbg() << "mount: attempting to mount device (" << major << "," << minor << ") on " << mountpoint;
// Do a quick check to make sure we're not passing nullptr to Ext2FS::create!
if (dev == nullptr)
@ -2757,15 +2767,14 @@ int Process::sys$mount(const char* device, const char* mountpoint)
// We currently only support ext2. Sorry :^)
auto ext2fs = Ext2FS::create(*disk_device);
if (!ext2fs->initialize()) {
dbgprintf("mount: failed to create ext2fs!\n");
dbg() << "mount: could not find ext2 filesystem on " << device;
return -ENODEV; // Hmmm, this doesn't seem quite right....
}
// Let's mount the volume now
VFS::the().mount(ext2fs, mountpoint);
dbgprintf("mount: successfully mounted ext2 volume on %s to %s!\n", device, mountpoint);
return ESUCCESS;
auto result = VFS::the().mount(ext2fs, mountpoint_custody);
dbg() << "mount: successfully mounted " << device << " on " << mountpoint;
return result;
}
ProcessTracer& Process::ensure_tracer()