如何通过 SELinux 策略向通过 rootless podman play kube 绑定安装的卷授予读/写访问权限?

如何通过 SELinux 策略向通过 rootless podman play kube 绑定安装的卷授予读/写访问权限?

我最近从 Docker 切换到波德曼并创建了一个兼容 Kubernetes 的吊舱定义yaml 文件。
使用 rootless 成功加载定义podman play kube然后,命令声明 Pod 和容器启动,但是读取访问被拒绝当尝试列出内容时主机路径容器内的束缚体积。

加载定义并转储“前端”容器日志

$ podman play kube ./my-pod.yaml ; sleep 1 ; podman logs front
/var/www  # pwd
0 0       # echo `id -u` `id -g`
total 0   # ls -lha   
ls: cannot open '.': Permission denied

我相信否认一定与SELinux限制策略,因为文件自主访问控制权限在主机目录上似乎足够宽松......

$ pwd ; ls -lhaZ
/home/stph/my-project
drwxrwxr-x.  9 stph stph unconfined_u:object_r:user_home_t:s0 4.0K Apr 15 22:30 .
drwxrwxr-x.  4 stph stph unconfined_u:object_r:user_home_t:s0 4.0K Apr 15 22:30 ..
drwxr-xr-x.  5 stph stph unconfined_u:object_r:user_home_t:s0 4.0K Apr  3 14:11 ui

所以,我天真地应用了同样的SELinux标签从主机目录到 pod,但显然并且不出所料,它没有解决任何问题。

根据,可能有一些关于私人标签的事情podman 玩 kube文档:

注意:HostPath 卷类型创建者玩库贝将被给予SELinux私人标签

我相信这仅意味着主机路径卷不能由多个容器共享(我在这里可能会弄错)。

Podman pod yaml 定义文件

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  hostname: my-pod-host
  volumes:
  - name: projdir
    hostPath:
      path: /home/stph/my-project
      type: Directory
  securityContext:
    seLinuxOptions:
      level: s0
      role: object_r
      type: user_home_t
      user: unconfined_u
  containers:
  - name: front
    image: node:lts-alpine
    workingDir: /var/www
    env:
      - name: API_ENTRYPOINT
        value: "http://localhost:80/api"
    ports:
      - containerPort: 5000
        hostIP: 127.0.0.1
        hostPort: 5000
        protocol: TCP
    volumeMounts:
    - name: projdir
      mountPath: /var/www
      subPath: ui
    command: ['/bin/sh', '-c', 'pwd ; echo `id -u` `id -g` ; ls -lha']

/etc/subuid 映射

cat /etc/subuid
stph:1000:1
stph:100000:65536

podman inspect front输出摘录

{
  "Id": "c93a3d6...",
  "Created": "2020-04-15T19:01:14.034867742+02:00",
  "Path": "docker-entrypoint.sh",
  "Args": [
    "/bin/sh",
    "-c",
    "pwd ; echo `id -u` `id -g` ; ls -lha"
  ],
  "State": {
    "OciVersion": "1.0.1-dev",
    "Status": "exited",
    ...
  },
  "Image": "f77abbe...",
  "ImageName": "node:lts-alpine",
  "Rootfs": "",
  "Pod": "26bd784...",
  "ResolvConfPath": "/run/user/1000/containers/overlay-containers/93c4707.../userdata/resolv.conf",
  "HostnamePath": "/run/user/1000/containers/overlay-containers/c93a3d.../userdata/hostname",
  "HostsPath": "/run/user/1000/containers/overlay-containers/93c4707.../userdata/hosts",
  "StaticDir": ".../containers/storage/overlay-containers/c93a3d.../userdata",
  "OCIConfigPath": ".../containers/storage/overlay-containers/c93a3d.../userdata/config.json",
  "OCIRuntime": "crun",
  "LogPath": ".../containers/storage/overlay-containers/c93a3d.../userdata/ctr.log",
  "LogTag": "",
  "ConmonPidFile": "/run/user/1000/containers/overlay-containers/c93a3d.../userdata/conmon.pid",
  "Name": "front",
  "RestartCount": 0,
  "Driver": "overlay",
  "MountLabel": "system_u:object_r:container_file_t:s0:c57,c80",
  "ProcessLabel": "system_u:system_r:container_t:s0:c57,c80",
  "Mounts": [
    {
      "Type": "bind",
      "Name": "",
      "Source": "/home/stph/my-project",
      "Destination": "/var/www",
      "Driver": "",
      "Mode": "",
      "Options": [
          "rbind"
      ],
      "RW": true,
      "Propagation": "rprivate"
    }
  ],
  "Namespace": "",
  "IsInfra": false,
  "Config": {
    "Hostname": "ski-pod-host",
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
      "TERM=xterm",
      "NODE_VERSION=12.16.1",
      "YARN_VERSION=1.22.0",
      "API_ENTRYPOINT=http://localhost:80",
      "HOSTNAME=ski-pod-host",
      "HOME=/root",
      "container=libpod"
    ],
    "Cmd": [
      "docker-entrypoint.sh", "/bin/sh", "-c", "pwd ; echo `id -u` `id -g` ; ls -lha"
    ],
    "Image": "node:lts-alpine",
    "Volumes": null,
    "WorkingDir": "/var/www",
    "Entrypoint": "",
    "StopSignal": 15
    ...
  },
  "HostConfig": {
    "Binds": [
      "/home/stph/my-project:/var/www:rw,rprivate,rbind"
    ],
    "NetworkMode": "container:93c4707...",
    "RestartPolicy": {
      "Name": "",
      "MaximumRetryCount": 0
    },
    "AutoRemove": false,
    "VolumeDriver": "",
    "VolumesFrom": null,
    "IpcMode": "container:93c4707...",
    "Cgroup": "",
    "Cgroups": "default",
    "Privileged": false,
    "ReadonlyRootfs": false,
    "SecurityOpt": [],
    ...
  }
  ...
}

相关journalctl -a -r记录不显示任何与拒绝访问或未经授权的操作相关的内容。

关于如何调试/解决这种情况有什么建议吗?谢谢。

答案1

我有同样的问题,我找到了这个解决方案。您无需尝试从主机应用相同的标签,只需禁用标签即可:

  securityContext:
    seLinuxOptions:
      type: spc_t

禁用type: spc_tSELinux 的标签。这相当于使用podman参数运行命令--security-opt label=disable。例如:

podman run -it --security-opt label=disable -v .:/foo alpine

我在这里找到了解决方案:https://github.com/containers/libpod/pull/5307#issuecomment-590830455

我希望它有帮助。

相关内容