1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:38:10 +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:
Liav A 2021-09-04 16:23:45 +03:00 committed by Andreas Kling
parent a595345e7c
commit e490c17bde
5 changed files with 79 additions and 7 deletions

View file

@ -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;
}