Add basic Apple sandbox support for "chroot" builds on darwin [WIP]#434
Add basic Apple sandbox support for "chroot" builds on darwin [WIP]#434copumpkin wants to merge 1 commit intoNixOS:masterfrom
Conversation
src/libstore/build.cc
Outdated
There was a problem hiding this comment.
I'm not sure what the conventions are in this code base, but why is a foreach macro being used here, when a ranged-based for loop appears 12 lines above?
There was a problem hiding this comment.
Yeah, foreach is obsolete now that we're requiring C++11.
|
Is |
|
@edolstra The idea is that it's not really a global system-wide thing, different derivations will need different accesses. In fact, while I haven't looked through this impl yet to see if @copumpkin has done it already, the plan is to have |
|
I see that indeed |
|
The idea that derivations can bind-mount arbitrary parts of the host filesystem sounds very scary to me, and not something we should have on Linux. |
|
Why? It's a private mount namespace... |
src/libstore/build.cc
Outdated
There was a problem hiding this comment.
This gives a compile error on Linux:
src/libstore/build.cc: In member function 'void nix::DerivationGoal::runChild()':
src/libstore/build.cc:54:25: error: '__APPLE__' was not declared in this scope
|
For example, you can set and get access to that directory, even if /root is not world-readable. |
|
Ah OK, fair enough. So what can we do about this issue? Perhaps set a set of acceptable parent dirs, like |
|
Well, on Linux we don't really need it because we have a pure stdenv. But a set of permissible parent directories sounds good. |
|
Well... Except for @copumpkin Can you add a setting to limit the parent dirs allowed for the per-drv attr? This will probably mean that stuff in |
|
That all makes sense. I think we can limit parents on darwin to I'll make the changes tonight. @edolstra, apart from that, does this seem like a reasonable direction to take things? |
|
Never mind the |
|
Yes, looks good to me. |
|
Great, I'm really excited. I think this will be a key ingredient to accelerate a quality pure darwin stdenv (which is already pretty close to being usable). So far I've been doing all my development with an ad-hoc sandbox wrapped around my calls to |
|
@copumpkin FWIW, even with those reasonable defaults I'd like the set of allowable prefixes to be configurable. |
|
@shlevy definitely! I was going to make it another configurable field with a sensible default. Can I ship a different default conf file based on target platform? |
|
@copumpkin You can wrap the default logic (which is in c++, not a default config file) in ifdefery. I'm not sure another way. |
|
Fair enough 😄 |
bf4b744 to
5327c1c
Compare
|
Updated again and also fixed a slight bug that let pure-darwin's cmake see too much and thus fail. I don't think the resulting sandbox profile is the best just yet (or that the code to generate it is the best factored), but I really want to get a MVP for this working so I can get back to the more difficult pure-darwin nixpkgs work. Thus I'm not trying to make this PR as good as it can get, but do plan on revisiting/cleaning up this code once I have pure-darwin in a place where more people can work on it. Main things I'd like to do better:
|
|
@edolstra actually, I feel a little awkward about keeping "dirs" in the name because neither the configuration key nor the derivation attribute are necessarily directories. In practice, the per-derivation one will usually be individual files, and the configuration one contains one individual file. It seems a little icky to have "/bin/sh" as one of my Anyway, I'm not going to bikeshed this too much, since as I said earlier my main goal is to get the basic functionality in (which we can refine before the next big release) so I can accelerate the stdenv development. If you feel strongly about the "dirs" name, I'll put it in for now and move forward. |
|
Also, does anyone other than @edolstra have opinions on this? A quick recap:
|
|
Hey, I have an opinion too! 👅 |
|
Oh, I thought you were asking for opinions about the work overall 😀 Yeah, I'm in favor of deps or files over dirs. |
|
(also, my goal is to make "chroot" builds the default on darwin after the pure stdenv gets fully merged) |
5327c1c to
d2d54ea
Compare
|
This patch still has a root information leak, namely, you can determine whether files in inaccessible directories exist. For instance, if will print otherwise it prints |
There was a problem hiding this comment.
This should be Error, not SysError.
There was a problem hiding this comment.
Will fix tonight!
On Jan 12, 2015, at 08:31, Eelco Dolstra [email protected] wrote:
In src/libstore/build.cc:
/\* This works like the above, except on a per-derivation level */Strings impurePaths = tokenizeString<Strings>(get(drv.env, "__impureHostDeps"));
for (auto & i : impurePaths) {bool found = false;Path canonI = canonPath(i, true);/\* If only we had a trie to do this more efficiently :) luckily, these are generally going to be pretty small */for (auto & a : allowedPaths) {Path canonA = canonPath(a, true);if (canonI == canonA || isInDir(canonI, canonA)) {found = true;break;}}if (!found) This should be Error, not SysError.throw SysError(format("derivation '%1%' requested impure path ‘%2%’, but it was not in allowed-impure-host-deps (‘%3%’)") % drvPath % i % allowed);—
Reply to this email directly or view it on GitHub.
|
Yeah, I'm not seeing it primarily as a security mechanism, but I've also noticed that the types of errors you get aren't what they'd be if you were in a blank system. It's still what Apple itself (and Chrome) uses to wall off their system services, so I expect that apart from the information issues it should still be reasonably secure. I don't think there's anything much we can do about the FS error types, but am open to suggestions!
|
|
Oh, sorry, you're talking about the permitted prefix mechanism! Can people catch those nix-level errors?
|
|
@copumpkin I think the issue @edolstra points to can be fixed simply by checking the prefix before checking if the file exists. |
|
Oh, I thought I was already doing that! Will definitely add that later and On Monday, January 12, 2015, Shea Levy [email protected] wrote:
|
|
@shlevy Yes, but you have to ensure that the path does not contain |
|
Ah, so perhaps we should catch the exception from |
|
@edolstra can you elaborate on the precise threat you're seeing? That a non-root user can learn things about the root account, when running with the daemon? Is it that ": No such file or directory" part that (I assume?) comes from I don't have too much of a sense of how the daemon works, so apologies for being dense. I can easily turn off the |
|
@copumpkin Yes, that's exactly right. I've just merged this PR with resolveSymlinks disabled. Thanks! |
|
Thank you! How long before |
|
It's pretty easy to make an unstable "release", IMO if we have users using it we should just release it. |
|
Looks like the hydra build |
|
@copumpkin What am I missing? I have a clean nix 1.9 on OSX 10.10 with CLI tools installed. When I try to install something, the derivation build fails with: My /etc/nix/nix.conf is: |
|
@datakurre nix knows how to sandbox things, but the darwin stdenv in nixpkgs is currently still quite impure and not set up to behave properly in the presence of sandboxes. There's a massive effort off in a branch called cc @pikajude |
|
@copumpkin Thanks. I try to continue with pure-darwin copumpkin/nixpkgs#77 |
This uses Apple's
sandbox-exectool (mostly undocumented beyond its man page, but there's a bit of information on it floating around the web) to give build children very precise and limited capabilities when building. They can't talk to the network, see other processes on the system, or see any paths outside of what we explicitly include. This works through a lispy sandbox specification language. My change just teaches nix to construct such a specification on the fly and then feeds it into the existing Apple sandbox mechanism.I've bundled this with a new attribute on
derivationto allow for per-derivation impurities. This is crucial for darwin, since in a partially closed system, we're going to need to call out to many of the system libraries we can't build ourselves at times. The sandbox and precise instructions to pierce it allow us to keep that impurity to a minimum.The attribute is called
__impureHostDepsfor now, but I'm open to better names or designs. My current view is that it could even replace some of the functionality in current linux chroot builds. In my pure darwin nixpkgs, for example, I have/bin/shin that list, and that makes sense on linux, too, along with some of the ubiquitous/devpaths.Looking for comments, criticism, and the like. I have no automated tests for the functionality, but I'm already using the code in this branch on my pure darwin branch and it seems to work.
Relevant to #317 and #361.