Skip to content

feat!: add safety check if not running as PID 1. #153

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

Closed
wants to merge 2 commits into from
Closed

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Apr 26, 2024

Relates to #144
This PR adds a safety check to guard against users accidentally running envbuilder on their local system.
It can be bypassed by setting an environment variable.
This is a breaking change for use-cases outside of containers.

@johnstcn johnstcn requested a review from mtojek April 26, 2024 09:38
@johnstcn johnstcn self-assigned this Apr 26, 2024
Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relates to

I guess it resolves it?

Comment on lines +36 to +38
if env, found := os.LookupEnv("DANGEROUS_DISABLE_PID_CHECK"); found && env == "1" {
_, _ = fmt.Fprintln(os.Stderr, `Bypassing PID check as DANGEROUS_DISABLE_PID_CHECK=1.`)
} else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: will move this inside Options once Bruno's PR is in.

@johnstcn
Copy link
Member Author

Relates to

I guess it resolves it?

I prefer closing issues myself :-)

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Could this be problematic for some container deployments as well? I don't have any concrete example in mind, just thinking there may be cases when there's an actual init and this would make it harder to use envbuilder.

There's also other ways to detect we're in a container and (allow?) execution even if not PID 1.

	local -r cgroup_file='/proc/1/cgroup'
	local -r nspawn_file='/run/host/container-manager'
	[[ -r "$cgroup_file" && "$(< $cgroup_file)" = *(lxc|docker)* ]] \
		|| [[ "$container" == "lxc" ]] \
		|| [[ "$container" == "oci" ]] \
		|| [[ "$container" == "podman" ]] \
		|| [[ -r "$nspawn_file" ]]

I didn't think about this much so I'm not saying we want to do this, just putting it out there.

> **Note:** Envbuilder performs destructive filesystem operations! To guard against accidental data loss, it
> will refuse to run if it detects it is not running as PID 1.
> If you need to bypass this behaviour for any reason, you can bypass this safety check by setting
> `DANGEROUS_BYPASS_PID_CHECK=1`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using the prefix, ENVBUILDER_DANGEROUS_DISABLE_PID_CHECK (also observe bypass/disable used inconsistently).

if os.Getpid() != 1 {
// TODO: rebase once https://github.com/coder/envbuilder/pull/140 is in
if env, found := os.LookupEnv("DANGEROUS_DISABLE_PID_CHECK"); found && env == "1" {
_, _ = fmt.Fprintln(os.Stderr, `Bypassing PID check as DANGEROUS_DISABLE_PID_CHECK=1.`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_, _ = fmt.Fprintln(os.Stderr, `Bypassing PID check as DANGEROUS_DISABLE_PID_CHECK=1.`)
_, _ = fmt.Fprintln(os.Stderr, `ENVBUILDER_DANGEROUS_DISABLE_PID_CHECK enabled, skipping PID check.`)

I think we're better of not logging the value here. Also, how would you feel about changing it to something not so easily typed? Perhaps even "i_really_know_what_im_doing_please_let_me_brake_my_system". 😂

if env, found := os.LookupEnv("DANGEROUS_DISABLE_PID_CHECK"); found && env == "1" {
_, _ = fmt.Fprintln(os.Stderr, `Bypassing PID check as DANGEROUS_DISABLE_PID_CHECK=1.`)
} else {
_, _ = fmt.Fprintln(os.Stderr, `WARNING: Not running as PID 1, so exiting IMMEDIATELY!`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_, _ = fmt.Fprintln(os.Stderr, `WARNING: Not running as PID 1, so exiting IMMEDIATELY!`)
_, _ = fmt.Fprintln(os.Stderr, `WARNING: Envbuilder is not running as PID 1, exiting IMMEDIATELY!`)

} else {
_, _ = fmt.Fprintln(os.Stderr, `WARNING: Not running as PID 1, so exiting IMMEDIATELY!`)
_, _ = fmt.Fprintln(os.Stderr, `This is a safety check to guard against accidental data loss when run outside of a container.`)
_, _ = fmt.Fprintln(os.Stderr, `To bypass this check, set DANGEROUS_DISABLE_PID_CHECK=1.`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_, _ = fmt.Fprintln(os.Stderr, `To bypass this check, set DANGEROUS_DISABLE_PID_CHECK=1.`)
_, _ = fmt.Fprintln(os.Stderr, `To skip this check, set ENVBUILDER_DANGEROUS_DISABLE_PID_CHECK=1.`)

@mtojek
Copy link
Member

mtojek commented Apr 26, 2024

There's also other ways to detect we're in a container and (allow?) execution even if not PID 1.

It is an interesting point. Do we intend to support running envbuilder in a different way than using the official container image?

@johnstcn
Copy link
Member Author

There's also other ways to detect we're in a container and (allow?) execution even if not PID 1.

It is an interesting point. Do we intend to support running envbuilder in a different way than using the official container image?

At present, we only distribute envbuilder via Docker image. We do not provide precompiled binaries outside of this.

@johnstcn
Copy link
Member Author

johnstcn commented Apr 26, 2024

Looking further at the options, FORCE_SAFE should be handling this but doesn't appear to be referenced anywhere in the codebase.

@johnstcn
Copy link
Member Author

Opening a separate PR with the actual correct fix.

@johnstcn johnstcn closed this Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants