I’m thinking of setting up multi user nix on a compute cluster. The advantage would be to have a shared storage where common packages are reused, this is a great advantage compared to conda where every environment duplicates storage and inodes.

However, the packages are installed as root. As such I’m a bit wary of whether a user installing something could have the system run malware as root by installing a package.

What are the safeguards in place and how do I know I can trust them?

  • moonpiedumplings@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 hour ago

    Yes, although there have been a few CVE’s related to escalating privileges or breaking out of the sandbox. You’re going to want to keep those updating and keep on top of those.

    There is one concern, in that the nix store (/nix/store) is world readable. It is not world writable, which is good, but there is a problem in that secrets can potentially be copied into the nix store. If you copy a file containing environment variables or the like into the nix store, it could theoretically be found it. This one is on the users of nix to be careful of.

    With Nix flakes, the entire git repo that the flake originates from is copied into the nix store. Meaning if you have a nix flake in something that is supposed to be a private repo, or contains tracked sensitive data (untracked files are not copied into the nix store), then it could be found and inspected by other users.

    My big concern with multi user in this case is not merely the Nix daemon though, but also Linux itself. Linux has been hit with a LOT of privilege escalation and container escape issues over the past few years, and many of them have been zero day’s. Given this, I no longer really have the same level of trust for Linux with regards to multi user isolation, for in cases like these.

    Of course, in academic computing, I would probably just do multi user anyways, for simplicity. If you install tracking and monitoring, then you can attach malicious actions to identities. Because every user is registered and operates within the institution, if they break the terms of use for computing equipment, then you can punish them within the institution, or pursue greater legal action.

    And then, you also would want to enforce 2FA to help minimize stolen accounts. While that can still be phished via fake login pages that ask for 2FA, it handles most of the things. Isolate the server via firewalls, and then it becomes a small enough target that doesn’t have enough value (assuming of course, your research isn’t too valuable and worth targeting), and you mostly don’t have to worry about it.

    Sure, people will root it. But then they’ll send you a nicely worded email explaining how they rooted it and how to fix it. Which is what my friend did at my school, on our shared ssh server. Just keep it updated to handle the low hanging fruit.

    However, if you are going to give it to untrusted users with unknown or temporary (not within the institution) identities, then things change, and you have to take it a lot more seriously. I no longer have confidence in just Linux’s user based isolation.

    The first line solution I would go to, is to put users in containers and mount the nix store (and nix daemon) in and out. Something like a docker/podman container or Incus container. Of course, container escapes are still possible. If you are even more concerned about those, then you would want virtual machines. It is still possible to share the nix store between virtual machines, but it is more complicated.

    In addition to that, virtual machines have a performance tradeoff (usually 95% or more of the host’s performance though), but there might be issues with sharing GPU compute resources among virtual machines, depending on the hardware you are using. Enterprise GPU’s usually support it though.

  • sudoer777@lemmy.ml
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 hours ago

    However, the packages are installed as root. As such I’m a bit wary of whether a user installing something could have the system run malware as root by installing a package.

    I’m pretty sure the packages are built as unprivileged users in a sandbox, and you can also restrict what users can use certain Nix features.

    The main security flaw with a multi-user Nix system is that everything you build ends up in a world-readable store.

  • balsoft@lemmy.ml
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    22 hours ago

    Assuming you’re talking about Nix the package manager,

    Nothing is actually “installed as root”, except for stuff that the root user installs themselves. The Nix Store (where all the “derivations”/packages are stored) can be thought of more as a cache, just because a package is there doesn’t mean it’s used anywhere. Users don’t get to choose the cache “key” (i.e. the directory name in /nix/store) either - it is determined by all the build instructions and dependencies needed to build the package, and Nix doesn’t (well, at the very least shouldn’t) give users any control about the package build process after it starts.

    When users install something, Nix fetches or builds that package into /nix/store - which doesn’t affect other users in any way - and then just symlinks that particular package into some user-owned directory in their $PATH (e.g. $HOME/.local/share/nix/profiles/default or so), which also doesn’t affect root or other users in any way.

    So, basically, if some user installs malware only they are affected - the fact that it’s also in the shared /nix/store is irrelevant since there’s nothing in other user’s profiles or $PATH or whatever that references it in any way.

    The most likely vulnerability is something like this:

    1. The attacker guesses the nix store path of some package that root will use in the future (e.g. glibc from a more up-to-date Nixpkgs version compared to what root currently uses) - note that users can’t replace a derivation that’s already in the store, so they need to guess a future derivation path
    2. The attacker forces Nix to build that package from source instead of fetching from a trusted substituter
    3. The attacker finds a way to breach Nix’s sandbox during the build and inject their own backdoor into the resulting package ← this is the difficult part, there are currently no such known sandbox holes
    4. The attacker then waits until root starts using that package in the store, at which point the backdoor becomes actively ran as root.

    I must add that this is theoretical and I don’t think has ever happened in practice on a multi-user system.

    Sorry, I’m pretty bad at explaining stuff, hopefully it makes some sense :)

    • libewa@feddit.org
      link
      fedilink
      arrow-up
      1
      ·
      12 hours ago

      Note that any user listed in nix.settings.trusted-users can place arbitrary directories into the Nix store, and can choose the store key.

    • ranzispa@mander.xyzOP
      link
      fedilink
      arrow-up
      1
      ·
      18 hours ago

      Hello, thank you for the in depth explanation! I am not very familiar with nix, and yes I’m talking about the package manager. I have installed NixOs recently to toy around with it and see whether it could be a good solution to certain problems we have. I have determined that NixOs itself is not mature enough for what we need, but I do see value in the nix package manager that other solutions do not offer.

      Regarding the 4 points you raise

      1. I’m not too worried about hash squatting, I guess there’s no way around it, but other ways of managing packages have way worse flaws than that.
      2. I do want my users to be able to build packages. In most cases I imagine we’ll use pre built packages, but often packages available are not optimized for certain architectures.
      3. How does the nix sandbox work? Just so I know what I’m working with. Do you know where I can read about it?
      4. Does root actively run installed packages at any point? If that is the case I’d be a bit wary to use nix for this purpose.
      • balsoft@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        18 hours ago

        I’d like to note that the 4 points are not separate issues, they are all requirements in order for an attacker to get access.

        I’m not too worried about hash squatting, I guess there’s no way around it, but other ways of managing packages have way worse flaws than that.

        “hash squatting” is not really an issue on its own. Unless an attacker can breach the sandbox, given the same derivation hash, the resulting derivation output would be the same regardless of which user requested the build.

        I do want my users to be able to build packages. In most cases I imagine we’ll use pre built packages, but often packages available are not optimized for certain architectures.

        Actually, Nix without the ability to build derivations would just be useless. In Nix, everything is a derivation, even if you just want to have multiple packages installed into your user profile at once, behind the scenes it’s a derivation which simply combines binaries from multiple other derivations, which will not be available in the remote cache and needs to be “built” (in the most trivial sense possible) locally.

        How does the nix sandbox work? Just so I know what I’m working with. Do you know where I can read about it?

        You can think of the Nix sandbox as a kind of lightweight container, into which Nix mounts all the dependencies needed to build the derivation (including source code and such), and then runs the builder command. On Linux it is using User Namespaces (and a few other sandboxing tools), in a similar way to what Docker does (in fact there is some work to use runc as a sandbox backend but it’s not usable yet). I don’t know if it’s described in a lot of detail anywhere, but a brief description is available in the manual.

        Does root actively run installed packages at any point? If that is the case I’d be a bit wary to use nix for this purpose.

        Once again, just random packages in Nix Store are not “installed” in any real sense. In order for any user (incl. root) to install/run something, they have to be the one explicitly doing it, e.g. via nix profile install or nix shell. That also means that if you’re not using NixOS, you can actually just not do anything as a root user and then no code from /nix/store will be executed with root privileges at all.

        To reiterate, unless your threat model involves sophisticated attackers[1], users installing packages with Nix is as secure as them just building code from source manually, or dropping binaries into their own $HOME/.local/bin. One user installing a package into their profile does not affect other users or root in any way.

        From what I can tell from your other replies, you are setting up a common build server with your coworkers. In that case I’d say it’s 100% fine, both workplaces where I’ve been doing Nix work had a server like this, there were never any issues with privilege escalation or malware spreading between users.


        1. The attackers need to have access to an unpatched 0-day Nix sandbox breach or a critical nix-daemon issue, which is very unlikely. If that makes you feel any safer, I know that Nix has been scanned for vulnerabilities by at least one frontier-class LLM with no such vulnerabilities discovered, so just a script-kiddie with access to ChatGPT won’t be able to find anything like that. ↩︎

        • ranzispa@mander.xyzOP
          link
          fedilink
          arrow-up
          1
          ·
          11 hours ago

          Thank you, you reassured me quite a bit. I’ll have a good read of the manual.

          It is not really a build server, it’s a compute server. I’ll have users installing hundreds of different software packages, most of them using incompatible libraries and thus I was thinking of using Nix. Alternatives would be LMOD, which however requires the administrator to install very single piece of software, which… We don’t really have a dedicated system administrator and I don’t really want to go through the compilation instructions of every single piece of software we use. Alternatively everyone could compile their own software or install it through something like conda, but that eats up a lot of storage space since every library would be duplicated across users.

          • balsoft@lemmy.ml
            link
            fedilink
            arrow-up
            2
            ·
            10 hours ago

            Well, in that case remember to force everyone to use the same Nixpkgs version (otherwise you’ll still end up with a lot of duplicated packages), and run nix-store --optimise; nix-collect-garbage -d regularly (ideally via cron or something).

  • kevincox@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    20 hours ago

    In theory it is safe. When a Nix package is built it isn’t “installed”. Unless root is running/installing random packages out of the Nix store there is no problem. As long as the user’s aren’t added to the trusted-users option they shouldn’t be able to cause any problems for other users.

    However like any multi-user system you are sharing a Linux kernel. A kernel is a very complex piece of software with a huge attack surface. Privileged escalation vulnerabilities are commonly found. (This also applies to the nix-daemon, but it is a bit smaller attack surface but vulnerabilities are still occasionally found.) So you shouldn’t assume strong security isolation. I would say that a setup like this is acceptable for mostly-trusted people like coworkers or friends that are not expected to actively exploit vulnerabilities but definitely wouldn’t let random unknown users use the system.

    So if you want strong isolation use a VM or separate hardware, but then you won’t be able to share the builds and packages defeating the point in this case.

    • ranzispa@mander.xyzOP
      link
      fedilink
      arrow-up
      1
      ·
      19 hours ago

      Thank you for the clear explanation. This is good enough for me I guess then. What I am most afraid is not really a colleague building a malicious package on purpose, but rather them installing something which is then run as root and gets access to the whole system. I understand packages are built by unprivileged users, but in the end they are owned by root.

      What I wonder is whether in the way it is possible that something is run as root without someone specifically deciding it should be.

  • GunnarGrop@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    21 hours ago

    If you mean Nix, the package manager, then no packages are actually installed as root. Every package is installed in the Nix store, but programs that need root privileges can’t be run by any user.

    One of the (many) great things about Nix is that users don’t need root privileges to install packages.

    • ranzispa@mander.xyzOP
      link
      fedilink
      arrow-up
      1
      ·
      19 hours ago

      Thank you for the explanation. Yes, I’m talking about the nix package manager, not about the operating system.

      As far as I understand packages are built by less privileges users, but libraries and binaries are owned by the root user in the nix store.

      https://nix.dev/manual/nix/2.34/installation/multi-user.html

      What I wonder is wheter this could lead to root executing untrusted code. I will have several users installing packages and I can not trust that they will check every package. Thus I’d like to know whether I can trust the system to be resilient without careful attention.

  • [object Object]@lemmy.ca
    link
    fedilink
    arrow-up
    1
    ·
    22 hours ago

    Are you talking about NixOS?

    You can install post user nix home manager distributions which are separate from the shared nix environment (not sure if that’s the right term)

  • just_another_person@lemmy.world
    link
    fedilink
    arrow-up
    2
    arrow-down
    1
    ·
    23 hours ago

    Do you want explain a bit more?

    Linux is Linux. If you’re using the standard install packages like Pam, then it’s built specifically for multiple users.

    Elaborate more if you could.