Linux命令-sesearch(SELinux 策略规则搜索)
快速参考
sesearch 用于在 SELinux 策略中搜索允许的规则。它可以查找哪些类型可以访问哪些类型、哪些域可以执行哪些程序等,是分析和调试 SELinux 策略的核心工具。与 seinfo(查询策略对象)不同,sesearch 查询的是策略规则。
应用场景:分析 SELinux 拒绝原因、查找类型之间的允许关系、验证策略修改是否生效。
基本语法
bash
# 基本语法
sesearch [选项] [策略文件]
# 查找允许的规则
sesearch --allow
# 查找特定源类型的允许规则
sesearch --allow -s httpd_t
# 查找特定目标类型的允许规则
sesearch --allow -t httpd_sys_content_t
# 查找特定类别的规则
sesearch --allow -c file
安装 sesearch
bash
# Debian/Ubuntu
sudo apt update
sudo apt install setools
# RHEL/CentOS
sudo yum install setools-console
# Fedora
sudo dnf install setools-console
# 验证安装
which sesearch
sesearch --help
常用选项
bash
# --allow 查找 allow 规则
sesearch --allow -s httpd_t
# --neverallow 查找 neverallow 规则
sesearch --neverallow
# --auditallow 查找 auditallow 规则
sesearch --auditallow
# --dontaudit 查找 dontaudit 规则
sesearch --dontaudit
# -s 源类型 指定源类型(subject)
sesearch --allow -s httpd_t
# -t 目标类型 指定目标类型(object)
sesearch --allow -t httpd_sys_content_t
# -c 类别 指定对象类别(file, dir, lnk_file 等)
sesearch --allow -c file
# --source 仅显示源类型
sesearch --allow -s httpd_t --source
# --target 仅显示目标类型
sesearch --allow -t httpd_sys_content_t --target
# --count 仅统计数量
sesearch --allow -s httpd_t --count
实际应用场景
bash
# === 场景1:分析 HTTP 服务可以访问哪些文件 ===
# 查找 httpd_t 域可以读取哪些文件类型
sesearch --allow -s httpd_t -c file -p read
# 查找 httpd_t 域可以写入哪些文件类型
sesearch --allow -s httpd_t -c file -p write
# 查看 httpd_t 域的所有允许规则
sesearch --allow -s httpd_t | less
# === 场景2:分析为什么被 SELinux 拒绝 ===
# 1. 查看审计日志
sudo ausearch -m AVC -ts recent
# 2. 找出阻止的操作(例如:httpd 访问 user_home_t)
# 日志显示:denied { read } for pid=... comm="httpd" name="index.html" dev="sda2" ino=... scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:user_home_t:s0 tclass=file
# 3. 查找是否允许 httpd_t 读取 user_home_t
sesearch --allow -s httpd_t -t user_home_t -c file -p read
# 如果无输出,说明策略不允许
# 4. 解决方案:修改布尔值或策略
sudo setsebool -P httpd_enable_homedirs on
# === 场景3:验证策略修改是否生效 ===
# 1. 添加自定义策略规则(需要编写 .te 文件并编译)
# ...(略)
# 2. 验证规则已添加
sesearch --allow -s myapp_t -t etc_t -c file
# === 场景4:列出所有域转换规则 ===
# 查找哪些域可以转换到 httpd_t
sesearch --type_trans -t httpd_t
# 查找 httpd_t 可以转换到哪些域
sesearch --type_trans -s httpd_t
规则类型详解
bash
# === allow 规则(允许)===
# 语法:allow 源类型 目标类型:类别 权限;
# 示例:allow httpd_t httpd_sys_content_t:file read;
# 查找所有 allow 规则
sesearch --allow | head -20
# === neverallow 规则(禁止)===
# 语法:neverallow 源类型 目标类型:类别 权限;
# 示例:neverallow ~admin_t ~security_t:security load_policy;
# 查找所有 neverallow 规则
sesearch --neverallow
# === auditallow 规则(审计允许)===
# 即使允许,也记录审计日志
sesearch --auditallow
# === dontaudit 规则(不审计)===
# 即使拒绝,也不记录审计日志(减少日志噪音)
sesearch --dontaudit
与 seinfo 配合使用
bash
# seinfo 查询策略对象,sesearch 查询策略规则
# 1. 先用 seinfo 查找相关类型
seinfo -t | grep httpd
# 2. 再用 sesearch 查找相关规则
sesearch --allow -s httpd_t
# 3. 查找某个类型可以访问哪些文件类型
sesearch --allow -s httpd_t -c file | awk '{print $3}' | sort -u
# 4. 查找某个文件类型可以被哪些域访问
sesearch --allow -t httpd_sys_content_t -c file | awk '{print $1}' | sort -u
高级搜索
bash
# === 搜索特定权限 ===
# 查找所有可以读取文件的规则
sesearch --allow -c file -p read
# 查找所有可以写入文件的规则
sesearch --allow -c file -p write
# 查找所有可以执行文件的规则
sesearch --allow -c file -p execute
# === 搜索特定类别 ===
# 文件
sesearch --allow -c file
# 目录
sesearch --allow -c dir
# 符号链接
sesearch --allow -c lnk_file
# 套接字
sesearch --allow -c unix_stream_socket
# === 搜索域转换 ===
# 查找可以转换到 httpd_t 的规则
sesearch --type_trans -t httpd_t
# 查找 httpd_t 可以转换到哪些类型
sesearch --type_trans -s httpd_t
# === 搜索角色允许 ===
sesearch --role_allow
故障排查
bash
# 问题1:sesearch 命令找不到
# 解决:安装 setools
sudo apt install setools
# 问题2:sesearch 报错 "policy version mismatch"
# 原因:策略版本不匹配
# 解决:更新 policycoreutils
sudo apt install policycoreutils
# 问题3:搜索无结果
# 原因:类型名错误,或确实无此规则
# 解决:先用 seinfo 确认类型存在
seinfo -t | grep "类型名"
总结
核心价值 :
sesearch是分析和调试 SELinux 策略的必备工具,帮助理解"为什么被拒绝"和"如何修改策略"。
常用组合:
sesearch --allow -s 源类型:查找源类型的所有允许规则sesearch --allow -t 目标类型:查找目标类型的所有允许规则sesearch --allow -s 源 -t 目标 -c 类别 -p 权限:精确查找seinfo -t | grep 关键词:先查找相关类型,再用 sesearch 搜索规则
注意:修改 SELinux 策略需要谨慎,错误的策略可能导致服务无法运行或安全漏洞。