Skip to content

Rewrite chroot-user.rb in C/C++ #19047

Closed

Description

@abbradar

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).

Activity

  1. added
    0.kind: enhancement
    on Sep 28, 2016
  2. self-assigned this
    on Sep 28, 2016
  3. added
    6.topic: closure size
    on Sep 28, 2016
  4. groxxda commented on Sep 28, 2016

    @groxxda
    Contributor

    Can't we just use systemd-nspawn to setup the chroot?

  5. abbradar commented on Sep 28, 2016

    @abbradar
    MemberAuthor

    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?

  6. groxxda commented on Sep 28, 2016

    @groxxda
    Contributor

    systemd-nspawn needs root privileges.
    btw man 7 user_namespaces has C code to set them up, the rest is probably bash only?

  7. abbradar commented on Sep 28, 2016

    @abbradar
    MemberAuthor

    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.

  8. groxxda commented on Sep 28, 2016

    @groxxda
    Contributor

    @abbradar bind mounts, sysfs, tmpfs and procfs should be possible

  9. Mic92 commented on Sep 28, 2016

    @Mic92
    Member

    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.

  10. abbradar commented on Sep 28, 2016

    @abbradar
    MemberAuthor

    @Mic92 Oh, I should look at it! cc @lethalman in case he's interested in this thread.

    @groxxda

     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.

  11. groxxda commented on Sep 28, 2016

    @groxxda
    Contributor

    @abbradar why do we have to retain user id?

    # ./userns_child_exec -U -z whoami
    About to exec whoami
    root
    
    # ./userns_child_exec -U -z touch foo
    About to exec touch
    
    # ./userns_child_exec -U -z ls -l foo
    About to exec ls
    -rw-r--r-- 1 root root     0 28. Sep 20:29 foo
    
    # ls -l foo
    -rw-r--r-- 1 groxxda users     0 28. Sep 20:29 foo
    
  12. abbradar commented on Sep 28, 2016

    @abbradar
    MemberAuthor

    @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.

  13. cstrahan commented on Oct 1, 2016

    @cstrahan
    Contributor

    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.

  14. self-assigned this
    on Oct 1, 2016
  15. abbradar commented on Oct 1, 2016

    @abbradar
    MemberAuthor

    Several possible issues:

    1. We need native fork and then unshare before any calls to pthreads (or you'll get EINVAL or something close); I remember that Go has a runtime so there may be problems;
    2. 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.

  16. Mic92 commented on Oct 1, 2016

    @Mic92
    Member

    @cstrahan You cannot use clone() properly because of the language runtime. That's why docker uses a little C for that job.

  17. cstrahan commented on Oct 1, 2016

    @cstrahan
    Contributor

    @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).

  18. cstrahan commented on Oct 1, 2016

    @cstrahan
    Contributor

    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).

    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 😄.

  19. abbradar commented on Oct 1, 2016

    @abbradar
    MemberAuthor

    @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)

  20. cstrahan commented on Oct 1, 2016

    @cstrahan
    Contributor

    @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.

  21. abbradar commented on Oct 1, 2016

    @abbradar
    MemberAuthor

    @cstrahan No problem even if these were doubts ~_^. From unshare(2) man page:

    CLONE_NEWUSER requires that the calling process is not threaded; specifying CLONE_NEWUSER automatically implies CLONE_THREAD.

  22. cstrahan commented on Oct 1, 2016

    @cstrahan
    Contributor

    @abbradar Ah, dunno why I didn't try reading the man page -- thanks for pointing that out 😉.

  23. spacekitteh commented on Dec 24, 2016

    @spacekitteh
    Contributor

    Anybody done any more work on this? Also, this should probably be tagged with security, due to #21327

  24. Mic92 commented on Dec 24, 2016

    @Mic92
    Member

    @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.

  25. spacekitteh commented on Dec 24, 2016

    @spacekitteh
    Contributor

    @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.

  26. abbradar commented on Dec 24, 2016

    @abbradar
    MemberAuthor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

0.kind: enhancement6.topic: closure size

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions