diff --git a/libcontainer/env.go b/libcontainer/env.go index 71b2f69d0..96a357c20 100644 --- a/libcontainer/env.go +++ b/libcontainer/env.go @@ -22,10 +22,7 @@ import ( // // Returns the prepared environment. func prepareEnv(env []string, uid int) ([]string, error) { - if env == nil { - return nil, nil - } - var homeIsSet bool + homeIsSet := false // Deduplication code based on dedupEnv from Go 1.22 os/exec. diff --git a/libcontainer/env_test.go b/libcontainer/env_test.go index 18dc52e88..f7b3a21cf 100644 --- a/libcontainer/env_test.go +++ b/libcontainer/env_test.go @@ -21,6 +21,10 @@ func TestPrepareEnv(t *testing.T) { tests := []struct { env, wantEnv []string }{ + { + env: nil, + wantEnv: []string{home}, + }, { env: []string{}, wantEnv: []string{home}, diff --git a/tests/integration/env.bats b/tests/integration/env.bats index 4521fe735..e40184d55 100644 --- a/tests/integration/env.bats +++ b/tests/integration/env.bats @@ -126,3 +126,20 @@ _EOF_ [[ "$output" != *'TERM='* ]] [[ "$output" != *'PATH='* ]] } + +@test "env HOME is set for runc exec -p with no process.env" { + runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox + [ "$status" -eq 0 ] + + # "env" is not set. + cat <<_EOF_ >process.json +{ + "args": ["/bin/env"], + "cwd": "/" +} +_EOF_ + runc exec -p process.json test_busybox + [ "$status" -eq 0 ] + # Env should have HOME set from container's /etc/passwd. + [[ "$output" == *'HOME=/root'* ]] +}