|
| 1 | +/* ==================================================================== |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + contributor license agreements. See the NOTICE file distributed with |
| 4 | + this work for additional information regarding copyright ownership. |
| 5 | + The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + (the "License"); you may not use this file except in compliance with |
| 7 | + the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +==================================================================== */ |
| 17 | +package org.apache.poi.hpsf; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | + |
| 21 | +import org.apache.poi.util.LittleEndian; |
| 22 | +import org.apache.poi.util.LittleEndianByteArrayInputStream; |
| 23 | +import org.apache.poi.util.RecordFormatException; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * Verifies that HPSF readers reject crafted lengths whose subsequent |
| 28 | + * allocation-size arithmetic would silently overflow signed integer math. |
| 29 | + */ |
| 30 | +class TestOverflowHardening { |
| 31 | + |
| 32 | + /** |
| 33 | + * Reproduces the int*2 multiplication overflow in {@link UnicodeString#read}: |
| 34 | + * a length of {@code 0x40000001} produces {@code length*2 == 0x80000002}, |
| 35 | + * which wraps to a negative int. {@link Math#multiplyExact} now surfaces |
| 36 | + * the overflow as an {@link ArithmeticException} at the parser, where the |
| 37 | + * malformed field actually lives. |
| 38 | + */ |
| 39 | + @Test |
| 40 | + void unicodeStringLengthMultiplicationOverflowRejected() { |
| 41 | + byte[] data = new byte[4]; |
| 42 | + LittleEndian.putInt(data, 0, 0x40000001); |
| 43 | + LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(data, 0); |
| 44 | + |
| 45 | + UnicodeString us = new UnicodeString(); |
| 46 | + assertThrows(RecordFormatException.class, () -> us.read(lei)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Reproduces the long*long multiplication overflow in |
| 51 | + * {@link Array.ArrayHeader#getNumberOfScalarValues}: with three dimensions |
| 52 | + * of size {@code 0x80000000} the unchecked product |
| 53 | + * {@code 2^31 * 2^31 * 2^31 = 2^93} wraps inside a 64-bit long and the |
| 54 | + * subsequent {@code > Integer.MAX_VALUE} guard at {@code Array.read} is |
| 55 | + * silently bypassed. {@link Math#multiplyExact} now rejects the crafted |
| 56 | + * array header with an {@link ArithmeticException}. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + void arrayDimensionMultiplicationOverflowRejected() { |
| 60 | + // ArrayHeader layout: type (4) + numDimensions (4) + numDimensions * (size:4 + indexOffset:4) |
| 61 | + // 3 dimensions of size 0x80000000 each -> product overflows 2^63 (long max) |
| 62 | + byte[] data = new byte[4 + 4 + 3 * (4 + 4)]; |
| 63 | + int off = 0; |
| 64 | + LittleEndian.putInt(data, off, Variant.VT_I4); |
| 65 | + off += 4; |
| 66 | + LittleEndian.putInt(data, off, 3); |
| 67 | + off += 4; |
| 68 | + for (int i = 0; i < 3; i++) { |
| 69 | + LittleEndian.putUInt(data, off, 0x80000000L); |
| 70 | + off += 4; |
| 71 | + LittleEndian.putInt(data, off, 0); |
| 72 | + off += 4; |
| 73 | + } |
| 74 | + LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(data, 0); |
| 75 | + |
| 76 | + Array a = new Array(); |
| 77 | + assertThrows(RecordFormatException.class, () -> a.read(lei)); |
| 78 | + } |
| 79 | +} |
0 commit comments