Skip to content

Commit b27f88f

Browse files
supercomputer7linusg
authored andcommitted
Kernel+Userland: Refine preventing syscall annotations of Regions option
Instead of using a special case of the annotate_mapping syscall, let's introduce a new prctl option to disallow further annotations of Regions as new syscall Region(s).
1 parent 08de5ab commit b27f88f

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

Kernel/API/prctl_numbers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88

99
#define PR_SET_DUMPABLE 1
1010
#define PR_GET_DUMPABLE 2
11+
#define PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS 3
12+
#define PR_GET_NO_NEW_SYSCALL_REGION_ANNOTATIONS 4

Kernel/Syscalls/mmap.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,18 +578,16 @@ ErrorOr<FlatPtr> Process::sys$annotate_mapping(Userspace<void*> address, int fla
578578
if (flags == to_underlying(VirtualMemoryRangeFlags::None))
579579
return EINVAL;
580580

581+
if (!address)
582+
return EINVAL;
583+
581584
if (!Memory::is_user_address(address.vaddr()))
582585
return EFAULT;
583586

584587
return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
585588
if (space->enforces_syscall_regions() && (flags & to_underlying(VirtualMemoryRangeFlags::SyscallCode)))
586589
return EPERM;
587590

588-
if (!address) {
589-
space->set_enforces_syscall_regions(true);
590-
return 0;
591-
}
592-
593591
auto* region = space->find_region_containing(Memory::VirtualRange { address.vaddr(), 1 });
594592
if (!region)
595593
return EINVAL;

Kernel/Syscalls/prctl.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ ErrorOr<FlatPtr> Process::sys$prctl(int option, FlatPtr arg1, [[maybe_unused]] F
2121
return EINVAL;
2222
protected_data.dumpable = arg1;
2323
return 0;
24+
case PR_GET_NO_NEW_SYSCALL_REGION_ANNOTATIONS:
25+
return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
26+
return space->enforces_syscall_regions();
27+
});
28+
case PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS:
29+
if (arg1 != 0 && arg1 != 1)
30+
return EINVAL;
31+
bool prohibit_new_annotated_syscall_regions = (arg1 == 1);
32+
return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
33+
if (space->enforces_syscall_regions() && !prohibit_new_annotated_syscall_regions)
34+
return EPERM;
35+
36+
space->set_enforces_syscall_regions(prohibit_new_annotated_syscall_regions);
37+
return 0;
38+
});
39+
return 0;
2440
}
2541
return EINVAL;
2642
});

Userland/Libraries/LibELF/DynamicLinker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <AK/ScopeGuard.h>
1818
#include <AK/Vector.h>
1919
#include <Kernel/API/VirtualMemoryAnnotations.h>
20+
#include <Kernel/API/prctl_numbers.h>
2021
#include <LibC/bits/pthread_integration.h>
2122
#include <LibC/link.h>
2223
#include <LibC/sys/mman.h>
@@ -677,7 +678,7 @@ void ELF::DynamicLinker::linker_main(DeprecatedString&& main_program_path, int m
677678

678679
s_loaders.clear();
679680

680-
int rc = syscall(SC_annotate_mapping, nullptr);
681+
int rc = syscall(SC_prctl, PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS, 1, 0);
681682
if (rc < 0) {
682683
VERIFY_NOT_REACHED();
683684
}

0 commit comments

Comments
 (0)