Container TPM resource manager node can refer to another TPM
Description
Reported from github.com/
When a tpm device is attached to a container, Incus derives the device numbers of the resource manager node (pathrm, usually /dev/tpmrm0) from the raw device numbers reported by swtpm, using a wrong constant and the raw device's major number instead of reading the kernel's own numbers from sysfs. As a result, the resource manager node for TPM index 0 does not refer to any device, and the resource manager node for TPM index 244 refers to the resource manager of TPM index 0 rather than the container's own TPM.
Details
A container whose swtpm instance is assigned TPM index 244 is given, and allowed by its device cgroup, a resource manager node for TPM index 0. That TPM belongs to the host when the host has its own TPM, or to whichever instance first started a TPM device otherwise. TPM indices are allocated lowest-free, so a client able to add tpm devices to its instances (allowed in restricted projects) can reach index 244 by attaching enough devices.
Containers with TPM index 0 get a non-functional resource manager node; this is a functional defect with no security impact.
Virtual machines are not affected, as they use a socket to swtpm.
Proof of concept
When a tpm device is attached to a container, Incus derives the device numbers of the resource manager node (pathrm, usually /dev/tpmrm0) from the raw device numbers reported by swtpm, using a wrong constant and the raw device's major number instead of reading the kernel's own numbers from sysfs. As a result, the resource manager node for TPM index 0 does not refer to any device, and the resource manager node for TPM index 244 refers to the resource manager of TPM index 0 rather than the container's own TPM.
Impact
Do not allow untrusted clients to attach tpm devices to containers, or remove host and other TPM devices from systems where they can.