Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1004] Wrap service loader calls in privileged actions if the securit… #1005

Merged
merged 1 commit into from Sep 7, 2021

Conversation

jamezp
Copy link
Contributor

@jamezp jamezp commented Aug 3, 2021

…y manager is enabled. Also ensure the LinkageError for the ClientBuilder is thrown if a security manager is enabled.

Resolves #1004

Signed-off-by: James R. Perkins jperkins@redhat.com

@jamezp
Copy link
Contributor Author

jamezp commented Aug 3, 2021

I forgot to mention, if this seems acceptable I can port the changes over to whichever branches desired.

@arjantijms
Copy link
Contributor

@jamezp do note that SecurityManager, AccessController etc have been deprecated for removal in JDK 17. So we may want to remove such privilege actions, not add them.

@jansupol
Copy link
Contributor

jansupol commented Aug 4, 2021

There is quite a lot of code duplication in the world of suppliers.

JEP 411 deprecates the SecurityManager in JDK 17, but for JDK 17, it means a warning is issued. Jakarta REST 3.1 is supposed to be working with JDK 11-17 which still has the SecurityManager functional. But I'm surprised noone complained about this untill now (2.0 was released with EE 7)

@jamezp You seem to use the words may/might, is this a real issue or the could-potentially-happen one?

@jamezp
Copy link
Contributor Author

jamezp commented Aug 4, 2021

@jamezp do note that SecurityManager, AccessController etc have been deprecated for removal in JDK 17. So we may want to remove such privilege actions, not add them.

Yes, I thought about that before sending this. However, there still is no clear path for a replacement that I'm aware of. I know that some installations require the security manager to be enabled.

The real purpose of this is so RESTEasy no longer has to use a fork of the spec :)

@jamezp
Copy link
Contributor Author

jamezp commented Aug 4, 2021

There is quite a lot of code duplication in the world of suppliers.

JEP 411 deprecates the SecurityManager in JDK 17, but for JDK 17, it means a warning is issued. Jakarta REST 3.1 is supposed to be working with JDK 11-17 which still has the SecurityManager functional. But I'm surprised noone complained about this untill now (2.0 was released with EE 7)

Yes. AFAIK there is no replacement for the security manager at this point. I have a feeling it won't be removed anytime soon :)

@jamezp You seem to use the words may/might, is this a real issue or the could-potentially-happen one?

Sorry, yes this is a real issue. I've not tried in other application servers, but I can say in WildFly if you run with a security manager then there are failures. There is a fix in a fork which WildFly and JBoss EAP use. However it would be nice to let the fork die and use the spec itself :)

@mkarg
Copy link
Contributor

mkarg commented Aug 4, 2021

I'd rather vote for removal of security manager instead of correcting it.

@spericas
Copy link
Contributor

spericas commented Aug 4, 2021

@jamezp What is the use case for running WildFly under a security manager? The JDK team seems to simply by JEP 411 that this is no longer needed/used in server-side Java.

@andymc12
Copy link
Contributor

andymc12 commented Aug 4, 2021

I know that we (IBM) have customers who require Java 2 security for their app servers - even in Java/Jakarta EE. Personally, I've never been a fan of Java 2 security, but we do have some users who need it. I believe most of our Java 2 security customers are governmental users or are contractors for governments that require it. Those requirements might start to be lifted now that Java 17 is deprecating the security manager, but in general (and until we get a little closer to prereq'ing Java 17), I'm in favor of adding doPrivs where necessary.

@jamezp
Copy link
Contributor Author

jamezp commented Aug 4, 2021

@jamezp What is the use case for running WildFly under a security manager? The JDK team seems to simply by JEP 411 that this is no longer needed/used in server-side Java.

@spericas I can't say I know the requirements specifically for WildFly, but I know we (Red Hat) have JBoss EAP customers that are required to run the container with the security manager enabled.

I agree with @andymc12 that I don't like it, but I don't really have a choice but to support it :)

