Visitar URL original
LGTM.com - Java UnsafeTlsVersion false positive · Issue #4059 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20240324153743/https://github.com/github/codeql/issues/4059
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

LGTM.com - Java UnsafeTlsVersion false positive #4059

Open
JLLeitschuh opened this issue Aug 12, 2020 · 3 comments
Open

LGTM.com - Java UnsafeTlsVersion false positive #4059

JLLeitschuh opened this issue Aug 12, 2020 · 3 comments

Comments

@JLLeitschuh
Copy link
Contributor

Description of the false positive

I'm running the beta of the CodeQL GitHub actions driven code-scanning feature. Since the gradle/gradle repo hasn't been built recently on LGTM.com, unfortunately, I can't link directly to it at the moment.

https://github.com/gradle/gradle/blob/1e9444f521ab3bbec1a5b852c1e0c4e12fb81819/subprojects/resources-http/src/main/java/org/gradle/internal/resource/transport/http/DefaultSslContextFactory.java#L122-L122

The false positive here is from this logic:

SSLContext sslcontext = SSLContext.getInstance("TLS")

This is the query flagging this issue.

/**
* @name Unsafe TLS version
* @description SSL and older TLS versions are known to be vulnerable.
* TLS 1.3 or at least TLS 1.2 should be used.
* @kind path-problem
* @problem.severity error
* @precision high
* @id java/unsafe-tls-version
* @tags security
* external/cwe/cwe-327
*/
import java
import SslLib
import DataFlow::PathGraph
from DataFlow::PathNode source, DataFlow::PathNode sink, UnsafeTlsVersionConfig conf
where conf.hasFlowPath(source, sink)
select sink.getNode(), source, sink, "$@ is unsafe", source.getNode(),
source.getNode().asExpr().(StringLiteral).getValue()

@big-guy, and I spent some time investigating this query result earlier today and we believe but are not confident that this is a false positive.

Using the following sample code:

public class App {
    public String getGreeting() {
        return "Hello world.";
    }
    public static void main(String[] args) throws Exception {
        System.out.println(new App().getGreeting());
        SSLSocket socket = connect("plugins.gradle.org", 443);
        for (String protocol : socket.getEnabledProtocols()) {
            System.out.println(protocol);
        }
    }
    public static SSLSocket connect(String host, int port)
        throws Exception {
        SSLContext context = SSLContext.getInstance("[PICK TLS VERSION]");
        context.init(null, null, null);
        return (SSLSocket) context.getSocketFactory().createSocket(host, port);
    }
}

All test are run with JDK 11.

Run with TLS you get the following output:

$ ./gradlew run
> Task :run
Hello world.
TLSv1.3
TLSv1.2
TLSv1.1
TLSv1
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Run with TLSv1.3 you get the following output:

$ ./gradlew run
> Task :run
Hello world.
TLSv1.3
TLSv1.2
TLSv1.1
TLSv1
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Run with TLSv1.2 you get the following output:

$ ./gradlew run
> Task :run
Hello world.
TLSv1.2
TLSv1.1
TLSv1
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Unfortunately, it seems like SSLContext.getInstance("...") doesn't seem to set a minimum-supported TLS version (as you might expect), but instead seems to be an upper-bound for supported version.

It seems like the vulnerability here actually exists when you pick SSLContext.getInstance("[ANYTHING]") but fail to restrict the TLS supported versions using SSLSocket#setEnabledProtocols.

Our understanding could be wrong here. I'd love if someone who has a better understanding of the Java TLS stack and TLS downgrade attacks could jump in here.

@JLLeitschuh
Copy link
Contributor Author

CC: @artem-smotrakov

@Marcono1234
Copy link
Contributor

but fail to restrict the TLS supported versions using SSLSocket#setEnabledProtocols.

There are also:

And Apache's HttpClient class org.apache.http.conn.ssl.SSLConnectionSocketFactory has multiple constructors which allow restricting the supported protocols.
Though Apache's HttpCore method SSLContextBuilder#setProtocol is equally vulnerable because it delegates to SSLContext#getInstance:
https://github.com/apache/httpcomponents-core/blob/0c4939370f11cd3b32c156c45ee71990cb52a22c/httpcore5/src/main/java/org/apache/hc/core5/ssl/SSLContextBuilder.java#L351-L356

@JLLeitschuh
Copy link
Contributor Author

JLLeitschuh commented Aug 12, 2020 •

In our case at Gradle, you can see that we're actually using one of those SSLConnectionSocketFactory methods.

builder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContextFactory.createSslContext(), SSL_PROTOCOLS, null, hostnameVerifier));

- https://github.com/gradle/gradle/blob/1e9444f521ab3bbec1a5b852c1e0c4e12fb81819/subprojects/resources-http/src/main/java/org/gradle/internal/resource/transport/http/HttpClientConfigurer.java#L122

Our SSL_PPROTOCOLS list is actually vulnerable here in this case.

    private static final String[] SSL_PROTOCOLS;

    private static final String HTTPS_PROTOCOLS = "https.protocols";

    static {
        String httpsProtocols = System.getProperty(HTTPS_PROTOCOLS);
        if (httpsProtocols != null) {
            SSL_PROTOCOLS = httpsProtocols.split(",");
        } else if (JavaVersion.current().isJava7() || (JavaVersion.current().isJava8() && Jvm.current().isIbmJvm())) {
            SSL_PROTOCOLS = new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"};
        } else {
            SSL_PROTOCOLS = null;
        }
    }

By default, we fall back to using null which means that the Apache client will automatically support all TLS versions.

Unfortunately, this true vulnerability (at least I think it is) is not captured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants