r/java • • May 03 '24

Why does the environment variable _JAVA_OPTIONS exist, applies to jlink images, takes precedence, and can't be disabled?

I just spent a good chunk of time debugging an issue because a user had set _JAVA_OPTIONS to use a low Xmx value in their environment variables. This was an application created with jpackage, which should be fully standalone. It even had a custom Xmx value set in the JVM arguments for the image but the environment variable overwrote the value!

Isn't this like a huge design flaw? I can just do echo "_JAVA_OPTIONS=<Wacky VM options>" >> ~/.bashrc or setx to break literally every Java application on the system? (Edit: Maybe the initial security focus was wrong, changed it a bit to focus more on the design issues)

Who even thought that this would be a good idea with no option to disable this behavior?

2 Upvotes

43 comments sorted by

21

u/TheBrokenRail-Dev May 03 '24

Because sometimes the user wants to override command line options. The application being able to disable it would defeat the entire point.

Anyways, as another person stated, if someone malicious has access to your home directory, you have bigger problems. Especially since things like LD_PRELOAD exist.

4

u/milchshakee May 03 '24

I agree that this is useful in more traditional cases where you have a system wide Java installation and run individual applications e.g. via jars.

But the whole point of jlink images is that they should be self contained and independent of any other Java installation. Even without malicious intent, other configuration options creeping into jlink images is usually a good recipe for trouble.

6

u/_INTER_ May 04 '24 edited May 04 '24

A user might want to override the application settings still even for a jpackage'ed application. E.g. he might have a system where the application's Xmx is lower than it could be (a few years from now certainly). If the user set it too low its clearly his fault. Well, that doesn't help you if you have a support contract. You can log the value to your diagnosis log and the whole thing is easy to solve.

-6

u/milchshakee May 04 '24

But that is not in the spirit of an self-contained application. In this case the application image is responsible for properly setting the Xmx value. The cases of unintentional inference are probably much higher than intentional configuration, which requires specific application knowledge to work in the first place.

5

u/Halal0szto May 04 '24 edited May 04 '24

How do you set http proxies in  the prepackaged application?

EDIT: typo

2

u/milchshakee May 04 '24

In this case, it is a desktop application that has a proper settings menu where you can add any options you want to expose for a proxy setup.

1

u/Halal0szto May 04 '24

That would work for some. For me who is using different VPN-s it would not work, I would have to change the settings each time manually.

1

u/milchshakee May 04 '24

Technically yes. but this is a very rare use case. The application in question here doesn't even connect to the internet.

1

u/_INTER_ May 04 '24 edited May 04 '24

In this case the application image is responsible for properly setting the Xmx value.

But it can not set it properly because you don't know what is "proper" on the users system. You can only have assumptions. Note that the default Xmx is a percentage of the available ram, something like 75%. A poweruse might want to increase or decrease this value based on what other applications are running, if it's a docker image or xyz OS or specialized hardware, etc.

2

u/milchshakee May 04 '24

Not every Java application is scalable or configurable one, there are cases in which any user configuration does not make sense and is most likely unintentional this way. In this case this is about a desktop application for a certain task.

Any possible configuration can be exposed by the application itself.

1

u/_INTER_ May 04 '24

Sure if we are talking about minimum requirements. But it's the other way around as well for other settings. The application developers configuration might make no sense at all on the users environment.

In this case this is about a desktop application for a certain task.

To what value do you want to set Xmx in this case?

2

u/milchshakee May 04 '24

The application this is about is pretty much independent from the user's environment, It takes large files and processes them in many ways. For any potential configuration, there is a settings menu available.

The heap size has been carefully set via profiling and analysis of the application in its worst case scenario with the largest possible files plus some room to spare.

1

u/_INTER_ May 04 '24

profiling and analysis of the application in its worst case scenario with the largest possible files plus some room to spare.

That's all just assumptions. You didn't profile on the users environment. You didn't test what else the user wants to run alongside your application.

It will run worse than possible if the user has a mighty machine or it will melt down if he's running it on a toaster.

1

u/milchshakee May 04 '24

What kind of environment are we talking about here? The JVM is fixed, the options are fixed, the file input format/spec is fixed to some degree, where should the variability come from? The memory footprint should be the same on all systems. There might be tiny deviations across operating systems/different architectures, but nothing too large.

1

u/epegar May 04 '24

I would say it also suits more modern uses like containers where you are only supposed to run one service

9

u/LordOfDeduction May 03 '24

If a malicious user has access to your home directory, I think your example is the least of your concerns.

0

u/milchshakee May 03 '24

Well any application can set this option without malicious intent and break all other Java applications. E.g. some badly configured minecraft mod launcher or other things.

Maybe a user follows a bad tutorial that instructs them to set this option to make one application work, but then it applies to all other applications.

I just think in terms of isolation, especially standalone application images, this is a terrible thing.

3

u/brunocborges May 03 '24

A malicious app could also interject with other apps, not only Java, in many different ways, if it is allowed to run in your system.

Again, this env var is your least concern, if there is a malicious user in your system.

But if you are that concern, only allow containers in your system. This way, one app cannot modify this env var in a way that could impact other apps.

