selinux avc 打标签

selinux

1、一个安全上下文包含三个元素:用户(user)、角色(role)和类型标识符(type identifiers)

安全上下文的形式如下:user:role:type

2、对进程来说:分别表示用户、角色、类型标识符也被称为域
例如配置的进程 selinux

ps -Al -Z

LABEL                          F S   UID   PID  PPID C PRI  NI BIT    SZ WCHAN  TTY          TIME CMD
u:r:init:s0                    4 S     0     1     0 0  19   0  32  1409 do_epoll_wait ? 00:00:03 init
...
u:r:watchdog_service:s0        4 S  1100   227     1 0  19   0  32   811 hrtimer_nanosleep ? 00:00:00 watchdog_servic
u:r:hilogd:s0                  4 S  1036   236     1 0  19   0  32  8166 do_sys_poll ?   00:03:02 hilogd
u:r:hdf_devmgr:s0              4 S  3044   237     1 0  19   0  32  1641 futex_wait_queue_me ? 00:00:00 hdf_devmgr
u:r:hiview:s0                  4 S  1201   238     1 0  19   0  32  7491 binder_ioctl_write_read ? 00:00:42 hiview
u:r:storage_manager:s0         4 S  1090   239     1 0  19   0  32  8164 binder_ioctl_write_read ? 00:00:34 storage_manager
u:r:device_manager:s0          4 S  3062   240     1 0  19   0  32  3110 binder_ioctl_write_read ? 00:00:09 device_manager
u:r:samgr:s0                   4 S  5555   241     1 0  19   0  32  3615 binder_ioctl_write_read ? 00:00:22 samgr
u:r:storage_daemon:s0          4 S     0   242     1 0  19   0  32  2938 binder_ioctl_write_read ? 00:00:10 storage_daemon
u:r:param_watcher:s0           4 S  1101   243     1 0  19   0  32  3249 binder_ioctl_write_read ? 00:00:00 param_watcher
u:r:appspawn:s0                4 S     0   244     1 0  39 -20  32 158278 do_epoll_wait ? 00:00:01 appspawn

3、对客体来说:前两项基本没有实际用途,role通常为object_r,user通常位创建这个对象的进程的user,对访问控制没有影响
例如 /system/bin/xxx 下面的文件

-rwxr-xr-x 1 shell root u:object_r:hdcd_exec:s0                 358848 2023-12-04 17:54 hdcd
lrwxr-xr-x 1 shell root u:object_r:system_bin_file:s0                6 2023-12-04 17:55 head -> toybox
lrwxr-xr-x 1 shell root u:object_r:system_bin_file:s0                6 2023-12-04 17:55 help -> toybox
lrwxr-xr-x 1 shell root u:object_r:system_bin_file:s0                6 2023-12-04 17:55 hexedit -> toybox
-rwxr-xr-x 1 shell root u:object_r:hidumper_exec:s0              46544 2023-12-04 17:54 hidumper
-rwxr-xr-x 1 shell root u:object_r:hihopelog_exec:s0              3770 2023-12-04 17:54 hihope.boot.log.sh

例如etc 下的文件

-rw-r--r--  1 root root u:object_r:system_etc_file:s0      797 2023-12-04 17:54 access_token.cfg
-rw-r--r--  1 root root u:object_r:system_etc_file:s0      672 2023-12-04 17:54 accessibility.cfg
-rw-r--r--  1 root root u:object_r:system_etc_file:s0     1606 2023-12-04 17:55 accountmgr.cfg
-rw-r--r--  1 root root u:object_r:system_etc_file:s0      815 2023-12-04 17:54 appspawn.cfg
-rw-r--r--  1 root root u:object_r:system_etc_file:s0     1091 2023-12-04 17:55 audio_policy.cfg
-rw-r--r--  1 root root u:object_r:system_etc_file:s0      770 2023-12-04 17:55 avsession_service.cfg
-rw-r--r--  1 root root u:object_r:system_etc_file:s0      676 2023-12-04 17:54 bgtaskmgr_service.cfg

关于param 参数设置selinux 标签

# param ls -r
Parameter information:
selinux  : u:object_r:musl_param:s0
    dac  : root(rww) root(rww) (r--)
    name : musl.log.enable
    value: false
Parameter information:
selinux  : u:object_r:musl_param:s0
    dac  : root(rww) root(rww) (r--)
    name : musl.log.ld.all
    value: true
Parameter information:
selinux  : u:object_r:hook_param:s0
    dac  : root(rww) shell(rww) (r-w)
    name : libc.hook_mode
    value:
Parameter information:
selinux  : u:object_r:ohos_boot_param:s0
    dac  : system(rww) bootctrl(rww) (r-w)
    name : ohos.boot.hardware
    value: rk3568
...
Parameter information:
selinux  : u:object_r:devinfo_public_param:s0
    dac  : root(rww) root(rww) (r-w)
    name : const.product.name
    value: OpenHarmony 3.2
Parameter information:
selinux  : u:object_r:devinfo_public_param:s0
    dac  : root(rww) root(rww) (r-w)
    name : const.product.cpu.abilist
    value: default
Parameter information:
selinux  : u:object_r:devinfo_public_param:s0
    dac  : root(rww) root(rww) (r-w)
    name : const.product.devicetype
    value: default
Parameter information:
selinux  : u:object_r:devinfo_public_param:s0
    dac  : root(rww) root(rww) (r-w)
    name : const.product.brand
    value: default
...
Parameter information:
selinux  : u:object_r:startup_init_param:s0
    dac  : root(rww) root(rww) (r-w)
    name : persist.init.bootevent.enable
    value: true
