Docker安全--关于Docker使用root与非root用户的场景中的容器与host中的执行用户的研究

/*************************************************
* Author : Samson
* Date : 08/15/2015
* Test platform:
* gcc 4.8.2
* GNU bash, 4.3.11(1)-release (x86_64-pc-linux-gnu)
* ***********************************************/

结论

实际进行测试的Dockerfile是shadowsocks的Dockerfile,在此Dockerfile中添加两行即
可使后续运行应用程序时的权限为非root用户,将进行对比docker中的进程状态与Host
环境中的应用程序的运行状态进行对比,可以看出在docker中以普通用户权限运行的程
序在host主机中运行的也是普通用户权限,在docker中以root用户权限运行的程序在host
主机中运行的也是root用户权限。

前提说明

为了区分root与非root用户的区别,若在Dockerfile中不使用USER进行指定用户的情况
下,将会默认按root的权限进行启动应用程序,为了安全考虑,除非必须使用root权限,
绝不使用root权限,那么就在Dockerfile中要执行程序前使用USER指定非root用户来执行
应用程序,只需要在执行程序前添加一个非root权限用户并使用USER命令切换到此非root用户即可。

具体修改方法

添加如下两行在ENTRYPOINT [“/bin/tini”, “–”, “/usr/local/bin/shadowsocks.sh”]的前面:

RUN useradd noroot -u 1000 -s /bin/bash
USER noroot

编译镜像方法

然后在Dockerfile目录下分别在修改前后执行:
修改前:

docker build -rm -t rootyygy .

修改后:

docker build -rm -t norootyygy .

其中rootyygy为默认root用户执行应用程序的镜像,norootyygy为普通用户执行应用程序的镜像。

启动镜像:

docker run -d --name rootyygy rootyygy
docker run -d --name norootyygy norootyygy

得到两个容器的进程号:

$ docker inspect -f {{.State.Pid}} rootyygy
9818
$ docker inspect -f {{.State.Pid}} norootyygy
9875

进入两个容器中:

noroot容器中:

sudo nsenter --target 9875 --mount --uts --ipc --net --pid
在容器中显示进程:
root@b5ddee5e9e3b:/# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
noroot        1  0.0  0.0   1104     4 ?        Ss   08:14   0:00 /bin/tini -- /usr/local/bin/shadowsocks.sh
noroot        7  0.0  0.0  20032  2816 ?        S    08:14   0:00 /bin/bash /usr/local/bin/shadowsocks.sh
noroot        8  0.0  0.3  45764 14368 ?        S    08:14   0:00 /usr/bin/python /usr/local/bin/ssserver -c /etc/shadowsocks.json
root         20  0.0  0.0  20256  3192 ?        S    08:17   0:00 -bash
root         25  0.0  0.0  17488  2040 ?        R+   08:21   0:00 ps aux
在host主机的进程查看:
$ ps aux | grep shadowsocks
ufo       10594  0.0  0.0   1104     4 ?        Ss   16:51   0:00 /bin/tini -- /usr/local/bin/shadowsocks.sh
ufo       10599  0.0  0.0  20032  2808 ?        S    16:51   0:00 /bin/bash /usr/local/bin/shadowsocks.sh
ufo       10600  0.0  0.3  45764 14292 ?        S    16:51   0:00 /usr/bin/python /usr/local/bin/ssserver -c /etc/shadowsocks.json
ufo       10874  0.0  0.0  10152  1960 pts/1    S+   16:52   0:00 grep shadowsocks

root容器中:

sudo nsenter --target 9818 --mount --uts --ipc --net --pid
在容器中显示进程:
root@4d3813cacaab:/# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0   1104     4 ?        Ss   08:13   0:00 /bin/tini -- /usr/local/bin/shadowsocks.sh
root          6  0.0  0.0  20032  2748 ?        S    08:13   0:00 /bin/bash /usr/local/bin/shadowsocks.sh
root          7  0.0  0.3  45764 14392 ?        S    08:13   0:00 /usr/bin/python /usr/local/bin/ssserver -c /etc/shadowsocks.json
root         19  0.0  0.0  20256  3232 ?        S    08:16   0:00 -bash
root         24  0.0  0.0  17488  2044 ?        R+   08:24   0:00 ps aux
在host主机的进程查看:
$ ps aux | grep shadowsocks
root       9818  0.0  0.0   1104     4 ?        Ss   16:13   0:00 /bin/tini -- /usr/local/bin/shadowsocks.sh
root       9823  0.0  0.0  20032  2748 ?        S    16:13   0:00 /bin/bash /usr/local/bin/shadowsocks.sh
root       9824  0.0  0.3  45764 14392 ?        S    16:13   0:00 /usr/bin/python /usr/local/bin/ssserver -c /etc/shadowsocks.json
ufo       10298  0.0  0.0  10152  1964 pts/1    R+   16:48   0:00 grep shadowsocks
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值