The Wayback Machine - http://web.archive.org/web/20240324153517/https://github.com/github/codeql/issues/7486
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

[Feature Request] support add constraint on typeVariable while perform virtual dispatch in java query #7486

Open

KiruaLawliet opened this issue Dec 29, 2021 · 5 comments
Labels
enhancement New feature or request Java

Comments

@KiruaLawliet
Copy link

KiruaLawliet commented Dec 29, 2021

Is it possible to add class type constraint on TypeVariable while perform virtual dispatch in java query?

Take this sinppet for example:

E data = (E) in.readObject();
data.equals(....);

It seems data can be any class and have data flow tracked into equals method as long as the class has one even if the class is unserializable.

@KiruaLawliet KiruaLawliet added the question Further information is requested label Dec 29, 2021
@KiruaLawliet KiruaLawliet changed the title [Feature Request] support add constraint on the typeVariable parameter while perform virtual dispatch in java query [Feature Request] support add constraint on typeVariable while perform virtual dispatch in java query Dec 29, 2021
@adityasharad
Copy link
Collaborator

adityasharad commented Jan 4, 2022

It sounds like you are observing data flow starting from data in the above data.equals(...) and ending at this within an equals method, but the equals implementation is inside a class that doesn't implement Serializable, so it cannot be the actual implementation reached by the equals call. Is that correct?

@adityasharad adityasharad added the awaiting-response The CodeQL team is awaiting further input or clarification from the original reporter of this issue. label Jan 4, 2022
@KiruaLawliet
Copy link
Author

KiruaLawliet commented Jan 5, 2022

@adityasharad Yes

@github-actions
Copy link
Contributor

github-actions bot commented Jan 20, 2022

This issue is stale because it has been open 14 days with no activity. Comment or remove the Stale label in order to avoid having this issue closed in 7 days.

@github-actions github-actions bot added the Stale label Jan 20, 2022
@smowton smowton removed the Stale label Jan 20, 2022
@adityasharad adityasharad removed the awaiting-response The CodeQL team is awaiting further input or clarification from the original reporter of this issue. label Jan 20, 2022
@smowton
Copy link
Contributor

smowton commented Jan 28, 2022

Can reproduce with test case

import java.io.ObjectInputStream;
import java.io.Serializable;

public class Test2<E> {

  public static String source() { return "tainted"; }

  public static void sink(Object o) { }

  public void test(ObjectInputStream in) {

    try { 

      String tainted = source();
      E data = (E) in.readObject();
      data.equals(tainted);

    }
    catch(Throwable t) {}

  }

}
/**
 * @kind path-problem
 */

import java

import semmle.code.java.dataflow.DataFlow
import DataFlow::PathGraph

class TestConfig extends DataFlow::Configuration {

  TestConfig() { this = "TestConfig" }

  override predicate isSource(DataFlow::Node n) {
    n.asExpr() = any(MethodAccess ma | ma.getMethod().getName() = "source")
  }

  override predicate isSink(DataFlow::Node n) {
    n.asExpr() = any(MethodAccess ma | ma.getMethod().getName() = "sink").getAnArgument()
  }

}

from TestConfig c, DataFlow::PathNode src, DataFlow::PathNode sink
where c.hasFlowPath(src, sink)
select src, src, sink, "message"

@smowton
Copy link
Contributor

smowton commented Jan 28, 2022

@KiruaLawliet the cause is that readObject does not return Serializable, but rather Object. I will look into noting that the return is in fact constrained to return a Serializable type, but this won't be a high priority because it is a relatively rare scenario.

Possible workarounds: if you were to constrain the type variable E extends Serializable, or to make an explicit typecast to Serializable before calling equals, then CodeQL will notice the type constraint.

@smowton smowton added enhancement New feature or request and removed question Further information is requested labels Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Java
Projects
None yet
Development

No branches or pull requests

3 participants