4.7. SELinux 上下文 - 标记文件


在运行 SELinux 的系统上,所有进程和文件都采用代表安全相关信息的标记方式。此信息称为 SELinux 上下文。对于文件,这可使用 ls -Z 命令查看:
~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
在这个示例中,SELinux 提供了一个用户(unconfined_u)、角色(object_r)、类型(user_home_t)和级别(s0)。此信息用于制定访问控制决策。在 DAC 系统上,访问权限根据 Linux 用户和组群 ID 进行控制。在 DAC 规则后检查 SELinux 策略规则。如果 DAC 规则首先拒绝访问,则不会使用 SELinux 策略规则。
注意
默认情况下,新建的文件和目录继承其父目录的 SELinux 类型。例如,当在 /etc 目录中创建带有 etc_t 类型的新文件时,新文件将继承相同的类型:
~]$ ls -dZ - /etc
drwxr-xr-x. root root system_u:object_r:etc_t:s0       /etc
~]# touch /etc/file1
~]# ls -lZ /etc/file1
-rw-r--r--. root root unconfined_u:object_r:etc_t:s0   /etc/file1
SELinux 提供多个命令来管理文件系统标记,如 chconsemanage fcontextrestoreconmatchpathcon

4.7.1. 临时更改:chcon

chcon 命令更改文件的 SELinux 上下文。但是,使用 chcon 命令所做的更改不会在文件系统重新标记之间保留,或者执行 restorecon 命令。SELinux 策略控制用户是否能够修改任何给定文件的 SELinux 上下文。使用 chcon 时,用户提供要更改的所有或部分 SELinux 上下文。文件类型不正确是 SELinux 拒绝访问的常见原因。

快速参考

  • 运行 chcon -t type file-name 命令来更改文件类型,其中 type 是 SELinux 类型,如 httpd_sys_content_tfile-name 是文件或目录名称:
    ~]$ chcon -t httpd_sys_content_t file-name
  • 运行 chcon -R -t type directory-name 命令来更改目录及其内容的类型,其中 type 是 SELinux 类型,如 httpd_sys_content_tdirectory-name 是目录名称:
    ~]$ chcon -R -t httpd_sys_content_t directory-name

过程 4.6. 更改文件或目录的类型

以下步骤演示了更改类型,而不是 SELinux 上下文的其他属性。本节中的示例对目录的工作方式相同,例如 file1 是一个目录。
  1. 更改到您的主目录。
  2. 创建新文件并查看其 SELinux 上下文:
    ~]$ touch file1
    ~]$ ls -Z file1
    -rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
    
    在本例中,file1 的 SELinux 上下文包括 SELinux unconfined_u user、object_r 角色、user_home_t 类型和 s0 级别。有关 SELinux 上下文的每个部分的描述,请参阅 第 2 章 SELinux Contexts
  3. 输入以下命令将类型更改为 samba_share_tt 选项仅更改类型。然后查看更改:
    ~]$ chcon -t samba_share_t file1
    ~]$ ls -Z file1 
    -rw-rw-r--  user1 group1 unconfined_u:object_r:samba_share_t:s0 file1
    
  4. 使用以下命令,恢复 file1 文件的 SELinux 上下文。使用 -v 选项查看哪些更改:
    ~]$ restorecon -v file1
    restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0
    
    在本例中,以前的类型 samba_share_t 恢复到正确的 user_home_t 类型。当使用 targeted 策略(Red Hat Enterprise Linux 中的默认 SELinux 策略时),restorecon 命令读取 /etc/selinux/targeted/contexts/files/ 目录中的文件,以查看哪些 SELinux 上下文文件应具有哪些 SELinux 上下文文件。

过程 4.7. 更改目录及其内容类型

以下示例演示了创建新目录,并将目录的文件类型及其内容更改为 Apache HTTP 服务器使用的类型。如果您希望 Apache HTTP 服务器使用不同的文档根(而不是 /var/www/html/),则使用本例中的配置:
  1. 以 root 用户身份,创建一个新的 web/ 目录,然后在该目录内 3 个空文件(file1、 file2 和 file3)。其中的 web/ 目录和文件标记为 default_t 类型:
    ~]# mkdir /web
    ~]# touch /web/file{1,2,3}
    ~]# ls -dZ /web
    drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
    
    ~]# ls -lZ /web
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
    
  2. 以 root 用户身份,输入以下命令将 web/ 目录(及其内容)的类型改为 httpd_sys_content_t
    ~]# chcon -R -t httpd_sys_content_t /web/
    ~]# ls -dZ /web/
    drwxr-xr-x  root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/
    
    ~]# ls -lZ /web/
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
    
  3. 要恢复默认 SELinux 上下文,以 root 用户身份使用 restorecon 工具:
    ~]# restorecon -R -v /web/
    restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    
