Skip to content

Investigate system(3) without /bin/sh #1434

Closed

Description

@shlevy

As can be seen in in #1424, there are occasional problems caused by the /bin/sh impurity. While removing it from the chroot or system completely would be a lot of work because of shebang scripts, it should be doable to at least make system(3) not rely on /bin/sh. I'll come up with an implementation and hopefully it will be acceptable to merge.

Activity

  1. ghost assigned on Dec 30, 2013
  2. 7c6f434c commented on Dec 31, 2013

    @7c6f434c
    Member

    I vote in favour of cloning the C part of the patch into NixPkgs glibc build.

  3. shlevy commented on Dec 31, 2013

    @shlevy
    MemberAuthor

    From IRC yesterday ( @civodul @viric ):

    [17:52] <shlevy> Eh I'm not sure there's really an issue with just looking up in $PATH for glibc final
    [17:53] <civodul> shlevy: well, that's at least a problem for setuid binaries
    [17:54] <shlevy> civodul: isn't it already gospel that system and popen shouldn't be used in setuid binaries?
    [17:54] <shlevy>        Do not use system() from a program with set-user-ID or set-group-ID privileges,  because  strange
    [17:54] <shlevy>        values for some environment variables might be used to subvert system integrity.  Use the exec(3)
    [17:54] <shlevy>        family of functions instead, but not execlp(3) or execvp(3).  system() will not,  in  fact,  work
    [17:54] <shlevy>        properly from programs with set-user-ID or set-group-ID privileges on systems on which /bin/sh is
    [17:55] <shlevy>        bash version 2, since bash 2 drops privileges on startup.  (Debian uses  a  modified  bash  which
    [17:55] <shlevy>        does not do this when invoked as sh.)
    [17:55] <shlevy> from man 3 system :)
    [17:56] <viric> setuid is chosen by users, not programmers
    [17:56] <civodul> right
    [17:56] <civodul> i would be very conservative here
    
  4. vcunat commented on Jan 1, 2014

    @vcunat
    Member

    Well, I thought the scheme is to hardcode sh, except for some bootstrapping glibc versions.

    As for libc's system', change Awk to look forsh' in $PATH. This patch is only meant to be used during bootstrapping, where we don't know in advance the absolute file name of `sh'.

  5. shlevy commented on Feb 3, 2014

    @shlevy
    MemberAuthor

    Updated the milestone to 14.yy as I'm not sure I'll be able to get to this this month.

  6. vcunat commented on Mar 2, 2014

    @vcunat
    Member

    Hmm, I have WIP for hardcoding shell in glibc, but it introduces a chicken-egg problem: currently the final glibc is linked from the final bash. Any ideas?

  7. vcunat commented on Mar 2, 2014

    @vcunat
    Member

    (cont.) This results in the state that bash in glibc gets hardcoded to a bootstrap version of bash.

    The easiest way to resolve this 2-cycle seems (to me) to have both final versions in one derivation output. I would do that e.g. by building bash separately, and then when building glibc copy bash and change its linking section to use libc from the common $out (linking section is the only reference of bash to glibc).

  8. vcunat commented on Mar 2, 2014

    @vcunat
    Member

    If you agree, I can implement that to catch the glibc-2.19 merge.

  9. shlevy commented on Mar 2, 2014

    @shlevy
    MemberAuthor

    My idea was: Buildglibc with a link to bootstrap sh in $out/bin, then when building bash move the glibc build (with hash-rewriting) into bash's $out before the build, then before make install unlink $out/bin/sh if needed.

    An alternative idea would be to provide a 'config file' for glibc found in an env var that points it to bash and anything else it might need.

  10. vcunat commented on Mar 2, 2014

    @vcunat
    Member

    That sounds the same way of breaking the 2-cycle, just the other way around :-) IMO it's a more difficult one, as it isn't enough to re-write hash -- name+length change too (bootstrap-utils vs. bash, I think).

  11. shlevy commented on Mar 2, 2014

    @shlevy
    MemberAuthor

    No, you wouldn't rewrite the hash of the sh symlink, you'd leave it pointing to bootstrap-utils then it would be replaced by the currently building bash's sh program during make install. You'd have to make the glibc and bash names the same length though.

    I like this way better because bash should ideally be compiled against the same exact version of the library it will run against. Note that you'll have to fix the hashes in the other direction too, as bash will surely have self-references.

  12. civodul commented on Mar 3, 2014

    @civodul
    Contributor

    Howdy!

    FWIW, the approach taken in Guix is described at:

    https://lists.gnu.org/archive/html/bug-guix/2013-01/msg00041.html

    We found out about limitations though:

    https://lists.gnu.org/archive/html/guix-devel/2014-02/msg00141.html

    Presumably we’ll end up building Bash from within the glibc derivation.

    Ludo’.

  13. shlevy commented on Mar 3, 2014

    @shlevy
    MemberAuthor

    Yeah, having a static bash just for system and popen is another idea but there's the problem you pointed out plus the additional bother of needing two versions of bash at all. I think separate derivations are preferable, but YMMV of course.

  14. shlevy commented on Mar 3, 2014

    @shlevy
    MemberAuthor

    Taking a crack this afternoon

  15. 4 remaining items

  16. civodul commented on Mar 4, 2014

    @civodul
    Contributor

    Eelco Dolstra [email protected] skribis:

    Maybe the easiest solution is to look up sh in $PATH (as done in https://gitorious.org/guix/guix/source/5d4fd2671aaadc5b0b7e0c331fa5e2a1d7e5c4d8:distro/packages/patches/glibc-bootstrap-system.patch), except for setuid programs, which would fall back to /bin/sh.

    We purposefully limited that approach to the bootstrap glibc, on the
    grounds that it may break expectations, and/or lead to security issues.

    For instance, if a program or library fiddles with $PATH, then
    subsequent calls to system(3) or popen(3) could behave differently.

    Ludo’.

  17. vcunat commented on Mar 5, 2014

    @vcunat
    Member

    Having final glibc find sh in $PATH seems like an impurity that could bite people, e.g. #1870.

  18. edolstra commented on Mar 5, 2014

    @edolstra
    Member

    Relying on an environment variable inside a build is never an impurity... and in any case, #1870 is about a shebang reference to /bin/sh, which this issue doesn't address.

  19. vcunat commented on Mar 5, 2014

    @vcunat
    Member

    I was referring to run-time glibc -- the one that all packages depend on. For bootstrapping versions this approach seems fine (and it was supported by me in the first comment on this issue).

  20. shlevy commented on Mar 13, 2014

    @shlevy
    MemberAuthor

    I'm in favor of hard-coding the path to sh, so if you don't want to go that route I'll close this.

  21. vcunat commented on Mar 13, 2014

    @vcunat
    Member

    I am in favor of hard-coding the path. For the final glibc I see no better way. I even have some WIP on it, but that's been stalled for a week, as I see that much simpler and less controversial changes have "problems" getting into master (#1840).

  22. shlevy commented on Mar 13, 2014

    @shlevy
    MemberAuthor

    I pretty much have a working impl of this already locally, but @edolstra seems to prefer the other way.

  23. edolstra commented on Mar 14, 2014

    @edolstra
    Member

    @vcunat What changes have problems getting into master?

  24. vcunat commented on Mar 14, 2014

    @vcunat
    Member

    @edolstra perhaps I misunderstand what you expect of stdenv-changing updates. Do you expect me to merge that branch now (discussed at the end of #1840)? I thought it was preferable to have a full Hydra rebuild before merging.

  25. offlinehacker commented on Mar 15, 2014

    @offlinehacker
    Contributor

    /bin/sh is also a security concern. If you remove it, most of the exploits will just fail, and nixos will suddenly become super secure :D

  26. bjornfor commented on Jan 10, 2018

    @bjornfor
    Contributor

    Is there any hope in getting rid of /bin/sh? I see several old and closed issues on this subject,
    but no agreed upon solution yet.

    Background: I wanted to set up some reproducible builds and got into trouble when running libfaketime + perl because it invokes host /bin/sh. The build crashed. I'm looking into using proot -b $(command -v sh):/bin/sh buildscript as workaround.

  27. 7c6f434c commented on Jan 10, 2018

    @7c6f434c
    Member

    @bjornfor in context of Nix builds, sandbox builds can mount Nix-built Bash package's bin/sh to /bin/sh

  28. bjornfor commented on Jan 11, 2018

    @bjornfor
    Contributor

    Right. I forgot to say that this is on non-NixOS. Setting up sandbox / multi-user builds on non-NixOS currently requires more setup, so I've opted for single-user mode and fixing all impurities in the builds instead. (I have one NixOS machine with sanboxing as reference. The other machines run Ubuntu and I don't control them.) Now the builds are pure, except for Nix itself!

    Even if sandboxing setup becomes trivial, it will only affect builds, not runtime. So /bin/sh is still an issue.

  29. civodul commented on Jan 11, 2018

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions