r/javahelp • • Jun 21 '24

How does Java handle untrusted code?

When I run Java code, Java has access to basically everything on my computer.

  • It can read/update/delete most of my files.

  • It can send messages to/from the internet.

  • It can pop up a million pop ups whenever it wants.

  • It can even control my mouse, keyboard, screen, printer, etc.

As a result, it seems like the only recourse for non-technical users is to pray really hard, or phone a technical friend. Otherwise, there's no way for them to know whether or not this Java program will tear their computer apart.

Compare that to the JavaScript world. Yes, they run on the browser instead of directly on the OS. But in the JavaScript world, code is untrusted by default.

  • If I want access to the camera or microphone, I must ask the user for permission.

  • If I want to read or save a file, I must ask the user for permission.

  • If I want to make a popup or go full screen, I must ask the user for permission.

Obviously, this strategy isn't flawless. The user's personal info that is stored in the browser can still be exploited. But even then, the level of damage one can do with Java code on the desktop vs JavaScript code in the browser is night and day.

I understand that that is comparing apples and oranges, but my question remains -- does Java have any way to sandbox an application, so that it can only break out with explicit acknowledgement and permission from the user?

3 Upvotes

27 comments sorted by

View all comments

4

u/roge- Jun 21 '24 edited Jun 21 '24

Does Java have any way to sandbox an application, so that it can only break out with explicit acknowledgement and permission from the user?

Java used to have the concept of a Security Manager, which was intended to allow users to prevent code from accessing some of these more sensitive APIs. However, Security Managers were deprecated in JDK 17 since it was hard to maintain and ensure good security.

Nowadays, if you want to run untrusted code, you either have to use a programming language or runtime that's designed for untrusted code, e.g. JavaScript or WebAssembly, or rely on hardware-level or OS-level virtualization, e.g. KVM, Jails, or cgroups+chroot (the latter of which requires particular care for untrusted code, I might add).

Running untrusted code securely is largely considered out-of-scope for the standard Java runtime these days, which is also the case for most other programming languages and runtimes, as others have pointed out.

1

u/davidalayachew Jun 21 '24

Wow, so you are telling me that the Security Manager was abandoned because the act of maintaining that was simply not worth the effort for them?

Again, I have a lot more appreciateion for what Browsers and JavaScript have accomplished -- to be able to run untrusted code so easily.

3

u/edgmnt_net Jun 21 '24

Nothing like that will ever work well without changing the programming model. It works better for Android, but Android apps must use specific APIs to do stuff like sending notifications, while traditional OSes and their apps rely on direct lower level operations (like opening files) with much less semantic content. In the latter case, enforcing rules like "disable notifications" or "only allow this app to read these files" can be very hard to accomplish precisely because the operations do not provide sufficient information to decide things straightforwardly, they are way too raw or general. Also note how the browser only allows JavaScript to open files that have been explicitly picked by a user through a browser-controlled file picker, there's no such thing as opening a file by path.

Nobody is going to be able to reasonably write, maintain and inspect policies for Java apps because Java APIs can do pretty much anything and application writers expect as much. Things could change, but that requires an entirely new ecosystem or at the very least a secure sandbox with a rich set of APIs for usual stuff.

1

u/davidalayachew Jun 21 '24

Makes a lot of sense. It sounds like that the default assumptions are the true culprit here, and trying to uproot the ground you stand on won't happen.

And you are saying that Android got away with it because it was literally a new OS, and therefore, could be designed from the groundup with this sandboxing in mind. Makes sense.

5

u/roge- Jun 21 '24

Not just that it wasn't worth the effort. It also lulled people into a false sense of security. Java was not designed to be entirely zero-trust, so it's extremely difficult to bolt support for running untrusted code onto the side.

Back when applets (and therefore Security Managers) were more common, vulnerabilities related to Java were rampant.

I think it was a good decision to ditch Security Managers.

2

u/davidalayachew Jun 21 '24

Makes a lot of sense, ty vm.