簡體   English   中英

如何使用 SELinux 在 Kube.netes 中掛載 HostPath Volume

[英]How to mount HostPath Volume in Kubernetes with SELinux

我正在嘗試將hostPath 卷掛載到 Kube.netes Pod 中。 下面顯示了hostPath卷規范的示例,該示例取自文檔。 我正在部署到運行啟用了 SELinux 的 RHEL 7 的主機。

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory

當我的 Pod 嘗試讀取已從底層主機掛載的文件時,我收到“權限被拒絕”錯誤。 當我運行setenforce 0關閉 SELinux 時,錯誤消失,我可以訪問該文件。 當我將一個目錄綁定到 Docker 容器時,我得到了同樣的錯誤。

此處描述了該問題,並且在使用 Docker 時,可以通過使用zZ綁定安裝標志來修復,如Docker文檔中所述。

雖然我可以通過運行來解決問題

chcon -Rt svirt_sandbox_file_t /path/to/my/host/dir/to/mount

我認為這是一個令人討厭的 hack,因為我需要在我的 Kube.netes 集群中的每個主機上執行此操作,還因為我在 YAML 規范中描述的 Kube.netes 部署並未完整描述需要什么完成以使我的 YAML 正常運行。 關閉 SELinux 不是一個選項。

我可以看到 Kube.netes 在此處的文檔中提到了 SELinux 安全上下文,但是我無法在沒有出現權限被拒絕錯誤的情況下成功地將 hostPath 卷掛載到 pod 中。

YAML 需要是什么樣子才能使容器成功地從運行 SELinux 的底層主機掛載 HostPath 卷?

更新:

我正在訪問的文件是具有以下標簽的 CA 證書:

system_u:object_r:cert_t:s0

當我使用以下選項時:

securityContext:
  seLinuxOptions:
    level: "s0:c123,c456"

然后通過ausearch -m avc -ts recent檢查訪問控制審計錯誤,我可以看到存在權限被拒絕錯誤,其中容器的級別 label 為s0:c123,c456 ,所以我可以看到級別 label 有效. 我已將 label 設置為s0

但是,如果我嘗試將type label 更改為cert_t ,容器甚至不會啟動,就會出現錯誤:

container_linux.go:247: starting container process caused "process_linux.go:364: container init caused \"write /proc/self/task/1/attr/exec: invalid argument\""

我似乎無法更改容器的類型 label。

您可以使用seLinuxOptions分配 SELinux 標簽:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
    securityContext:
      seLinuxOptions: # it may don’t have the desired effect
        level: "s0:c123,c456"
  securityContext:
    seLinuxOptions:
      level: "s0:c123,c456"
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory

根據文檔

感謝Phil指出這一點。 根據問題評論,它似乎只在Pod.spec.securityContext工作

  • seLinuxOptions :支持 SELinux 標簽的卷被重新標記為可通過 seLinuxOptions 下指定的標簽訪問。 通常你只需要設置電平部分。 這將設置為Pod 中的所有容器以及 Volumes 提供的多類別安全 (MCS) 標簽。

擴展VAS 的答案,因為它部分正確:

在重新標記hostPath卷指向的路徑目標時,您只能指定 SELinux 標簽的級別部分 這是由自動完成這樣seLinuxOptions.level在您指定的屬性securityContext

但是, seLinuxOptions.type等屬性目前對卷重新標記沒有影響。 在撰寫本文時,這仍然是Kubernetes 中的一個懸而未決的問題

您可以嘗試使用完全權限:

 ...
 image: k8s.gcr.io/test-webserver
 securityContext:
   privileged: true
 ...

使用selinux可以解決這個問題。 參考文章: https://zhimin-wen.medium.com/selinux-policy-for-openshift-containers-40baa1c86aa5

另外:可以參考selinux參數設置掛載目錄的增刪改查 https://selinuxproject.org/page /ObjectClassesPerms

我的設置:

如果一個目錄的 selinux 是 unconfined_u:object_r:kube.netes_file_t:s0,你可以定義一個 selinux 策略:

module myapp 1.0;

require {
 type kubernetes_file_t;
 type container_t;
 class file { create open read unlink write getattr execute setattr link };
 class dir { add_name create read remove_name write };
}

#============= container_t ==============

#!!!! This avc is allowed in the current policy
allow container_t kubernetes_file_t:dir { add_name create read remove_name write };

#!!!! This avc is allowed in the current policy
allow container_t kubernetes_file_t:file { create open read unlink write getattr execute setattr link };

在節點上運行命令:

sudo checkmodule -M -m -o myapp.mod myapp.te
sudo semodule_package -o myapp.pp -m myapp.mod
sudo semodule -i myapp.pp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM