宝玛科技网
您的当前位置:首页容器挂载volume出现“Permission denied”的问题定位解决

容器挂载volume出现“Permission denied”的问题定位解决

来源:宝玛科技网
Linux localhost.localdomain 3.10.0-862.el7.x86_ #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_ x86_ x86_ GNU/Linux
docker run -v /home/centosDir/:/home -it -d --name=centos 49f7960eb7e4 /bin/bash
chcon -Rt svirt_sandbox_file_t /home/centosDir
[root@localhost overlay2]# ls -Z /home/
drwxr-xr-x. root root unconfined_u:object_r:user_home_dir_t:s0 centosDir
drwx------. charlie charlie unconfined_u:object_r:user_home_dir_t:s0 charlie

使用docker inspect centos查看容器的文件策略,如下,可以看到容器需要的挂载类型为svirt_sandbox_file_t,进程运行域为svirt_lxc_net_t,因此解决方法为将挂载文件修改为与容器需要的类型一样即可

"MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c161,c568",
"ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c161,c568",

使用chcon -Rt svirt_sandbox_file_t /home/centosDir修改后,MAC变为如下(主要是type字段),这样就可以正常访问了:

[root@cfd98c17be4b /]# ls -Z /
lrwxrwxrwx. root root system_u:object_r:container_file_t:s0:c268,c731 bin -> usr/bin
drwxr-xr-x. root root system_u:object_r:container_file_t:s0:c268,c731 dev
drwxr-xr-x. root root system_u:object_r:container_file_t:s0:c268,c731 etc
drwxr-xr-x. root root unconfined_u:object_r:container_file_t:s0 home

 

MAC了解

启用MAC的配置文件在/etc/selinux/config下,可以看到一条配置:SELINUX=enforcing,也可以使用sestatus查看当前状态

[root@localhost selinux]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

只要SELinux的工作模式是Enforcing,根据SELinux所选择的策略结果集,给所有文件和进程都打上安全标签,即:安全上下文(security context)。这一行为是在将SELinux的模式由disabled模式更改为enforcing模式后的第一次启动时完成的.

不同的进程只在自己所属的域内运行,运行在域中的进程只对授权的类型具有读写权限,强制访问控制的标准是基于程序的域类型而不是基于用户的域类型

默认情况下,Linux用户是非的,对于非的进程(非的用户运行在 unconfined_t 域中), SELinux 的策略规则仍然适用,然而有关允许进程运行在非域的规则几乎允许所有的访问。此时,相当于 SELinux 不起作用

 

安全上下文有五个元素组成,以冒号分隔

user:role:type:sensitivity:category

User:指示登录系统的用户类型,如root,user_u,system_u,多数本地进程都属于自由(unconfined)进程

 Role:定义文件,进程和用户的用途:文件:object_r,进程和用户:system_r

 Type:指定数据类型,规则中定义何种进程类型访问何种文件Target策略基于type实现,多服务共用:public_content_t

 Sensitivity:访问的需要,由组织定义的分层安全级别,如unclassified, secret,top,secret, 一个对象有且只有一个sensitivity,分0-15级,s0最低,Target策略默认使用s0

 Category:对于特定组织划分不分层的分类,如FBI Secret,NSA secret, 一个对象可以有多个categroy, c0-c1023共1024个分类, Target 策略不使用category

 

 

参考:

http://blog.51cto.com/zhaotianyu/1817939

https://yq.aliyun.com/articles/486704?spm=5176.10695662.19966101.searchclickresult.312d10b5HpsmDH

http://cn.linux.vbird.org/linux_basic/0440processcontrol_5.php#mac

http://jiayu0x.com/2014/12/24/Linux-authority-and-access-control-2/

http://www.cse.psu.edu/~trj1/cse543-f07/slides/03-PolicyConcepts.pdf

http://www.informit.com/articles/article.aspx?p=606586&seqNum=2

 

转载于:https://www.cnblogs.com/charlieroro/p/9491159.html

因篇幅问题不能全部显示,请点此查看更多更全内容