Linux/Android之seccomp介绍及使用(一百一十六)
2023-09-14 09:09:56 时间
1.背景
seccomp是Linux的一种安全机制,android 8.1以后版本使用seccomp.
主要功能是限制直接通过syscall去调用某些系统函数
2.功能介绍
1.linux的沙箱机制,可以限制进程对系统调用的访问,从系统调用号,到系统调用的参数,都可以检查和限制.
2.有两种模式
SECCOMP_MODE_STRICT, 进程只能访问read,write,_exit,sigreturn系统调用.
SECCOM_MODE_FILTER,通过设置bpf规则,来过滤和检查系统调用号,和系统调用参数,来决定对进程访问系
统调用的处理.
3.systemd,container都使用seccomp机制来限定对进程的对系统调用的访问权限.
3.strict模式和filter模式应用
1.strict模式
# emacs strict.c
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
int main(){
int output = open("test.txt", O_WRONLY);
const char *val = "test";
//1.通过prctl函数设置seccomp的模式为strict
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
//写入
write(output, val, strlen(val)+1);
//设置完毕seccomp以后再次尝试open,设置了secomp的模式是strict,直接sign -9 信号
int input = open("test.txt", O_RDONLY);
}
2.基于BPF(伯克利分组过滤器)的seccomp
安装依赖包
# sudo apt install libseccomp-dev
上述基于prctl系统调用的seccomp机制不够灵活,在linux 3.5之后引入了基于BPF的可定制的系统调用过滤功能。
# emacs filter.c
#include <stdio.h>
#include <unistd.h>
#include <seccomp.h>
int main() {
printf("step 1: unrestricted\n");
// Init the filter
scmp_filter_ctx ctx;
ctx = seccomp_init(SCMP_ACT_KILL); // default action: kill
// setup basic whitelist
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigreturn), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
// setup our rule
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(dup2), 2, SCMP_A0(SCMP_CMP_EQ, 1),SCMP_A1(SCMP_CMP_EQ, 2));
// build and load the filter
seccomp_load(ctx);
printf("step 2: only 'write' and dup2(1, 2) syscalls\n");
// Redirect stderr to stdout
dup2(1, 2);
printf("step 3: stderr redirected to stdout\n");
// Duplicate stderr to arbitrary fd
dup2(2, 42);
printf("step 4: !! YOU SHOULD NOT SEE ME !!\n");
// Success (well, not so in this case...)
return 0;
}
相关文章
- ucosii操作系统和linux,请高手介绍下uCOSII和Linux的差异?
- Android剪裁图片简单的方法详解手机开发
- 结构Linux下查看目录结构的方法(linux如何查看目录)
- 编辑Linux文本网页编辑技巧介绍(linux文本网页)
- 开启Linux之旅:使用RLinux系统(r-linux)
- 熟悉Linux,不再迷失——常用Linux系统介绍(常用的linux系统)
- Android与Linux:一段持久而又充满变数的联系(安卓内核linux)
- 为 Linux 开发提供强劲支持:软件开发工具介绍(linux软件开发工具)
- Linux虚拟IP绑定:一种快捷有效的网络配置方案(linux虚拟ip绑定)
- 探究 Linux 内核构成的奥秘(linux内核组成)
- Linux系统的主要版本介绍(linux主要版本)
- Linux环境中如何顺利执行Java程序?(linux下执行java)
- 探索Linux世界之Android厨房:DIY个性化安卓系统(linux安卓厨房)
- Linux下du命令使用介绍(linux下du)
- Linux扫描和存储功能介绍(linux扫描存储)
- Linux下开启百度音乐聆听乐章(linux百度音乐)
- Linux工程师:介绍我的技能与经验(linux工程师简历)
- Linux下快速修改密码的简单命令(linux 修改密码命令)
- 如何在 Linux 中查看 IP 地址?(linux查看ip)
- Linux 命令大全:详细介绍常用命令及数量(linux有多少命令)
- Linux进程内存空间:最佳布局(linux 进程内存布局)
- 精通Linux:轻松掌握选择题技巧(linux选择题)
- Linux下查看文件行数简易操作(linux 查看文件行数)
- Linux介绍PPT:介绍Linux系统新朋友(linux简介ppt)
- 极速访问:Linux系统远程登录方式介绍(linux系统 远程登录)