Skip to content

Commit 746a5c2

Browse files
committed
libcontainer/configs/validate: improve rootlessEUIDMount
1. Avoid splitting mount data into []string if it does not contain options we're interested in. This should result in slightly less garbage to collect. 2. Use if / else if instead of continue, to make it clearer that we're processing one option at a time. 3. Print the whole option as a sting in an error message; practically this should not have any effect, it's just simpler. 4. Improve some comments. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 055041e commit 746a5c2

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

libcontainer/configs/validate/rootless.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ func rootlessEUIDMount(config *configs.Config) error {
5252
// convinced that's a good idea. The kernel is the best arbiter of
5353
// access control.
5454

55+
// Check that the options list doesn't contain any uid= or gid= entries
56+
// that don't resolve to root.
5557
for _, mount := range config.Mounts {
56-
// Check that the options list doesn't contain any uid= or gid= entries
57-
// that don't resolve to root.
58+
// Look for a common substring; skip further processing
59+
// if there can't be any uid= or gid= options.
60+
if !strings.Contains(mount.Data, "id=") {
61+
continue
62+
}
5863
for _, opt := range strings.Split(mount.Data, ",") {
5964
if str, ok := strings.CutPrefix(opt, "uid="); ok {
6065
uid, err := strconv.Atoi(str)
@@ -63,18 +68,16 @@ func rootlessEUIDMount(config *configs.Config) error {
6368
continue
6469
}
6570
if _, err := config.HostUID(uid); err != nil {
66-
return fmt.Errorf("cannot specify uid=%d mount option for rootless container: %w", uid, err)
71+
return fmt.Errorf("cannot specify %s mount option for rootless container: %w", opt, err)
6772
}
68-
continue
69-
}
70-
if str, ok := strings.CutPrefix(opt, "gid="); ok {
73+
} else if str, ok := strings.CutPrefix(opt, "gid="); ok {
7174
gid, err := strconv.Atoi(str)
7275
if err != nil {
7376
// Ignore unknown mount options.
7477
continue
7578
}
7679
if _, err := config.HostGID(gid); err != nil {
77-
return fmt.Errorf("cannot specify gid=%d mount option for rootless container: %w", gid, err)
80+
return fmt.Errorf("cannot specify %s mount option for rootless container: %w", opt, err)
7881
}
7982
}
8083
}

0 commit comments

Comments
 (0)