News for hackers. Stuff that matters.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @10:14AM (#26090204)
I see a lot of people saying things like "this is why package signing is important" and "we need to know who the developers are" and "we need to audit everything." Some of that is true to some degree, but let me ask you this: why do we consider it acceptable that code you install through a package manager implicitly gets to do anything to your system that you can do? That seems silly! Surely we can do better than that?

This article from Agoric is extremely relevant here, from a previous such incident (re: the event-stream package on npm): https://medium.com/agoric/pola-would-have-prevented-the-even...

Put simply: in many cases, the dependencies you install don't need nearly as much authority as we give them right now. Maybe some of these packages need network access (I see a few named "logger" which might be shipping logs remotely) but do they need unrestricted filesystem access? Probably not! (They don't necessarily even need unrestricted network access either; what they're communicating with is likely pretty well-known.)

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by eecc on Wednesday February 10, 2021 @10:52AM (#26090786)
Uh, well the original developers of the Sun JVM didn’t do such a bad job after all when designing it: https://docs.oracle.com/javase/7/docs/technotes/guides/secur...
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by WatchDog on Wednesday February 10, 2021 @04:47PM (#26095253)
Javas security manager system, is usually not in effect for the majority of use cases. While maven/grade dependencies, can't run code on installation, generally once the application is ran/tested, it will be with full user permissions, not under a security manager.

The security manager is an additional layer of security that most languages don't have, however Java applets have shown it to be full of holes and generally unsuitable for running untrusted code.

The applet security posture has contributed a great deal towards negative opinion towards the language, probably would have been better off never having existed.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Thursday February 11, 2021 @07:06AM (#26100631)
There have been hundreds, maybe thousands of local privilege escalation vulnerabilities on Linux. People still find bugs in basic programs like sudo that have been there for decades. Still, nobody would ever suggest that Linux should have the same security approach as Windows 95 or that it's generally unsuitable for running code you didn't write!

Sandboxing code is hard, regardless of what language, runtime or operating system approach you use.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by szc on Thursday February 11, 2021 @01:27AM (#26098842)
The Sun JVM, as originally implemented, can express operations that are not valid for Java objects. There are parts of the JVM that attempt to constrain opcode sequences to only be from "valid java compilers operating on java objects".

In 1996, Java was being overwhelmed by exploits because the mapping of the language to the VM was not well matched. There was a Java summit with lots of interesting people. This summit was also when Sun got confirmation that MicroSoft had quite a few engineers working on an independently implemented runtime. To Sun's credit, they did get rather more serious about Java security -- but they had already created a rocky foundation.

It is my opinion, that the business model Sun had "in mind" for Java was a free runtime for everyone that they were in control of, but to make money from selling an "official" Java compiler suite.

I do not believe that the Sun Java JVM was created with security in mind.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by easton on Wednesday February 10, 2021 @10:38AM (#26090578)
I believe that Deno (the "successor" to Node being written by Ryan Dahl) is supposed to fix this for server-side JavaScript/TypeScript. It doesn't grant any permissions to anything unless you specifically give them out (so you can say that only a specific module gets access to the filesystem, for instance, and on top of that it can only access /srv and not /etc).

https://deno.land/[email protected]/getting_started/permissions

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @10:57AM (#26090873)
This looks like it's... getting there, but still too coarse-grained. It looks like those permissions are granted to the whole Deno process? So if your program needed both access to sensitive data on the filesystem and network access, and it used a malicious dependency, that dependency could take advantage of those permissions and exfiltrate that data.

I could be wrong, but I don't see any mention of permissions on imported code: https://deno.land/[email protected]/examples/import_export

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by jonny_eh on Wednesday February 10, 2021 @11:11AM (#26091050)
You're right. Deno only has app-level permissions. We need module level too.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by kevincox on Wednesday February 10, 2021 @12:39PM (#26092270)
I think it could be something like only the root module could import net, fs, os... then in order for modules to access those things the root module would need to pass it in explicitly. Of course if you don't import the module at all there is no access.

Of course JS isn't a great language for this. A malicious program could spider the object graph looking for something valuable. You would have to be very careful to keep these objects hidden. And a container library would have huge amounts of access with it needs none. (For example if you want to store a hashmap of open sockets)

A stronger typed language like Rust or Haskell could do better, as you container library can be prevented from casting T to File. However even that is not enough as you can just manually cast a pointer if you somehow know what type it is. (And there is a small amount of reflection that can do this even in safe code).

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by jonny_eh on Wednesday February 10, 2021 @12:43PM (#26092327)
> Of course JS isn't a great language for this. A malicious program could spider the object graph looking for something valuable.

Deno can provide extra syntax or annotations for imports to allow the dev to explicitly allow permission per-import. These can be in the source code, or in a config file.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by kevincox on Wednesday February 10, 2021 @12:50PM (#26092426)
How would this work exactly? How do you control what module's permission to use for any IO?

For example what if you have a callback library that calls a function that does IO? What if you pass an IO function directly as a callback? (For example File.close) If it is the file where the call is textually written how do you handle dynamic calls? (or are they forbidden).

I think the capability model is probably the right one here.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by korijn on Wednesday February 10, 2021 @05:07PM (#26095447)
Wether controls are coarse or fine (all the way to function level, or even line by line), you still need to audit the source code to see if a package is not going to abuse the permissions you grant it. Right?
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @05:50PM (#26095905)
Not to nearly the same extent; the key is to not grant unnecessary authority in the first place.

Let's say I'm using a `left-pad` function that someone else wrote, and I'm using a system in which modules aren't granted any authority except what you give them. If I then call

  left-pad('foo', 5')
...I don't really have to worry that it'll go rummaging around my filesystem for my private keys and exfiltrate them somewhere. After all, I didn't give it access to my filesystem or the network! (Side-channel attacks notwithstanding, things get thorny real quick at that point.)

Now, you still have to worry about the function not doing what being correct - it might return an empty string, it might go into an infinite loop, etc - but you've tremendously reduced the scope of what this module could do if the developer were malicious.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by korijn on Thursday February 11, 2021 @05:39PM (#26108361)
In obvious cases like that, I agree with you. In real life however my dependencies are rarely that simple.

So let's imagine a REST API library that needs disk and network access to do it's work. How do I know it doesn't abuse my permission?

And what about its transient dependencies?

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by jonny_eh on Thursday February 11, 2021 @06:18PM (#26108843)
But why would a REST API library need disk access? If it required that, it would raise suspicion for me.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by korijn on Friday February 12, 2021 @05:04PM (#26119083)
Maybe it support serving files (static content)? Or writing them to disk? Either way can we rather argue the point instead of the example?
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by h1fra on Wednesday February 10, 2021 @11:57AM (#26091697)
yes while it goes in the right direction in theory, in practice it provides "almost" no additional security.

the only good differentiator right now that could definitely be implemented in nodejs directly is the the flag `--allow-net=<domain>`. it could prevent data exfiltration but requires the whole stack to require this flag.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nogbit on Wednesday February 10, 2021 @10:54AM (#26090832)
I think this is critical. The actual runtime of any code needs to do way more than what it’s doing now.

Simply relying on package signing and the like permits trusted but malicious actors. With Deno packages configured well it can really lock down and limit a ton of attack vectors.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by salawat on Wednesday February 10, 2021 @02:30PM (#26093575)
>trusted but malicious actors

I...think you might have bigger problems going on there. You're tryingto throw a tech solution at a problem that is fundamentally human in nature.

That tends to leave nobody satisfied.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by josephg on Wednesday February 10, 2021 @02:53PM (#26093830)
Tech solutions are the best solutions when they work! Fighting with your spouse over who does the dishes? Buy a dishwasher! Don’t want your ISP snooping on traffic? Use https / a VPN!

Unfortunately, package signing does nothing to protect against the threat vector presented here. The authentication system in npm is working fine. The problem is we put too much trust in software from the internet.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by salawat on Wednesday February 10, 2021 @04:51PM (#26095285)
...Hence my classification of it as a human problem. I apologize, this is a quirk of my personal vernacular. This is a problem that emergently arises out of the way human beings interact with each other socially, even before tool use comes into the picture.

Alice has a thing. Bob had a thing that Alice figured would make her life easier so integrates it without looking too hard at it. Alice didn't reallize that by adding Bob's thing, something Alice wanted private was no longer the case even if her primary use case was solved.

The technical solution is making Alice's thing include a really onerous to configure permissions framework that takes the work of getting a thing set up and increases the task list from program thing to program and configure permissions for thing.

The human solution is to realize you don't know Bob from Adam, or his motivations, and to observe what Bob's thing actually does. Then depending on criticality, remake something similar, or actually take the time to get to know Bob and see if he can make what you want for you under some sort of agreement that facilitates good business and trust all around. You can't be sampling for malicious changes in real-time, so it's all about risk management. The issue in our case, is a lot of these projects are essentially gifts with no active attention paid to them after a certain point. It's a variant of cargo cults. You want this thing? Go here, get that, presto. Businesses, developers, (and their exploiters) like that. The price though is that once a project is abandoned, and the rights transferred to someone you don't know, you have to rerun your risk management calculation again.

The thing people should be worried about is all the PHB's (pointy-haired bosses) who just got ammo for their NMIH (Not-Made-In-House) cannons now that supply chain attacks are becoming increasingly visible vectors for attack.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by salawat on Wednesday February 10, 2021 @06:09PM (#26096113)
In a rare self-reply, this feeds into the reason why I scratch my head at the whole license based IP distribution thing.

By bringing licenses into it, you push for a business relationship first, but discourage further toolmaking. Programs are math. Rederivation and application should really be the norm, but can't be if we're drawing boxes around arrangements of symbols and saying "Do not cross."

It's the weird contradiction at the core of what we do as software people that still keeps me scratching my head. We all run to make a hydrant to mark, then try to make rent extracting business around it instead of maximizing the number of variants of hopefully practical and efficient ways to allow everyone else to solve their own problems.

I'm not against people being able to make a living doing what they love, but the incentive structure seems all out of jibe with what I understood to be the overall goal.

Or something. Still wrapping my head around it I guess.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by Gehinnn on Wednesday February 10, 2021 @05:00PM (#26095384)
What about reviews and review certificates then? If you review a the package [email protected] you could publicly certify that it is not malicious and maybe earn some money with it. In turn, you back your claim with a financial security that you pay in case the package actually contains malicious code.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by josephg on Wednesday February 10, 2021 @07:48PM (#26096868)
Thats a great idea - but in a centralized system like npm or cargo you don't need certificates to implement that. (Certs might be a nice implementation though.)

So yeah, there might be a "trusted security reviews with payments" shaped technical solution. I'd love to see someone flesh that out - that sounds like a potential solution to this problem (unlike developer-signed packages).

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by bluetech on Wednesday February 10, 2021 @11:37AM (#26091390)
This is so obviously what needs to happen, it's really surprising it's not a feature in all major languages by now. I bet in 10 years time, giving dependencies complete control would seem crazy.

Here is an interesting proposal on how to possibly get there in JS with import maps: https://guybedford.com/secure-modular-runtimes

Deno uses ambient permissions for the entire process and unfortunately missed the opportunity to do it right.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by detaro on Wednesday February 10, 2021 @10:27AM (#26090401)
Indeed, being able to apply capabilities on a package level would be great, but I don't know many languages/environments that implement this as a first-class feature.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by newhouseb on Wednesday February 10, 2021 @11:40AM (#26091423)
The WASM ecosystem is exploring this through the use of what they call "nanoprocesses" wherein libraries are wrapped into modules and provided access to nothing by default [1]. This seems to be more of a pattern and consequence of how WASM works than a specific feature.

1. https://hacks.mozilla.org/2019/11/announcing-the-bytecode-al... (ignore the title, it's irrelevant to the excellent explanation that constitutes 70% of the post)

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @10:36AM (#26090530)
Yeah. JavaScript is probably the closest to being there (with things like SES[0], LavaMoat[1], etc.) but we're not quite there yet. It's just shocking that this sort of thing is as seemingly obscure as it is; it's like the whole industry has collectively thrown up their hands and said code execution is unavoidably radioactively dangerous. (While simultaneously using package managers that... well.) But it doesn't have to be!

[0] https://github.com/Agoric/ses-shim

[1] https://github.com/LavaMoat/LavaMoat

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by eecc on Wednesday February 10, 2021 @10:52AM (#26090791)
Java does. Of course it’s never been used systematically and it has received precious little attention to DevOps ergonomics, but the infrastructure is there
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by bluetech on Wednesday February 10, 2021 @01:07PM (#26092680)
Java actually doesn't need to add any features for it to support this, it needs to remove them. I recommend reading this excellect article: http://habitatchronicles.com/2017/05/what-are-capabilities/

The gist is:

Rule #1: All instance variables must be private

Rule #2: No mutable static state or statically accessible authority

Rule #3: No mutable state accessible across thread boundaries

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by detaro on Wednesday February 10, 2021 @11:12AM (#26091060)
I didn't know that. Do you happen to have a link with more details at hand?
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by sid- on Wednesday February 10, 2021 @12:00PM (#26091739)
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by dwohnitmok on Wednesday February 10, 2021 @10:59AM (#26090900)
Safe Haskell is one in this vein (it's lower level and you would apply a capability layer on top), although like other past efforts on this front it's mostly languished in obscurity even among the Haskell community and is used by very few people.

The main hope at the moment seems to be JS.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by bryanrasmussen on Wednesday February 10, 2021 @05:37PM (#26095781)
>why do we consider it acceptable that code you install through a package manager implicitly gets to do anything to your system that you can do?

I thought it was because operating systems still use access based instead of capabilities based security?

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ptx on Wednesday February 10, 2021 @10:35AM (#26090517)
Could you solve this in Java using the SecurityManager stuff that was used to sandbox applets, or is all that considered broken these days? (I'm not sure if you can different SecurityManagers for different parts of the app though.)
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by jjav on Wednesday February 10, 2021 @02:09PM (#26093389)
Yes, with Java you can.

That's how the web/application server containers worked (probably still do, but I've been disconnected). The server classes have different permissions from the application code classes (loaded from the .war/etc files). If an application code method calls into a system class, the permissions which apply are those or the application since that method is in the calling stack frame.

I wrote this support into several Java web container and J2EE application server products back in the day. AFAIK, all that still works great today in Java.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @11:01AM (#26090918)
I'm not familiar enough with Java to have a strong opinion on this, but this HN comment from the linked article mentions that you can only have one SecurityManager per app, so sounds like that's still too coarse-grained: https://news.ycombinator.com/item?id=18599365
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mindcrime on Wednesday February 10, 2021 @11:39AM (#26091414)
In my experience, the biggest problem with the Java SecurityManager approach is that it's thought of as too difficult to understand / cumbersome to configure (and I'm not saying this belief is wrong), and so most apps either run with no SecurityManager explicitly configured or configure things the "simplest possible way" which usually winds up being approximately equivalent to "anybody can do anything".
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Wednesday February 10, 2021 @03:34PM (#26094378)
Oracle’s own secure coding guidelines for Java [1] actually now recommend adopting a capability-based approach rather than relying on SecurityManager:

> FUNDAMENTALS-5: Minimise the number of permission checks Java is primarily an object-capability language. SecurityManager checks should be considered a last resort.

(Note: quite a lot of Java’s standard library is not designed along object-capability lines so you should take this advice with a pinch of salt).

[1]: https://www.oracle.com/java/technologies/javase/seccodeguide...

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Wednesday February 10, 2021 @04:12PM (#26094886)
They are not in tension. The Java security architecture is a mix of capability and module-level security.

It's probably worth posting a quick refresher. The system is old but people don't use it much these days, and the documentation isn't that good. At one point I wrote a small JavaFX PDF viewer that sandboxed the PDF rendering code, to learn the system. I lost the source code apparently, but the hard part wasn't coding it (only a small bit of code was required), it was learning how to configure and use it. I tested the sandbox by opening a PDF that contained an exploit for an old, patched security bug and by using an old, vulnerable version of the PDFbox library. The sandbox successfully stopped the exploit.

Fortunately the Java team still maintain the sandbox and via new technology like the module system and GraalVM, are reinforcing it. In fact, GraalVM introduces a new sandboxing technology as well that's simpler to use than the SecurityManager, however, it's also probably less appropriate for the case of blocking supply chain attacks.

Java's internal security is based on two key ideas:

1. Code that can protect its private state. When the SecurityManager is enabled and a module is sandboxed, it isn't allowed to use reflection to override field or method visibility.

2. Stack walks.

Let's tackle these backwards. Imagine it's time to do something privileged, like open a file. The module containing the file API will be highly privileged as it must be able to access native code. It will have a method called read() or something like that. Inside that method the code will create a new permission object that represents the permission to open files under a certain path. Then it will use AccessController, like this:

   FilePermission perm = new FilePermission("/temp/testFile", "read");
   AccessController.checkPermission(perm);
The checkPermission call will then do a stack walk to identify the defining module of every method on the stack. Each module has its own set of granted permissions, the access controller will intersect them to determine what permissions the calling code should have. Note: intersection. That means if any unprivileged code is on the stack at all the access check fails and checkPermission will throw an exception. For example, if an unprivileged module registers a callback from a highly privileged module, that doesn't work: the low privileged module will be on the stack and so the privilege is dropped.

Access control contexts are themselves reified as objects, so instead of doing a permission check immediately you can 'snapshot' the permissions available at a certain point and use it later from somewhere else. And, starting a thread copies the permissions available at that point into the new thread context. So you cannot, in the simple case, elevate privilege.

Stack walking and permission intersection is slow. It was optimised a lot in Java 9 and 10 so the performance impact of enabling sandboxing is much less than it once was, but it's clearly not zero overhead. Therefore the JVM provides other techniques. One is the notion of a capability, known from many other systems. Instead of doing a permission check on every single file read (slow), do it once and then create a File object. The File object allows reading of the underlying native file via its private fields. Whoever has a pointer to the File object can thus read from it. Because pointers cannot be forged in Java, this is secure as long as you don't accidentally lose your pointer or pass it to code that shouldn't have it.

Sometimes you need to wrap a privileged operation to "dilute" it somehow. For example, imagine you have a module that allows arbitrary socket access. You also have an HTTP client. You would like the HTTP client to have network access, but for it to be usable by other modules that should only be able to contact specific hosts. Given what I've described so far that wouldn't work: the highly privileged code that can do native calls would do a stack walk, discover the unprivileged module on the stack and throw an exception. But there's a fix: AccessController.doPrivileged. This is kind of like sudo. It takes a lambda and truncates the stack that's examined for access checks at the point of use. Therefore it allows a module to use its own assigned permissions regardless of who is calling it. Of course, that is powerful and must be used carefully. In this case the HTTP client would itself check a different, HTTP specific permission. If that permission passed, then it would assert its own power to make arbitrary network connections and go ahead and use the lower level API.

There are a few more pieces but they aren't core. One is the class called SecurityManager. This is the most famous part of the API but in fact, it's no longer really needed. SecurityManager simply delegates to AccessController now. Its API is slightly more convenient for the set of built in permissions. For the purposes of understanding the design you can effectively ignore it. The SecurityManager needs to be activated using a system property as otherwise, for performance reasons, permission checks are skipped entirely at the check sites. Beyond that it can be left alone, or alternatively, customised to implement some unusual security policy. Another piece is the policy language. Permissions are not intrinsic properties of a module in the JVM but rather assigned via an external file. The final piece is the module system. This isn't relevant to the sandbox directly, but it makes it easier to write secure code by adding another layer of protection around code to stop it being accessed by stuff that shouldn't have access to it. After a careful review of the old JVM sandbox escapes from the applet days, the Java team concluded that the module system would have blocked around half of them.

So as you can see the design is very flexible. There's really nothing else like it out there, except maybe .NET CAS but I believe they got rid of that.

Unfortunately there are some pieces missing, if we want to re-awaken this kraken.

The first is that modules have no way to advertise what permissions they need to operate. That has to be specified in an external, per-JVM file, and there are no conventions for exposing this, therefore build tools can't show you permissions or integrate the granting of them.

The second is that some code isn't sandbox compatible. The most common reason for this is that it wants to reflectively break into JVM internals, for example to get better performance. Of course that's not allowed inside a sandbox.

A third is that some code isn't secure when sandboxed because it will, for example, create a File object for your entire home directory and then put it into a global public static field i.e. it doesn't treat its capabilities with care. The module system can help with this because it can make global variables less global, but it's still not ideal.

The final piece is some sort of community consensus that sandboxing matters. Bug reports about sandboxing will today mostly be ignored or closed, because developers don't understand how to use it and don't see the benefit. It's fixable with some better tutorials, better APIs, better tooling etc. But first people have to decide that supply chain attacks are a new thing that matters and can't be ignored any longer.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @05:38PM (#26095792)
> Sometimes you need to wrap a privileged operation to "dilute" it somehow. For example, imagine you have a module that allows arbitrary socket access. You also have an HTTP client. You would like the HTTP client to have network access, but for it to be usable by other modules that should only be able to contact specific hosts. Given what I've described so far that wouldn't work: the highly privileged code that can do native calls would do a stack walk, discover the unprivileged module on the stack and throw an exception.

Not sure if this is something Java enables, but in principle you could do this in a capability-style way as well. Let's say you have an HTTP client module that you want to allow another module to use, but only to make requests to a specific host. You could write a wrapper with a subset of the HTTP client's functionality, only including (for example) a send() method that would send an HTTP request to the specified host. You'd then pass that to the module that you want to be able to make HTTP connections (rather than the raw HTTP client), and provided your wrapper object doesn't expose functionality from the underlying module that would let a client specify arbitrary hosts, you're in a pretty good spot.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Wednesday February 10, 2021 @06:34PM (#26096313)
That's the same thing I was just describing but recursed another level. It doesn't help by itself. Something needs to have permission to use the higher level of privilege - raw network access in my example, 'raw' http client access in yours. And something else needs to check that permission. Yes, you could wrap that privilege in a capability afterwards, but the reason Java has both capabilities and stack walking is because something needs to authenticate code and then authorise the production of a capability to start with.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @08:31PM (#26097182)
Maybe I'm missing something about the use case, but I'm not sure I quite follow.

Sure, something needs to have permission to use the higher level of privilege. On your typical POSIX OS, your program is probably born with the ability to create arbitrary TCP/UDP sockets by default; on a capability OS, maybe you've explicitly provided it with access to your network stack. Regardless, at the entry point to your program you presumably have modules providing arbitrary network access in scope somehow.

If I'm understanding correctly, the case you described is that you have an HTTP client module that you'd like to have direct access to the network, but you'd like to restrict the consumers of the HTTP client to only querying certain hosts. From the start of your program, you'd instantiate an HTTP client (passing it a capability to use the network interface) then instantiate one of those HTTP client proxy objects that only allows communication with one host (passing it a capability to use the HTTP client). From there, you pass the capability to that proxy object to the unprivileged consumer of the module.

This seems to work without any kind of stack walking authentication logic, just normal variable scope, provided the language is capability-based. Am I missing something?

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Thursday February 11, 2021 @02:36AM (#26099178)
Exactly. What usually happens in capability systems is that the main() method gets all the capabilities (or whatever capabilities the user allowed it) and then does dependency injection to distribute those to other components. No need for complex stack-based authentication or policy rule evaluation.

Indeed, if you look at the history of Java sandbox escapes they are largely confused deputy attacks: some privileged code source can be tricked into doing something it shouldn’t do.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Thursday February 11, 2021 @07:29AM (#26100764)
You can build a sandboxing language without any sort of stack walking. SEL4+C does this. It doesn't have especially good usability at scale, and it's not easy to modularise.

You're imagining a system where there's no specific authentication system for code. Instead in order to use a library, you need to explicitly and manually obtain all the capabilities it needs then pass them in, and in main() you get a kind of god object that can do everything that then needs to be progressively wrapped. If a library needs access to a remote service, you have to open the socket yourself and pass that in, and the library then needs to plumb it through the whole stack manually to the point where it's needed. If the library develops a need for a new permission then the API must change and again, the whole thing has to be manually plumbed through. This is unworkable when you don't control all the code in question and thus can't change your APIs, and as sandboxing is often used for plugins, well, that's a common problem.

There's no obvious way to modularise or abstract away that code. It can't come from the library itself because that's what you're trying to sandbox. So you have to wire up the library to the capabilities yourself. In some cases this would be extremely painful. What if the library in question is actually a networking library like Netty? There could be dozens or hundreds of entry points that eventually want to open a network connection of some sort.

What does this god object look like? It would need to hold basically the entire operating system interface via a single access point. That's not ideal. In particular, loading native code would need to also be a capability, which means any library that optimised by introducing a C version of something would need to change its entire API, potentially in many places. This sort of design pattern would also encourage/force every library to have a similar "demi-god" object approach, to reduce the pain of repeatedly passing in or creating capabilities. Sometimes that would work OK, other times it wouldn't.

The stack walking approach is a bit like SELinux. It allows for a conventional OO class library, without the need for some sort of master or god object, and all the permissions things need can be centralised in one place. Changes to permissions are just one or two extra lines in the security config file rather than a potentially large set of code diffs.

Now all that said, reasonable people can totally disagree about all of this. The JVM has been introducing more capability objects with time. For example the newer MethodHandle reflection object is a capability. FileChannel is a capability (I think!). You could build a pure capability language that runs on the JVM and maybe someone should. Perhaps the usability issues are not as big a deal as they seem. It would require libraries to be wrapped and their APIs changed, including the Java standard library, but the existing functionality could all be reused. The new libraries would just be a thin set of wrappers and forwarders over pre-existing functionality, but there'd be no way for anything except the god object to reach code that'd do a stack walk. Then the security manager can be disabled, and no checks will occur. It'd be a pure object capability approach.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Thursday February 11, 2021 @10:25AM (#26102419)
> If a library needs access to a remote service, you have to open the socket yourself and pass that in, and the library then needs to plumb it through the whole stack manually to the point where it's needed.

You don't need to do this. There are a variety of ways to handle this, just as you would any other kind of dependency injection:

1. Design libraries to actually be modular so that dependencies (including capabilities) can be injected just where they are needed.

2. Pass in a factory object that lets the library construct sockets as and when it needs them. You can then enforce any arbitrary checks at the point of creating the socket. (This is much more flexible than a Java policy file).

3. Use a powerbox pattern [1] to allow the user to be directly asked each time the library attempts to open a socket. This is not always good UX, but sometimes it is the right solution.

> If the library develops a need for a new permission then the API must change and again, the whole thing has to be manually plumbed through.

Capturing permission requirements in the API is a good thing! With the stack walking/policy based approach I won't know the library needs a new permission until some library call suddenly fails at runtime.

[1]: http://wiki.erights.org/wiki/Powerbox

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Thursday February 11, 2021 @02:44PM (#26106157)
The policy file isn't required, by the way. That's just a default implementation. My PDF viewer had a hard-coded policy and didn't use the file.

OK, so in a pure capability language how would you implement this: program A depends on dynamically loaded/installed plugin B written by some third party, that in turn depends on library C. One day library C gets a native implementation of some algorithm to speed it up. To load that native library requires a capability, as native code can break the sandbox. However:

1. You can't change the API of C because plugin B depends on it and would break.

2. You can't pass in a "load native library" capability to plugin B because you don't know in advance that B wants to use C, and if you did, B could just grab the capability before it gets passed to C and abuse it. So you need to pass the capability directly from A to C. But now A has to have a direct dependency on C and initialise it even if it's not otherwise being used by A or B.

Stack walking solves both these problems. You can increase the set of permissions required by library C without changing its callers, and you don't have the problem of needing to short-circuit everything and create a screwed up dependency graph.

With the stack walking/policy based approach I won't know the library needs a new permission until some library call suddenly fails at runtime

You often wouldn't need to. What permissions a module has is dependent on its implementation. It's legitimate for a library to be upgraded such that it needs newer permissions but that fact is encapsulated and abstracted away - just like if it needed a newer Java or a newer transitive dependency.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Thursday February 11, 2021 @08:19PM (#26109762)
> OK, so in a pure capability language how would you implement this: program A depends on dynamically loaded/installed plugin B written by some third party, that in turn depends on library C. One day library C gets a native implementation of some algorithm to speed it up. To load that native library requires a capability, as native code can break the sandbox.

Now, I'm a little outside my area of expertise due to not having worked with capability systems very much yet. (There aren't that many of them and they're still often obscure, so even just trying to gain experience with them is difficult at this point.)

But that said... in an ideal capability system, isn't the idea that native code could just break the sandbox also wrong? I would imagine that in such a system, depending on another module that's running native code would be just fine, and the capability system's constraints would still apply. Maybe that could be supported by the OS itself on a capability OS; maybe the closest thing we'll get to native code for that on our existing POSIX systems is something like WASI[0].

> You often wouldn't need to. What permissions a module has is dependent on its implementation. It's legitimate for a library to be upgraded such that it needs newer permissions but that fact is encapsulated and abstracted away - just like if it needed a newer Java or a newer transitive dependency.

If our goal is to know that the dependencies we're using don't have more authority than they need, isn't it a problem if a module's permissions may increase without explicit input from the module's user (transitive or otherwise)?

[0] https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webas...

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Friday February 12, 2021 @02:26AM (#26111409)
One of the foundations of object-capability security is memory safety, so loading arbitrary native code does subvert that. You can get around this by, for example, requiring native code to be loaded in a separate process. As you say, a capability OS and/or CPU architecture [1] is able to confine native code.

> isn’t it a problem if a module’s permissions may increase without explicit input from the module’s user (transitive or otherwise)?

Exactly right.

[1]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Friday February 12, 2021 @11:41AM (#26115479)
isn't it a problem if a module's permissions may increase without explicit input from the module's user (transitive or otherwise)?

The modules permissions can't increase without explicit input e.g. changes to the policy file. But the person who cares about the sandbox integrity is the user of the overall software or computing system. The plugin developer doesn't really care how the API is implemented or what permissions it needs. They just want it to work. The person who cares is the person who owns the resource or data an attacker may be trying to breach.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Thursday February 11, 2021 @03:31PM (#26106697)
Typically libraries don’t directly load one another. The language runtime does this.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Friday February 12, 2021 @11:39AM (#26115467)
Yes, but authorisation to do so must come from somewhere. In Java it's ambient. In a pure caps system, I'm not sure how it'd work.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Friday February 12, 2021 @03:39PM (#26118292)
The beauty of object-capability security is that it completely aligns with normal object-oriented design. So you can always recast these discussions to not be about security: how would I inject any other new dependency I needed without changing the API of all intermediaries? And there is a whole literature of design patterns for doing this.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Saturday February 13, 2021 @07:47AM (#26123306)
All you'd do there is make the injector a semantic equivalent of the AccessController. The injector must have some sort of security policy after all, to decide whether a component is allowed to request injection of a capability. Whether you structure it as a single subsystem is responsible for intercepting object construction and applying policy based on the home module of what's being constructed, or whether you determine that module via stack walks, the end result is very similar: some central engine decides what components can do and then applies that policy.

The Java approach is nice because it avoids any need for DI. DI is not a widely accepted pattern. There are no DI engines that would have any support for this kind of policy-driven injection. And whilst popular in some areas of software like Java web servers, it hardly features in most other languages and areas, there are no programming languages with built in support for it and that includes modern languages like Kotlin. DI engines meanwhile have changed how they work pretty radically over time - compare something like the original Spring XML DI to Guice to Dagger3. Plus, DI is awkward when the dependency you need isn't a singleton. How would I express for example "I need a capability injected that gives me access to the $HOME/.local/cache/app-name directory"? Annotation based DI struggles with this, but with the AccessController it's natural: the component just requests what it needs, and that's checked against a policy, which can be dynamically loaded from a file, or built by code.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Tuesday February 16, 2021 @04:33AM (#26152379)
Your argument has gone from “this is impossible with capabilities” to “this doesn’t scale” to “nobody uses design patterns”.

You are confusing DI frameworks (often terrible) with the general concept of dependency injection, which is in fact extremely widely used.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Thursday February 11, 2021 @02:48AM (#26099240)
The File example is a good illustration of why Java is _not_ a capability-secure language. Every File object in Java has a getParentFile() method that allows you to navigate up the hierarchy right to the root and then from there access every file on the filesystem. Java’s standard library is full of these kinds of design flaws. So in practice you can only apply capability-based thinking to small subsets of a codebase and have to fallback on the (much weaker) stack walking checks if you want strong isolation.

The problem with Java’s stack walking is that it is too complex and too easy to find privileged code that can be coaxed into performing unintended operations. There are plenty of old write ups of Java sandbox bypass bugs due to this, eg http://benmmurphy.github.io/blog/2015/10/21/zdi-13-075-2013-...

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Thursday February 11, 2021 @07:03AM (#26100606)
I shouldn't have used File as an example, that was confusing. I was trying to explain capabilities and stack walking in an abstract sense but was also using Java as a concrete example. Big mistake.

You're right that java.io.File isn't a capability. It just represents a file path with a few utility methods to list files, and therefore does a stack walk when you try to access the filesystem. A FileChannel is a file capability in the sense I meant above, because it represents an opened file, not a path. There's an access check once, when it's opened, and then the rest of the time there aren't any stack walks.

It's a pity that Ben Murphy didn't write up all his bugs. There are only two listed there. A few patterns cropped up repeatedly in the old sandbox escapes:

1. Accessing internal code you weren't meant to have access to. Often, some sorts of privileged pseudo-reflection API. Fixing this is the goal of Jigsaw.

2. Serialization acting as a back door, allowing the internal private state of classes to be tampered with. Serialization security has been improved with time and they're now working on a feature that will make it harder to screw this up, by allowing serialization to use normal constructors instead of this ad-hoc form of reflection.

3. Overly general frameworks that allowed attackers to construct nearly arbitrary programs out of privileged objects by chaining them together (this crops up in gadget attacks too). There's probably no platform level fix for this, people just have to be aware of the risks when working in sandboxed context.

I don't think a pure capability language is workable, to be honest. At least not at a large scale. In the purest sense you need a god object passed into the start of your program which vends all possible capabilities, and every library would require the user to construct and pass in all resources it needs externally, including things it might possibly need. And that code isn't easily modularised because you can't just use helpers provided by the library itself: that's the very same code you're trying to sandbox. There's no good way to make that usable or extensible. The combination of stack walking and capabilities lets you find the right balance in terms of API design between simplicity of use and sandbox simplicity.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Thursday February 11, 2021 @10:09AM (#26102204)
Are you aware of the history of object-capability programming languages? There are multiple actual demonstrations of real-world ocaps programming languages and projects built with them:

* The (now defunct) E programming language: http://erights.org and Joule: https://en.wikipedia.org/wiki/Joule_(programming_language) * Pony: https://www.ponylang.io * Monte: http://www.monte-language.org * The Midori project at Microsoft: http://joeduffyblog.com/2015/11/10/objects-as-secure-capabil...

It's actually not at all unworkable to use object-capability for large programs. In fact, one of the main benefits of ocaps is how well it aligns with well-established good software design principles such as dependency injection, avoiding singletons, avoiding global mutable state, and so on.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mike_hearn on Thursday February 11, 2021 @02:35PM (#26106048)
I know about E and Midori. I haven't looked at the others. As far as I know the only one that could realistically be said to have been used for large programs was Midori but very little about it was ever published, just a few blog posts. And Midori was cancelled. Presumably it wasn't so compelling.

I'd like to see a more modern attempt that wasn't as totally obscure as those other languages. However, nobody is doing that.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by saagarjha on Thursday February 11, 2021 @03:09AM (#26099337)
Escaping the VM via an out-of-bounds write doesn't really show that stack walking is broken :/
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nmadden on Thursday February 11, 2021 @03:52AM (#26099567)
That’s just the first example. As the author of that series writes, most of the exploits are not due to memory corruption. Most are confused deputy attacks where privileged code can be tricked into performing dangerous operations.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by saagarjha on Thursday February 11, 2021 @04:23AM (#26099722)
Oh, I don't doubt it. I'm just saying that the particular example you linked wasn't that great.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by avereveard on Wednesday February 10, 2021 @12:28PM (#26092109)
a security manager can examine the call stack and know which class from which package is asking to perform a privileged action within the app; the class object will tell you which loader loaded it, and you can ask the loader the physical location of where the class comes from if you want to be really sure no one has overloaded it.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by mindcrime on Wednesday February 10, 2021 @03:33PM (#26094363)
Yes, it's a powerful and flexible mechanism. The problem is - how many people know (in detail) how to configure all of this power so as to take best advantage of it? My subjective perception is a that the answer is something like "a very small percentage of Java developers".
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by thw0rted on Monday February 15, 2021 @06:10AM (#26141572)
I'm reading through all these responses and it sounds like nobody read the article. Everybody keeps bringing up JVM SecurityManager, or how granular Deno's permission system is, or a syntax for granting runtime permissions to modules (like your Agoric link). That's not what happened here. The actual attack in the article was a post-install script run by the package manager. That means whatever kind of limits you might place on runtime capabilities of the library wouldn't have mattered. You need a system that lets the package installer request granular permissions from the package manager, where the package manager runs the scripts in a sandbox and only explicitly-provided privileges are granted. I don't know of any package managers that support this feature today.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by safog on Wednesday February 10, 2021 @02:01PM (#26093315)
This is a really nice idea but considering we haven't even solved the relatively simple case of users giving permissions to apps and expecting them to behave responsibly, I'm not optimistic that we can solve the much more challenging case of importing library code.

e.g., If someone gives an app the ability to upload photos, it can silently read all photo metadata, upload all photos to a private server instead of uploading just the single photo that the user picked. This can be solved with OS level standard photo pickers but it hasn't been yet.

Same with package code. Maybe a package needs network access for stuff it genuinely needs to do. However it can (and probably will) at some point go above and beyond in the amount of data it collects. FB Mobile SDK outage is a good example of this. https://www.bugsnag.com/blog/sdks-should-not-crash-apps

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by nahuel0x on Wednesday February 10, 2021 @11:09AM (#26091015)
Giving fine-grained permissions to sandboxed libraries is the way forward, and probably a really good use case for WASM.
Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by edejong on Wednesday February 10, 2021 @02:53PM (#26093834)
It sounds like you are interested in a (distributed) capability-based security model. [1]

[1] https://enacademic.com/dic.nsf/enwiki/295618

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by ryukafalz on Wednesday February 10, 2021 @03:37PM (#26094414)
Yes, I didn't use the term explicitly in my comment but you're precisely right. ;)

Agoric (the company whose blog post I linked to) and the people behind it have done a ton of object capability work over the years.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by jjn2009 on Wednesday February 10, 2021 @06:22PM (#26096231)
Its unfortunate that the proposed realms is still just a proposal. Even still I've heard many arguments that since the method of isolation lives inside JS it cannot be expected to be entirely secure and you would be much better off relying on OS level security primitives, a point that comments I've read so far completely glosses over. I'd love for someone to prove me wrong that this is air tight so we can champion realms at my work.

POLA is good to live by regardless if it can be implemented.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by zomgwat on Wednesday February 10, 2021 @06:05PM (#26096078)
Not a complete answer (by any means) but keeping tight control over egress network access helps (I wish it was easier to limit egress access over port 443).

Systemd has some capability to restrict access to system resources. I haven't experimented with the capabilities yet so not sure what's all there.

Re:Dependency Confusion: How I Hacked Into Apple, Microsoft ... by linuxftw on Wednesday February 10, 2021 @06:29PM (#26096278)
I'm late to this conversation, but the thing that can restrict "doing anything" on your machine is SELinux.