-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Avoid repeated os.SetEnv calls in container init. #1983
Closed
Closed
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -79,47 +79,77 @@ func newContainerInit(t initType, pipe *os.File, consoleSocket *os.File, fifoFd | |
if err := json.NewDecoder(pipe).Decode(&config); err != nil { | ||
return nil, err | ||
} | ||
if err := populateProcessEnvironment(config.Env); err != nil { | ||
environmentMap, err := populateEnvironmentMap(config.Env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch t { | ||
case initSetns: | ||
return &linuxSetnsInit{ | ||
pipe: pipe, | ||
consoleSocket: consoleSocket, | ||
config: config, | ||
pipe: pipe, | ||
consoleSocket: consoleSocket, | ||
config: config, | ||
environmentMap: environmentMap, | ||
}, nil | ||
case initStandard: | ||
return &linuxStandardInit{ | ||
pipe: pipe, | ||
consoleSocket: consoleSocket, | ||
parentPid: unix.Getppid(), | ||
config: config, | ||
fifoFd: fifoFd, | ||
pipe: pipe, | ||
consoleSocket: consoleSocket, | ||
parentPid: unix.Getppid(), | ||
config: config, | ||
fifoFd: fifoFd, | ||
environmentMap: environmentMap, | ||
}, nil | ||
} | ||
return nil, fmt.Errorf("unknown init type %q", t) | ||
} | ||
|
||
// populateProcessEnvironment loads the provided environment variables into the | ||
// current processes's environment. | ||
func populateProcessEnvironment(env []string) error { | ||
for _, pair := range env { | ||
p := strings.SplitN(pair, "=", 2) | ||
if len(p) < 2 { | ||
return fmt.Errorf("invalid environment '%v'", pair) | ||
// parseEnvironmentVariable takes a environment variable with the format | ||
// key=value, and returns the key and value seperated. | ||
func parseEnvironmentVariable(env string) (key, value string, err error) { | ||
p := strings.SplitN(env, "=", 2) | ||
if len(p) < 2 { | ||
err = fmt.Errorf("invalid environment '%+v'", env) | ||
return | ||
} | ||
key = p[0] | ||
value = p[1] | ||
return | ||
} | ||
|
||
// createEnvironmentMap loads the provided environment variables into a | ||
// map. | ||
func populateEnvironmentMap(env []string) (map[string]string, error) { | ||
environ := os.Environ() | ||
mappedEnv := make(map[string]string, len(environ)+len(env)) | ||
|
||
for _, pair := range environ { | ||
key, _, err := parseEnvironmentVariable(pair) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := os.Setenv(p[0], p[1]); err != nil { | ||
return err | ||
mappedEnv[key] = pair | ||
} | ||
for _, pair := range env { | ||
key, _, err := parseEnvironmentVariable(pair) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mappedEnv[key] = pair | ||
} | ||
return nil | ||
|
||
// Certain functions that are used later, such as exec.LookPath, require that the PATH | ||
// environment variable is setup with the container's PATH. | ||
os.Setenv("PATH", mappedEnv["PATH"]) | ||
|
||
return mappedEnv, nil | ||
} | ||
|
||
// finalizeNamespace drops the caps, sets the correct user | ||
// and working dir, and closes any leaked file descriptors | ||
// before executing the command inside the namespace | ||
func finalizeNamespace(config *initConfig) error { | ||
func finalizeNamespace(config *initConfig, environmentMap map[string]string) error { | ||
// Ensure that all unwanted fds we may have accidentally | ||
// inherited are marked close-on-exec so they stay out of the | ||
// container | ||
|
@@ -145,7 +175,7 @@ func finalizeNamespace(config *initConfig) error { | |
if err := system.SetKeepCaps(); err != nil { | ||
return errors.Wrap(err, "set keep caps") | ||
} | ||
if err := setupUser(config); err != nil { | ||
if err := setupUser(config, environmentMap); err != nil { | ||
return errors.Wrap(err, "setup user") | ||
} | ||
if err := system.ClearKeepCaps(); err != nil { | ||
|
@@ -237,7 +267,7 @@ func syncParentHooks(pipe io.ReadWriter) error { | |
} | ||
|
||
// setupUser changes the groups, gid, and uid for the user inside the container | ||
func setupUser(config *initConfig) error { | ||
func setupUser(config *initConfig, environmentMap map[string]string) error { | ||
// Set up defaults. | ||
defaultExecUser := user.ExecUser{ | ||
Uid: 0, | ||
|
@@ -319,10 +349,8 @@ func setupUser(config *initConfig) error { | |
} | ||
|
||
// if we didn't get HOME already, set it based on the user's HOME | ||
if envHome := os.Getenv("HOME"); envHome == "" { | ||
if err := os.Setenv("HOME", execUser.Home); err != nil { | ||
return err | ||
} | ||
if _, ok := environmentMap["HOME"]; !ok { | ||
environmentMap["HOME"] = "HOME" + "=" + execUser.Home | ||
Comment on lines
+352
to
+353
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead, it's easier to just set HOME the old way (like it's done with PATH). |
||
} | ||
return nil | ||
} | ||
|
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you be more specific? Wondering it may cause security issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mappedEnv["PATH"]
contains, for example,PATH=/usr/bin:/usr/sbin
(rather than/usr/bin:/usr/sbin
. As a result, the first PATH element will be wrong.