Browse code

Merge pull request #40291 from akhilerm/privileged-device

35991- make `--device` works at privileged mode

Brian Goff authored on 2020/01/03 03:09:31
Showing 2 changed files
... ...
@@ -275,6 +275,7 @@ func validateHostConfig(hostConfig *containertypes.HostConfig, platform string)
275 275
 	if hostConfig == nil {
276 276
 		return nil
277 277
 	}
278
+
278 279
 	if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
279 280
 		return errors.Errorf("can't create 'AutoRemove' container with restart policy")
280 281
 	}
... ...
@@ -805,6 +805,7 @@ func WithDevices(daemon *Daemon, c *container.Container) coci.SpecOpts {
805 805
 		// Build lists of devices allowed and created within the container.
806 806
 		var devs []specs.LinuxDevice
807 807
 		devPermissions := s.Linux.Resources.Devices
808
+
808 809
 		if c.HostConfig.Privileged && !rsystem.RunningInUserNS() {
809 810
 			hostDevices, err := devices.HostDevices()
810 811
 			if err != nil {
... ...
@@ -813,6 +814,25 @@ func WithDevices(daemon *Daemon, c *container.Container) coci.SpecOpts {
813 813
 			for _, d := range hostDevices {
814 814
 				devs = append(devs, oci.Device(d))
815 815
 			}
816
+
817
+			// adding device mappings in privileged containers
818
+			for _, deviceMapping := range c.HostConfig.Devices {
819
+				// issue a warning that custom cgroup permissions are ignored in privileged mode
820
+				if deviceMapping.CgroupPermissions != "rwm" {
821
+					logrus.WithField("container", c.ID).Warnf("custom %s permissions for device %s are ignored in privileged mode", deviceMapping.CgroupPermissions, deviceMapping.PathOnHost)
822
+				}
823
+				// issue a warning that the device path already exists via /dev mounting in privileged mode
824
+				if deviceMapping.PathOnHost == deviceMapping.PathInContainer {
825
+					logrus.WithField("container", c.ID).Warnf("path in container %s already exists in privileged mode", deviceMapping.PathInContainer)
826
+					continue
827
+				}
828
+				d, _, err := oci.DevicesFromPath(deviceMapping.PathOnHost, deviceMapping.PathInContainer, "rwm")
829
+				if err != nil {
830
+					return err
831
+				}
832
+				devs = append(devs, d...)
833
+			}
834
+
816 835
 			devPermissions = []specs.LinuxDeviceCgroup{
817 836
 				{
818 837
 					Allow:  true,