有关 chcon 的详情,请查看 chcon(1) 手册页。
注意
类型强制是 SELinux targeted 策略中使用的主要权限控制。在大多数情况下,可以忽略 SELinux 用户和角色。

4.7.2. 持久性更改:semanage fcontext

semanage fcontext 命令用于更改文件的 SELinux 上下文。要显示新创建的文件和目录的上下文,以 root 用户身份输入以下命令:
~]# semanage fcontext -C -l
以下工具使用 semanage fcontext 所做的更改。当重新标记文件系统时,会使用 setfiles 工具,restorecon 实用程序恢复默认的 SELinux 上下文。这意味着 semanage fcontext 所做的更改是持久的,即使文件系统被重新标记也是如此。SELinux 策略控制用户是否能够修改任何给定文件的 SELinux 上下文。

快速参考

在文件系统重新标记后进行 SELinux 上下文更改:
  1. 输入以下命令,记得使用文件或目录的完整路径:
    ~]# semanage fcontext -a options file-name|directory-name
  2. 使用 restorecon 实用程序应用上下文更改:
    ~]# restorecon -v file-name|directory-name

将正则表达式与 semanage fcontext 搭配使用

要使 semanage fcontext 命令正常工作,您可以使用完全限定的路径或 Perl 兼容的正则表达式(PCRE)。使用中唯一的 PCRE 标志是 PCRE2_DOTALL,它会导致 . 通配符与任何内容(包括新行)匹配。代表路径的字符串被处理为字节,这意味着非 ASCII 字符不会被单个通配符匹配。
请注意,使用 semanage fcontext 指定的 file-context 定义会以相反的顺序评估它们是如何定义的:无论 stem 长度如何,都会首先评估最新的条目。存储在 file_contexts.local 中的本地文件上下文修改的优先级高于策略模块中指定的优先级。这意味着,每当在 file_contexts.local 中找到给定文件路径的匹配项时,不会考虑其他 file-context 定义。
重要
使用 semanage fcontext 命令指定的 file-context 定义有效地覆盖所有其他 file-context 定义。因此,所有正则表达式都应尽可能具体,以避免对文件系统中其他部分产生意外影响。
有关生效的 file-context 定义和标志中使用的正则表达式类型的更多信息,请参阅 semanage-fcontext (8) 手册页。

过程 4.8. 更改文件或目录的类型

以下示例演示了更改文件类型,而不是 SELinux 上下文的其他属性。本例对目录的工作方式相同,例如,如果 file1 是目录。
  1. 以 root 用户身份,在 /etc 目录中创建新文件。默认情况下,/etc 中新创建的文件使用 etc_t 类型标记:
    ~]# touch /etc/file1
    ~]$ ls -Z /etc/file1
    -rw-r--r--  root root unconfined_u:object_r:etc_t:s0       /etc/file1
    
    要列出有关目录的信息,请使用以下命令:
    ~]$ ls -dZ directory_name
  2. 以 root 用户身份,输入以下命令将 file1 类型更改为 samba_share_ta 选项添加新的记录,而 -t 选项则定义类型(samba_share_t)。请注意,运行这个命令不会直接更改类型; file1 仍然使用 etc_t 类型进行标记:
    ~]# semanage fcontext -a -t samba_share_t /etc/file1
    ~]# ls -Z /etc/file1
    -rw-r--r--  root root unconfined_u:object_r:etc_t:s0       /etc/file1
    
    ~]$ semanage fcontext -C -l
    /etc/file1    unconfined_u:object_r:samba_share_t:s0
    
  3. 以 root 用户身份,使用 restorecon 实用程序更改类型。因为 semanage/etc/file1file_contexts.local 中添加一个条目,restorecon 将类型改为 samba_share_t
    ~]# restorecon -v /etc/file1
    restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0
    

过程 4.9. 更改目录及其内容类型

