News for hackers. Stuff that matters.
Re:$ sudo rm -rf / === NPM install by notpublic on Tuesday November 09, 2021 @03:39PM (#29167149)
If you are on a linux distro that supports apparmor (ubuntu/debian etc), such issues can be easily prevented by creating a sandbox profile and alias commands like node/npm to run using it.

  alias npm='aa-exec -p sandbox npm'
  alias node='aa-exec -p sandbox node'
sandbox profile:

  # /etc/apparmor.d/sandbox
  include <tunables/global>

  profile sandbox {
    #include <abstractions/base>
    #include <abstractions/consoles>
    #include <abstractions/nameservice>

    /sys/** r,
    /{usr/,}bin/* ixr,

    # nodejs install dir
    owner /home/*/nodejs/**/* rix,

    owner /tmp/**           rw,
    owner /tmp/**/          rw,

    owner /home/*/.npm/_* lrw,
    owner /home/*/.npm/_*/ lrw,
    owner /home/*/.npm/_*/** lrw,
    owner /home/*/.npmrc r,

    owner /home/**/node_modules/ rwix,
    owner /home/**/node_modules/** rwix,
    owner /home/**/package.json rw,
    owner /home/**/package-lock.json rw,

    owner /home/*/projects/**/ r,
    owner /home/*/projects/** r,
  }
To load the profile:

sudo apparmor_parser -r /etc/apparmor.d/sandbox

edit: For completeness, added step to load the profile.

Re:$ sudo rm -rf / === NPM install by kelnos on Tuesday November 09, 2021 @03:53PM (#29167328)
I think you have a definition of "easily" that many might not subscribe to.

If this policy isn't available and enabled by default in some package containing common policies, do you really expect your average developer to a) even know anything about AppArmor, or b) know how to write an AppArmor policy file without spending a few hours digging through docs, reading examples, and engaging in a bunch of trial and error?

(If your answer is "yes" to either of those questions, I'd suggest you might overestimate what is common knowledge.)

Re:$ sudo rm -rf / === NPM install by notpublic on Tuesday November 09, 2021 @04:40PM (#29167860)
Fair enough. I meant easy as in without installing any additional software, minimal config and ease of use.

AppArmor has come a long way. It's worth learning about it. They have quite a few builtin profiles that you can take advantage of.

Re:$ sudo rm -rf / === NPM install by BoorishBears on Tuesday November 09, 2021 @04:14PM (#29167576)
Only on HN can you use the word "easy" to describe a set of steps that follow directly after, and still end up having to clarify the word easy referred to your steps, not to the entire concept of the underlying technology.
Re:$ sudo rm -rf / === NPM install by dathinab on Tuesday November 09, 2021 @11:10PM (#29170993)
People mean the steps are not easy. Because it's now about copying them but being able to create them yourself on the fly for whatever you need.
Re:$ sudo rm -rf / === NPM install by BoorishBears on Tuesday November 09, 2021 @11:47PM (#29171182)
I really don't get what this is trying to say.

Like, the tone of the comment makes no sense, the hard part isn't on the OP, and OP wasn't saying AppArmor is easy, just the content of their comment is easy (which it is!)

It's like saying flipping a light switch to turn on a light is easy... the having someone fly in complaining that understanding industrial power generation is not easy at all.

Re:$ sudo rm -rf / === NPM install by dathinab on Friday November 12, 2021 @03:44PM (#29204307)
> content of their comment is easy (which it is!)

It's not, it has subtle gaps in the sandbox which still allow some of the attacks it's supposedly protecting you from.

Re:$ sudo rm -rf / === NPM install by varajelle on Wednesday November 10, 2021 @02:16AM (#29171988)
it's easy, just do 'rm rf /'

My point is: how do we know the instructions are actually safe?

Re:$ sudo rm -rf / === NPM install by BoorishBears on Wednesday November 10, 2021 @09:38AM (#29175063)
Oh brother. It's like wrestling a pig in the mud.

If you don't trust it, don't run it... but I don't know how anyone in software development would be unable to function if they've never been able to trust someone else's config file suggestion.

Re:$ sudo rm -rf / === NPM install by Tommek on Tuesday November 09, 2021 @04:38PM (#29167833)
We reached a state where code monkeys believe they are developers. Of course you should know about the machine you are developing on.
Re:$ sudo rm -rf / === NPM install by vlovich123 on Tuesday November 09, 2021 @07:00PM (#29169243)
There's an insane amount of tooling with varying degrees of overlap. You've got SELinux, BPF, seccomp, AppArmor as different approaches to security. Each of these takes quite a bit of dedicated time to master and most of them have lots of nuance & there can be overlap depending on what you're trying to secure. In a work environment, I'd typically outsource this to security engineers/experts OR spend my time learning those things for what I need them for - knowing the tools are out there are sufficient starting points for research when I need that knowledge.

None of this applies to maintaining my development machine. You might want to revisit your gate keeping.

Re:$ sudo rm -rf / === NPM install by fartcannon on Tuesday November 09, 2021 @07:22PM (#29169465)
Im not a developer, but in the field I work in, a couple hours dedicated to learning a tool that would help you isnt much of an ask.

And the little demo script GP wrote seems pretty straight forward to me.

Re:$ sudo rm -rf / === NPM install by zelphirkalt on Tuesday November 09, 2021 @08:03PM (#29169832)
What other tasks are planned in your current sprint?
Re:$ sudo rm -rf / === NPM install by Tommek on Thursday November 11, 2021 @03:51AM (#29185892)
Then you are not a devolper but an assembly line worker.
Re:$ sudo rm -rf / === NPM install by theteapot on Tuesday November 09, 2021 @06:58PM (#29169224)
Nah. I agree with the other guy. AppArmour takes some time to learn and isn't super mainstream yet. At least it's a step forward from SELinux, but it still feels uncooked and adhoc to me.
Re:$ sudo rm -rf / === NPM install by pabs3 on Tuesday November 09, 2021 @10:07PM (#29170615)
Something like bubblewrap (which uses Linux namespaces) or firejail would be a simpler way to do a sandbox.
Re:$ sudo rm -rf / === NPM install by dathinab on Tuesday November 09, 2021 @11:07PM (#29170975)
That is neither simple nor does it prevent some of the most common npm supply chain attacks (I think, no export with appamore).

The most common attacks is to mess with thinks you build for release in ways which then allow attacking the consumer.

For example by tweaking package, package lock, or even the source code.

Only the last one is prevented here.

Then there is also the thing that installing (user wide or even global) system utilities with npm is a thing, and wouldn't work with your apparmore profile (all need their own profile)

Similar locally running node programs are an attack vector and your profile allow modifying the `node_modules` of such programs even if they don't use npm...

Also you need root rights for loading profiles which can make having a profile for each project a problem as the dev doesn't necessary has root rights. (E.g. in a company with company managed desktop systems this is the default.)

Don't get me wrong. I'm not saying you shouldn't use apparmore and other sandboxes. I even think it's the future of Linux.

My point is appamore isn't simple and it's one of the most simple solutions out there.

Or in other words we still have quite a way to go.

And it's not just about doing sand-boxing, but also about managing sandboxed programs, e.g. last time I looked the app stores of both snap and flatpack where a horror-house of not-properly maintained packages with security vulnerabilities, as well as doggy potentially malicious "unofficial" packages. Even if you sandbox is perfect you still care about security, e.g. you don't want a openoffic package to send copies of all documents you use somewhere else... and that's if the sandbox if used (... flatpack ... facepalm).

Re:$ sudo rm -rf / === NPM install by dathinab on Tuesday November 09, 2021 @11:13PM (#29171012)
> I even think it's the future of Linux.

Let me correct it:

It MUST be the future of Linux or Linux will have serious problems.

Re:$ sudo rm -rf / === NPM install by megous on Tuesday November 09, 2021 @08:40PM (#29170127)
This solves only `npm install` and not other ways the package can be compromised.

And you're still giving it plenty of access to any projects under your home dir, or opportunities to modify other npm packages that are being installed, to gain execution outside of sandbox later on.