Skip to content

Commit d5911d2

Browse files
committed
namespaces: allow configuring keep-id userns size
Introduce a new option "size" to configure the maximum size of the user namespace configured by keep-id. Closes: containers#24837 Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 1e0f03b commit d5911d2

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

docs/source/markdown/options/userns.container.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Podman allocates unique ranges of UIDs and GIDs from the `containers` subordinat
4040

4141
The option `--userns=keep-id` uses all the subuids and subgids of the user.
4242
The option `--userns=nomap` uses all the subuids and subgids of the user except the user's own ID.
43-
Using `--userns=auto` when starting new containers does not work as long as any containers exist that were started with `--userns=keep-id` or `--userns=nomap`.
43+
Using `--userns=auto` when starting new containers does not work as long as any containers exist that were started with `--userns=nomap` or `--userns=keep-id` without limiting the user namespace size.
4444

4545
Valid `auto` options:
4646

@@ -62,6 +62,7 @@ For details see **--uidmap**.
6262

6363
- *uid*=UID: override the UID inside the container that is used to map the current user to.
6464
- *gid*=GID: override the GID inside the container that is used to map the current user to.
65+
- *size*=SIZE: override the size of the configured user namespace. It is useful to not saturate all the available IDs.
6566

6667
**nomap**: creates a user namespace where the current rootless user's UID:GID are not mapped into the container. This option is not allowed for containers created by the root user.
6768

pkg/namespaces/namespaces.go

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type KeepIDUserNsOptions struct {
2626
UID *uint32
2727
// GID is the target uid in the user namespace.
2828
GID *uint32
29+
// MaxSize is the maximum size of the user namespace.
30+
MaxSize *uint32
2931
}
3032

3133
// CgroupMode represents cgroup mode in the container.
@@ -148,6 +150,13 @@ func (n UsernsMode) GetKeepIDOptions() (*KeepIDUserNsOptions, error) {
148150
}
149151
v := uint32(s)
150152
options.GID = &v
153+
case "size":
154+
s, err := strconv.ParseUint(val, 10, 32)
155+
if err != nil {
156+
return nil, err
157+
}
158+
v := uint32(s)
159+
options.MaxSize = &v
151160
default:
152161
return nil, fmt.Errorf("unknown option specified: %q", opt)
153162
}

pkg/specgen/namespaces.go

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/containers/common/pkg/config"
1313
"github.com/containers/podman/v5/libpod/define"
1414
"github.com/containers/podman/v5/pkg/namespaces"
15+
"github.com/containers/podman/v5/pkg/rootless"
1516
"github.com/containers/podman/v5/pkg/util"
1617
"github.com/containers/storage/pkg/fileutils"
1718
"github.com/containers/storage/pkg/unshare"
@@ -514,6 +515,9 @@ func SetupUserNS(idmappings *storageTypes.IDMappingOptions, userns Namespace, g
514515
if err != nil {
515516
return user, err
516517
}
518+
if opts.MaxSize != nil && !rootless.IsRootless() {
519+
return user, fmt.Errorf("cannot set max size for user namespace when not running rootless")
520+
}
517521
mappings, uid, gid, err := util.GetKeepIDMapping(opts)
518522
if err != nil {
519523
return user, err

pkg/util/utils.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func ParseSignal(rawSignal string) (syscall.Signal, error) {
173173
return sig, nil
174174
}
175175

176-
func getRootlessKeepIDMapping(uid, gid int, uids, gids []idtools.IDMap) (*stypes.IDMappingOptions, int, int, error) {
176+
func getRootlessKeepIDMapping(uid, gid int, uids, gids []idtools.IDMap, maxSize int) (*stypes.IDMappingOptions, int, int, error) {
177177
options := stypes.IDMappingOptions{
178178
HostUIDMapping: false,
179179
HostGIDMapping: false,
@@ -185,6 +185,11 @@ func getRootlessKeepIDMapping(uid, gid int, uids, gids []idtools.IDMap) (*stypes
185185
for _, g := range gids {
186186
maxGID += g.Size
187187
}
188+
if maxSize > 0 {
189+
// If maxSize is set, we need to ensure that the mappings are within the available range
190+
maxUID = min(maxUID, maxSize-1)
191+
maxGID = min(maxGID, maxSize-1)
192+
}
188193

189194
options.UIDMap, options.GIDMap = nil, nil
190195

@@ -240,13 +245,17 @@ func GetKeepIDMapping(opts *namespaces.KeepIDUserNsOptions) (*stypes.IDMappingOp
240245
if opts.GID != nil {
241246
gid = int(*opts.GID)
242247
}
248+
maxSize := 0
249+
if opts.MaxSize != nil {
250+
maxSize = int(*opts.MaxSize)
251+
}
243252

244253
uids, gids, err := rootless.GetConfiguredMappings(true)
245254
if err != nil {
246255
return nil, -1, -1, fmt.Errorf("cannot read mappings: %w", err)
247256
}
248257

249-
return getRootlessKeepIDMapping(uid, gid, uids, gids)
258+
return getRootlessKeepIDMapping(uid, gid, uids, gids, maxSize)
250259
}
251260

252261
// GetNoMapMapping returns the mappings and the user to use when nomap is used

pkg/util/utils_test.go

+62-1
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ func TestGetRootlessKeepIDMapping(t *testing.T) {
595595
tests := []struct {
596596
uid, gid int
597597
uids, gids []idtools.IDMap
598+
size int
598599
expectedOptions *stypes.IDMappingOptions
599600
expectedUID, expectedGID int
600601
expectedError error
@@ -627,10 +628,70 @@ func TestGetRootlessKeepIDMapping(t *testing.T) {
627628
expectedUID: 0,
628629
expectedGID: 0,
629630
},
631+
{
632+
uid: 0,
633+
gid: 0,
634+
uids: []idtools.IDMap{{ContainerID: 0, HostID: 100000, Size: 65536}},
635+
gids: []idtools.IDMap{{ContainerID: 0, HostID: 100000, Size: 65536}},
636+
expectedOptions: &stypes.IDMappingOptions{
637+
HostUIDMapping: false,
638+
HostGIDMapping: false,
639+
UIDMap: []idtools.IDMap{{ContainerID: 0, HostID: 0, Size: 1}, {ContainerID: 1, HostID: 1, Size: 1023}},
640+
GIDMap: []idtools.IDMap{{ContainerID: 0, HostID: 0, Size: 1}, {ContainerID: 1, HostID: 1, Size: 1023}},
641+
},
642+
expectedUID: 0,
643+
expectedGID: 0,
644+
size: 1024,
645+
},
646+
{
647+
uid: 0,
648+
gid: 0,
649+
uids: []idtools.IDMap{{ContainerID: 0, HostID: 100000, Size: 65536}},
650+
gids: []idtools.IDMap{{ContainerID: 0, HostID: 100000, Size: 65536}},
651+
expectedOptions: &stypes.IDMappingOptions{
652+
HostUIDMapping: false,
653+
HostGIDMapping: false,
654+
UIDMap: []idtools.IDMap{{ContainerID: 0, HostID: 0, Size: 1}},
655+
GIDMap: []idtools.IDMap{{ContainerID: 0, HostID: 0, Size: 1}},
656+
},
657+
expectedUID: 0,
658+
expectedGID: 0,
659+
size: 1,
660+
},
661+
{
662+
uid: 0,
663+
gid: 0,
664+
uids: []idtools.IDMap{{ContainerID: 0, HostID: 100000, Size: 65536}},
665+
gids: []idtools.IDMap{{ContainerID: 0, HostID: 100000, Size: 65536}},
666+
expectedOptions: &stypes.IDMappingOptions{
667+
HostUIDMapping: false,
668+
HostGIDMapping: false,
669+
UIDMap: []idtools.IDMap{{ContainerID: 0, HostID: 0, Size: 1}, {ContainerID: 1, HostID: 1, Size: 1}},
670+
GIDMap: []idtools.IDMap{{ContainerID: 0, HostID: 0, Size: 1}, {ContainerID: 1, HostID: 1, Size: 1}},
671+
},
672+
expectedUID: 0,
673+
expectedGID: 0,
674+
size: 2,
675+
},
676+
{
677+
uid: 1000,
678+
gid: 1000,
679+
uids: []idtools.IDMap{},
680+
gids: []idtools.IDMap{},
681+
expectedOptions: &stypes.IDMappingOptions{
682+
HostUIDMapping: false,
683+
HostGIDMapping: false,
684+
UIDMap: []idtools.IDMap{{ContainerID: 1000, HostID: 0, Size: 1}},
685+
GIDMap: []idtools.IDMap{{ContainerID: 1000, HostID: 0, Size: 1}},
686+
},
687+
expectedUID: 1000,
688+
expectedGID: 1000,
689+
size: 1000000,
690+
},
630691
}
631692

632693
for _, test := range tests {
633-
options, uid, gid, err := getRootlessKeepIDMapping(test.uid, test.gid, test.uids, test.gids)
694+
options, uid, gid, err := getRootlessKeepIDMapping(test.uid, test.gid, test.uids, test.gids, test.size)
634695
assert.Nil(t, err)
635696
assert.Equal(t, test.expectedOptions, options)
636697
assert.Equal(t, test.expectedUID, uid)

test/e2e/run_userns_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ var _ = Describe("Podman UserNS support", func() {
158158
Expect(session.OutputToString()).To(Equal("0"))
159159
})
160160

161+
It("podman --userns=keep-id:size", func() {
162+
session := podmanTest.Podman([]string{"run", "--userns=keep-id:size=10", ALPINE, "sh", "-c", "(awk 'BEGIN{SUM=0} {SUM += $3} END{print SUM}' < /proc/self/uid_map)"})
163+
session.WaitWithDefaultTimeout()
164+
165+
if isRootless() {
166+
Expect(session).Should(ExitCleanly())
167+
Expect(session.OutputToString()).To(Equal("10"))
168+
} else {
169+
Expect(session).Should(ExitWithError(125, "cannot set max size for user namespace when not running rootless"))
170+
}
171+
})
172+
161173
It("podman --userns=keep-id --user root:root", func() {
162174
session := podmanTest.Podman([]string{"run", "--userns=keep-id", "--user", "root:root", "alpine", "id", "-u"})
163175
session.WaitWithDefaultTimeout()

0 commit comments

Comments
 (0)