运行 来自未受限用户时如何在文件上保留 SELinux 上下文

How to preserve SELinux context on a file while running from unconfined user

我的一些文件的 SELinux 上下文与我的 openstack-swift setup

有一些问题

设置详情:

守护进程是 运行ning swift-object-replicator,具有以下 SELinux 上下文

system_u:system_r:swift_t:s0 swift ... /usr/bin/python /usr/bin/swift-object-replicator /etc/swift/object-server.conf

此守护进程定期调用脚本。该脚本创建的文件具有以下 SELinux 上下文

system_u:object_r:swift_var_cache_t:s0 /var/cache/swift/object.recon

哪个是正确的!!

问题

如果我 运行 来自终端的相同脚本(该守护程序正在内部调用)作为 'root',object.recon 文件 SELinux 上下文被修改如下 -rw-------. swift swift unconfined_u:object_r:var_t:s0 /var/cache/swift/object.recon

然后我开始在该守护程序的日志文件中看到错误消息

知道为什么上下文会改变以及如何保留它,即使我希望从终端触发脚本

我找到了两种保留上下文的方法:

  • 使用 运行con 来 运行 具有正确上下文的命令

    runcon -t swift_t -r system_r swift-object-replicator /etc/swift/object-server.conf -ov
    
  • 或者,定义 SELinux type_transition 规则,以便具有 unconfined_t 域的用户在执行脚本时转换到正确的域

    require {
     type unconfined_t;
     type swift_exec_t;
     type swift_t;
     role unconfined_r;
     class process transition;
    }
    
    role unconfined_r types swift_t;
    
    type_transition unconfined_t swift_exec_t:process swift_t;