Repository navigation
nsexec: fix argv overflow in try_mapping_tool with many ID mappings - #5507
Conversation
kolyshkin
left a comment
There was a problem hiding this comment.
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).
|
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 |
ef80daa to
b833398
Compare
|
You're right about the leading delimiter, the count skipped it while the fill loop stored an empty field, so Dropped the dynamic sizing and split this up: the first commit is just the fix ( Test results, rootless with
|
|
b833398 to
0cb07f3
Compare
|
All three done.
Test results, rootless with
|
0cb07f3 to
5e98039
Compare
kolyshkin
left a comment
There was a problem hiding this comment.
last commit needs
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
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>
5e98039 to
b42449d
Compare
|
Added, the test commit now carries your Two other things in this push: rebased on current No code change, the tree is byte-identical to the last push. |
|
gentle ping |
|
This needs a second LGTM @opencontainers/runc-maintainers |
| # As root, uid_map and gid_map are written directly. | ||
| requires rootless_idmap | ||
|
|
||
| local n=16 |
There was a problem hiding this comment.
Is there a specific reason for 16? I think 340 would be more appropriate.
try_mapping_tool()collects thenewuidmap/newgidmaparguments into a fixed 20-entryargv(MAX_ARGV), which only has room for 5 ID mappings; the kernel accepts up to 340 since Linux 4.15.argc == MAX_ARGVand returns before storing the terminatingNULL, soexecve(2)walks pastargv; with 7 or more the trailing fields are also silently dropped.Fix, one commit each:
NULL-terminateargv, and error out if the mappings do not fit;MAX_ARGVto cover 340 mappings (UID_GID_MAP_MAX_EXTENTS);userns.batstest with 16 uid and 16 gid mappings.Repro: rootless,
newuidmapinstalled (so the directuid_mapwrite getsEPERMand the tool path runs),config.jsonwith 6linux.uidMappings.runc init error(s): nsexec-0: failed to execv: Bad address, start fails. 5 mappings start fine.-fsanitize=addressover the same loop reports a stack-buffer-overflow READ at theexecveargv walk for 6 mappings, and is clean at 5.EINVALfrom the kernel on theuid_mapwrite, before the tool is tried.