-
Notifications
You must be signed in to change notification settings - Fork 45
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
base: main
Are you sure you want to change the base?
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,10 +62,18 @@ 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. | ||||||
|
@@ -65,22 +86,31 @@ func (d *DeviceNode) fillMissingInfo() error { | |||||
return nil | ||||||
} | ||||||
|
||||||
deviceType, major, minor, err := deviceInfoFromPath(d.HostPath) | ||||||
di, err := deviceInfoFromPath(d.HostPath) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to stat CDI host device %q: %w", d.HostPath, err) | ||||||
} | ||||||
|
||||||
if d.Type == "" { | ||||||
d.Type = deviceType | ||||||
d.Type = di.deviceType | ||||||
} else { | ||||||
if d.Type != deviceType { | ||||||
if d.Type != di.deviceType { | ||||||
return fmt.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)", | ||||||
d.Path, d.HostPath, d.Type, deviceType) | ||||||
d.Path, d.HostPath, d.Type, di.deviceType) | ||||||
} | ||||||
} | ||||||
if d.Major == 0 && d.Type != "p" { | ||||||
d.Major = major | ||||||
d.Minor = minor | ||||||
|
||||||
if d.FileMode == nil { | ||||||
d.FileMode = &di.fileMode | ||||||
} | ||||||
|
||||||
if d.Type == "p" { | ||||||
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.
Suggested change
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. We use 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. Yes, please do. |
||||||
return nil | ||||||
} | ||||||
|
||||||
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.