Kernel: mmap() should fail with ENODEV for directories

This commit is contained in:
Andreas Kling 2020-01-08 12:33:36 +01:00
parent 3f35cd2f7d
commit 50056d1d84
Notes: sideshowbarker 2024-07-19 10:16:21 +09:00
2 changed files with 19 additions and 0 deletions

View file

@ -339,6 +339,8 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params)
auto description = file_description(fd);
if (!description)
return (void*)-EBADF;
if (description->is_directory())
return (void*)-ENODEV;
if ((prot & PROT_READ) && !description->is_readable())
return (void*)-EACCES;
if ((prot & PROT_WRITE) && !description->is_writable())

View file

@ -105,6 +105,22 @@ void test_ftruncate_negative()
close(fd);
}
void test_mmap_directory()
{
int fd = open("/tmp", O_RDONLY | O_DIRECTORY);
ASSERT(fd >= 0);
auto* ptr = mmap(nullptr, 4096, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
if (ptr != MAP_FAILED) {
fprintf(stderr, "Boo! mmap() of a directory succeeded!\n");
return;
}
if (errno != ENODEV) {
fprintf(stderr, "Boo! mmap() of a directory gave errno=%d instead of ENODEV!\n", errno);
return;
}
close(fd);
}
int main(int, char**)
{
int rc;
@ -123,6 +139,7 @@ int main(int, char**)
test_read_past_eof();
test_ftruncate_readonly();
test_ftruncate_negative();
test_mmap_directory();
return 0;
}