Type 基本概念
Type 的定义
Type(类型) 是 SeLinux 安全上下文中最核心的组成部分,它定义了主体(进程)和客体(资源)的安全属性,是访问控制决策的主要依据。
Type 在上下文中的位置
bash
u:r:type:s0
└─────┬────┘
类型标识
Type 的分类体系
1. 域类型(Domain Types)
用于标识进程的安全属性,决定进程能执行什么操作。
系统关键域类型
sepolicy
# 系统核心进程
type system_server, domain; # 系统服务
type surfaceflinger, domain; # 显示服务
type mediaserver, domain; # 媒体服务
type init, domain; # init 进程
type kernel, domain; # 内核
# 应用进程
type system_app, domain; # 系统应用
type platform_app, domain; # 平台应用
type untrusted_app, domain; # 第三方应用
type priv_app, domain; # 特权应用
服务域类型
sepolicy
type netd, domain; # 网络服务
type vold, domain; # 卷管理
type zygote, domain; # 应用孵化器
type audioserver, domain; # 音频服务
type cameraserver, domain; # 相机服务
2. 文件类型(File Types)
用于标识文件系统对象的安全属性。
可执行文件类型
sepolicy
type init_exec, exec_type, file_type; # init 可执行文件
type system_file, file_type; # 系统文件
type vendor_file, file_type; # 供应商文件
type apk_data_file, file_type; # APK 数据文件
数据文件类型
sepolicy
type system_data_file, file_type; # 系统数据文件
type app_data_file, file_type; # 应用数据文件
type logd_data_file, file_type; # 日志数据文件
type tmpfs, file_type; # 临时文件系统
3. 设备类型(Device Types)
用于标识硬件设备的安全属性。
sepolicy
type device, dev_type; # 通用设备
type block_device, dev_type; # 块设备
type graphics_device, dev_type; # 图形设备
type audio_device, dev_type; # 音频设备
type camera_device, dev_type; # 相机设备
Type 的定义机制
基础类型定义语法
1. 简单类型定义
sepolicy
# 定义新类型
type my_daemon, domain;
type my_data_file, file_type;
type my_device, dev_type;
# 定义执行类型(用于可执行文件)
type my_daemon_exec, exec_type, file_type;
2. 属性关联
sepolicy
# 定义属性
attribute my_component_domain;
attribute my_component_file;
# 将类型关联到属性
typeattribute my_daemon my_component_domain;
typeattribute my_data_file my_component_file;
Type 的属性系统
属性定义示例
sepolicy
# 域属性定义
attribute domain; # 所有域类型
attribute coredomain; # 核心域
attribute appdomain; # 应用域
attribute mlstrustedsubject; # 多级安全信任主体
# 文件属性定义
attribute file_type; # 所有文件类型
attribute exec_type; # 可执行文件类型
attribute dev_type; # 设备文件类型
属性继承机制
sepolicy
# 通过属性组织相关类型
attribute platform_app_domain;
typeattribute system_app platform_app_domain;
typeattribute platform_app platform_app_domain;
# 基于属性的统一权限规则
allow platform_app_domain system_data_file:dir search;
Type 的创建和管理
完整的 Type 定义流程
1. 创建域类型定义文件
my_daemon.te:
sepolicy
# === 类型定义 ===
type my_daemon, domain; # 定义进程域类型
type my_daemon_exec, exec_type, file_type; # 定义可执行文件类型
# === 属性关联 ===
# 关联到标准属性集
typeattribute my_daemon coredomain;
typeattribute my_daemon mlstrustedsubject;
# === 域转换声明 ===
# 声明从 init 到 my_daemon 的域转换
type_transition init my_daemon_exec:process my_daemon;
# === 基本权限 ===
# 允许基本进程操作
allow my_daemon self:process { fork sigchld };
allow my_daemon my_daemon_exec:file { execute read open map };
# === 系统资源访问 ===
allow my_daemon system_prop:property_service { get set };
allow my_daemon logd_socket:sock_file write;
2. 文件上下文关联
file_contexts:
sepolicy
# 可执行文件类型关联
/system/bin/my_daemon u:object_r:my_daemon_exec:s0
# 数据文件类型关联
/data/vendor/my_daemon/.* u:object_r:my_daemon_data_file:s0
/vendor/etc/my_daemon/.* u:object_r:my_daemon_config_file:s0
# 设备文件类型关联
/dev/my_hardware_device u:object_r:my_device:s0
Type 的命名规范
Android 命名约定
sepolicy
# 进程域类型命名
system_app # 系统应用
vendor_hal_audio # 供应商音频 HAL
platform_service # 平台服务
# 文件类型命名
system_file # 系统文件
vendor_exec # 供应商可执行文件
app_data_file # 应用数据文件
# 设备类型命名
graphics_device # 图形设备
camera_device # 相机设备
block_device # 块设备
自定义类型命名最佳实践
sepolicy
# 公司/项目前缀
company_daemon # 公司守护进程
project_service # 项目服务
# 功能描述性命名
media_decoder # 媒体解码器
network_monitor # 网络监控器
sensor_processor # 传感器处理器
Type 在访问控制中的作用
访问决策基础
SeLinux 基于源类型和目标类型的组合来决定是否允许访问:
sepolicy
# 访问控制规则格式
allow source_type target_type:class permission;
# 实际示例
allow system_app system_data_file:file { read write };
# ↑源类型 ↑目标类型 ↑类别 ↑权限
实际决策流程
1. 进程访问文件
less
进程: system_app (u:r:system_app:s0)
↓ 尝试读取
文件: system_data_file (u:object_r:system_data_file:s0)
↓ SeLinux 检查
规则: allow system_app system_data_file:file read;
↓ 决策结果
允许访问 ✓
2. 进程间通信
php
进程A: platform_app (u:r:platform_app:s0)
↓ 尝试连接
进程B: my_daemon (u:r:my_daemon:s0)
↓ SeLinux 检查
规则: allow platform_app my_daemon:unix_stream_socket connectto;
↓ 决策结果
允许连接 ✓
Type 的继承和层次结构
属性继承体系
sepolicy
# 基础属性定义
attribute domain; # 所有进程域
attribute file_type; # 所有文件类型
# 专用属性继承
attribute coredomain; # ← 继承自 domain
attribute appdomain; # ← 继承自 domain
attribute exec_type; # ← 继承自 file_type
attribute system_file_type; # ← 继承自 file_type
实际继承示例
sepolicy
# 定义属性层次
attribute my_company_domain;
attribute my_company_service extends my_company_domain;
attribute my_company_daemon extends my_company_domain;
# 类型关联
type my_audio_service, domain;
typeattribute my_audio_service my_company_service;
type my_network_daemon, domain;
typeattribute my_network_daemon my_company_daemon;
# 基于继承的权限
allow my_company_domain system_prop:property_service get;
allow my_company_service logd_socket:sock_file write;
allow my_company_daemon self:capability net_admin;
Type 转换机制
进程域转换
1. 自动域转换
sepolicy
# 当 init 执行 my_daemon_exec 时自动转换到 my_daemon 域
type_transition init my_daemon_exec:process my_daemon;
# 转换规则
allow init my_daemon_exec:file execute; # 允许执行
allow init my_daemon:process transition; # 允许域转换
allow my_daemon my_daemon_exec:file entrypoint; # 允许作为入口点
2. 手动域转换
c
// 在代码中手动设置进程安全上下文
#include <selinux/selinux.h>
int set_process_security_context(const char* context) {
return setcon(context);
}
// 使用示例
set_process_security_context("u:r:my_daemon:s0");
文件类型转换
创建时的类型转换
sepolicy
# 当 my_daemon 在 system_data_file 目录创建文件时,自动设置为 my_data_file 类型
type_transition my_daemon system_data_file:dir my_data_file;
# 需要的支持规则
allow my_daemon system_data_file:dir { write add_name };
allow my_daemon my_data_file:file { create setattr };
实际案例分析
Android 系统 Type 定义实例
System Server 类型定义
sepolicy
# frameworks/base/sepolicy/system_server.te
# 类型定义
type system_server, domain;
type system_server_exec, exec_type, file_type;
# 属性关联
typeattribute system_server coredomain;
typeattribute system_server mlstrustedsubject;
# 域转换
type_transition init system_server_exec:process system_server;
# 核心权限
allow system_server self:process { execmem };
allow system_server system_server_exec:file { execute read map };
# 系统服务权限
allow system_server activity_service:service_manager find;
allow system_server package_service:service_manager find;
allow system_server window_service:service_manager find;
应用数据文件类型定义
sepolicy
# frameworks/base/sepolicy/app.te
# 应用数据文件类型
type app_data_file, file_type;
type system_app_data_file, file_type;
type platform_app_data_file, file_type;
# 文件上下文关联(在 file_contexts 中)
/data/data/.*/ u:object_r:app_data_file:s0
/data/system/.* u:object_r:system_data_file:s0
/data/app/.* u:object_r:apk_data_file:s0
自定义服务 Type 完整示例
1. 类型定义文件
vendor/company/sepolicy/my_service.te:
sepolicy
# === 类型定义 ===
type my_service, domain;
type my_service_exec, exec_type, file_type;
type my_service_data_file, file_type;
type my_service_socket, file_type;
# === 属性关联 ===
typeattribute my_service coredomain;
# === 域转换 ===
type_transition init my_service_exec:process my_service;
# === 基本权限 ===
allow my_service self:process { fork sigchld };
allow my_service my_service_exec:file { execute read map };
# === 数据文件访问 ===
allow my_service my_service_data_file:dir { search write };
allow my_service my_service_data_file:file { create read write };
# === 套接字通信 ===
allow my_service my_service_socket:sock_file { create write };
allow my_service self:unix_stream_socket { connectto };
# === 系统资源 ===
allow my_service system_prop:property_service { get set };
allow my_service logd_socket:sock_file write;
2. 文件上下文配置
vendor/company/sepolicy/file_contexts:
sepolicy
# 可执行文件
/vendor/bin/my_service u:object_r:my_service_exec:s0
# 数据目录
/data/vendor/my_service/.* u:object_r:my_service_data_file:s0
# 套接字文件
/dev/socket/my_service u:object_r:my_service_socket:s0
Type 的调试和验证
类型验证命令
bash
# 查看进程类型
ps -Z | grep my_service
# 输出: u:r:my_service:s0
# 查看文件类型
ls -Z /vendor/bin/my_service
# 输出: u:object_r:my_service_exec:s0
# 验证域转换
cat /proc/$(pidof my_service)/attr/current
# 输出: u:r:my_service:s0
类型冲突检测
bash
# 检查类型定义冲突
sepolicy-check -s my_service /vendor/etc/selinux/
# 验证类型使用
sesearch -A -s my_service -t system_data_file
# 检查类型权限
sepolicy-analyze -d my_service /vendor/etc/selinux/vendor_sepolicy.cil
总结
Type 是 SeLinux 安全模型的核心,它:
- 定义安全边界:通过类型划分系统和应用组件的安全域
- 控制访问权限:基于源-目标类型组合决定访问权限
- 支持继承层次:通过属性系统建立类型层次结构
- 启用自动转换:支持进程域和文件类型的自动转换
- 提供细粒度控制:实现比传统 Linux 权限更精细的访问控制
正确理解和定义 Type 是开发安全 Android 系统的关键,它确保了系统组件之间的隔离和保护,防止权限提升和未授权访问。