[userns-remap] Bug: ToHost incorrectly maps non-root container UID to root when it collides with RootPair
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 76/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 静か
- 技術スタック
- go
調査の方向性
moby/sys/user/idtools.go の IdentityMapping.ToHost() から始め、RootPair()、UIDMaps、GIDMaps がどのように使用されているかを調べます。1000 から始まる subuid 範囲で userns-remap の再現を確認します。コンテナの UID/GID 1000 が適切な非 root のホスト ID にマッピングされ、コンテナの root は引き続き remap された root ID にマッピングされれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
When userns-remap is enabled, the ToHost method in moby/sys/user/idtools.go incorrectly skips UID/GID remapping for non-root users whose container UID happens to equal the host-side root UID returned by RootPair(). This causes files owned by such users to be chown'd to the remapped root UID on the host, and thus appear as root:root inside the container.
Affected Component
github.com/moby/sys/user — IdentityMapping.ToHost() method
Steps to Reproduce
-
Configure Docker with
userns-remapand/etc/subuid:rootless:1000:65536 -
Pull an image that contains a non-root user with UID=1000 (e.g.,
mcr.microsoft.com/playwright:v1.55.1-jammywhich creates apwuserwith UID 1000 viaadduser). -
Run the image:
docker run -it --rm mcr.microsoft.com/playwright:v1.55.1-jammy ls -l /home
Expected Behavior
drwxr-x--- 2 pwuser pwuser 57 Sep 23 2025 pwuser
Actual Behavior
drwxr-x--- 2 root root 73 Sep 23 2025 pwuser
Root Cause
In ToHost() (line 109-126 of idtools.go):
func (i IdentityMapping) ToHost(uid, gid int) (int, int, error) {
var err error
ruid, rgid := i.RootPair()
if uid != ruid {
ruid, err = toHost(uid, i.UIDMaps)
if err != nil {
return ruid, rgid, err
}
}
if gid != rgid {
rgid, err = toHost(gid, i.GIDMaps)
}
return ruid, rgid, err
}
The method assumes that if uid == RootPair().UID, then uid must be the container root (UID 0) that has already been mapped, and skips the toHost() call as an optimization. However, this assumption is incorrect when the subuid ParentID coincides with a non-root container UID.
For example, with subuid = rootless:1000:65536:
RootPair()returns(1000, 1000)(host-side root)- Container root (UID 0) maps to host UID 1000
- A non-root user in the image with UID 1000 should map to host UID 2000
But ToHost(1000, 1000) sees uid == ruid (1000 == 1000), skips the toHost() call, and returns (1000, 1000) — the remapped root UID. The file gets chown'd to 1000:1000 on the host, which is the same as the remapped root, and appears as root:root inside the container.
This bug is especially common because:
UID=1000is the default first non-root user on many Linux distributions (Ubuntu/Debian)subuidranges often start at 1000 when configured for rootless Docker- Many Docker images create a user with UID=1000
Suggested Fix
The ToHost method should always perform the toHost() mapping, and only skip it for the actual container root (UID 0) — not for any arbitrary UID that happens to collide with RootPair().
Remove the optimization entirely. The performance impact is negligible:
func (i IdentityMapping) ToHost(uid, gid int) (int, int, error) {
ruid, err := toHost(uid, i.UIDMaps)
if err != nil {
return -1, -1, err
}
rgid, err := toHost(gid, i.GIDMaps)
return ruid, rgid, err
}
- 主要言語
- Go
- スター
- 102
- フォーク
- 62
- 平均マージ
- 6分
- マージ済み PR(30日)
- 2
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
moby/sys のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
-
devices: ignore .incus-mounts in GetDevices対応中かも @SashaMIT が 8 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
-
capability.LastCap fails when /proc/sys/kernel/cap_last_cap is unavailable対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 50/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 74/100
-
user: parseLine silently ignores uid/gid conversion errors, malformed ids parse as 0対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
cloudflare/terraform-provider-cloudflare#7423 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
router-for-me/CLIProxyAPI#6746 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
ViewData can pair the in-flight proposal with the prepared flag of another proposal対応中かも @HagarMeir が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
hyperledger/SmartBFT#710 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100