nix icon indicating copy to clipboard operation
nix copied to clipboard

Safety of exec* functions

Open l4l opened this issue 4 years ago • 5 comments

I believe that all of the functions of exec group must be marked with unsafe. The reason is quite simple: these calls are equivalent to calling an extern "C" function (which is unsafe). In particular it's quite trivial to get a segfault with only safe rust which executes a C binary.

l4l avatar Dec 16 '20 12:12 l4l

IIRC exec stops execution of current process before loading new image, so from Rust PoV exec is indistinguishable from exit.

Could you show code that triggers UB?

MikailBag avatar Dec 17 '20 23:12 MikailBag

There's nothing special, I simply call execv* on binary (firefox in particular) but without necessary arguments which leads to segfault. Personally it was surprising to have a segfault without a single line of unsafe code (well, actually there was one, but it took some time to figure out that it wasn't really related to segfault).

You statement regarding stopping execution make sense indeed, though from the code it might be quite hard to find the issue source (since the first thing to do with a segfault is usually grep for unsafe keyword). Feel free to close the issue anyway if it isn't UB indeed.

l4l avatar Dec 18 '20 07:12 l4l

@l4l Could you clarify where the segfault is occurring? Is it Firefox (or the dynamic loader, post exec) that's segfaulting because it's being given the wrong arguments, or is it happing in the exec call itself because something about the argv or other setup is incorrect?

If it's the former, I think it's out of scope of nix (but I'm also not a maintainer, so take that with a grain of salt). If it's the latter, it sounds like a correctness error in nix's exec wrapper.

woodruffw avatar Jan 04 '21 21:01 woodruffw

@woodruffw yeah, I figured out later and forgot about that discussion. The segfault comes from firefox due to incorrect arguments, in particular I forgot to pass argv[0] as the binary path (which by the way works fine for most of other binaries).

l4l avatar Jan 05 '21 08:01 l4l

Then it's a bug in Firefox which is not checking for NULL properly.

Kixunil avatar Jan 12 '21 11:01 Kixunil