-
Notifications
You must be signed in to change notification settings - Fork 0
Define native WSL layout and state identity #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package hostenv | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "path" | ||
| "strconv" | ||
| "strings" | ||
| "unicode" | ||
| "unicode/utf8" | ||
| ) | ||
|
|
||
| const wslNamespaceDomain = "container-bin/wsl2-state/v1\x00" | ||
|
|
||
| // WSLLayout is the fixed user-local filesystem and Docker-state contract for | ||
| // one native WSL2 distribution. It is computed without consulting XDG or PATH | ||
| // environment variables so a launcher or project cannot redirect trust state. | ||
| // The frontend remains gated until the later filesystem, Docker integration, | ||
| // path and end-to-end qualification slices land. | ||
| type WSLLayout struct { | ||
| Distro string | ||
| UID uint32 | ||
| Home string | ||
| BinaryPath string | ||
| ManagementShim string | ||
| ShimDir string | ||
| ConfigDir string | ||
| RegistryPath string | ||
| LockPath string | ||
| StateDir string | ||
| StateNamespace string | ||
| } | ||
|
|
||
| // NativeWSLLayout returns the WSL2 layout for a classified runtime. The | ||
| // machine ID must be the canonical /etc/machine-id value; combining it with the | ||
| // exact WSL distribution name and numeric Linux UID prevents distributions or | ||
| // users sharing Docker Desktop's daemon from silently sharing ContainerBin | ||
| // volumes. | ||
| func (r Runtime) NativeWSLLayout(home string, uid uint32, machineID string) (WSLLayout, error) { | ||
| if r.Kind != WSL2Native { | ||
| return WSLLayout{}, fmt.Errorf("native WSL layout requires runtime kind %q, got %q", WSL2Native, r.Kind) | ||
| } | ||
| if err := validateDistroIdentity(r.Distro); err != nil { | ||
| return WSLLayout{}, err | ||
| } | ||
| home, err := validateWSLHome(home) | ||
| if err != nil { | ||
| return WSLLayout{}, err | ||
| } | ||
| machineID, err = validateMachineID(machineID) | ||
| if err != nil { | ||
| return WSLLayout{}, err | ||
| } | ||
|
|
||
| configDir := path.Join(home, ".config/container-bin") | ||
| stateDir := path.Join(home, ".local/state/container-bin") | ||
| shimDir := path.Join(home, ".local/bin") | ||
| binaryPath := path.Join(home, ".local/lib/container-bin/cb") | ||
| identity := wslNamespaceDomain + r.Distro + "\x00" + machineID + "\x00" + strconv.FormatUint(uint64(uid), 10) | ||
| sum := sha256.Sum256([]byte(identity)) | ||
| return WSLLayout{ | ||
| Distro: r.Distro, | ||
| UID: uid, | ||
| Home: home, | ||
| BinaryPath: binaryPath, | ||
| ManagementShim: path.Join(shimDir, "cb"), | ||
| ShimDir: shimDir, | ||
| ConfigDir: configDir, | ||
| RegistryPath: path.Join(configDir, "container-bin.toml"), | ||
| LockPath: path.Join(configDir, "container-bin.lock"), | ||
| StateDir: stateDir, | ||
| StateNamespace: "wsl2-" + hex.EncodeToString(sum[:16]), | ||
| }, nil | ||
| } | ||
|
|
||
| func validateDistroIdentity(distro string) error { | ||
| if distro == "" { | ||
| return errors.New("native WSL layout requires a distribution identity") | ||
| } | ||
| if strings.TrimSpace(distro) != distro || !utf8.ValidString(distro) { | ||
| return errors.New("WSL distribution identity is not canonical UTF-8") | ||
| } | ||
| for _, r := range distro { | ||
| if unicode.IsControl(r) { | ||
| return errors.New("WSL distribution identity contains a control character") | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func validateWSLHome(home string) (string, error) { | ||
| if home == "" || !utf8.ValidString(home) || strings.ContainsRune(home, '\x00') || strings.ContainsRune(home, '\\') { | ||
| return "", errors.New("native WSL home must be a canonical absolute Linux path") | ||
| } | ||
| for _, r := range home { | ||
| if unicode.IsControl(r) { | ||
| return "", errors.New("native WSL home contains a control character") | ||
| } | ||
| } | ||
| clean := path.Clean(home) | ||
| if !path.IsAbs(home) || clean != home || clean == "/" { | ||
| return "", errors.New("native WSL home must be a canonical absolute non-root Linux path") | ||
| } | ||
| if clean == "/mnt" || strings.HasPrefix(clean, "/mnt/") { | ||
| return "", errors.New("native WSL home must be distribution-local, not under /mnt") | ||
| } | ||
| return clean, nil | ||
| } | ||
|
|
||
| func validateMachineID(machineID string) (string, error) { | ||
| if len(machineID) != 32 || machineID != strings.ToLower(machineID) { | ||
| return "", errors.New("WSL machine ID must be 32 canonical lowercase hexadecimal characters") | ||
| } | ||
| decoded, err := hex.DecodeString(machineID) | ||
| if err != nil { | ||
| return "", errors.New("WSL machine ID must be 32 canonical lowercase hexadecimal characters") | ||
| } | ||
| allZero := true | ||
| for _, b := range decoded { | ||
| if b != 0 { | ||
| allZero = false | ||
| break | ||
| } | ||
| } | ||
| if allZero { | ||
| return "", errors.New("WSL machine ID must not be all zeroes") | ||
| } | ||
| return machineID, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package hostenv | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| const testMachineID = "0123456789abcdef0123456789abcdef" | ||
|
|
||
| func TestNativeWSLLayoutIsFixedAndDistributionScoped(t *testing.T) { | ||
| runtime := Runtime{Kind: WSL2Native, Distro: "Ubuntu-24.04"} | ||
| layout, err := runtime.NativeWSLLayout("/home/alice", 1000, testMachineID) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| want := WSLLayout{ | ||
| Distro: "Ubuntu-24.04", | ||
| UID: 1000, | ||
| Home: "/home/alice", | ||
| BinaryPath: "/home/alice/.local/lib/container-bin/cb", | ||
| ManagementShim: "/home/alice/.local/bin/cb", | ||
| ShimDir: "/home/alice/.local/bin", | ||
| ConfigDir: "/home/alice/.config/container-bin", | ||
| RegistryPath: "/home/alice/.config/container-bin/container-bin.toml", | ||
| LockPath: "/home/alice/.config/container-bin/container-bin.lock", | ||
| StateDir: "/home/alice/.local/state/container-bin", | ||
| StateNamespace: "wsl2-98d6411d95851ca46df32643077388c2", | ||
| } | ||
| if layout != want { | ||
| t.Fatalf("NativeWSLLayout =\n%+v\nwant\n%+v", layout, want) | ||
| } | ||
| if strings.Contains(layout.StateNamespace, runtime.Distro) || strings.Contains(layout.StateNamespace, testMachineID) { | ||
| t.Fatalf("state namespace exposes raw identity: %q", layout.StateNamespace) | ||
| } | ||
|
|
||
| again, err := runtime.NativeWSLLayout("/home/alice", 1000, testMachineID) | ||
| if err != nil || again.StateNamespace != layout.StateNamespace { | ||
| t.Fatalf("state namespace is not stable: (%q, %v)", again.StateNamespace, err) | ||
| } | ||
| otherDistro, err := (Runtime{Kind: WSL2Native, Distro: "Debian"}).NativeWSLLayout("/home/alice", 1000, testMachineID) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| otherMachine, err := runtime.NativeWSLLayout("/home/alice", 1000, "fedcba9876543210fedcba9876543210") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| otherUser, err := runtime.NativeWSLLayout("/home/alice", 1001, testMachineID) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| caseVariant, err := (Runtime{Kind: WSL2Native, Distro: "ubuntu-24.04"}).NativeWSLLayout("/home/alice", 1000, testMachineID) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for name, namespace := range map[string]string{ | ||
| "distro": otherDistro.StateNamespace, | ||
| "machine": otherMachine.StateNamespace, | ||
| "user": otherUser.StateNamespace, | ||
| "distro casing": caseVariant.StateNamespace, | ||
| } { | ||
| if namespace == layout.StateNamespace { | ||
| t.Errorf("%s identity collapsed to %q", name, namespace) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestNativeWSLLayoutRejectsAmbiguousInputs(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| runtime Runtime | ||
| home string | ||
| machineID string | ||
| want string | ||
| }{ | ||
| {name: "wrong runtime", runtime: Runtime{Kind: WindowsNative}, home: "/home/alice", machineID: testMachineID, want: "requires runtime kind"}, | ||
| {name: "missing distro", runtime: Runtime{Kind: WSL2Native}, home: "/home/alice", machineID: testMachineID, want: "distribution identity"}, | ||
| {name: "padded distro", runtime: Runtime{Kind: WSL2Native, Distro: " Ubuntu"}, home: "/home/alice", machineID: testMachineID, want: "canonical UTF-8"}, | ||
| {name: "distro control", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu\n"}, home: "/home/alice", machineID: testMachineID, want: "canonical UTF-8"}, | ||
| {name: "relative home", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "home/alice", machineID: testMachineID, want: "canonical absolute"}, | ||
| {name: "root home", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/", machineID: testMachineID, want: "non-root"}, | ||
| {name: "unclean home", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/../alice", machineID: testMachineID, want: "canonical absolute"}, | ||
| {name: "trailing slash", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/alice/", machineID: testMachineID, want: "canonical absolute"}, | ||
| {name: "Windows filesystem home", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/mnt/c/Users/alice", machineID: testMachineID, want: "distribution-local"}, | ||
| {name: "backslash home", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/alice\\x", machineID: testMachineID, want: "canonical absolute"}, | ||
| {name: "short machine ID", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/alice", machineID: "abcd", want: "32 canonical"}, | ||
| {name: "uppercase machine ID", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/alice", machineID: strings.ToUpper(testMachineID), want: "32 canonical"}, | ||
| {name: "non-hex machine ID", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/alice", machineID: strings.Repeat("g", 32), want: "32 canonical"}, | ||
| {name: "zero machine ID", runtime: Runtime{Kind: WSL2Native, Distro: "Ubuntu"}, home: "/home/alice", machineID: strings.Repeat("0", 32), want: "all zeroes"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := tc.runtime.NativeWSLLayout(tc.home, 1000, tc.machineID) | ||
| if err == nil || !strings.Contains(err.Error(), tc.want) { | ||
| t.Fatalf("NativeWSLLayout error = %v, want %q", err, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestClassifiedPaddedDistroDoesNotCollapseStateIdentity(t *testing.T) { | ||
| runtime := classify("linux", "6.6.87.2-microsoft-standard-WSL2", " Ubuntu-24.04", "") | ||
| if runtime.Distro != " Ubuntu-24.04" { | ||
| t.Fatalf("classify() distro = %q, want raw identity", runtime.Distro) | ||
| } | ||
| _, err := runtime.NativeWSLLayout("/home/alice", 1000, testMachineID) | ||
| if err == nil || !strings.Contains(err.Error(), "canonical UTF-8") { | ||
| t.Fatalf("NativeWSLLayout error = %v, want padded identity rejection", err) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.