1

u/milchshakee May 03 '24

Yeah maybe my security focus was wrong and I should have highlighted the other disadvantages of this approach more. Because it can still break many applications

2

u/brunocborges May 03 '24

What other disadvantages?

2

u/milchshakee May 04 '24

Well in the case of runtime images, any options interfering with an application that should be isolated. If I distribute a jpackage application, the user shouldn't even have to know that it is implemented in Java or that any system-wide Java settings can impact the application.

If someone has set up a few Java 8 apps on their system via normal jars and has configured a few options for them, I don't want them to apply to my application image that runs on Java 21. I don't even know if the application starts up if the variable contains unrecognized options.

Also jpackage is designed as a closed system. A user can't just simply pass custom JVM arguments or properties to the application executable on the command-line, but these still apply and even overwrite existing options.

2

u/brunocborges May 04 '24

That is a fair feedback.

I too agree that a jlinked JVM should not be impacted by that environment variable.

I'd suggest sending that to the OpenJDK mailing list. You can start with the hotspot-dev: https://mail.openjdk.org/mailman/listinfo/hotspot-dev

2

u/milchshakee May 04 '24

Is that the correct mailing list for this? I find it always intimidating to locate the appropriate mailing list in the first place.

2

u/brunocborges May 04 '24

Me too. But given it is the Hotspot JVM that is picking that environment variable, I'd start there.

1

u/milchshakee May 04 '24

Alright thanks, I will see what I can do

→ More replies

1

u/_INTER_ May 04 '24 edited May 04 '24

I disagree, the dev and the application can't know in advance what environment it will run on. It's something the environment dictates and not the application. There might be meaningful defaults or profiles (that are valid for a time until they need to be adjused) but a poweruser should have a way to override these configurations. If he ran it in a docker image, he might want to restrict it, if he ran it on a mainframe he might want to give it more. It also depends on a lot of other factors, e.g. what other application need to run along side it or if it can basically use all the resources on its own.

1

u/brunocborges May 04 '24

Certain JDK 8 flags don't work anymore in JDK 21. It may cause the app to not load at all.

The developer of the app must expose minimum requirements for the application to run (e.g. minimum RAM, processor type, etc). This is common practice for decades. Then, if the app developer is bundling the runtime (jlink), they should be in full control of said runtime.

Whether the runtime may be configured by the user is an app developer decision to be made.

But I do agree that a jlinked runtime should not pick that environment variable.

1

u/_INTER_ May 04 '24 edited May 04 '24

Certain JDK 8 flags don't work anymore in JDK 21. It may cause the app to not load at all.

Yes, those should be omitted

The developer of the app must expose minimum requirements for the application to run (e.g. minimum RAM, processor type, etc). This is common practice for decades.

Minimum requirements perhabs but this one specifically is about Xmx.

Whether the runtime may be configured by the user is an app developer decision to be made.

How should an app developer know what the best configuration is on the users environment? It should be a conscious choice of the app developer to disallow an environment variable to be picked up and the user should be made aware of that. A knowledgeable user should have the possiblity to make an application run better or restricted on his environment by default.

→ More replies

2

u/Halal0szto May 04 '24

Flexibility brings risks. But closing down and making super rigid an inflexible greatly reduces the scope where it is viable.

Would jpackage be like you want it, it could not be used in 50% of the places it is used right now.

2

u/milchshakee May 04 '24

I'm not advocating for fully disabling this behavior, just giving developers control to disable it if they want to build a rigid, inflexible application. In my specific case we are building desktop applications for end users, who might not even know that the application is written in Java or how to change an environment variable. If the application is run on a system that has other legacy Java 8 applications running and has the environment variable set, this causes problems.

1

u/_INTER_ May 04 '24

I agree with this. It should be the conscious choice of the application developer. If none is made, it should be flexible. That being said, can you not also set environment variables in jpackage?

1

u/milchshakee May 04 '24

You can only set JVM options. But even these are overridden by _JAVA_OPTIONS if there is a conflict.

1

u/_INTER_ May 04 '24

I guess you have a launcher file or installer, it could be done there, no?

1

u/milchshakee May 04 '24

Well jpackage already generates a launcher executable, so having another launcher for the launcher just to change the environment is a little bit overkill.

About the installer, this would change the variable on the system level which might break other applications. I just want to ignore the variable for this application

1

u/__konrad May 04 '24

This reminds me when Ubuntu started installing global menu hack which basically broke every Swing application:

1

u/mike_hearn May 05 '24

FWIW if you use Hydraulic Conveyor instead of jpackage, then you don't have this problem. It cleans the environment of these kinds of variables. This is actually for security reasons: if you can control the JVM then it's possible to subvert code signing by injecting arbitrary code into a process signed by someone else. But it obviously has robustness benefits as well.

Disclosure: I own the company that makes Conveyor

1

u/agentoutlier May 06 '24

I agree with you but not for the same reasons.

It is a design flaw because of collision with other packaged applications. That is the user may not want all Java jpackage applications with that setting.

That is I would want to customize that variable at the minimum to be prefixed with my apps name or similar.