Skip to content

Commit 6450fe0

Browse files
committed
PDFBOX-6189: Safely validate dimension to avoid OOM, by subbudvk; closes #437
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/branches/2.0@1932960 13f79535-47bb-0310-9956-ffa450edef68
1 parent b930912 commit 6450fe0

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/filter/CCITTFaxFilter.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,43 @@ public DecodeResult decode(InputStream encoded, OutputStream decoded,
6060
// decompress data
6161
int k = decodeParms.getInt(COSName.K, 0);
6262
boolean encodedByteAlign = decodeParms.getBoolean(COSName.ENCODED_BYTE_ALIGN, false);
63-
int arraySize = (cols + 7) / 8 * rows;
64-
// TODO possible options??
63+
if (cols <= 0 || rows <= 0)
64+
{
65+
throw new IOException("Invalid CCITT image dimensions: cols=" + cols + ", rows=" + rows);
66+
}
67+
68+
long arraySizeLong = ((long) cols + 7) / 8 * rows;
69+
70+
long maxBytes = 256 * 1024 * 1024L;
71+
String sysProp = System.getProperty(Filter.SYSPROP_CCITTFAX_MAXBYTES);
72+
73+
if (sysProp != null)
74+
{
75+
try
76+
{
77+
long parsed = Long.parseLong(sysProp);
78+
if (parsed > 0)
79+
{
80+
maxBytes = parsed;
81+
}
82+
// else ignore zero/negative values
83+
}
84+
catch (NumberFormatException e)
85+
{
86+
// ignore invalid value, keep default
87+
}
88+
}
89+
90+
if (arraySizeLong > maxBytes)
91+
{
92+
throw new IOException(
93+
"CCITT decode buffer too large (" + arraySizeLong + " bytes) for cols=" + cols +
94+
", rows=" + rows + "; max allowed=" + maxBytes +
95+
"; increase " + Filter.SYSPROP_CCITTFAX_MAXBYTES + " to override"
96+
);
97+
}
98+
99+
int arraySize = (int) arraySizeLong;
65100
byte[] decompressed = new byte[arraySize];
66101
CCITTFaxDecoderStream s;
67102
int type;

pdfbox/src/main/java/org/apache/pdfbox/filter/Filter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public abstract class Filter
5050
*/
5151
public static final String SYSPROP_DEFLATELEVEL = "org.apache.pdfbox.filter.deflatelevel";
5252

53+
/**
54+
* CCITTFax decode buffer size cap System Property. Sets the maximum number of bytes that
55+
* CCITTFaxFilter is allowed to pre-allocate for a single image decode buffer. PDF-controlled
56+
* /Columns and /Rows values are validated against this limit before allocation to prevent
57+
* denial-of-service via crafted image dimensions. The default is 256 MB. To raise the cap for
58+
* high-resolution legitimate documents, use
59+
* {@code System.setProperty(Filter.SYSPROP_CCITTFAX_MAXBYTES, String.valueOf(512 * 1024 * 1024L));}
60+
*/
61+
public static final String SYSPROP_CCITTFAX_MAXBYTES = "org.apache.pdfbox.filter.ccittmaxbytes";
62+
5363
/**
5464
* Constructor.
5565
*/

0 commit comments

Comments
 (0)