Skip to content
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

Undocumented panics in std::process on unix for strings with interior nuls #30858

Closed
kamalmarhubi opened this Issue Jan 12, 2016 · 7 comments

Comments

Projects
None yet
4 participants
@kamalmarhubi
Copy link
Contributor

kamalmarhubi commented Jan 12, 2016

All the strings are taken as AsRef<OsStr> and converted to CString with unwrapping: https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/process.rs#L83-L85

An example for Command::new (playground):

use std::process::Command;

fn main() {
    Command::new("\0");
}

The panic should be documented, or the conversion unwrap should be delayed until a call to one of the methods that start the process so that the error can be returned in a Result.

Version:

$ rustc --version --verbose
rustc 1.5.0 (3d7cd77e4 2015-12-04)
binary: rustc
commit-hash: 3d7cd77e442ce34eaac8a176ae8be17669498ebc
commit-date: 2015-12-04
host: x86_64-unknown-linux-gnu
release: 1.5.0
@talchas

This comment has been minimized.

Copy link

talchas commented Jan 12, 2016

This also occurs with arg(), but env() in fact doesn't panic /at all/ for \0 (just silently gives you an environ you probably don't intend), because of accidents of implementation, which seems very sketchy.

@kamalmarhubi

This comment has been minimized.

Copy link
Contributor Author

kamalmarhubi commented Jan 12, 2016

Given that it's possible to delay the conversion until spawning, I think that is the leas surprising thing to do. The env vars can be checked at that point as well.

@kamalmarhubi

This comment has been minimized.

Copy link
Contributor Author

kamalmarhubi commented Jan 12, 2016

That would also be similar to what happens if you attempt to create a file (playground):

use std::fs::File;

fn main() {
    println!("{:?}", File::create("/tmp/\0"));
}

prints

Err(Error { repr: Custom(Custom { kind: InvalidInput, error: StringError("path contained a null") }) })
@kamalmarhubi

This comment has been minimized.

Copy link
Contributor Author

kamalmarhubi commented Jan 12, 2016

@talchas

This also occurs with arg(), but env() in fact doesn't panic /at all/ for \0 (just silently gives you an environ you probably don't intend), because of accidents of implementation, which seems very sketchy.

I decided the behaviour with env() is different enough to warrant its own bug. It isn't very good: http://is.gd/swL9IL

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Jan 14, 2016

triage: I-nominated

@kamalmarhubi

This comment has been minimized.

Copy link
Contributor Author

kamalmarhubi commented Jan 15, 2016

I've got a patch in the works for this.

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

std: Make fields on sys::*::process::Command private
This is preparation for changing the representation of string types in
the unix versions to fix handling of interior nuls.

Refs rust-lang#30858
Refs rust-lang#30862

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

std: Properly handle interior NULs in std::process on unix
This delays errors to the point of calling `Command::spawn()` or one of
its equivalents.

The prior behavior for environment variables was to
silently truncate at the first NUL; for other arguments it was to panic
when passing them in.

Fixes rust-lang#30858
Fixes rust-lang#30862

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

std: Properly handle interior NULs in std::process on unix
This delays errors to the point of calling `Command::spawn()` or one of
its equivalents.

The prior behavior for environment variables was to silently truncate at
the first NUL; for other arguments it was to panic when passing them in.

Fixes rust-lang#30858
Fixes rust-lang#30862

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

std: Make fields on sys::*::process::Command private
This is preparation for changing the representation of string types in
the unix versions to fix handling of interior nuls.

Refs rust-lang#30858
Refs rust-lang#30862

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Jan 20, 2016

std: Properly handle interior NULs in std::process on unix
This delays errors to the point of calling `Command::spawn()` or one of
its equivalents.

The prior behavior for environment variables was to silently truncate at
the first NUL; for other arguments it was to panic when passing them in.

Fixes rust-lang#30858
Fixes rust-lang#30862
@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Jan 21, 2016

triage: P-medium

The libs team discussed this in triage yesterday and the conclusion was that the defer-the-error strategy is fine for Command. We discussed briefly that this may be altering the contract of Command (e.g. changing panics to results), but the Result behavior seems more consistent with the std::fs APIs.

@rust-highfive rust-highfive added P-medium and removed I-nominated labels Jan 21, 2016

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Feb 3, 2016

std: Properly handle interior NULs in std::process
This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes rust-lang#30858
Fixes rust-lang#30862

bors added a commit that referenced this issue Feb 3, 2016

Auto merge of #31056 - kamalmarhubi:std-process-nul-chars, r=alexcric…
…hton

This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes #30858
Fixes #30862

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Feb 3, 2016

std: Properly handle interior NULs in std::process
This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes rust-lang#30858
Fixes rust-lang#30862

bors added a commit that referenced this issue Feb 3, 2016

Auto merge of #31056 - kamalmarhubi:std-process-nul-chars, r=alexcric…
…hton

This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes #30858
Fixes #30862

bors added a commit that referenced this issue Feb 3, 2016

Auto merge of #31056 - kamalmarhubi:std-process-nul-chars, r=alexcric…
…hton

This reports an error at the point of calling `Command::spawn()` or one of
its equivalents.

Fixes #30858
Fixes #30862

@bors bors closed this in #31056 Feb 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.