Skip to content

nsexec: fix argv overflow in try_mapping_tool with many ID mappings - #5507

Merged
kolyshkin merged 3 commits into
opencontainers:mainfrom
alhudz:nsexec-mapping-tool-argv
Nov 21, 2026
Merged

kolyshkin merged 3 commits into
opencontainers:mainfrom
alhudz:nsexec-mapping-tool-argv

Conversation

@alhudz

@alhudz alhudz commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor
  1. try_mapping_tool() collects the newuidmap/newgidmap arguments into a fixed 20-entry argv (MAX_ARGV), which only has room for 5 ID mappings; the kernel accepts up to 340 since Linux 4.15.
  2. From 6 mappings upward the fill loop reaches argc == MAX_ARGV and returns before storing the terminating NULL, so execve(2) walks past argv; with 7 or more the trailing fields are also silently dropped.

Fix, one commit each:

  • always NULL-terminate argv, and error out if the mappings do not fit;
  • raise MAX_ARGV to cover 340 mappings (UID_GID_MAP_MAX_EXTENTS);
  • add a userns.bats test with 16 uid and 16 gid mappings.

Repro: rootless, newuidmap installed (so the direct uid_map write gets EPERM and the tool path runs), config.json with 6 linux.uidMappings.

  • expected: container starts.
  • actual: runc init error(s): nsexec-0: failed to execv: Bad address, start fails. 5 mappings start fine.
  • -fsanitize=address over the same loop reports a stack-buffer-overflow READ at the execve argv walk for 6 mappings, and is clean at 5.
  • after the patch: 1/5/6/7/20/100/340 mappings all start, and ASan/UBSan are clean on the new loop. 341 mappings get EINVAL from the kernel on the uid_map write, before the tool is tried.

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

It looks like counting and filling work a bit differently: if map begins with a delimiter (space or newline), it is skipped while counting but not skipped when filling argv elements, and it will result in the child writing outside of argv. So you're fixing one bug but adding another.

Can you also separate the fix (argv[argc] = NULL) from the enhancement (make argv size dynamic)?

