You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pkgs/build-support/build-fhs-userenv/chroot-user.rb consists mostly of syscalls (including fork()). There are even Ruby-specific workarounds. C/C++ would be a better language for the task. This should also reduce closure size and startup time of FHS-sandboxed apps (Ruby needs to load itself and its libraries from disk).
I'm not sure if it's capable of doing all things that we need for this, but it should be worth checking. Can it run a container as a process (not as a systemd service), and can it be used not by a superuser?
D: A pity. Well, let's continue with the old plan then. Unfortunately we can't do much more in Bash than we already do because we need to do mounts in the same process we set the namespaces, in children necessary privileges are already gone. I'm not completely sure about this but I remember that when I tried to write this in Bash completely long time ago (using unshare(1) for setting up namespaces mounts and chroot were a problem.
I can confirm that these mounts are possible from using unprivileged lxc containers. But you cannot bind mount something from the host this way, because it needs to be done after unshare.
UPDATE: ok even that should be possible if the correct order is maintained: unshare -> mount -> chroot.
nixpkgs git:(stable) ✗ nix-shell -A steam.env
steam-chrootenv:shlomo@abbradarbook:~/nixpkgs$ /run/current-system/sw/bin/mount --bind /tmp/tmp.17cvszFqco /tmp/tmp.1c7RygKNlH
mount: only root can use "--bind" option
steam-chrootenv:shlomo@abbradarbook:~/nixpkgs$ /run/current-system/sw/bin/mount -t sysfs none /tmp mount: only root can use "--types" option
I think that mount(1) checks if the user is root -- that's why this doesn't work. This means that we need to call mount(2) by ourselves...
@Mic92 (I was going to hit "Comment" but you were faster ~_^) I expect that LXC containers do user maps such that you have UID 0 in new userns -- we, however, need to retain user uid.
@groxxda Multiple user apps use uid and username for their purposes -- we don't want to lie to them. I can't give a complete example (I remember that Steam breaks) but for example we have /run/user/UID directories which apps can find based on uid.
I have another suggestion: the Go programming language.
Bear in mind that I'm generally not a super big fan of Go (in fact, while I use Go extensively at work, I generally use Haskell for my personal projects); however, I think that a higher level, simple (in terms of specification) language would be conducive to maintenance and further development. The binaries should be pretty small, and the closure should be pretty small, too.
I might give that a go (no pun intended) and see what we think.
We need native fork and then unsharebefore any calls to pthreads (or you'll get EINVAL or something close); I remember that Go has a runtime so there may be problems;
We need several syscalls which may not be readily available, namely unshare, mount and others. In Ruby I import them from libc dynamically and redefine constants from C by hand, but doing this (especially redefining constants) is one of the reasons I want to move from Ruby in the first place.
However these are only potential problems -- I don't know if they actually exist! I'm actually interested in how this turns out.
I'll need to give that a thorough reading before I understand why the runtime gets in the way (unless, perhaps, you have an explanation of why/how pthreads mucks things up).
I'll need to give that a thorough reading before I understand why the runtime gets in the way (unless, perhaps, you have an explanation of why/how pthreads mucks things up).
@cstrahan If you refer to my remark about fork and pthreads, it's actually unshare's limitation. You can look at how I workaround this in Ruby by calling imported fork instead of Ruby's builtin. You will know if you encounter it -- unshare will just fail. I think the best way to find out is with an experiment.
(notice that I don't know about Go runtime and haven't even used Go before, so, as I underline again, those might be non-issues)
@abbradar Interesting. I referred to the kernel's docs on unshare, but I don't see any mention of failure when used with pthreads. I don't doubt you; I'd just like to know how all this stuff works.
@spacekitteh a rewrite in a different language will not solve your grsecurity problems. I also do not see how the rewrite discussed here is related to security.
@spacekitteh You can drop your privileges via sudo right after you enter the environment as root -- it's less convenient but you get roughly what you want.
Activity
groxxda commentedon Sep 28, 2016
Can't we just use
systemd-nspawnto setup the chroot?abbradar commentedon Sep 28, 2016
I'm not sure if it's capable of doing all things that we need for this, but it should be worth checking. Can it run a container as a process (not as a systemd service), and can it be used not by a superuser?
groxxda commentedon Sep 28, 2016
systemd-nspawnneeds root privileges.btw
man 7 user_namespaceshas C code to set them up, the rest is probably bash only?abbradar commentedon Sep 28, 2016
D: A pity. Well, let's continue with the old plan then. Unfortunately we can't do much more in Bash than we already do because we need to do mounts in the same process we set the namespaces, in children necessary privileges are already gone. I'm not completely sure about this but I remember that when I tried to write this in Bash completely long time ago (using
unshare(1)for setting up namespaces mounts and chroot were a problem.Mic92 commentedon Sep 28, 2016
How about using this one? https://github.com/lethalman/nix-user-chroot
groxxda commentedon Sep 28, 2016
@abbradar bind mounts, sysfs, tmpfs and procfs should be possible
Mic92 commentedon Sep 28, 2016
I can confirm that these mounts are possible from using unprivileged lxc containers. But you cannot bind mount something from the host this way, because it needs to be done after
unshare.UPDATE: ok even that should be possible if the correct order is maintained: unshare -> mount -> chroot.
abbradar commentedon Sep 28, 2016
@Mic92 Oh, I should look at it! cc @lethalman in case he's interested in this thread.
@groxxda
I think that
mount(1)checks if the user is root -- that's why this doesn't work. This means that we need to callmount(2)by ourselves...@Mic92 (I was going to hit "Comment" but you were faster ~_^) I expect that LXC containers do user maps such that you have UID 0 in new userns -- we, however, need to retain user uid.
groxxda commentedon Sep 28, 2016
@abbradar why do we have to retain user id?
abbradar commentedon Sep 28, 2016
@groxxda Multiple user apps use uid and username for their purposes -- we don't want to lie to them. I can't give a complete example (I remember that Steam breaks) but for example we have
/run/user/UIDdirectories which apps can find based on uid.cstrahan commentedon Oct 1, 2016
I have another suggestion: the Go programming language.
Bear in mind that I'm generally not a super big fan of Go (in fact, while I use Go extensively at work, I generally use Haskell for my personal projects); however, I think that a higher level, simple (in terms of specification) language would be conducive to maintenance and further development. The binaries should be pretty small, and the closure should be pretty small, too.
I might give that a go (no pun intended) and see what we think.
abbradar commentedon Oct 1, 2016
Several possible issues:
forkand thenunsharebefore any calls to pthreads (or you'll getEINVALor something close); I remember that Go has a runtime so there may be problems;unshare,mountand others. In Ruby I import them from libc dynamically and redefine constants from C by hand, but doing this (especially redefining constants) is one of the reasons I want to move from Ruby in the first place.However these are only potential problems -- I don't know if they actually exist! I'm actually interested in how this turns out.
Mic92 commentedon Oct 1, 2016
@cstrahan You cannot use clone() properly because of the language runtime. That's why docker uses a little C for that job.
cstrahan commentedon Oct 1, 2016
@Mic92 Ah, you must be referring to this from
libcontainer:I'll need to give that a thorough reading before I understand why the runtime gets in the way (unless, perhaps, you have an explanation of why/how pthreads mucks things up).
cstrahan commentedon Oct 1, 2016
Oh, I'm guessing that's probably stuff like this: http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them
Though, if there are other details, I'd love to hear them 😄.
abbradar commentedon Oct 1, 2016
@cstrahan If you refer to my remark about fork and pthreads, it's actually
unshare's limitation. You can look at how I workaround this in Ruby by calling importedforkinstead of Ruby's builtin. You will know if you encounter it --unsharewill just fail. I think the best way to find out is with an experiment.(notice that I don't know about Go runtime and haven't even used Go before, so, as I underline again, those might be non-issues)
cstrahan commentedon Oct 1, 2016
@abbradar Interesting. I referred to the kernel's docs on unshare, but I don't see any mention of failure when used with pthreads. I don't doubt you; I'd just like to know how all this stuff works.
abbradar commentedon Oct 1, 2016
@cstrahan No problem even if these were doubts ~_^. From
unshare(2)man page:cstrahan commentedon Oct 1, 2016
@abbradar Ah, dunno why I didn't try reading the man page -- thanks for pointing that out 😉.
spacekitteh commentedon Dec 24, 2016
Anybody done any more work on this? Also, this should probably be tagged with security, due to #21327
Mic92 commentedon Dec 24, 2016
@spacekitteh a rewrite in a different language will not solve your grsecurity problems. I also do not see how the rewrite discussed here is related to security.
spacekitteh commentedon Dec 24, 2016
@Mic92 Anything related to grsecurity is security-related.
A rewrite into a compiled language means that a setuid wrapper can be created, which means I don't have to run shit as root.
abbradar commentedon Dec 24, 2016
@spacekitteh You can drop your privileges via
sudoright after you enter the environment asroot-- it's less convenient but you get roughly what you want.