Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rules for mmap and dup #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/seccomp/policy/DefaultPolicy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,19 @@ void DefaultPolicy::addExecutionControlRules(bool allowFork) {
void DefaultPolicy::addMemoryManagementRules() {
allowSyscalls(
{"brk",
"mmap",
"mmap2",
"munmap",
"mremap",
"mprotect",
"arch_prctl"});

// Allow mmap and mmap2 only on fd >= 3
for (const auto& syscall: {"mmap", "mmap2"}) {
rules_.emplace_back(SeccompRule(
syscall,
action::ActionAllow(),
filter::SyscallArg(4) >= 3));
Comment on lines +113 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, when this rule fails, we kill the tracee with "intercepted forbidden syscall", right?
Can we add a second rule which, when the fd is not >= 3, we return EACCES? That means

EACCES A file descriptor refers to a non-regular file. Or a file mapping was requested, but fd is not open for reading. Or MAP_SHARED was requested and PROT_WRITE is set, but fd is not open in read/write (O_RDWR) mode. Or PROT_WRITE is set, but the file is append-only.

which should basically tell whoever tried to mmap that this file is not mmapable and they should proceed with regular reads instead.

Also, I think we also need to handle MAP_ANONYMOUS somehow, so that the process can allocate memory? On my system it appears even in strace true, probably ld.so or libc prologue uses it.

The manpage says in case of MAP_ANONYMOUS

The fd argument is ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this.

If our conditions treat args as unsigned then -1 would be >= 3 and that'd pass through, but I'm not sure if I want to rely on programs passing -1 as fd when the kernel doesn't require it...
Maybe we could have another rule for MAP_ANONYMOUS?

}

rules_.emplace_back(SeccompRule{"madvise", action::ActionErrno{EINVAL}});
}

Expand All @@ -128,11 +134,19 @@ void DefaultPolicy::addInputOutputRules() {
syscall, action::ActionAllow(), filter::SyscallArg(0) > 0));
}

// Allow dup only on fd >= 3
rules_.emplace_back(SeccompRule(
"dup2", action::ActionAllow(), filter::SyscallArg(1) >= 3));
"dup", action::ActionAllow(), filter::SyscallArg(0) >= 3));
for (const auto& syscall: {"dup2", "dup3"}) {
rules_.emplace_back(SeccompRule(
syscall,
action::ActionAllow(),
filter::SyscallArg(0) >= 3 &&
filter::SyscallArg(1) >= 3));
}

// Allow reading from any file descriptor
allowSyscalls({"read", "readv", "dup", "fcntl", "fcntl64", "pread64"});
allowSyscalls({"read", "readv", "fcntl", "fcntl64", "pread64"});

rules_.emplace_back(SeccompRule("ioctl", action::ActionErrno(ENOTTY)));

Expand Down