UserlandEmulator: Fix data loss in realpath/readlink

This 'data loss' was introduced in 809a8ee693, because
I hoped we could eventually outlaw overlong paths entirely. This sparked some discussion:
https://github.com/SerenityOS/serenity/discussions/4357
Among other things, we agree that yeah, the Kernel can and should be able to return
paths of arbitrary length. This means that the 'arbitrary' maximum of PATH_MAX in
UserspaceEmulator should be considered to be unnecessary data loss, and as such, needs to
be fixed.
This commit is contained in:
Ben Wiederhake 2021-01-15 21:35:46 +01:00 committed by Andreas Kling
parent 5dc29065e1
commit 7ed002d1ca
Notes: sideshowbarker 2024-07-18 23:10:07 +09:00

View File

@ -1364,20 +1364,16 @@ int Emulator::virt$realpath(FlatPtr params_addr)
Syscall::SC_realpath_params params;
mmu().copy_from_vm(&params, params_addr, sizeof(params));
if (params.path.length > PATH_MAX) {
return -ENAMETOOLONG;
}
auto path = mmu().copy_buffer_from_vm((FlatPtr)params.path.characters, params.path.length);
char host_buffer[PATH_MAX] = {};
size_t host_buffer_size = min(sizeof(host_buffer), params.buffer.size);
auto host_buffer = ByteBuffer::create_zeroed(params.buffer.size);
Syscall::SC_realpath_params host_params;
host_params.path = { (const char*)path.data(), path.size() };
host_params.buffer = { host_buffer, host_buffer_size };
host_params.buffer = { (char*)host_buffer.data(), host_buffer.size() };
int rc = syscall(SC_realpath, &host_params);
if (rc < 0)
return rc;
mmu().copy_to_vm((FlatPtr)params.buffer.data, host_buffer, host_buffer_size);
mmu().copy_to_vm((FlatPtr)params.buffer.data, host_buffer.data(), host_buffer.size());
return rc;
}
@ -1759,20 +1755,16 @@ int Emulator::virt$readlink(FlatPtr params_addr)
Syscall::SC_readlink_params params;
mmu().copy_from_vm(&params, params_addr, sizeof(params));
if (params.path.length > PATH_MAX) {
return -ENAMETOOLONG;
}
auto path = mmu().copy_buffer_from_vm((FlatPtr)params.path.characters, params.path.length);
char host_buffer[PATH_MAX] = {};
size_t host_buffer_size = min(sizeof(host_buffer), params.buffer.size);
auto host_buffer = ByteBuffer::create_zeroed(params.buffer.size);
Syscall::SC_readlink_params host_params;
host_params.path = { (const char*)path.data(), path.size() };
host_params.buffer = { host_buffer, host_buffer_size };
host_params.buffer = { (char*)host_buffer.data(), host_buffer.size() };
int rc = syscall(SC_readlink, &host_params);
if (rc < 0)
return rc;
mmu().copy_to_vm((FlatPtr)params.buffer.data, host_buffer, host_buffer_size);
mmu().copy_to_vm((FlatPtr)params.buffer.data, host_buffer.data(), host_buffer.size());
return rc;
}