mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 14:45:07 +00:00
Kernel/SysFS: Ensure data stability when reading from Inodes
Like with the ProcFS, description data can change at anytime, so it's wise to ensure that when the userland reads from an Inode, data is consistent unless the userland indicated it wants to refresh the data (by seeking to offset 0, or re-attaching the Inode). Otherwise, if the data changes in the middle of the reading, it can cause silent corruption in output which can lead to random crashes.
This commit is contained in:
parent
a595345e7c
commit
e490c17bde
5 changed files with 79 additions and 7 deletions
|
@ -23,9 +23,9 @@ SysFSUSBDeviceInformation::~SysFSUSBDeviceInformation()
|
|||
{
|
||||
}
|
||||
|
||||
KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription*) const
|
||||
bool SysFSUSBDeviceInformation::output(KBufferBuilder& builder)
|
||||
{
|
||||
KBufferBuilder builder;
|
||||
VERIFY(m_lock.is_locked());
|
||||
JsonArraySerializer array { builder };
|
||||
|
||||
auto obj = array.add_object();
|
||||
|
@ -44,14 +44,54 @@ KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou
|
|||
obj.add("num_configurations", m_device->device_descriptor().num_configurations);
|
||||
obj.finish();
|
||||
array.finish();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto data = builder.build();
|
||||
if (!data)
|
||||
KResult SysFSUSBDeviceInformation::refresh_data(FileDescription& description) const
|
||||
{
|
||||
MutexLocker lock(m_lock);
|
||||
auto& cached_data = description.data();
|
||||
if (!cached_data) {
|
||||
cached_data = adopt_own_if_nonnull(new (nothrow) SysFSInodeData);
|
||||
if (!cached_data)
|
||||
return ENOMEM;
|
||||
}
|
||||
KBufferBuilder builder;
|
||||
if (!const_cast<SysFSUSBDeviceInformation&>(*this).output(builder))
|
||||
return ENOENT;
|
||||
auto& typed_cached_data = static_cast<SysFSInodeData&>(*cached_data);
|
||||
typed_cached_data.buffer = builder.build();
|
||||
if (!typed_cached_data.buffer)
|
||||
return ENOMEM;
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
ssize_t nread = min(static_cast<off_t>(data->size() - offset), static_cast<off_t>(count));
|
||||
if (!buffer.write(data->data() + offset, nread))
|
||||
return EFAULT;
|
||||
KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
|
||||
{
|
||||
dbgln_if(PROCFS_DEBUG, "SysFSUSBDeviceInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);
|
||||
|
||||
VERIFY(offset >= 0);
|
||||
VERIFY(buffer.user_or_kernel_ptr());
|
||||
|
||||
if (!description)
|
||||
return KResult(EIO);
|
||||
|
||||
MutexLocker locker(m_lock);
|
||||
|
||||
if (!description->data()) {
|
||||
dbgln("SysFSUSBDeviceInformation: Do not have cached data!");
|
||||
return KResult(EIO);
|
||||
}
|
||||
|
||||
auto& typed_cached_data = static_cast<SysFSInodeData&>(*description->data());
|
||||
auto& data_buffer = typed_cached_data.buffer;
|
||||
|
||||
if (!data_buffer || (size_t)offset >= data_buffer->size())
|
||||
return 0;
|
||||
|
||||
ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
|
||||
if (!buffer.write(data_buffer->data() + offset, nread))
|
||||
return KResult(EFAULT);
|
||||
|
||||
return nread;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#include <Kernel/Bus/USB/USBDevice.h>
|
||||
#include <Kernel/FileSystem/SysFS.h>
|
||||
#include <Kernel/KBufferBuilder.h>
|
||||
#include <Kernel/Locking/Mutex.h>
|
||||
|
||||
namespace Kernel::USB {
|
||||
|
||||
|
@ -29,6 +31,11 @@ protected:
|
|||
IntrusiveListNode<SysFSUSBDeviceInformation, RefPtr<SysFSUSBDeviceInformation>> m_list_node;
|
||||
|
||||
NonnullRefPtr<USB::Device> m_device;
|
||||
|
||||
private:
|
||||
bool output(KBufferBuilder& builder);
|
||||
virtual KResult refresh_data(FileDescription& description) const override;
|
||||
mutable Mutex m_lock { "SysFSUSBDeviceInformation" };
|
||||
};
|
||||
|
||||
class SysFSUSBBusDirectory final : public SysFSDirectory {
|
||||
|
|
|
@ -96,6 +96,22 @@ SysFSInode::SysFSInode(SysFS const& fs, SysFSComponent const& component)
|
|||
{
|
||||
}
|
||||
|
||||
void SysFSInode::did_seek(FileDescription& description, off_t new_offset)
|
||||
{
|
||||
if (new_offset != 0)
|
||||
return;
|
||||
auto result = m_associated_component->refresh_data(description);
|
||||
if (result.is_error()) {
|
||||
// Subsequent calls to read will return EIO!
|
||||
dbgln("SysFS: Could not refresh contents: {}", result.error());
|
||||
}
|
||||
}
|
||||
|
||||
KResult SysFSInode::attach(FileDescription& description)
|
||||
{
|
||||
return m_associated_component->refresh_data(description);
|
||||
}
|
||||
|
||||
KResultOr<size_t> SysFSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* fd) const
|
||||
{
|
||||
return m_associated_component->read_bytes(offset, count, buffer, fd);
|
||||
|
|
|
@ -96,6 +96,9 @@ protected:
|
|||
virtual KResult chown(UserID, GroupID) override;
|
||||
virtual KResult truncate(u64) override;
|
||||
|
||||
virtual KResult attach(FileDescription& description) override final;
|
||||
virtual void did_seek(FileDescription&, off_t) override final;
|
||||
|
||||
NonnullRefPtr<SysFSComponent> m_associated_component;
|
||||
};
|
||||
|
||||
|
|
|
@ -12,12 +12,17 @@
|
|||
#include <AK/StringView.h>
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/FileSystem/File.h>
|
||||
#include <Kernel/FileSystem/FileDescription.h>
|
||||
#include <Kernel/FileSystem/FileSystem.h>
|
||||
#include <Kernel/Forward.h>
|
||||
#include <Kernel/KResult.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
struct SysFSInodeData : public FileDescriptionData {
|
||||
OwnPtr<KBuffer> buffer;
|
||||
};
|
||||
|
||||
class SysFSComponent : public RefCounted<SysFSComponent> {
|
||||
public:
|
||||
virtual StringView name() const { return m_name->view(); }
|
||||
|
@ -25,6 +30,7 @@ public:
|
|||
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const { VERIFY_NOT_REACHED(); }
|
||||
virtual RefPtr<SysFSComponent> lookup(StringView) { VERIFY_NOT_REACHED(); };
|
||||
virtual KResultOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, FileDescription*) { return EROFS; }
|
||||
virtual KResult refresh_data(FileDescription&) const { return KSuccess; }
|
||||
|
||||
virtual NonnullRefPtr<SysFSInode> to_inode(SysFS const&) const;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue