|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.30: Preventing unauthorized volume mode conversion moves to GA" |
| 4 | +date: 2024-04-30 |
| 5 | +slug: prevent-unauthorized-volume-mode-conversion-ga |
| 6 | +--- |
| 7 | + |
| 8 | +**Author:** Raunak Pradip Shah (Mirantis) |
| 9 | + |
| 10 | +With the release of Kubernetes 1.30, the feature to prevent the modification of the volume mode |
| 11 | +of a [PersistentVolumeClaim](/docs/concepts/storage/persistent-volumes/) that was created from |
| 12 | +an existing VolumeSnapshot in a Kubernetes cluster, has moved to GA! |
| 13 | + |
| 14 | + |
| 15 | +## The problem |
| 16 | + |
| 17 | +The [Volume Mode](/docs/concepts/storage/persistent-volumes/#volume-mode) of a PersistentVolumeClaim |
| 18 | +refers to whether the underlying volume on the storage device is formatted into a filesystem or |
| 19 | +presented as a raw block device to the Pod that uses it. |
| 20 | + |
| 21 | +Users can leverage the VolumeSnapshot feature, which has been stable since Kubernetes v1.20, |
| 22 | +to create a PersistentVolumeClaim (shortened as PVC) from an existing VolumeSnapshot in |
| 23 | +the Kubernetes cluster. The PVC spec includes a dataSource field, which can point to an |
| 24 | +existing VolumeSnapshot instance. |
| 25 | +Visit [Create a PersistentVolumeClaim from a Volume Snapshot](/docs/concepts/storage/persistent-volumes/#create-persistent-volume-claim-from-volume-snapshot) |
| 26 | +for more details on how to create a PVC from an existing VolumeSnapshot in a Kubernetes cluster. |
| 27 | + |
| 28 | +When leveraging the above capability, there is no logic that validates whether the mode of the |
| 29 | +original volume, whose snapshot was taken, matches the mode of the newly created volume. |
| 30 | + |
| 31 | +This presents a security gap that allows malicious users to potentially exploit an |
| 32 | +as-yet-unknown vulnerability in the host operating system. |
| 33 | + |
| 34 | +There is a valid use case to allow some users to perform such conversions. Typically, storage backup |
| 35 | +vendors convert the volume mode during the course of a backup operation, to retrieve changed blocks |
| 36 | +for greater efficiency of operations. This prevents Kubernetes from blocking the operation completely |
| 37 | +and presents a challenge in distinguishing trusted users from malicious ones. |
| 38 | + |
| 39 | +## Preventing unauthorized users from converting the volume mode |
| 40 | + |
| 41 | +In this context, an authorized user is one who has access rights to perform **update** |
| 42 | +or **patch** operations on VolumeSnapshotContents, which is a cluster-level resource. |
| 43 | +It is up to the cluster administrator to provide these rights only to trusted users |
| 44 | +or applications, like backup vendors. |
| 45 | +Users apart from such authorized ones will never be allowed to modify the volume mode |
| 46 | +of a PVC when it is being created from a VolumeSnapshot. |
| 47 | + |
| 48 | +To convert the volume mode, an authorized user must do the following: |
| 49 | + |
| 50 | +1. Identify the VolumeSnapshot that is to be used as the data source for a newly |
| 51 | + created PVC in the given namespace. |
| 52 | +2. Identify the VolumeSnapshotContent bound to the above VolumeSnapshot. |
| 53 | + |
| 54 | + ```shell |
| 55 | + kubectl describe volumesnapshot -n <namespace> <name> |
| 56 | + ``` |
| 57 | + |
| 58 | +3. Add the annotation [`snapshot.storage.kubernetes.io/allow-volume-mode-change: "true"`](/docs/reference/labels-annotations-taints/#snapshot-storage-kubernetes-io-allowvolumemodechange) |
| 59 | + to the above VolumeSnapshotContent. The VolumeSnapshotContent annotations must include one similar to the following manifest fragment: |
| 60 | + |
| 61 | + ```yaml |
| 62 | + kind: VolumeSnapshotContent |
| 63 | + metadata: |
| 64 | + annotations: |
| 65 | + - snapshot.storage.kubernetes.io/allow-volume-mode-change: "true" |
| 66 | + ... |
| 67 | + ``` |
| 68 | + |
| 69 | +**Note**: For pre-provisioned VolumeSnapshotContents, you must take an extra |
| 70 | +step of setting `spec.sourceVolumeMode` field to either `Filesystem` or `Block`, |
| 71 | +depending on the mode of the volume from which this snapshot was taken. |
| 72 | + |
| 73 | +An example is shown below: |
| 74 | + |
| 75 | + ```yaml |
| 76 | + apiVersion: snapshot.storage.k8s.io/v1 |
| 77 | + kind: VolumeSnapshotContent |
| 78 | + metadata: |
| 79 | + annotations: |
| 80 | + - snapshot.storage.kubernetes.io/allow-volume-mode-change: "true" |
| 81 | + name: <volume-snapshot-content-name> |
| 82 | + spec: |
| 83 | + deletionPolicy: Delete |
| 84 | + driver: hostpath.csi.k8s.io |
| 85 | + source: |
| 86 | + snapshotHandle: <snapshot-handle> |
| 87 | + sourceVolumeMode: Filesystem |
| 88 | + volumeSnapshotRef: |
| 89 | + name: <volume-snapshot-name> |
| 90 | + namespace: <namespace> |
| 91 | + ``` |
| 92 | +
|
| 93 | +Repeat steps 1 to 3 for all VolumeSnapshotContents whose volume mode needs to be |
| 94 | +converted during a backup or restore operation. This can be done either via software |
| 95 | +with credentials of an authorized user or manually by the authorized user(s). |
| 96 | +
|
| 97 | +If the annotation shown above is present on a VolumeSnapshotContent object, |
| 98 | +Kubernetes will not prevent the volume mode from being converted. |
| 99 | +Users should keep this in mind before they attempt to add the annotation |
| 100 | +to any VolumeSnapshotContent. |
| 101 | +
|
| 102 | +## Action required |
| 103 | +
|
| 104 | +The `prevent-volume-mode-conversion` feature flag is enabled by default in the |
| 105 | +external-provisioner `v4.0.0` and external-snapshotter `v7.0.0`. Volume mode change |
| 106 | +will be rejected when creating a PVC from a VolumeSnapshot unless the steps |
| 107 | +described above have been performed. |
| 108 | + |
| 109 | +## What's next |
| 110 | + |
| 111 | +To determine which CSI external sidecar versions support this feature, please head |
| 112 | +over to the [CSI docs page](https://kubernetes-csi.github.io/docs/). |
| 113 | +For any queries or issues, join [Kubernetes on Slack](https://slack.k8s.io/) and |
| 114 | +create a thread in the #csi or #sig-storage channel. Alternately, create an issue in the |
| 115 | +CSI external-snapshotter [repository](https://github.com/kubernetes-csi/external-snapshotter). |
0 commit comments