SeLinux Type(类型)深度解析

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 安全模型的核心,它:

  1. 定义安全边界:通过类型划分系统和应用组件的安全域
  2. 控制访问权限:基于源-目标类型组合决定访问权限
  3. 支持继承层次:通过属性系统建立类型层次结构
  4. 启用自动转换:支持进程域和文件类型的自动转换
  5. 提供细粒度控制:实现比传统 Linux 权限更精细的访问控制

正确理解和定义 Type 是开发安全 Android 系统的关键,它确保了系统组件之间的隔离和保护,防止权限提升和未授权访问。

相关推荐
2501_915921433 小时前
iOS混淆与IPA加固全流程(iOS混淆 IPA加固 Ipa Guard实战)
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者83 小时前
iOS 26 App 开发阶段性能优化 从多工具协作到数据驱动的实战体系
android·ios·小程序·uni-app·iphone·webview·1024程序员节
2501_915106324 小时前
深入剖析 iOS 26 系统流畅度,多工具协同监控与性能优化实践
android·ios·性能优化·小程序·uni-app·cocoa·iphone
小彤花园4 小时前
GooglePlay更改签名秘钥报错(2025最新版)
android·google
Answer_momo5 小时前
一文读懂 Kotlin 数据流 Flow 的使用
android
雨白5 小时前
Kotlin Flow 入门:构建响应式异步数据流
android·kotlin
阿里云云原生6 小时前
告别手动埋点!Android 无侵入式数据采集方案深度解析
android·云原生
Tlaster6 小时前
使用KMP实现原生UI + Compose混合的社交客户端
android·ios·开源
袁煦丞 cpolar内网穿透实验室7 小时前
安卓旧机变服务器,KSWEB部署Typecho博客并实现远程访问:cpolar内网穿透实验室第645个成功挑战
android·运维·服务器·远程工作·内网穿透·cpolar