+ " to " + targetTypeURL;
} else {
errorMessage = AccessController.doPrivileged((PrivilegedAction<String>) () -> {
ClassLoader loader = pClass.getClassLoader();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should refactor into private method instead of duplicating code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be done if I've understood correctly :)

@spericas
Copy link
Contributor

spericas commented Aug 6, 2021

@jamezp @andymc12 Just reviewed JEP 411. Looks to me that it would be OK to fix this for 3.1. We should probably review this for 4.0 as clearly a lot of this functionality is going away. I still think that the "security" provided by the Java Security Manager is a lot less useful in practice than on paper.

spericas
spericas previously approved these changes Aug 12, 2021
Copy link
Contributor

@spericas spericas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Only comment is that findFirstService's could create an Action to avoid duplicating code blocks.

@jamezp
Copy link
Contributor Author

jamezp commented Aug 12, 2021

LGTM. Only comment is that findFirstService's could create an Action to avoid duplicating code blocks.

Done.

Copy link
Contributor

@andymc12 andymc12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I like this changes - the performance changes and adding getClassLoader calls into doPriv blocks. I only have a question about whether ServiceLoader.load(...) and subsequent iterating requires a doPriv block. Thanks!

if (System.getSecurityManager() == null) {
return action.run();
}
return AccessController.doPrivileged(action);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand putting the getClassLoader calls into a doPriv block, but I don't understand the ServiceLoader calls. Does that need to be in a doPriv? The javadoc doesn't mention that it throws a SecurityException.

Copy link
Contributor Author

@jamezp jamezp Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ServiceLoader.load() is actually safe, but it captures the AccessContrelContext is captured and during iteration a security exception could be thrown on Constructor.newInstance().

From the JavaDocs:

Service loaders always execute in the security context of the caller. Trusted system code should typically invoke the methods in this class, and the methods of the iterators which they return, from within a privileged security context.

if (System.getSecurityManager() == null) {
return action.run();
}
return AccessController.doPrivileged(action);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. Can we avoid the doPriv on the ServiceLoader.load(...) calls?

@spericas
Copy link
Contributor

LGTM. Only comment is that findFirstService's could create an Action to avoid duplicating code blocks.

Done.

Note that PrivilegedAction is a functional interface, so I think you could simplify this using capturing lambdas for even better readability without the need inner classes.

…y manager is enabled. Also ensure the LinkageError for the ClientBuilder is thrown if a security manager is enabled.

Resolves jakartaee#1004

Signed-off-by: James R. Perkins <jperkins@redhat.com>
@jamezp
Copy link
Contributor Author

jamezp commented Aug 12, 2021

Note that PrivilegedAction is a functional interface, so I think you could simplify this using capturing lambdas for even better readability without the need inner classes.

Good point. Updated!

Copy link
Contributor

@andymc12 andymc12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks!

@spericas spericas added this to the 3.1 milestone Sep 7, 2021
@spericas spericas added this to In progress in Jakarta REST 3.1 Sep 7, 2021
@spericas
Copy link
Contributor

spericas commented Sep 7, 2021

Merging as it has been approved 25 days ago.

@spericas spericas merged commit 08e4014 into jakartaee:master Sep 7, 2021
alwin-joseph pushed a commit to alwin-joseph/rest that referenced this pull request Sep 28, 2021
…y manager is enabled. Also ensure the LinkageError for the ClientBuilder is thrown if a security manager is enabled. (jakartaee#1005)

Resolves jakartaee#1004

Signed-off-by: James R. Perkins <jperkins@redhat.com>
alwin-joseph pushed a commit to alwin-joseph/rest that referenced this pull request Sep 28, 2021
…y manager is enabled. Also ensure the LinkageError for the ClientBuilder is thrown if a security manager is enabled. (jakartaee#1005)

Resolves jakartaee#1004

Signed-off-by: James R. Perkins <jperkins@redhat.com>
@spericas spericas moved this from In progress to Done in Jakarta REST 3.1 Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

Partial implementation of privileged actions should be completed
6 participants