Parameter information:
selinux  : u:object_r:startup_init_param:s0
    dac  : system(rww) servicectrl(rww) (r-w)
    name : startup.service.ctl.ueventd
    value: 5
Parameter information:
selinux  : u:object_r:startup_init_param:s0
    dac  : system(rww) servicectrl(rww) (r-w)
    name : startup.service.ctl.hilogd
    value: 2

3、te 访问规则
allow Source type(s) Target type(s): Object class(es) Permission(s)
比如这样的访问规则:

allow user_t bin_t : file {read execute getattr};

表示允许域为user_t的进程对type为bin_t的文件具有读、执行、得到属性的操作

4、class SELinux的客体除了有type,还有class,进一步对客体的类型进行划分
SELinux中几个常用的客体类别(class),及其权限

file:read、write、execute、getattr、create
dir:read、write、search、rmdir
process:signal、transition(域的转换)、fork、getattr
socket:bind、listen、connect、accept
filesystem:mount、unmount

5、te 策略
在TE模型中,主体通常是正在运行的进程,而不是用户。客体可以是文件、进程、socket等。
TE规则主要分为两大类:access vector(AV)和type rules,AV允许审计,而type rules决定控制策略
1)类型(type)、属性(attributes)、别名(aliases)
type:在selinux中,将对资源的访问抽象为主体对客体的访问,主体分为多个type,也叫做域,客体也分为多个type,每个type里还能更加细分出class,selinux的访问规则就是基于type建立的规则,所以叫TE模型。

attributes:用来引用一组具有相同标识符的一组类型

alases:对于策略而言,别名标准符和类型标识符是一致的

一个type申明语句的格式如下,中括号中代表的是可选项:

type type_name [alias alias_set] [, attribute_set]

例子:type httpd_user_content_t ,file_type
例子:

举一个例子说明属性的作用

假设现在新建了一个type为backup_t:

type backup_t;

需要对backup_t赋予所有文件的读权限,如果系统中有shadow_t文件和httpd_user_content_t类型的文件,那么需要以下两条的规则来赋予文件读权限

allow backup_t httpd_user_content_t :file read

allow backup_t shadow_t:file read

假设现在多了一个类型的文件,那么就需要重新为backup_t新增一条规则,这样不仅麻烦而且错误率很高

现在有了一个属性file_type

attribute file_type;

然后将所有具有file_type属性的文件的读权限赋予backup_t

allow backup_t file_type:file read;

这样,以后每次有新的文件type出现,只需要将新文件的type域file_type属性关联起来就行,backup_t会自动获得读权限

以上的例子简单说明了属性的使用,一个type可以关联到多个属性,将type和attribute关联起来可以使用typeattribute,格式为:

typeattribute type_name attrib_name

例子如下:
attribute file_type
allow backup_t file_type:file read
typeattribute httpd_user_content_t file_type
这样处理之后,后面如果新增客体文件的话 只需要使用 typeattribute xxx file_type 把新的客体文件和file_type 关联

别名用来保证兼容性,使用typealias来关联,格式如下:

typealias type_name alias alias_names;

6、对于主体 进程来说 设置 selinux 标签 只需要在 服务中 配置 “secon” : “u:r:xxx:s0”,
例如

{
    "jobs" : [{
            "name" : "boot",
            "cmds" : [
                "write /proc/sys/vm/swappiness 150",
                "write /sys/block/zram0/comp_algorithm lzo-rle"
            ]
        }
    ],
    "services" : [{
            "name" : "hihope_zram",
            "path" : ["/system/bin/hihope_zram", "/vendor/etc/hihope.fstab.enableswap"],
            "uid" : "root",
            "gid" : ["root", "system"],
            "secon" : "u:r:hihope_zram:s0",
            "once" : 1
        }
    ]
}

对于客体的来说
需要设置 file_contexts
例如 /system/bin/hihope_zram u:object_r:hihope_zram_exec:s0

7、对于 type 的理解
例如上面 定义的两个selinux 标签 一个是 服务的hihope_zram 即主体;一个是客体的hihope_zram_exec;
那么需要在 定义两个type
type hihope_zram;
type hihope_zram_exec;

看下 下面的例子

type hihope_zram_exec, exec_attr, file_attr, system_file_attr;

type hihope_zram, nativedomain, domain;

其中给hihope_zram_exec 后面添加了attribute: exec_attr, file_attr, system_file_attr 这些 attribute 是在base/security/selinux 中添加的属性;

8、对于avc te标签采用 ./audit2allow -i avc.log > local.te 工具生成 ,其中 avc.log 文件 是通过 dmesg |grep avc 过滤出来的。
生成的local.te 文件 如下

#============= stress_testing ==============
allow stress_testing dev_console_file:chr_file { read write };
allow stress_testing dev_unix_socket:dir search;
allow stress_testing distributeddata:fd use;
allow stress_testing hilog_param:file { map open read };
allow stress_testing kernel:unix_stream_socket connectto;
allow stress_testing musl_param:file { map open read };
allow stress_testing persist_param:parameter_service set;
allow stress_testing self:unix_dgram_socket setopt;
allow stress_testing stress_testing_exec:file { entrypoint execute map read };

#============= system_basic_hap ==============
allow system_basic_hap limit_domain:binder call;

#============= system_core_hap ==============
allow system_core_hap appspawn:unix_stream_socket { read write };
allow system_core_hap dev_mali:chr_file ioctl;
allow system_core_hap distributeddata:fd use;

#============= telephony_sa ==============
allow telephony_sa system_file:file { getattr open read };

#============= ueventd ==============
allow ueventd musl_param:file { map open read };

#============= watchdog_service ==============
allow watchdog_service dev_console_file:chr_file { read write };
allow watchdog_service musl_param:file read;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值