-
Notifications
You must be signed in to change notification settings - Fork 47
Ensure that filemode is set for device nodes #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ package cdi | |
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
@@ -31,16 +32,28 @@ const ( | |
| fifoDevice = "p" | ||
| ) | ||
|
|
||
| type deviceInfo struct { | ||
| // cgroup properties | ||
| deviceType string | ||
| major int64 | ||
| minor int64 | ||
|
|
||
| // device node properties | ||
| fileMode os.FileMode | ||
| } | ||
|
|
||
| // deviceInfoFromPath takes the path to a device and returns its type, | ||
| // major and minor device numbers. | ||
| // | ||
| // It was adapted from https://github.com/opencontainers/runc/blob/v1.1.9/libcontainer/devices/device_unix.go#L30-L69 | ||
| func deviceInfoFromPath(path string) (devType string, major, minor int64, _ error) { | ||
| func deviceInfoFromPath(path string) (*deviceInfo, error) { | ||
| var stat unix.Stat_t | ||
| err := unix.Lstat(path, &stat) | ||
| if err != nil { | ||
| return "", 0, 0, err | ||
| return nil, err | ||
| } | ||
|
|
||
| var devType string | ||
| switch stat.Mode & unix.S_IFMT { | ||
| case unix.S_IFBLK: | ||
| devType = blockDevice | ||
|
|
@@ -49,38 +62,71 @@ func deviceInfoFromPath(path string) (devType string, major, minor int64, _ erro | |
| case unix.S_IFIFO: | ||
| devType = fifoDevice | ||
| default: | ||
| return "", 0, 0, errors.New("not a device node") | ||
| return nil, errors.New("not a device node") | ||
| } | ||
| devNumber := uint64(stat.Rdev) //nolint:unconvert // Rdev is uint32 on e.g. MIPS. | ||
| return devType, int64(unix.Major(devNumber)), int64(unix.Minor(devNumber)), nil | ||
|
|
||
| di := deviceInfo{ | ||
| deviceType: devType, | ||
| major: int64(unix.Major(devNumber)), | ||
| minor: int64(unix.Minor(devNumber)), | ||
| fileMode: os.FileMode(stat.Mode &^ unix.S_IFMT), | ||
| } | ||
|
|
||
| return &di, nil | ||
| } | ||
|
|
||
| // fillMissingInfo fills in missing mandatory attributes from the host device. | ||
| func (d *DeviceNode) fillMissingInfo() error { | ||
| hasMinimalSpecification := d.Type != "" && (d.Major != 0 || d.Type == fifoDevice) | ||
|
|
||
| // Ensure that the host path and the container path match. | ||
| if d.HostPath == "" { | ||
| d.HostPath = d.Path | ||
| } | ||
|
|
||
| if d.Type != "" && (d.Major != 0 || d.Type == "p") { | ||
| // Try to extract the device info from the host path. | ||
| di, err := deviceInfoFromPath(d.HostPath) | ||
| if err != nil { | ||
| // The error is only considered fatal if the device is not already | ||
| // minimally specified since it is allowed for a device vendor to fully | ||
| // specify a device node specification. | ||
| if !hasMinimalSpecification { | ||
| return fmt.Errorf("failed to stat CDI host device %q: %w", d.HostPath, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| deviceType, major, minor, err := deviceInfoFromPath(d.HostPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to stat CDI host device %q: %w", d.HostPath, err) | ||
| // Even for minimally-specified device nodes, we update the file mode if | ||
| // required. This is useful for rootless containers where device node | ||
| // requests may be treated as bind mounts. | ||
|
Comment on lines
+100
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elezar A question related to me trying to understand the essence of that comment better. If we have a rootless container, where IIUC devices are bind-mounted into the container instead of being mknod'ded, will the devices not always end up with the permissions of the bind-mounted device on the host side ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I may have been mixing behaviour that I saw in another case. I will update the comment in a follow-up. |
||
| if d.FileMode == nil { | ||
| d.FileMode = &di.fileMode | ||
| } | ||
|
|
||
| // If the device is minimally specified, we make no further updates and | ||
| // don't perform additional checks. | ||
| if hasMinimalSpecification { | ||
| return nil | ||
| } | ||
|
|
||
| if d.Type == "" { | ||
| d.Type = deviceType | ||
| } else { | ||
| if d.Type != deviceType { | ||
| return fmt.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)", | ||
| d.Path, d.HostPath, d.Type, deviceType) | ||
| } | ||
| d.Type = di.deviceType | ||
| } | ||
| if d.Major == 0 && d.Type != "p" { | ||
| d.Major = major | ||
| d.Minor = minor | ||
| if d.Type != di.deviceType { | ||
| return fmt.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)", | ||
| d.Path, d.HostPath, d.Type, di.deviceType) | ||
| } | ||
|
|
||
| // For a fifoDevice, we do not update the major and minor number. | ||
| if d.Type == fifoDevice { | ||
| return nil | ||
| } | ||
|
|
||
| // Update the major and minor number for the device node if required. | ||
| if d.Major == 0 { | ||
| d.Major = di.major | ||
| d.Minor = di.minor | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also set d.FileMode here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you set it to in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same as below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is an early return meaning that we don't query the device node. I can make things a bit more consistent though. Let me see what I can come up with.