And maybe we can live without dynamic sizing, just make MAX_ARGS bigger (and error out if we're out of it).

@kolyshkin

Copy link
Copy Markdown
Contributor

This needs a test case, too (something similar like a test with many ranges in userns.bats).

@kolyshkin

Copy link
Copy Markdown
Contributor

This needs a test case, too (something similar like a test with many ranges in userns.bats).

Something like this maybe:

# newuidmap/newgidmap are given 3 arguments per mapping; make sure
# a large number of mappings is passed to them correctly.
@test "userns with many id mappings [rootless newuidmap]" {
     # As root, uid_map and gid_map are written directly.
     requires rootless_idmap
     
    local n=16
    update_config --argjson n "$n" \
             --argjson uid "$(id -u)" --argjson ustart "$ROOTLESS_UIDMAP_START" \
             --argjson gid "$(id -g)" --argjson gstart "$ROOTLESS_GIDMAP_START" '
               .linux.uidMappings = [{"hostID": $uid, "containerID": 0, "size": 1}]
                     + [range(1; $n) | {"hostID": ($ustart + .), "containerID": ., "size": 1}]
             | .linux.gidMappings = [{"hostID": $gid, "containerID": 0, "size": 1}]
                     + [range(1; $n) | {"hostID": ($gstart + .), "containerID": ., "size": 1}]
             | .process.args = ["sh", "-c", "cat /proc/self/uid_map /proc/self/gid_map"]'
     
     runc run test_busybox
     [ "$status" -eq 0 ]
     [ "${#lines[@]}" -eq $((2 * n)) ]
}

(results in failed to execv: Bad address without the fix)

@alhudz
alhudz force-pushed the nsexec-mapping-tool-argv branch from ef80daa to b833398 Compare October 4, 2026 14:01
@alhudz

alhudz commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

You're right about the leading delimiter, the count skipped it while the fill loop stored an empty field, so argv could end up one entry short. The counting pass is gone now.

Dropped the dynamic sizing and split this up: the first commit is just the fix (argv[argc] = NULL after the loop, bailx() if the mappings don't fit), the second raises MAX_ARGV to cover the 340 mappings the kernel accepts (UID_GID_MAP_MAX_EXTENTS), and the third adds your test to userns.bats (only change is a shellcheck disable=SC2016 for the jq filter).

Test results, rootless with idmap:

  • main: failed to execv: Bad address
  • fix commit only: too many ID mappings for /usr/bin/newuidmap
  • all three: passes. 340 mappings start too; 341 gets EINVAL from the uid_map write before the tool is tried.

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Can you please make the the author of the third commit?
  2. Since #5429 the tests needs to be written slightly differently, here's the diff:
-	runc run test_busybox
-	[ "$status" -eq 0 ]
+	run -0 runc run test_busybox

@kolyshkin

Copy link
Copy Markdown
Contributor
  1. Also, please rebase on top of current main

@alhudz
alhudz force-pushed the nsexec-mapping-tool-argv branch from b833398 to 0cb07f3 Compare October 5, 2026 07:59
@alhudz

alhudz commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

All three done.

  1. The third commit now has you as its author (I read that as "make me the author"). It carries my Signed-off-by as committer; I haven't added yours.
  2. The test uses run -0 runc run test_busybox. That was also what failed the rootless jobs on the last push: since tests/int: revamp (drop runc(), use run -N, add asserts) #5429 a plain runc call no longer sets $lines, so the count check failed.
  3. Rebased on current main.

Test results, rootless with idmap:

  • this branch: userns.bats passes, including the new test
  • same test with nsexec.c from main: run -0 fails with failed to execv: Bad address

@kolyshkin
kolyshkin force-pushed the nsexec-mapping-tool-argv branch from 0cb07f3 to 5e98039 Compare October 6, 2026 07:07

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last commit needs
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

alhudz and others added 3 commits October 7, 2026 13:28
try_mapping_tool() collects the newuidmap/newgidmap arguments into a
MAX_ARGV-entry argv, but the terminating NULL is only stored if the
whole map fits. With 6 or more ID mappings the loop stops at
argc == MAX_ARGV, argv is left unterminated, execve(2) walks past its
end, and the container fails to start:

	nsexec-0: failed to execv: Bad address

Always NULL-terminate argv, and error out if the mappings do not fit
rather than passing a truncated list to the tool.

Signed-off-by: Alhuda Khan <al.hudz.k@gmail.com>
MAX_ARGV of 20 only has room for 5 ID mappings, which was the kernel
limit before Linux 4.15. Since then the kernel accepts up to 340
(UID_GID_MAP_MAX_EXTENTS), so any config with 6 or more mappings could
not be used with newuidmap/newgidmap.

Size argv for 340 mappings.

Signed-off-by: Alhuda Khan <al.hudz.k@gmail.com>
Run a rootless container with 16 uid and 16 gid mappings, so that
newuidmap/newgidmap get more arguments than the old MAX_ARGV allowed,
and check that all of the mappings are applied.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
[alhudz: add shellcheck disable=SC2016 for the jq filter]
Signed-off-by: Alhuda Khan <al.hudz.k@gmail.com>
@alhudz
alhudz force-pushed the nsexec-mapping-tool-argv branch from 5e98039 to b42449d Compare October 7, 2026 08:04
@alhudz

alhudz commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Added, the test commit now carries your Signed-off-by above mine and DCO is green.

Two other things in this push: rebased on current main again since it had moved on, and the committer on all three commits is now me. The previous push had you as committer as well as author, which was not right; authorship is unchanged.

No code change, the tree is byte-identical to the last push. CGO_CFLAGS="-g -O2 -Werror" make runc builds clean and go test ./libcontainer/nsenter/... passes locally. The Actions runs are sitting at action_required and need your approval to start.

@alhudz

alhudz commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

gentle ping

@kolyshkin

Copy link
Copy Markdown
Contributor

This needs a second LGTM @opencontainers/runc-maintainers

# As root, uid_map and gid_map are written directly.
requires rootless_idmap

local n=16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason for 16? I think 340 would be more appropriate.

@lifubang lifubang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kolyshkin kolyshkin mentioned this pull request Nov 21, 2026

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still LGTM

@kolyshkin
kolyshkin added this pull request to the merge queue Nov 21, 2026
Merged via the queue into opencontainers:main with commit e86228d Nov 21, 2026
94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants