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

[JENKINS-70108] CastBlock must contextualize invoker to avoid issues in mixed-trust scenarios #640

Merged
merged 4 commits into from Dec 14, 2022

Conversation

dwnusbaum
Copy link
Member

@dwnusbaum dwnusbaum commented Dec 12, 2022

See JENKINS-70108.

819cc9b introduced a new Block for casts, CastBlock, which did not implement CallSiteBlock and did not contextualize its Invoker. In cases where untrusted code calls trusted code that performs a cast, this could result in trusted code calling SandboxInvoker.cast instead of DefaultInvoker.cast. In practical terms, this means that casts inside of global Pipeline libraries were sometimes incorrectly blocked by the sandbox.

This PR makes CastBlock implement CallSiteBlock so that it can contextualize Invoker correctly before performing the cast.

TODO:

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

Comment on lines +707 to +709
"Script1.super(Script1).setBinding(Binding)",
"Script1.trusted",
"Script1.foo()");
Copy link
Member Author

@dwnusbaum dwnusbaum Dec 12, 2022

Choose a reason for hiding this comment

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

Previously, we would also intercept new File(String), which was wrong because the cast is in trusted code and so it should not be intercepted by the sandbox.

@@ -527,7 +527,7 @@ public void regexpOperator() throws Throwable {
@Test
public void arrayPassedToMethod() throws Throwable {
assertEvaluate(4, "def m(x) {x.size()}; def a = [1, 2]; a.size() + m(a)"); // control case
assertEvaluate(4, "def m(x) {x.size()}; def a = [1, 2].toArray(); a.length + m(List.of(a))"); // workaround #1
assertEvaluate(4, "def m(x) {x.size()}; def a = [1, 2].toArray(); a.length + m(Arrays.asList(a))"); // workaround #1
Copy link
Member Author

Choose a reason for hiding this comment

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

import java.util.List;
import org.codehaus.groovy.runtime.InvokerInvocationException;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;

public class CastBlock implements Block {
public class CastBlock extends CallSiteBlockSupport {
Copy link
Member Author

Choose a reason for hiding this comment

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

I verified that this does not cause problems deserializing CastBlocks serialized before this change.

Comment on lines +718 to +719
* 1. Add a CallSiteBlock parameter to ContinuationGroup.castToBoolean, and use it to contextualize the invoker
* used in that method. All Blocks that use the method would need to be updated to implement CallSiteBlock.
Copy link
Member Author

@dwnusbaum dwnusbaum Dec 13, 2022

Choose a reason for hiding this comment

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

This seems like the simplest option. Let me know if you think it's worth fixing. There are eight Blocks that would need to implement CallSiteBlock if we make this change. The second option might be better in terms of reducing overall complexity, but it would be more complex to implement.

@dwnusbaum dwnusbaum marked this pull request as ready for review December 13, 2022 16:25
@dwnusbaum dwnusbaum requested a review from a team as a code owner December 13, 2022 16:25
Copy link
Member

@jglick jglick left a comment

Choose a reason for hiding this comment

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

Fine to the extent I understand it.

@dwnusbaum dwnusbaum added the bug label Dec 13, 2022
@dwnusbaum dwnusbaum merged commit 2be4c99 into jenkinsci:master Dec 14, 2022
@dwnusbaum dwnusbaum deleted the JENKINS-70108 branch December 14, 2022 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants