Skip to content

buildFHSUserEnv doesn't work for non-root users in grsecurity #21327

Closed

Description

@spacekitteh

Issue description

Trying to set up an AOSP build environment to nix-shell into. nix-shelling into it with a non-root user errors out. It succeeds with root and sudo.

Steps to reproduce

shell.nix:

{ pkgs ? import <nixpkgs> {} }:

let
  jdk = pkgs.jdk8;
  fhs = pkgs.buildFHSUserEnv {
    name = "android-env";
    targetPkgs = pkgs: with pkgs;
      [ git
        gitRepo
        gnupg1
        python2
        curl
        procps
        openssl
        gnumake
        nettools
        androidenv.platformTools
        ccache
        ncurses
        jdk
        schedtool
        utillinux
        m4
        gperf
        perl
        libxml2
        zip
        unzip
        bison
        flex
        lzop
        bc
        which
        gcc
      ];
    multiPkgs = pkgs: with pkgs;
      [ zlib
      ];
    extraOutputsToInstall = [ "dev" ];
    runScript = "bash";
    profile = ''
      export USE_CCACHE=1
      export ANDROID_JAVA_HOME=${jdk.home}
    '';
  };
in fhs.env
[spacekitteh@spaceserv:~/code/copperhead]$ nix-shell
/nix/store/qxdag2fs1qh4alr19ax3s9lqh5dw2m24-chroot-user:44:in `block in make_fcall': Operation not permitted (Errno::EPERM)
        from /nix/store/qxdag2fs1qh4alr19ax3s9lqh5dw2m24-chroot-user:114:in `<main>'

Technical details

  • System: 17.03pre96925.1c50bdd (Gorilla)
  • uname -a: Linux spaceserv 4.8.14-grsec Compile glibc without -fstack-protector. #1-NixOS SMP Sat Dec 10 18:09:59 UTC 2016 x86_64 GNU/Linux
  • Nix version: nix-env (Nix) 1.11.4
  • Nixpkgs version:"17.03pre96925.1c50bdd"

Activity

  1. copumpkin commented on Dec 21, 2016

    @copumpkin
    Member

    That's expected, I think. Grsec disables unprivileged user namespaces because it doesn't trust them. This also caused some pain with chromium/chrome sandboxing. Not sure what they did to fix it there.

  2. spacekitteh commented on Dec 21, 2016

    @spacekitteh
    ContributorAuthor

    I seem to remember grsecurity having a privileged_chroot_group or something? I know it has other special groups.

  3. copumpkin commented on Dec 21, 2016

    @copumpkin
    Member

    Here's the context from last time we encountered this: #17460 (comment)

  4. spacekitteh commented on Dec 22, 2016

    @spacekitteh
    ContributorAuthor

    So, how can this be fixed?

  5. joachifm commented on Dec 22, 2016

    @joachifm
    Contributor

    You probably need to go via a suid wrapper

  6. spacekitteh commented on Dec 23, 2016

    @spacekitteh
    ContributorAuthor

    How?

  7. joachifm commented on Dec 23, 2016

    @joachifm
    Contributor

    The general idea is to have an initially privileged stub that sets up the sandbox, marked suid (called via wrapper most likely, in our case) to allow ordinary logins to use it. You'd need to read the buildFHSUserEnv sandbox code to see exactly what its sandbox init involves. The privileged stub would do pretty much the same thing (possibly sans user namespace, depending on whether it's just used to allow non-privileged users to perform the sandbox init).

  8. Mic92 commented on Dec 23, 2016

    @Mic92
    Member

    It would be basically mount namespaces + setuid back to the unprivileged user. An alternative would be proot: https://github.com/proot-me/PRoot This tools would introduce a little overhead due the use of ptrace.

  9. spacekitteh commented on Dec 24, 2016

    @spacekitteh
    ContributorAuthor

    I don't speak Ruby.

    <snip>
        $unshare.call CLONE_NEWNS
    <snip>
        $mount.call nil, '/', nil, MS_REC | MS_SLAVE, nil
    <snip>
        $unshare.call CLONE_NEWNS | CLONE_NEWUSER
    <snip>
          write_file '/proc/self/setgroups', 'deny'
    <snip>
        write_file '/proc/self/uid_map', "#{uid} #{uid} 1"
        write_file '/proc/self/gid_map', "#{gid} #{gid} 1"
    <snip>
      # Do rbind mounts.
      mounts.each do |from, rto|
        to = "#{root}/#{rto}"
        FileUtils.mkdir_p to
        $mount.call from, to, nil, MS_BIND | MS_REC, nil
      end
    
      # Don't make root private so privilege drops inside chroot are possible
      File.chmod(0755, root)

    These things seem to be the issue - IIRC, grsec allows reading from /proc/self/, but does it allow writing to it?

  10. copumpkin commented on Dec 24, 2016

    @copumpkin
    Member
  11. spacekitteh commented on Dec 24, 2016

    @spacekitteh
    ContributorAuthor

    Ah, okay. So does that mean the env.rb file has to be rewritten in C or something, or?

    Also, is that from GRKERNSEC_CHROOT_CAPS?

  12. spacekitteh commented on Dec 24, 2016

    @spacekitteh
    ContributorAuthor

    Would fakeroot work, do you think?

  13. abbradar commented on Dec 24, 2016

    @abbradar
    Member

    @spacekitteh I can't talk about Grsecurity because I haven't used it much, but if I understood #17460 (comment) correctly you won't be able to use CLONE_NEWUSER and, therefore, CLONE_NEWNS as a regular user at all with current NixOS Grsecurity configuration. What this means is that you either need some sort of setuid wrapper, yes, or to disable this particular Grsecurity limitation.

    See also #19047 as setuid interpreted scripts are forbidden (so Ruby is out of question).

    Fakeroot won't likely help, but something like proot may (but you need to rewrite chroot-user.rb's logic as proot call). Proot has its limitations (as it's implemented as a "debugger" which overrides several syscalls).

  14. copumpkin commented on Dec 28, 2016

    @copumpkin
    Member

    The main issue here is that spender doesn't trust the user namespace code enough to grant unprivileged access to it. You might disagree, in which case I'd just turn off the kernel patch rather than try to circumvent it with a setuid wrapper that will basically have the same effect in a more roundabout way.

  15. abbradar commented on Dec 28, 2016

    @abbradar
    Member

    @copumpkin Exactly, see #21387 (that's why I closed it). However I left this one open because I don't know if we have any mechanism to change this grsecurity setting (e.g. with a build flag) currently.

  16. joachifm commented on Dec 30, 2016

    @joachifm
    Contributor

    AFAIK, there's no way to disable this restriction. So if a setuid wrapper is not desired, this issue has to be closed as a wontfix.

  17. joachifm commented on Dec 30, 2016

    @joachifm
    Contributor

    Closing, this is a feature and not a bug :) See #21387 (comment) for a solution that doesn't require modifying buildFHSUserEnv, if you really, really need this.

  18. spacekitteh commented on Jan 1, 2017

    @spacekitteh
    ContributorAuthor

    But #21387 (comment) would disable it for EVERYTHING, whereas a stub would disable it only when setting up a FHS env, right?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions