From 7c7343de987afbda3843140bc8d665f90b5e424f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 2 Aug 2019 19:30:30 +0200 Subject: [PATCH] Kernel: mount() should fail if the provided device is not a disk device In the future, we should allow mounting any block device. At the moment there is too much filesystem code that depends on the underlying device being a DiskDevice. --- Kernel/Process.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 28f4e0fd8b..a428b026ff 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -2755,20 +2755,27 @@ int Process::sys$mount(const char* device_path, const char* mountpoint) auto major = metadata_or_error.value().major_device; auto minor = metadata_or_error.value().minor_device; - auto* dev = VFS::the().get_device(major, minor); - auto* disk_device = static_cast(dev); + + auto* device = VFS::the().get_device(major, minor); + if (!device) { + dbg() << "mount: device (" << major << "," << minor << ") not found"; + return -ENODEV; + } + + if (!device->is_disk_device()) { + dbg() << "mount: device (" << major << "," << minor << ") is not a DiskDevice"; + return -ENODEV; + } + + auto& disk_device = static_cast(*device); 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) - return -ENODEV; - // We currently only support ext2. Sorry :^) - auto ext2fs = Ext2FS::create(*disk_device); + auto ext2fs = Ext2FS::create(disk_device); if (!ext2fs->initialize()) { dbg() << "mount: could not find ext2 filesystem on " << device_path; - return -ENODEV; // Hmmm, this doesn't seem quite right.... + return -ENODEV; } // Let's mount the volume now