以下示例演示了创建新目录,并将目录的文件类型及其内容更改为 Apache HTTP 服务器使用的类型。如果您希望 Apache HTTP 服务器使用不同的文档根而不是 /var/www/html/,则使用本例中的配置:
  1. 以 root 用户身份,创建一个新的 web/ 目录,然后在该目录内 3 个空文件(file1、 file2 和 file3)。其中的 web/ 目录和文件标记为 default_t 类型:
    ~]# mkdir /web
    ~]# touch /web/file{1,2,3}
    ~]# ls -dZ /web
    drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
    
    ~]# ls -lZ /web
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
    
  2. 以 root 用户身份,输入以下命令将 web/ 目录及其中的文件更改为 httpd_sys_content_t 的类型。a 选项添加新的记录,-t 选项定义类型(httpd_sys_content_t)。"/web (/1.8)?" 正则表达式会导致 semanage 应用到 web/,以及其中的文件。请注意,运行这个命令不会直接更改类型;其中中的 web/ 和文件仍使用 default_t 类型标记:
    ~]# semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
    ~]$ ls -dZ /web
    drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
    
    ~]$ ls -lZ /web
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
    
    semanage fcontext -a -t httpd_sys_content_t "/web (/xz)?" 命令会将以下条目添加到 /etc/selinux/targeted/contexts/files/file_contexts.local:
    /web(/.*)?    system_u:object_r:httpd_sys_content_t:s0
    
  3. 以 root 用户身份,使用 restorecon 实用程序更改 web/ 的类型,以及其中的所有文件。-R 用于递归,这意味着 web/ 下的所有文件和目录都标有 httpd_sys_content_t 类型。由于 semanagefile.contexts.local 添加了一个条目 /web (/ instead)?restorecon 将类型改为 httpd_sys_content_t
    ~]# restorecon -R -v /web
    restorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    restorecon reset /web/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    restorecon reset /web/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    restorecon reset /web/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    
    请注意,默认情况下,新创建的文件和目录继承其父目录的 SELinux 类型。

过程 4.10. 删除添加的上下文

以下示例演示了添加和删除 SELinux 上下文。如果上下文是正则表达式的一部分,例如 /web (/ quota )?,请使用正则表达式的引号:
~]# semanage fcontext -d "/web(/.*)?"
  1. 要删除上下文,请以 root 用户身份输入以下命令,其中 file-name|directory-namefile_contexts.local 中的第一个部分:
    ~]# semanage fcontext -d file-name|directory-name
    以下是 file_contexts.local 中的上下文示例:
    /test    system_u:object_r:httpd_sys_content_t:s0
    
    第一部分正在测试 。要防止 test/ 目录在运行 restorecon 后标记为 httpd_sys_content_t,或者在文件系统重新标记之后,请以 root 用户身份输入以下命令,从 file_contexts.local 中删除上下文:
    ~]# semanage fcontext -d /test
  2. 以 root 用户身份,使用 restorecon 实用程序恢复默认的 SELinux 上下文。
有关 semanage 的详情,请查看 semanage(8)semanage-fcontext(8) 手册页。
重要
使用 semanage fcontext -a 更改 SELinux 上下文时,请使用文件或目录的完整路径以避免在文件系统重新标记或运行 restorecon 命令后标记文件。

4.7.3. 如何确定文件上下文

确定文件上下文是基于文件上下文定义,该定义在系统安全策略( .fc 文件)中指定的。根据系统策略,semanage generate file_contexts.homedirsfile_contexts 文件。
系统管理员可以使用 semanage fcontext 命令自定义 file-context 定义。这些自定义存储在 file_contexts.local 文件中。
当标记实用程序(如 matchpathconrestorecon )正在确定给定路径的正确标签时,它会首先搜索本地更改(file_contexts.local)。如果工具找不到匹配的模式,它会搜索 file_contexts.homedirs 文件,最后是 file_contexts 文件。但是,每当找到与给定文件路径的匹配项时,搜索结束时,实用程序会查找任何其他文件上下文定义。这意味着与主目录相关的文件上下文的优先级高于其余的优先级,本地自定义会覆盖系统策略。
由系统策略 指定的 file-context 定义( file_contexts.homedirsfile_contexts 文件的内容)根据评估前的 stem (任何通配符前路径前缀)的长度进行排序。这意味着选择最具体的路径。但是,使用 semanage fcontext 指定的 file-context 定义会以相反的顺序评估它们是如何定义的:无论 stem 长度如何,都会首先评估最新的条目。
有关以下信息的更多信息:
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.