Kubernetes 1.30:防止未經授權的磁碟區模式轉換移至 GA

隨著 Kubernetes 1.30 的發布,防止修改 Kubernetes 叢集中從現有 VolumeSnapshot 建立的 PersistentVolumeClaim 卷模式的功能已移至 GA!

問題

卷模式 PersistentVolumeClaim 指的是儲存裝置上的底層卷是否格式化為檔案系統,或呈現為原始塊裝置給使用它的 Pod。

使用者可以利用自 Kubernetes v1.20 起已穩定的 VolumeSnapshot 功能,從 Kubernetes 叢集中的現有 VolumeSnapshot 建立 PersistentVolumeClaim(縮寫為 PVC)。PVC 規範包括 dataSource 欄位,該欄位可以指向現有的 VolumeSnapshot 實例。請造訪 從 Volume Snapshot 建立 PersistentVolumeClaim 以取得有關如何在 Kubernetes 叢集中從現有 VolumeSnapshot 建立 PVC 的更多詳細資訊。

當利用上述功能時,沒有邏輯驗證原始卷(已拍攝快照)的模式是否與新建立卷的模式相符。

這構成了一個安全漏洞,允許惡意使用者可能利用主機作業系統中尚未發現的漏洞。

允許某些使用者執行此類轉換是有合理的用例的。通常,儲存備份供應商在備份操作過程中轉換卷模式,以檢索已變更的塊,從而提高操作效率。這阻止 Kubernetes 完全阻止該操作,並在區分受信任使用者和惡意使用者方面提出了挑戰。

防止未經授權的使用者轉換卷模式

在這種情況下,授權使用者是指有權對 VolumeSnapshotContents(叢集級資源)執行 updatepatch 操作的使用者。
叢集管理員有責任僅向受信任的使用者或應用程式(如備份供應商)提供這些權限。除了此類授權使用者之外的使用者,在從 VolumeSnapshot 建立 PVC 時,永遠不允許修改 PVC 的卷模式。

若要轉換卷模式,授權使用者必須執行以下操作

  1. 識別要用作給定命名空間中新建立 PVC 資料來源的 VolumeSnapshot。
  2. 識別繫結到上述 VolumeSnapshot 的 VolumeSnapshotContent。
kubectl describe volumesnapshot -n <namespace> <name>
  1. 將註解 snapshot.storage.kubernetes.io/allow-volume-mode-change: "true" 新增到上述 VolumeSnapshotContent。VolumeSnapshotContent 註解必須包含類似於以下資訊清單片段的註解
kind: VolumeSnapshotContent
metadata:
  annotations:
    - snapshot.storage.kubernetes.io/allow-volume-mode-change: "true"
...

注意:對於預先佈建的 VolumeSnapshotContents,您必須採取額外步驟,根據從中拍攝快照的卷模式,將 spec.sourceVolumeMode 欄位設定為 FilesystemBlock

以下範例所示

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotContent
metadata:
  annotations:
  - snapshot.storage.kubernetes.io/allow-volume-mode-change: "true"
  name: <volume-snapshot-content-name>
spec:
  deletionPolicy: Delete
  driver: hostpath.csi.k8s.io
  source:
    snapshotHandle: <snapshot-handle>
  sourceVolumeMode: Filesystem
  volumeSnapshotRef:
    name: <volume-snapshot-name>
    namespace: <namespace>

針對所有卷模式需要在備份或還原操作期間轉換的 VolumeSnapshotContents 重複步驟 1 至 3。這可以透過具有授權使用者憑證的軟體或由授權使用者手動完成。

如果上述註解出現在 VolumeSnapshotContent 物件上,Kubernetes 將不會阻止卷模式被轉換。使用者在嘗試將註解新增到任何 VolumeSnapshotContent 之前,應牢記這一點。

所需操作

prevent-volume-mode-conversion 功能標誌在 external-provisioner v4.0.0 和 external-snapshotter v7.0.0 中預設為啟用。除非已執行上述步驟,否則在從 VolumeSnapshot 建立 PVC 時,卷模式變更將被拒絕。

下一步

若要判斷哪些 CSI 外部 Sidecar 版本支援此功能,請前往 CSI 文件頁面。如有任何疑問或問題,請加入 Kubernetes on Slack 並在 #csi 或 #sig-storage 頻道中建立主題。或者,在 CSI external-snapshotter repository 中建立問題。