|
| 1 | +// + build linux |
| 2 | + |
| 3 | +package vtpmhelper |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "syscall" |
| 9 | + |
| 10 | + "github.com/opencontainers/runc/libcontainer/configs" |
| 11 | + "github.com/opencontainers/runc/libcontainer/vtpm" |
| 12 | + |
| 13 | + "github.com/opencontainers/runtime-spec/specs-go" |
| 14 | + "golang.org/x/sys/unix" |
| 15 | +) |
| 16 | + |
| 17 | +func addVTPMDevice(spec *specs.Spec, hostpath, devpath string, major, minor uint32) { |
| 18 | + var filemode os.FileMode = 0600 |
| 19 | + |
| 20 | + device := specs.LinuxDevice{ |
| 21 | + Path: hostpath, |
| 22 | + Devpath: devpath, |
| 23 | + Type: "c", |
| 24 | + Major: int64(major), |
| 25 | + Minor: int64(minor), |
| 26 | + FileMode: &filemode, |
| 27 | + } |
| 28 | + spec.Linux.Devices = append(spec.Linux.Devices, device) |
| 29 | + |
| 30 | + major_p := new(int64) |
| 31 | + *major_p = int64(major) |
| 32 | + minor_p := new(int64) |
| 33 | + *minor_p = int64(minor) |
| 34 | + |
| 35 | + ld := &specs.LinuxDeviceCgroup{ |
| 36 | + Allow: true, |
| 37 | + Type: "c", |
| 38 | + Major: major_p, |
| 39 | + Minor: minor_p, |
| 40 | + Access: "rwm", |
| 41 | + } |
| 42 | + spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, *ld) |
| 43 | +} |
| 44 | + |
| 45 | +// Create a VTPM |
| 46 | +func CreateVTPM(spec *specs.Spec, vtpmdev *specs.VTPM, devnum int) (*vtpm.VTPM, error) { |
| 47 | + |
| 48 | + vtpm, err := vtpm.NewVTPM(vtpmdev.Statepath, vtpmdev.StatepathIsManaged, vtpmdev.TPMVersion, vtpmdev.CreateCertificates, vtpmdev.Runas, vtpmdev.PcrBanks) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + // Start the vTPM process; once stopped, the device pair will |
| 54 | + // also disappear |
| 55 | + vtpm.CreatedStatepath, err = vtpm.Start() |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + hostdev := vtpm.GetTPMDevname() |
| 61 | + major, minor := vtpm.GetMajorMinor() |
| 62 | + |
| 63 | + devpath := fmt.Sprintf("/dev/tpm%d", devnum) |
| 64 | + addVTPMDevice(spec, hostdev, devpath, major, minor) |
| 65 | + |
| 66 | + // check if /dev/vtpmrm%d is available |
| 67 | + host_tpmrm := fmt.Sprintf("/dev/tpmrm%d", vtpm.GetTPMDevNum()) |
| 68 | + if fileInfo, err := os.Lstat(host_tpmrm); err == nil { |
| 69 | + if stat_t, ok := fileInfo.Sys().(*syscall.Stat_t); ok { |
| 70 | + devNumber := stat_t.Rdev |
| 71 | + devpath = fmt.Sprintf("/dev/tpmrm%d", devnum) |
| 72 | + addVTPMDevice(spec, host_tpmrm, devpath, unix.Major(devNumber), unix.Minor(devNumber)) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return vtpm, nil |
| 77 | +} |
| 78 | + |
| 79 | +func setVTPMHostDevOwner(vtpm *vtpm.VTPM, uid, gid int) error { |
| 80 | + hostdev := vtpm.GetTPMDevname() |
| 81 | + // adapt ownership of the device since only root can access it |
| 82 | + if err := os.Chown(hostdev, uid, gid); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + host_tpmrm := fmt.Sprintf("/dev/tpmrm%d", vtpm.GetTPMDevNum()) |
| 87 | + if _, err := os.Lstat(host_tpmrm); err == nil { |
| 88 | + // adapt ownership of the device since only root can access it |
| 89 | + if err := os.Chown(host_tpmrm, uid, gid); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// SetVTPMHostDevsOwner sets the owner of the host devices to the |
| 98 | +// container root's mapped user id; if root inside the container is |
| 99 | +// uid 1000 on the host, the devices will be owned by uid 1000. |
| 100 | +func SetVTPMHostDevsOwner(config *configs.Config, uid, gid int) error { |
| 101 | + if uid != 0 { |
| 102 | + for _, vtpm := range config.VTPMs { |
| 103 | + if err := setVTPMHostDevOwner(vtpm, uid, gid); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func DestroyVTPMs(vtpms []*vtpm.VTPM) { |
| 112 | + for _, vtpm := range vtpms { |
| 113 | + vtpm.Stop(vtpm.CreatedStatepath) |
| 114 | + } |
| 115 | +} |
0 commit comments