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

•

u/AutoModerator Jun 21 '24

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

14

u/NitronHX Jun 21 '24 edited Jun 21 '24

This question is not related to java but to any non-js language. C c++ kotlin .Net(C#), rust, zig ....

Also this is not a issue of aforementioned languages but platforms they run on. For example in android java is somewhat sandboxed since Android apps require permissions on this platform for camera, files ...

So the default way to run desktop apps is unsafe/trusted let's call it the "windows/Linux platform". There are ways to run desktop apps in a sandboxed environment if untrusted. For example flatpack is a Linux packaging format where all programms run sandboxed. On Linux you can use chroot if it's about file access that you are. Windows has a desktop app called sandboxie that's a well sandbox for other desktop apps. Maybe you run a Linux distro that is focused on sandboxing and security, where every time you login a empheral container is started.

When you run java in the browser "platform" (yes this is still possible) it runs as "safely" as js.

Binaries are as unsafe as the platforms they run on, and the OS is not a safe platform

Do I assume correctly that your question is "how do I deal with untrusted binaries?"

3

u/davidalayachew Jun 21 '24

Do I assume correctly that your question is "how do I deal with untrusted binaries?"

Effectively, yes. In which case, you have answered my question. Ty vm.

4

u/NitronHX Jun 21 '24

Also practical advice:

  • for User: you have a jar but scared to execute it? Create a VM / container.

  • for Developer:

    • Linux: distribute as flatpack and you have automatic sandboxing

    • Windows: UWP/windows store maybe?

    • Mac: never used no idea

1

u/davidalayachew Jun 21 '24

Much appreciated.

I think there would be a lot of value in OS providers making it super easy for non-technical users to run untrusted code in a VM.

7

u/MrRickSancezJr Jun 21 '24

Javascript isn't doing any of that. That's your browser keeping you safe. Safe'ish by the way. Any great malware or virus you ever got from ads or other misclicks was likely powered by JS.

I've even tried to disable some safety features in the past on Chrome to do some shady injection stuff for data entry, and it wouldn't even work. One specifically was setting a file input directly to a file location. Easy fix, just sent key strokes to it instead.

HTML might be the only language that by itself that can't do you any harm. I'm sure someone is gonna comment on this on a way to do it too lol

1

u/davidalayachew Jun 21 '24

HTML might be the only language that by itself that can't do you any harm. I'm sure someone is gonna comment on this on a way to do it too lol

Fingerprinting is the obvious one. Have images load upon hover, which HTML can do natively. Nothing is truly safe, it seems, unless you are aware of what is running under the hood.

And thanks for the clarifications.

5

u/_jetrun Jun 21 '24 edited Jun 21 '24

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.

Yes. That's true for ANY native application. It has nothing to do with Java specifically.

But in the JavaScript world, code is untrusted by default.

No it's not.

Run JavaScript in a node container or as an electron app and see what happens. You'll be able to access any OS resource at the privilege level of the user that executed the process.

Browsers, on the other hand, need to heavily sandbox executing code because otherwise any random page you accidentally navigated to could maliciously take over your PC. That's not quite the same kind of risk vector that native application expose, because installing/executing a native application requires you to give EXPLCIT permission, unlike navigating to some page.

Browser sandboxing does come at a cost in that there are types of applications that you cannot build. For example, I cannot build a web application that can position itself on a specific monitor and/or span multiple monitors. I cannot build a web application that can index my hard drive and sync files on my hard drive with the cloud. I cannot build an application that can connect to an SFTP server on my local network and pull down some files. etc. etc. etc.

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.

Because one is a native application, and one is a browser application. Native applications will have direct access to most OS resources, doesn't matter if they are built with Java, C++, Go, Rust, .NET, and yes, even JavaScript.

OP, there has been a trend by OS-vendors to introduce more and more sandboxing of native applications, with some platforms being completely closed-off and fully controlled by the OS-vendor (i.e. iOS). Those OS-vendors can then demand a percentage cut of all your revenue (doesn't matter the source), or capriciously prevent you from building certain types of applications (like a competing browser engine), or kick you off the platform and you're not long able to build any application. I contend that this isn't a good thing, even if there are security benefits. Sandboxing has its place, but don't dismiss the freedom you have with non-sandboxed code.

1

u/davidalayachew Jun 21 '24

Very insightful, ty vm.

I now understand the difference between a native application and a browser application. And it makes sense why the fence is there for one and not the other.

I also see the weakness in sandboxing -- the fences that protect the users can also be used to exploit content creators and force them to follow every unreasonable demand you please, if they want to be on your platform. I've seen the fences rising, so what you are saying makes clear sense to me.

Based on this comment, I think my opinion has changed. It seems that the only true recourse here is that users just need to be smarter about software. They need to be able to better understand what they are running, and why it is running. Frankly, they need to start understanding code.

I had always assumed that we could someday live in a world where each person could specialize in the things that they wanted to, and delegate even the basic understanding of the rest to those who choose to be experts in this. But I think that that simply isn't possible. It sounds like, no matter what, no true trust or safety can occur until people truly understand what it is they are using, and how it came to be. They don't necessarily need to be an expert, and they certainly don't need to know how to make the things they rely on. But if they don't even understand the basics of the software running on their screen, then I now doubt that they are capable of safely executing any non-trivial software.

Very insightful, ty vm.

3

u/maethor 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?

The SecurityManger

http://baeldung.com/java-security-manager

However it's been deprecated for removal. It was important back in the early days when Java was run in the browser, but now it's considered unnecessary bloat.

But in the JavaScript world, code is untrusted by default

Browser world. Node.js is as trusted as anything else running locally (though they have an experimental permissions feature that doesn't look all that different from the SecurityManger - I wonder if they will eventually reach the same conclusion the OpenJDK people did).

1

u/davidalayachew Jun 21 '24

Thanks for the correction.

5

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jun 21 '24

But in the JavaScript world, code is untrusted by default.

This is all just one big ass misunderstanding on your part. You can use JavaScript in a completely trusted way exactly the same as Java, C++, Rust, whatever. Ever heard of NodeJS?

This has nothing to do with Java or JavaScript; the difference is whether the code is running in your browser or as it's own standalone program.

does Java have any way to sandbox an application

This is the responsibility of the operating system. So; no.

1

u/davidalayachew Jun 21 '24

I understand now, ty vm.

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.

2

u/Successful_Use5160 Jun 23 '24

You're describing a phenomenon present in most serious developing language, more so C/C++

1

u/davidalayachew Jun 23 '24

That's true. I ended up learning from the commentors here that I was conflating a desktop application with a browser application.

1

u/Dull-Tip7759 Jun 21 '24

you can use things like java decompiler to extract and view the code/perform static analysis for code that uses specific things, suck as file.delete(), socket I/O, etc.

Most reputable vendors won't risk destroying any positive reputation, but that begs who can be trusted? So many ill informed people, too.

1

u/davidalayachew Jun 21 '24

Yes, but one bad apple spoils the barrel. I was mentioning to someone else in another subreddit -- most bad actors use and dispose of names very quickly. Because of this, name and shame does very little to hold them accountable because they lose so little. They simply abandon the identity, acquire a new one, and try again.

2

u/blobjim Jun 21 '24

It looks like it might be possible to use Java inside a flatpak on Linux: https://github.com/flathub/org.freedesktop.Sdk.Extension.openjdk

It looks like the file org.freedesktop.Sdk.Extension.openjdk.yaml contains the details. I'm not sure if there are any actually published apps using it.

Someone has a java project to use the GTK UI library from Java (there used to be an official set of bindings a long time ago I think, but it wasn't really updated): https://github.com/bailuk/java-gtk

probably works better than trying to use java swing from a flatpak sandbox, because GTK presumably has integration with the various flatpak portals.

It would definitely be difficult to create a truly sandboxed app on something other than Android but it may be possible with enough work to do so on "desktop" linux. I assume it's gonna be a lot of work learning flatpak and making it work though, but I could be wrong.

It's a really good question!

0

u/[deleted] Jun 21 '24

[deleted]

0

u/davidalayachew Jun 21 '24

I see. But this necessarily requires the end user to be technical, yes? After all, if they do not create the VM themselves, there is nothing stopping me from creating a corrupt VM and sending them that.

This does make me appreciate the browser more. To be able to enable untrusted code to run as easily as it does must be a massive feat.

0

u/[deleted] Jun 21 '24

[deleted]

1

u/davidalayachew Jun 21 '24

I see what you are saying now, ty vm. Because it is an OS concern, that means that anything I do application-wise can only exploit what you forget to handle. In the end, the computer (or VM) running it is the one in control.

0

u/[deleted] Jun 21 '24

[deleted]

2

u/Beginning-Ladder6224 Extreme Brewer:snoo_biblethump: Jun 21 '24

I was searching for any mention of security manager.. now I got it :-) Yes.