Skip to content

Commit 3d60190

Browse files
author
Ma Shimiao
committed
runtimetest: add linux devices validation
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 089d971 commit 3d60190

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

cmd/runtimetest/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,62 @@ func validateDefaultFS(spec *rspec.Spec) error {
271271
return nil
272272
}
273273

274+
func validateLinuxDevices(spec *rspec.Spec) error {
275+
logrus.Debugf("validating linux devices")
276+
277+
for _, device := range spec.Linux.Devices {
278+
fi, err := os.Stat(device.Path)
279+
if err != nil {
280+
return err
281+
}
282+
fStat := fi.Sys().(*syscall.Stat_t)
283+
var devType string
284+
switch fStat.Mode & syscall.S_IFMT {
285+
case syscall.S_IFCHR:
286+
devType = "c"
287+
break
288+
case syscall.S_IFBLK:
289+
devType = "b"
290+
break
291+
case syscall.S_IFIFO:
292+
devType = "p"
293+
break
294+
default:
295+
devType = "unmatched"
296+
}
297+
if devType != device.Type || (devType == "c" && device.Type == "u") {
298+
return fmt.Errorf("device %v expected type is %v, actual is %v", device.Path, device.Type, devType)
299+
}
300+
if devType != "p" {
301+
dev := fStat.Rdev
302+
major := (dev >> 8) & 0xfff
303+
minor := (dev & 0xff) | ((dev >> 12) & 0xfff00)
304+
if int64(major) != device.Major || int64(minor) != device.Minor {
305+
return fmt.Errorf("%v device number expected is %v:%v, actual is %v:%v", device.Path, device.Major, device.Minor, major, minor)
306+
}
307+
}
308+
if device.FileMode != nil {
309+
expected_perm := *device.FileMode & os.ModePerm
310+
actual_perm := fi.Mode() & os.ModePerm
311+
if expected_perm != actual_perm {
312+
return fmt.Errorf("%v filemode expected is %v, actual is %v", device.Path, expected_perm, actual_perm)
313+
}
314+
}
315+
if device.UID != nil {
316+
if *device.UID != fStat.Uid {
317+
return fmt.Errorf("%v uid expected is %v, actual is %v", device.Path, *device.UID, fStat.Uid)
318+
}
319+
}
320+
if device.GID != nil {
321+
if *device.GID != fStat.Gid {
322+
return fmt.Errorf("%v uid expected is %v, actual is %v", device.Path, *device.GID, fStat.Gid)
323+
}
324+
}
325+
}
326+
327+
return nil
328+
}
329+
274330
func validateDefaultDevices(spec *rspec.Spec) error {
275331
logrus.Debugf("validating linux default devices")
276332

@@ -395,6 +451,7 @@ func validate(context *cli.Context) error {
395451
linuxValidations := []validation{
396452
validateDefaultFS,
397453
validateDefaultDevices,
454+
validateLinuxDevices,
398455
validateSysctls,
399456
validateMaskedPaths,
400457
validateROPaths,

0 commit comments

Comments
 (0)