限制 Linux 应用程序的系统调用访问

发布于 2024-08-18 23:18:57 字数 539 浏览 4 评论 0原文

假设 Linux 二进制文件 foobar 有两种不同的操作模式:

  • 模式 A:行为良好的模式,其中系统调用 ab 和 <使用代码>c。
  • 模式 B:一种错误模式,其中使用系统调用 abcd

系统调用 abc 是无害的,而系统调用 d 有潜在危险,可能会导致机器不稳定。

进一步假设应用程序运行的两种模式中的哪一种是随机的:应用程序在模式 A 中运行的概率为 95%,在模式 B 中运行的概率为 5%。该应用程序没有源代码,因此无法修改,只能按原样运行。

我想确保应用程序无法执行系统调用d。执行系统调用 d 时,结果应该是 NOOP 或立即终止应用程序。

如何在 Linux 环境中实现这一点?

Assume a Linux binary foobar which has two different modes of operation:

  • Mode A: A well-behaved mode in which syscalls a, b and c are used.
  • Mode B: A things-gone-wrong mode in which syscalls a, b, c and d are used.

Syscalls a, b and c are harmless, whereas syscall d is potentially dangerous and could cause instability to the machine.

Assume further that which of the two modes the application runs is random: the application runs in mode A with probability 95 % and in mode B with probability 5 %. The application comes without source code so it cannot be modified, only run as-is.

I want to make sure that the application cannot execute syscall d. When executing syscall d the result should be either a NOOP or an immediate termination of the application.

How do I achieve that in a Linux environment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩人痒 2024-08-25 23:18:57

应用程序是静态链接的吗?

如果没有,你可以重写一些符号,例如让我们重新定义socket

int socket(int domain, int type, int protocol)
{
        write(1,"Error\n",6);
        return -1;
}

然后构建一个共享库:

gcc -fPIC -shared test.c -o libtest.so

让我们运行:

nc -l -p 6000

好的。

现在:

$ LD_PRELOAD=./libtest.so nc -l -p 6000
Error
Can't get socket

当您使用变量 LD_PRELOAD=./libtest.so 运行时会发生什么?它使用 libtest.so 中定义的符号覆盖 C 库中定义的符号。

Is the application linked statically?

If not, you may override some symbols, for example, let's redefine socket:

int socket(int domain, int type, int protocol)
{
        write(1,"Error\n",6);
        return -1;
}

Then build a shared library:

gcc -fPIC -shared test.c -o libtest.so

Let's run:

nc -l -p 6000

Ok.

And now:

$ LD_PRELOAD=./libtest.so nc -l -p 6000
Error
Can't get socket

What happens when you run with variable LD_PRELOAD=./libtest.so? It overrides with symbols defined in libtest.so over those defined in the C library.

风流物 2024-08-25 23:18:57

看来 systrace 正是您所需要的。从维基百科页面

应用程序只能进行策略中指定的系统调用。如果应用程序尝试执行未明确允许的系统调用,则会引发警报。

It seems that systrace does exactly what you need. From the Wikipedia page:

An application is allowed to make only those system calls specified as permitted in the policy. If the application attempts to execute a system call that is not explicitly permitted an alarm gets raised.

硪扪都還晓 2024-08-25 23:18:57

这是沙箱(具体来说,基于规则的执行)的一种可能应用。一种流行的实现是 SELinux

您必须编写策略< /a> 对应于您希望允许进程执行的操作。

This is one possible application of sandboxing (specifically, Rule-based Execution). One popular implementation is SELinux.

You will have to write the policy that corresponds to what you want to allow the process to do.

踏雪无痕 2024-08-25 23:18:57

这正是 seccomp-bpf 的用途。请参阅示例如何限制对系统调用

That's exactly what seccomp-bpf is for. See an example how to restrict access to syscalls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文