2018-02-05 16:05:59 -05:00
|
|
|
package idtools // import "github.com/docker/docker/pkg/idtools"
|
2015-10-08 11:46:10 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-16 18:44:10 -04:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2015-10-08 11:46:10 -04:00
|
|
|
"strings"
|
2016-04-06 17:24:17 -04:00
|
|
|
"sync"
|
2015-10-08 11:46:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// add a user and/or group to Linux /etc/passwd, /etc/group using standard
|
|
|
|
// Linux distribution commands:
|
2016-03-16 18:44:10 -04:00
|
|
|
// adduser --system --shell /bin/false --disabled-login --disabled-password --no-create-home --group <username>
|
|
|
|
// useradd -r -s /bin/false <username>
|
2015-10-08 11:46:10 -04:00
|
|
|
|
|
|
|
var (
|
2016-04-06 17:24:17 -04:00
|
|
|
once sync.Once
|
2016-03-16 18:44:10 -04:00
|
|
|
userCommand string
|
|
|
|
idOutRegexp = regexp.MustCompile(`uid=([0-9]+).*gid=([0-9]+)`)
|
pkg/idtools: refactor to avoid string-splitting
The package used a lot of string-formatting, followed by string-splitting.
This looked to originate from attempts to use templating to allow future
extensibility (9a3ab0358ecd657e3754677ff52250fd6cca4422).
Looking at the history of the package, only a single update was made to
these templates, 5 years go, which makes it unlikely that more templating
will be needed.
This patch simplifies the handling of arguments to use `[]string` instead
of a single `string` (and splitting to a `[]string`). This both simplifies
the code somewhat, and prevents user/group-names containing spaces to be
splitted (causing, e.g. `getent` to fail).
Note that user/group-names containing spaces are invalid (or at least
discouraged), there are situations where such names may be used, so we
should avoid breaking on such names.
Before this change, a user/group name with a space in its name would fail;
dockerd --userns-remap="user:domain users"
INFO[2020-08-19T10:26:59.288868661+02:00] Starting up
Error during groupname lookup for "domain users": getent unable to find entry "domain" in group database
With this change:
# Add some possibly problematic usernames for testing
# need to do this manually, as `adduser` / `useradd` won't accept these names
echo 'user name:x:1002:1002::/home/one:/bin/false' >> /etc/passwd; \
echo 'user name:x:1002:' >> /etc/group; \
echo 'user name:1266401166:65536' >> /etc/subuid; \
echo 'user name:1266401153:65536' >> /etc/subgid; \
echo 'user$HOME:x:1003:1003::/home/one:/bin/false' >> /etc/passwd; \
echo 'user$HOME:x:1003:' >> /etc/group; \
echo 'user$HOME:1266401166:65536' >> /etc/subuid; \
echo 'user$HOME:1266401153:65536' >> /etc/subgid; \
echo 'user'"'"'name:x:1004:1004::/home/one:/bin/false' >> /etc/passwd; \
echo 'user'"'"'name:x:1004:' >> /etc/group; \
echo 'user'"'"'name:1266401166:65536' >> /etc/subuid; \
echo 'user'"'"'name:1266401153:65536' >> /etc/subgid; \
echo 'user"name:x:1005:1005::/home/one:/bin/false' >> /etc/passwd; \
echo 'user"name:x:1005:' >> /etc/group; \
echo 'user"name:1266401166:65536' >> /etc/subuid; \
echo 'user"name:1266401153:65536' >> /etc/subgid;
# Start the daemon using those users
dockerd --userns-remap="user name:user name"
dockerd --userns-remap='user$HOME:user$HOME'
dockerd --userns-remap="user'name":"user'name"
dockerd --userns-remap='user"name':'user"name'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-20 04:40:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-03-16 18:44:10 -04:00
|
|
|
// default length for a UID/GID subordinate range
|
|
|
|
defaultRangeLen = 65536
|
|
|
|
defaultRangeStart = 100000
|
2015-10-08 11:46:10 -04:00
|
|
|
)
|
|
|
|
|
2016-03-16 18:44:10 -04:00
|
|
|
// AddNamespaceRangesUser takes a username and uses the standard system
|
|
|
|
// utility to create a system user/group pair used to hold the
|
|
|
|
// /etc/sub{uid,gid} ranges which will be used for user namespace
|
2015-10-08 11:46:10 -04:00
|
|
|
// mapping ranges in containers.
|
|
|
|
func AddNamespaceRangesUser(name string) (int, int, error) {
|
2016-03-16 18:44:10 -04:00
|
|
|
if err := addUser(name); err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, -1, fmt.Errorf("error adding user %q: %v", name, err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query the system for the created uid and gid pair
|
|
|
|
out, err := execCmd("id", name)
|
2015-10-08 11:46:10 -04:00
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, -1, fmt.Errorf("error trying to find uid/gid for new user %q: %v", name, err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
|
|
|
matches := idOutRegexp.FindStringSubmatch(strings.TrimSpace(string(out)))
|
|
|
|
if len(matches) != 3 {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, -1, fmt.Errorf("can't find uid, gid from `id` output: %q", string(out))
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
2016-03-16 18:44:10 -04:00
|
|
|
uid, err := strconv.Atoi(matches[1])
|
2015-10-08 11:46:10 -04:00
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, -1, fmt.Errorf("can't convert found uid (%s) to int: %v", matches[1], err)
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
2016-03-16 18:44:10 -04:00
|
|
|
gid, err := strconv.Atoi(matches[2])
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, fmt.Errorf("Can't convert found gid (%s) to int: %v", matches[2], err)
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
2016-03-16 18:44:10 -04:00
|
|
|
|
|
|
|
// Now we need to create the subuid/subgid ranges for our new user/group (system users
|
|
|
|
// do not get auto-created ranges in subuid/subgid)
|
|
|
|
|
|
|
|
if err := createSubordinateRanges(name); err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, -1, fmt.Errorf("couldn't create subordinate ID ranges: %v", err)
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
return uid, gid, nil
|
|
|
|
}
|
|
|
|
|
pkg/idtools: refactor to avoid string-splitting
The package used a lot of string-formatting, followed by string-splitting.
This looked to originate from attempts to use templating to allow future
extensibility (9a3ab0358ecd657e3754677ff52250fd6cca4422).
Looking at the history of the package, only a single update was made to
these templates, 5 years go, which makes it unlikely that more templating
will be needed.
This patch simplifies the handling of arguments to use `[]string` instead
of a single `string` (and splitting to a `[]string`). This both simplifies
the code somewhat, and prevents user/group-names containing spaces to be
splitted (causing, e.g. `getent` to fail).
Note that user/group-names containing spaces are invalid (or at least
discouraged), there are situations where such names may be used, so we
should avoid breaking on such names.
Before this change, a user/group name with a space in its name would fail;
dockerd --userns-remap="user:domain users"
INFO[2020-08-19T10:26:59.288868661+02:00] Starting up
Error during groupname lookup for "domain users": getent unable to find entry "domain" in group database
With this change:
# Add some possibly problematic usernames for testing
# need to do this manually, as `adduser` / `useradd` won't accept these names
echo 'user name:x:1002:1002::/home/one:/bin/false' >> /etc/passwd; \
echo 'user name:x:1002:' >> /etc/group; \
echo 'user name:1266401166:65536' >> /etc/subuid; \
echo 'user name:1266401153:65536' >> /etc/subgid; \
echo 'user$HOME:x:1003:1003::/home/one:/bin/false' >> /etc/passwd; \
echo 'user$HOME:x:1003:' >> /etc/group; \
echo 'user$HOME:1266401166:65536' >> /etc/subuid; \
echo 'user$HOME:1266401153:65536' >> /etc/subgid; \
echo 'user'"'"'name:x:1004:1004::/home/one:/bin/false' >> /etc/passwd; \
echo 'user'"'"'name:x:1004:' >> /etc/group; \
echo 'user'"'"'name:1266401166:65536' >> /etc/subuid; \
echo 'user'"'"'name:1266401153:65536' >> /etc/subgid; \
echo 'user"name:x:1005:1005::/home/one:/bin/false' >> /etc/passwd; \
echo 'user"name:x:1005:' >> /etc/group; \
echo 'user"name:1266401166:65536' >> /etc/subuid; \
echo 'user"name:1266401153:65536' >> /etc/subgid;
# Start the daemon using those users
dockerd --userns-remap="user name:user name"
dockerd --userns-remap='user$HOME:user$HOME'
dockerd --userns-remap="user'name":"user'name"
dockerd --userns-remap='user"name':'user"name'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-20 04:40:06 -04:00
|
|
|
func addUser(name string) error {
|
2016-04-06 17:24:17 -04:00
|
|
|
once.Do(func() {
|
|
|
|
// set up which commands are used for adding users/groups dependent on distro
|
|
|
|
if _, err := resolveBinary("adduser"); err == nil {
|
|
|
|
userCommand = "adduser"
|
|
|
|
} else if _, err := resolveBinary("useradd"); err == nil {
|
|
|
|
userCommand = "useradd"
|
|
|
|
}
|
|
|
|
})
|
pkg/idtools: refactor to avoid string-splitting
The package used a lot of string-formatting, followed by string-splitting.
This looked to originate from attempts to use templating to allow future
extensibility (9a3ab0358ecd657e3754677ff52250fd6cca4422).
Looking at the history of the package, only a single update was made to
these templates, 5 years go, which makes it unlikely that more templating
will be needed.
This patch simplifies the handling of arguments to use `[]string` instead
of a single `string` (and splitting to a `[]string`). This both simplifies
the code somewhat, and prevents user/group-names containing spaces to be
splitted (causing, e.g. `getent` to fail).
Note that user/group-names containing spaces are invalid (or at least
discouraged), there are situations where such names may be used, so we
should avoid breaking on such names.
Before this change, a user/group name with a space in its name would fail;
dockerd --userns-remap="user:domain users"
INFO[2020-08-19T10:26:59.288868661+02:00] Starting up
Error during groupname lookup for "domain users": getent unable to find entry "domain" in group database
With this change:
# Add some possibly problematic usernames for testing
# need to do this manually, as `adduser` / `useradd` won't accept these names
echo 'user name:x:1002:1002::/home/one:/bin/false' >> /etc/passwd; \
echo 'user name:x:1002:' >> /etc/group; \
echo 'user name:1266401166:65536' >> /etc/subuid; \
echo 'user name:1266401153:65536' >> /etc/subgid; \
echo 'user$HOME:x:1003:1003::/home/one:/bin/false' >> /etc/passwd; \
echo 'user$HOME:x:1003:' >> /etc/group; \
echo 'user$HOME:1266401166:65536' >> /etc/subuid; \
echo 'user$HOME:1266401153:65536' >> /etc/subgid; \
echo 'user'"'"'name:x:1004:1004::/home/one:/bin/false' >> /etc/passwd; \
echo 'user'"'"'name:x:1004:' >> /etc/group; \
echo 'user'"'"'name:1266401166:65536' >> /etc/subuid; \
echo 'user'"'"'name:1266401153:65536' >> /etc/subgid; \
echo 'user"name:x:1005:1005::/home/one:/bin/false' >> /etc/passwd; \
echo 'user"name:x:1005:' >> /etc/group; \
echo 'user"name:1266401166:65536' >> /etc/subuid; \
echo 'user"name:1266401153:65536' >> /etc/subgid;
# Start the daemon using those users
dockerd --userns-remap="user name:user name"
dockerd --userns-remap='user$HOME:user$HOME'
dockerd --userns-remap="user'name":"user'name"
dockerd --userns-remap='user"name':'user"name'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-20 04:40:06 -04:00
|
|
|
var args []string
|
|
|
|
switch userCommand {
|
|
|
|
case "adduser":
|
|
|
|
args = []string{"--system", "--shell", "/bin/false", "--no-create-home", "--disabled-login", "--disabled-password", "--group", name}
|
|
|
|
case "useradd":
|
|
|
|
args = []string{"-r", "-s", "/bin/false", name}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("cannot add user; no useradd/adduser binary found")
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
pkg/idtools: refactor to avoid string-splitting
The package used a lot of string-formatting, followed by string-splitting.
This looked to originate from attempts to use templating to allow future
extensibility (9a3ab0358ecd657e3754677ff52250fd6cca4422).
Looking at the history of the package, only a single update was made to
these templates, 5 years go, which makes it unlikely that more templating
will be needed.
This patch simplifies the handling of arguments to use `[]string` instead
of a single `string` (and splitting to a `[]string`). This both simplifies
the code somewhat, and prevents user/group-names containing spaces to be
splitted (causing, e.g. `getent` to fail).
Note that user/group-names containing spaces are invalid (or at least
discouraged), there are situations where such names may be used, so we
should avoid breaking on such names.
Before this change, a user/group name with a space in its name would fail;
dockerd --userns-remap="user:domain users"
INFO[2020-08-19T10:26:59.288868661+02:00] Starting up
Error during groupname lookup for "domain users": getent unable to find entry "domain" in group database
With this change:
# Add some possibly problematic usernames for testing
# need to do this manually, as `adduser` / `useradd` won't accept these names
echo 'user name:x:1002:1002::/home/one:/bin/false' >> /etc/passwd; \
echo 'user name:x:1002:' >> /etc/group; \
echo 'user name:1266401166:65536' >> /etc/subuid; \
echo 'user name:1266401153:65536' >> /etc/subgid; \
echo 'user$HOME:x:1003:1003::/home/one:/bin/false' >> /etc/passwd; \
echo 'user$HOME:x:1003:' >> /etc/group; \
echo 'user$HOME:1266401166:65536' >> /etc/subuid; \
echo 'user$HOME:1266401153:65536' >> /etc/subgid; \
echo 'user'"'"'name:x:1004:1004::/home/one:/bin/false' >> /etc/passwd; \
echo 'user'"'"'name:x:1004:' >> /etc/group; \
echo 'user'"'"'name:1266401166:65536' >> /etc/subuid; \
echo 'user'"'"'name:1266401153:65536' >> /etc/subgid; \
echo 'user"name:x:1005:1005::/home/one:/bin/false' >> /etc/passwd; \
echo 'user"name:x:1005:' >> /etc/group; \
echo 'user"name:1266401166:65536' >> /etc/subuid; \
echo 'user"name:1266401153:65536' >> /etc/subgid;
# Start the daemon using those users
dockerd --userns-remap="user name:user name"
dockerd --userns-remap='user$HOME:user$HOME'
dockerd --userns-remap="user'name":"user'name"
dockerd --userns-remap='user"name':'user"name'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-20 04:40:06 -04:00
|
|
|
|
|
|
|
if out, err := execCmd(userCommand, args...); err != nil {
|
|
|
|
return fmt.Errorf("failed to add user with error: %v; output: %q", err, string(out))
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
|
|
|
return nil
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 18:44:10 -04:00
|
|
|
func createSubordinateRanges(name string) error {
|
|
|
|
// first, we should verify that ranges weren't automatically created
|
|
|
|
// by the distro tooling
|
|
|
|
ranges, err := parseSubuid(name)
|
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return fmt.Errorf("error while looking for subuid ranges for user %q: %v", name, err)
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
2016-03-16 18:44:10 -04:00
|
|
|
if len(ranges) == 0 {
|
|
|
|
// no UID ranges; let's create one
|
|
|
|
startID, err := findNextUIDRange()
|
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return fmt.Errorf("can't find available subuid range: %v", err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
pkg/idtools: refactor to avoid string-splitting
The package used a lot of string-formatting, followed by string-splitting.
This looked to originate from attempts to use templating to allow future
extensibility (9a3ab0358ecd657e3754677ff52250fd6cca4422).
Looking at the history of the package, only a single update was made to
these templates, 5 years go, which makes it unlikely that more templating
will be needed.
This patch simplifies the handling of arguments to use `[]string` instead
of a single `string` (and splitting to a `[]string`). This both simplifies
the code somewhat, and prevents user/group-names containing spaces to be
splitted (causing, e.g. `getent` to fail).
Note that user/group-names containing spaces are invalid (or at least
discouraged), there are situations where such names may be used, so we
should avoid breaking on such names.
Before this change, a user/group name with a space in its name would fail;
dockerd --userns-remap="user:domain users"
INFO[2020-08-19T10:26:59.288868661+02:00] Starting up
Error during groupname lookup for "domain users": getent unable to find entry "domain" in group database
With this change:
# Add some possibly problematic usernames for testing
# need to do this manually, as `adduser` / `useradd` won't accept these names
echo 'user name:x:1002:1002::/home/one:/bin/false' >> /etc/passwd; \
echo 'user name:x:1002:' >> /etc/group; \
echo 'user name:1266401166:65536' >> /etc/subuid; \
echo 'user name:1266401153:65536' >> /etc/subgid; \
echo 'user$HOME:x:1003:1003::/home/one:/bin/false' >> /etc/passwd; \
echo 'user$HOME:x:1003:' >> /etc/group; \
echo 'user$HOME:1266401166:65536' >> /etc/subuid; \
echo 'user$HOME:1266401153:65536' >> /etc/subgid; \
echo 'user'"'"'name:x:1004:1004::/home/one:/bin/false' >> /etc/passwd; \
echo 'user'"'"'name:x:1004:' >> /etc/group; \
echo 'user'"'"'name:1266401166:65536' >> /etc/subuid; \
echo 'user'"'"'name:1266401153:65536' >> /etc/subgid; \
echo 'user"name:x:1005:1005::/home/one:/bin/false' >> /etc/passwd; \
echo 'user"name:x:1005:' >> /etc/group; \
echo 'user"name:1266401166:65536' >> /etc/subuid; \
echo 'user"name:1266401153:65536' >> /etc/subgid;
# Start the daemon using those users
dockerd --userns-remap="user name:user name"
dockerd --userns-remap='user$HOME:user$HOME'
dockerd --userns-remap="user'name":"user'name"
dockerd --userns-remap='user"name':'user"name'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-20 04:40:06 -04:00
|
|
|
out, err := execCmd("usermod", "-v", fmt.Sprintf("%d-%d", startID, startID+defaultRangeLen-1), name)
|
2016-03-16 18:44:10 -04:00
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return fmt.Errorf("unable to add subuid range to user: %q; output: %s, err: %v", name, out, err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 18:44:10 -04:00
|
|
|
ranges, err = parseSubgid(name)
|
2015-10-08 11:46:10 -04:00
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return fmt.Errorf("error while looking for subgid ranges for user %q: %v", name, err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
|
|
|
if len(ranges) == 0 {
|
|
|
|
// no GID ranges; let's create one
|
|
|
|
startID, err := findNextGIDRange()
|
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return fmt.Errorf("can't find available subgid range: %v", err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
pkg/idtools: refactor to avoid string-splitting
The package used a lot of string-formatting, followed by string-splitting.
This looked to originate from attempts to use templating to allow future
extensibility (9a3ab0358ecd657e3754677ff52250fd6cca4422).
Looking at the history of the package, only a single update was made to
these templates, 5 years go, which makes it unlikely that more templating
will be needed.
This patch simplifies the handling of arguments to use `[]string` instead
of a single `string` (and splitting to a `[]string`). This both simplifies
the code somewhat, and prevents user/group-names containing spaces to be
splitted (causing, e.g. `getent` to fail).
Note that user/group-names containing spaces are invalid (or at least
discouraged), there are situations where such names may be used, so we
should avoid breaking on such names.
Before this change, a user/group name with a space in its name would fail;
dockerd --userns-remap="user:domain users"
INFO[2020-08-19T10:26:59.288868661+02:00] Starting up
Error during groupname lookup for "domain users": getent unable to find entry "domain" in group database
With this change:
# Add some possibly problematic usernames for testing
# need to do this manually, as `adduser` / `useradd` won't accept these names
echo 'user name:x:1002:1002::/home/one:/bin/false' >> /etc/passwd; \
echo 'user name:x:1002:' >> /etc/group; \
echo 'user name:1266401166:65536' >> /etc/subuid; \
echo 'user name:1266401153:65536' >> /etc/subgid; \
echo 'user$HOME:x:1003:1003::/home/one:/bin/false' >> /etc/passwd; \
echo 'user$HOME:x:1003:' >> /etc/group; \
echo 'user$HOME:1266401166:65536' >> /etc/subuid; \
echo 'user$HOME:1266401153:65536' >> /etc/subgid; \
echo 'user'"'"'name:x:1004:1004::/home/one:/bin/false' >> /etc/passwd; \
echo 'user'"'"'name:x:1004:' >> /etc/group; \
echo 'user'"'"'name:1266401166:65536' >> /etc/subuid; \
echo 'user'"'"'name:1266401153:65536' >> /etc/subgid; \
echo 'user"name:x:1005:1005::/home/one:/bin/false' >> /etc/passwd; \
echo 'user"name:x:1005:' >> /etc/group; \
echo 'user"name:1266401166:65536' >> /etc/subuid; \
echo 'user"name:1266401153:65536' >> /etc/subgid;
# Start the daemon using those users
dockerd --userns-remap="user name:user name"
dockerd --userns-remap='user$HOME:user$HOME'
dockerd --userns-remap="user'name":"user'name"
dockerd --userns-remap='user"name':'user"name'
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-20 04:40:06 -04:00
|
|
|
out, err := execCmd("usermod", "-w", fmt.Sprintf("%d-%d", startID, startID+defaultRangeLen-1), name)
|
2016-03-16 18:44:10 -04:00
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return fmt.Errorf("unable to add subgid range to user: %q; output: %s, err: %v", name, out, err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-16 18:44:10 -04:00
|
|
|
func findNextUIDRange() (int, error) {
|
|
|
|
ranges, err := parseSubuid("ALL")
|
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, fmt.Errorf("couldn't parse all ranges in /etc/subuid file: %v", err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
|
|
|
sort.Sort(ranges)
|
|
|
|
return findNextRangeStart(ranges)
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 18:44:10 -04:00
|
|
|
func findNextGIDRange() (int, error) {
|
|
|
|
ranges, err := parseSubgid("ALL")
|
|
|
|
if err != nil {
|
2022-10-05 09:48:46 -04:00
|
|
|
return -1, fmt.Errorf("couldn't parse all ranges in /etc/subgid file: %v", err)
|
2016-03-16 18:44:10 -04:00
|
|
|
}
|
|
|
|
sort.Sort(ranges)
|
|
|
|
return findNextRangeStart(ranges)
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 18:44:10 -04:00
|
|
|
func findNextRangeStart(rangeList ranges) (int, error) {
|
|
|
|
startID := defaultRangeStart
|
|
|
|
for _, arange := range rangeList {
|
|
|
|
if wouldOverlap(arange, startID) {
|
|
|
|
startID = arange.Start + arange.Length
|
2015-10-08 11:46:10 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-16 18:44:10 -04:00
|
|
|
return startID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func wouldOverlap(arange subIDRange, ID int) bool {
|
|
|
|
low := ID
|
|
|
|
high := ID + defaultRangeLen
|
|
|
|
if (low >= arange.Start && low <= arange.Start+arange.Length) ||
|
|
|
|
(high <= arange.Start+arange.Length && high >= arange.Start) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|