Arbitrary file read vulnerability through the Jenkins CLI can lead to RCE
Research is free — Hunters explains how the bug works, the root-cause code pattern, how the fix addresses it, and how to test whether a target is affected, in chat. Investigate & write exploit is a paid run — the engine reads the advisory and fix commits, then builds and validates a working proof-of-concept exploit with reproduction steps.
Affected versions
Details
Jenkins has a built-in command line interface (CLI) to access Jenkins from a script or shell environment. Jenkins uses the args4j library to parse command arguments and options on the Jenkins controller when processing CLI commands. This command parser has a feature that replaces an @ character followed by a file path in an argument with the file’s contents (expandAtFiles). This feature is enabled by default and Jenkins 2.441 and earlier, LTS 2.426.2 and earlier does not disable it. This allows attackers to read arbitrary files on the Jenkins controller file system using the default character encoding of the Jenkins controller process. * Attackers with Overall/Read permission can read entire files. * Attackers without Overall/Read permission can read the first few lines of files. The number of lines that can be read depends on available CLI commands. As of publication of this advisory, the Jenkins security team has found ways to read the first three lines of files in recent releases of Jenkins without having any plugins installed, and has not identified any plugins that would increase this line count. Binary files containing cryptographic keys used for various Jenkins features can also be read, with some limitations (see note on binary files below). As of publication, the Jenkins security team has confirmed the following possible attacks in addition to reading contents of all files with a known file path. All of them leverage attackers' ability to obtain cryptographic keys from binary files, and are therefore only applicable to instances where that is feasible.
The fix
[SECURITY-3314]
core/src/main/java/hudson/cli/CLICommand.java+15 −1
@@ -26,6 +26,7 @@import edu.umd.cs.findbugs.annotations.CheckForNull;import edu.umd.cs.findbugs.annotations.NonNull;+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;import hudson.AbortException;import hudson.Extension;import hudson.ExtensionList;@@ -51,6 +52,7 @@import java.util.logging.Level;import java.util.logging.Logger;import jenkins.model.Jenkins;+import jenkins.util.SystemProperties;import org.apache.commons.discovery.ResourceClassIterator;import org.apache.commons.discovery.ResourceNameIterator;import org.apache.commons.discovery.resource.ClassLoaders;@@ -62,6 +64,7 @@import org.kohsuke.accmod.restrictions.NoExternalUse;import org.kohsuke.args4j.CmdLineException;import org.kohsuke.args4j.CmdLineParser;+import org.kohsuke.args4j.ParserProperties;import org.kohsuke.args4j.spi.OptionHandler;import org.springframework.security.access.AccessDeniedException;import org.springframework.security.authentication.BadCredentialsException;@@ -107,6 +110,16 @@*/@LegacyInstancesAreScopedToHudsonpublic abstract class CLICommand implements ExtensionPoint, Cloneable {++/**+* Boolean values to either allow or disallow parsing of @-prefixes.+* If a command line value starts with @, it is interpreted as being a file, loaded,+* and interpreted as if the file content would have been passed to the command line+*/+@SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "Accessible via System Groovy Scripts")+@Restricted(NoExternalUse.class)+public static boolean ALLOW_AT_SYNTAX = SystemProperties.getBoolean(CLICommand.class.getName() + ".allowAtSyntax");+/*** Connected to stdout and stderr of the CLI agent that initiated the session.* IOW, if you write to these streams, the person who launched the CLI command@@ -307,7 +320,8 @@ private void logAndPrintError(Throwable e, String errorMessage, String logMessag* @since 1.538*/protected CmdLineParser getCmdLineParser() {-return new CmdLineParser(this);+ParserProperties properties = ParserProperties.defaults().withAtSyntax(ALLOW_AT_SYNTAX);+return new CmdLineParser(this, properties);}/**
core/src/main/java/hudson/cli/declarative/CLIRegisterer.java+3 −1
@@ -59,6 +59,7 @@import org.jvnet.localizer.ResourceBundleHolder;import org.kohsuke.args4j.CmdLineException;import org.kohsuke.args4j.CmdLineParser;+import org.kohsuke.args4j.ParserProperties;import org.springframework.security.access.AccessDeniedException;import org.springframework.security.authentication.BadCredentialsException;import org.springframework.security.core.Authentication;@@ -131,7 +132,8 @@ protected CmdLineParser getCmdLineParser() {private CmdLineParser bindMethod(List<MethodBinder> binders) {registerOptionHandlers();-CmdLineParser parser = new CmdLineParser(null);+ParserProperties properties = ParserProperties.defaults().withAtSyntax(ALLOW_AT_SYNTAX);+CmdLineParser parser = new CmdLineParser(null, properties);// build up the call sequenceStack<Method> chains = new Stack<>();
test/src/test/java/jenkins/security/Security3314Test.java+55 −0
@@ -0,0 +1,55 @@+package jenkins.security;++import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;++import hudson.cli.CLICommandInvoker;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.Arrays;+import java.util.List;+import jenkins.model.Jenkins;+import org.junit.Rule;+import org.junit.Test;+import org.junit.runner.RunWith;+import org.junit.runners.Parameterized;+import org.jvnet.hudson.test.JenkinsRule;++@RunWith(Parameterized.class)+public class Security3314Test {+private String commandName;++@Rule+public final JenkinsRule j = new JenkinsRule();++/**+* connect-node to test the CLICommand behavior+* disable-job to test the CLIRegisterer behavior (@CLIMethod)+*/+@Parameterized.Parameters+public static List<String> commands() {+return Arrays.asList("connect-node", "disable-job");+}++public Security3314Test(String commandName) {+this.commandName = commandName;+}++@Test+public void commandShouldNotParseAt() throws Exception {+CLICommandInvoker command = new CLICommandInvoker(j, commandName);++Path tempPath = Files.createTempFile("tempFile", ".txt");+tempPath.toFile().deleteOnExit();+String content = "AtGotParsed";+Files.write(tempPath, content.getBytes());++final CLICommandInvoker.Result result = command+.authorizedTo(Jenkins.READ)+.invokeWithArgs("@" + tempPath);++assertThat(result.stderr(), containsString("@" + tempPath));+assertThat(result.stderr(), not(containsString("AtGotParsed")));+}+}
References
- ADVISORYhttps://nvd.nist.gov/vuln/detail/CVE-2024-23897
- WEBhttps://github.com/jenkinsci/jenkins/commit/554f03782057c499c49bbb06575f0d28b5200edb
- PACKAGEhttps://github.com/jenkinsci/jenkins
- WEBhttps://www.cisa.gov/known-exploited-vulnerabilities-catalog?field_cve=CVE-2024-23897
- WEBhttps://www.jenkins.io/changelog-stable/#v2.440.1
- WEBhttps://www.jenkins.io/security/advisory/2024-01-24/#SECURITY-3314
- WEBhttps://www.sonarsource.com/blog/excessive-expansion-uncovering-critical-security-vulnerabilities-in-jenkins
- WEBhttps://www.vicarius.io/vsociety/posts/the-anatomy-of-a-jenkins-vulnerability-cve-2024-23897-revealed-1
- WEBhttp://packetstormsecurity.com/files/176839/Jenkins-2.441-LTS-2.426.3-CVE-2024-23897-Scanner.html
- WEBhttp://packetstormsecurity.com/files/176840/Jenkins-2.441-LTS-2.426.3-Arbitrary-File-Read.html
- WEBhttp://www.openwall.com/lists/oss-security/2024/01/24/6