Security context
Medium· 6.5GHSA-cxqw-vjcr-gp5g CVE-2021-21607CWE-770Published May 24, 2022

Excessive memory allocation in graph URLs leads to denial of service in Jenkins

Research this vulnerability

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

0 → fixed in 2.263.22.264 → fixed in 2.275

Details

Jenkins renders several different graphs for features like agent and label usage statistics, memory usage, or various plugin-provided statistics. Jenkins 2.274 and earlier, LTS 2.263.1 and earlier does not limit the graph size provided as query parameters. This allows attackers to request or to have legitimate Jenkins users request crafted URLs that rapidly use all available memory in Jenkins, potentially leading to out of memory errors. Jenkins 2.275, LTS 2.263.2 limits the maximum size of graphs to an area of 10 million pixels. If a larger size is requested, the default size for the graph will be rendered instead. This threshold can be configured by setting the [Java system property](https://www.jenkins.io/doc/book/managing/system-properties/) `hudson.util.Graph.maxArea` to a different number on startup.

The fix

[SECURITY-2025]

Daniel Beck· Jan 11, 2021, 04:50 PM+791a890d68699
core/src/main/java/hudson/util/Graph.java+21 1
@@ -23,10 +23,14 @@
*/
package hudson.util;
+import com.google.common.annotations.VisibleForTesting;
+import jenkins.util.SystemProperties;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.plot.Plot;
+import org.kohsuke.accmod.Restricted;
+import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
@@ -56,6 +60,9 @@
* @since 1.320
*/
public abstract class Graph {
+ @Restricted(NoExternalUse.class)
+ /* package for test */ static /* non-final for script console */ int MAX_AREA = SystemProperties.getInteger(Graph.class.getName() + ".maxArea", 10_000_000); // 4k*2.5k
+
private final long timestamp;
private final int defaultW;
private final int defaultH;
@@ -95,7 +102,20 @@ private BufferedImage render(StaplerRequest req, ChartRenderingInfo info) {
Plot p = graph.getPlot();
p.setBackgroundPaint(plotBg);
- return graph.createBufferedImage(Integer.parseInt(w),Integer.parseInt(h),info);
+ int width = Integer.parseInt(w);
+ int height = Integer.parseInt(h);
+ Dimension safeDimension = safeDimension(width, height, defaultW, defaultH);
+ return graph.createBufferedImage(safeDimension.width, safeDimension.height, info);
+ }
+
+ @Restricted(NoExternalUse.class)
+ @VisibleForTesting
+ public static Dimension safeDimension(int width, int height, int defaultWidth, int defaultHeight) {
+ if (width <= 0 || height <= 0 || width > MAX_AREA/height) {
+ width = defaultWidth;
+ height = defaultHeight;
+ }
+ return new Dimension(width, height);
}
@NonNull private static Color stringToColor(@CheckForNull String s) {
core/src/test/java/hudson/util/Security2025GraphTest.java+58 0
@@ -0,0 +1,58 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2020, CloudBees, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package hudson.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.awt.*;
+
+public class Security2025GraphTest {
+
+ public static final int DEFAULT_W = 400;
+ public static final int DEFAULT_H = 300;
+
+ @Test
+ public void testDimensions() throws Exception {
+ final Dimension keep = Graph.safeDimension(Graph.MAX_AREA/1_000, 1000, DEFAULT_W, DEFAULT_H);
+ Assert.assertEquals(Graph.MAX_AREA/1_000, keep.width);
+ Assert.assertEquals(1_000, keep.height);
+
+ final Dimension keep2 = Graph.safeDimension(Graph.MAX_AREA/2, 2, DEFAULT_W, DEFAULT_H);
+ Assert.assertEquals(Graph.MAX_AREA/2, keep2.width);
+ Assert.assertEquals(2, keep2.height);
+
+ final Dimension resetArea = Graph.safeDimension(Graph.MAX_AREA, Graph.MAX_AREA, DEFAULT_W, DEFAULT_H);
+ Assert.assertEquals(DEFAULT_W, resetArea.width);
+ Assert.assertEquals(DEFAULT_H, resetArea.height);
+
+ final Dimension resetNegativeWidth = Graph.safeDimension(-50, 1000, DEFAULT_W, DEFAULT_H);
+ Assert.assertEquals(DEFAULT_W, resetNegativeWidth.width);
+ Assert.assertEquals(DEFAULT_H, resetNegativeWidth.height);
+
+ final Dimension resetNegativeHeight = Graph.safeDimension(1000, -50, DEFAULT_W, DEFAULT_H);
+ Assert.assertEquals(DEFAULT_W, resetNegativeHeight.width);
+ Assert.assertEquals(DEFAULT_H, resetNegativeHeight.height);
+ }
+}

References