【问题标题】:docker dropping capabilities not working as expected?docker drop 功能没有按预期工作?
【发布时间】:2021-12-26 00:19:42
【问题描述】:

来自这个 docker 文档页面 - https://docs.docker.com/engine/security/#linux-kernel-capabilities

只需要绑定到以下端口的进程(如 Web 服务器) 1024 不需要以 root 身份运行:他们可以被授予 net_bind_service。

下面是我的 Dockerfile -

FROM ubuntu:20.04
#FROM openjdk:11-jre-slim
LABEL description="This is hello-docker app"
LABEL version="1.0.0-snapshot"

#distro specific things before application specific things
#this will be cached, so you have to build with --no-cache option
RUN apt-get update && apt-get install -y openjdk-11-jdk

#just for demo - installed for demo
RUN apt-get install -y sudo
RUN sudo sh -c 'echo root:root | chpasswd'


#ENV APP_HOME /usr/apps
ENV APP_HOME=/myapps
RUN mkdir -p $APP_HOME

#with customuser/appuser
RUN groupadd  appgroup && useradd -g appgroup appuser
#RUN groupadd -g 999 appgroup && useradd -r -u 999 -g appgroup appuser
#COPY --chown=appuser:appgroup hello-docker-0.0.1-SNAPSHOT.jar /

COPY --chown=nobody:nogroup hello-docker-0.0.1-SNAPSHOT.jar $APP_HOME

#documenting that the application exposes these ports
EXPOSE 8080 8081

#switching to non-root user. This is recommended for security purpose
#USER appuser
USER nobody

WORKDIR $APP_HOME
RUN pwd
RUN ls -l

# this command (bash) can be overwritten while running the image using arguments
#CMD ["bash"]

#ENTRYPOINT ["sh", "-c"]
#CMD ["exec java  -jar hello-docker-0.0.1-SNAPSHOT.jar"]

ENTRYPOINT ["java", "-jar", "-Dserver.port=80",  "./hello-docker-0.0.1-SNAPSHOT.jar"]
  1. 正如,可以观察到 - 我已切换到nobody 用户。该用户不是 root。所以这个用户应该不能绑定到任何低于 1024 的端口,并且容器应该在启动时失败。但它成功了,我可以访问端口 80 上的 Web 应用程序。为什么。

  2. 我尝试明确删除该功能 - 但容器仍然成功。为什么还是????
    docker run --name hello-docker -it --cap-drop net_bind_service --rm -p 80:80 dockerdemo/hello-docker:1.0
    即使移除了网络绑定服务能力,容器也能成功运行。

非常感谢任何帮助。

编辑 -
Q3 - 与上述有关。 上面提到的同一个 docker 安全文档指向这个页面 - https://github.com/moby/moby/blob/master/oci/caps/defaults.go#L6-L19 这里的默认值是指:> 授予任何用户(root 或非 root)或仅 root 用户的功能。如果只有 root,那么授予非 root 用户的权限是什么。没有?

【问题讨论】:

    标签: docker linux-capabilities


    【解决方案1】:

    您无法绑定到特权端口。改变的是非特权端口不会从容器内部的正常 1024 开始。相反,它设置为 0,因此每个端口都是无特权的。特权限制对于单用户/单应用环境没有意义。

    查看此 PR 了解更多详情: https://github.com/moby/moby/pull/41030

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2013-08-29
      • 2013-04-28
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多