|
20 | 20 |
|
21 | 21 | import net.jpountz.lz4.LZ4Exception; |
22 | 22 | import net.jpountz.lz4.LZ4FastDecompressor; |
| 23 | +import net.jpountz.util.ByteBufferUtils; |
23 | 24 |
|
24 | 25 | import java.nio.ByteBuffer; |
25 | 26 |
|
| 27 | +import static org.elasticsearch.lz4.LZ4Constants.COPY_LENGTH; |
| 28 | +import static org.elasticsearch.lz4.LZ4Constants.ML_BITS; |
| 29 | +import static org.elasticsearch.lz4.LZ4Constants.RUN_MASK; |
| 30 | +import static org.elasticsearch.lz4.LZ4Utils.notEnoughSpace; |
| 31 | + |
26 | 32 | /** |
27 | | - * This file is forked from https://github.com/lz4/lz4-java. In particular, it forks the following file |
28 | | - * net.jpountz.lz4.LZ4JavaSafeFastDecompressor. |
29 | | - * |
30 | | - * It modifies the original implementation to use custom LZ4SafeUtils and SafeUtils implementations which |
31 | | - * include performance improvements. |
| 33 | + * This file is a vendored version of {@code net.jpountz.lz4.LZ4JavaSafeFastDecompressor} from |
| 34 | + * <a href="https://github.com/yawkat/lz4-java">yawkat/lz4-java</a>. To obtain the original file, check out the {@code lz4-java} repository |
| 35 | + * and run {@code mvn clean install} which will generate the original source of this class at |
| 36 | + * {@code target/generated-sources/mvel/net/jpountz/lz4/LZ4JavaSafeFastDecompressor.java}. |
| 37 | + * <p> |
| 38 | + * It modifies the original implementation to use local {@link LZ4SafeUtils} and {@link SafeUtils} implementations which include some |
| 39 | + * performance optimisations, and it also drops support for decompressing data from a direct (non-heap) {@link ByteBuffer}. |
| 40 | + * <p> |
| 41 | + * Differences from the original are annotated with [ES change from upstream] |
32 | 42 | */ |
33 | 43 | public class ESLZ4Decompressor extends LZ4FastDecompressor { |
34 | 44 | public static final LZ4FastDecompressor INSTANCE = new ESLZ4Decompressor(); |
35 | 45 |
|
36 | | - ESLZ4Decompressor() {} |
| 46 | + private ESLZ4Decompressor() {} |
| 47 | + |
| 48 | + @Override |
| 49 | + public int decompress(byte[] src, final int srcOff, byte[] dest, final int destOff, int destLen) { |
| 50 | + |
| 51 | + final int srcEnd = src.length; |
| 52 | + |
| 53 | + return decompress(src, srcOff, srcEnd - srcOff, dest, destOff, destLen); |
| 54 | + } |
37 | 55 |
|
38 | | - public int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int destLen) { |
39 | | - SafeUtils.checkRange(src, srcOff); |
| 56 | + private int decompress(byte[] src, final int srcOff, final int srcLen, byte[] dest, final int destOff, int destLen) { |
| 57 | + SafeUtils.checkRange(src, srcOff, srcLen); |
40 | 58 | SafeUtils.checkRange(dest, destOff, destLen); |
| 59 | + |
41 | 60 | if (destLen == 0) { |
42 | | - if (SafeUtils.readByte(src, srcOff) != 0) { |
| 61 | + // Allow `srcLen > 1` despite just one byte being consumed since this 'fast' decompressor does not have to fully consume the src |
| 62 | + if (srcLen < 1 || SafeUtils.readByte(src, srcOff) != 0) { |
43 | 63 | throw new LZ4Exception("Malformed input at " + srcOff); |
44 | | - } else { |
45 | | - return 1; |
46 | 64 | } |
47 | | - } else { |
48 | | - int destEnd = destOff + destLen; |
49 | | - int sOff = srcOff; |
50 | | - int dOff = destOff; |
51 | | - |
52 | | - while (true) { |
53 | | - int token = SafeUtils.readByte(src, sOff) & 255; |
54 | | - ++sOff; |
55 | | - int literalLen = token >>> 4; |
56 | | - if (literalLen == 15) { |
57 | | - byte len; |
58 | | - for (boolean var11 = true; (len = SafeUtils.readByte(src, sOff++)) == -1; literalLen += 255) { |
59 | | - } |
| 65 | + return 1; |
| 66 | + } |
60 | 67 |
|
61 | | - literalLen += len & 255; |
62 | | - } |
| 68 | + final int srcEnd = srcOff + srcLen; |
| 69 | + final int destEnd = destOff + destLen; |
63 | 70 |
|
64 | | - int literalCopyEnd = dOff + literalLen; |
65 | | - if (literalCopyEnd > destEnd - 8) { |
66 | | - if (literalCopyEnd != destEnd) { |
67 | | - throw new LZ4Exception("Malformed input at " + sOff); |
68 | | - } else { |
69 | | - LZ4SafeUtils.safeArraycopy(src, sOff, dest, dOff, literalLen); |
70 | | - sOff += literalLen; |
71 | | - return sOff - srcOff; |
| 71 | + int sOff = srcOff; |
| 72 | + int dOff = destOff; |
| 73 | + |
| 74 | + while (true) { |
| 75 | + if (sOff >= srcEnd) { |
| 76 | + throw new LZ4Exception("Malformed input at " + sOff); |
| 77 | + } |
| 78 | + final int token = SafeUtils.readByte(src, sOff) & 0xFF; |
| 79 | + ++sOff; |
| 80 | + |
| 81 | + // literals |
| 82 | + int literalLen = token >>> ML_BITS; |
| 83 | + if (literalLen == RUN_MASK) { |
| 84 | + byte len = (byte) 0xFF; |
| 85 | + while (sOff < srcEnd && (len = SafeUtils.readByte(src, sOff++)) == (byte) 0xFF) { |
| 86 | + literalLen += 0xFF; |
| 87 | + if (literalLen < 0) { |
| 88 | + throw new LZ4Exception("Too large literalLen"); |
72 | 89 | } |
73 | 90 | } |
| 91 | + literalLen += len & 0xFF; |
| 92 | + } |
| 93 | + |
| 94 | + final int literalCopyEnd = dOff + literalLen; |
| 95 | + // Check for overflow |
| 96 | + if (literalCopyEnd < dOff) { |
| 97 | + throw new LZ4Exception("Too large literalLen"); |
| 98 | + } |
74 | 99 |
|
75 | | - LZ4SafeUtils.wildArraycopy(src, sOff, dest, dOff, literalLen); |
76 | | - sOff += literalLen; |
77 | | - int matchDec = SafeUtils.readShortLE(src, sOff); |
78 | | - sOff += 2; |
79 | | - int matchOff = literalCopyEnd - matchDec; |
80 | | - if (matchOff < destOff) { |
| 100 | + if (notEnoughSpace(destEnd - literalCopyEnd, COPY_LENGTH) || notEnoughSpace(srcEnd - sOff, COPY_LENGTH + literalLen)) { |
| 101 | + |
| 102 | + if (literalCopyEnd != destEnd) { |
| 103 | + throw new LZ4Exception("Malformed input at " + sOff); |
| 104 | + } else if (notEnoughSpace(srcEnd - sOff, literalLen)) { |
81 | 105 | throw new LZ4Exception("Malformed input at " + sOff); |
| 106 | + |
| 107 | + } else { |
| 108 | + LZ4SafeUtils.safeArraycopy(src, sOff, dest, dOff, literalLen); |
| 109 | + sOff += literalLen; |
| 110 | + dOff = literalCopyEnd; |
| 111 | + break; // EOF |
82 | 112 | } |
| 113 | + } |
83 | 114 |
|
84 | | - int matchLen = token & 15; |
85 | | - if (matchLen == 15) { |
86 | | - byte len; |
87 | | - for (boolean var15 = true; (len = SafeUtils.readByte(src, sOff++)) == -1; matchLen += 255) { |
88 | | - } |
| 115 | + LZ4SafeUtils.wildArraycopy(src, sOff, dest, dOff, literalLen); |
| 116 | + sOff += literalLen; |
| 117 | + dOff = literalCopyEnd; |
89 | 118 |
|
90 | | - matchLen += len & 255; |
91 | | - } |
| 119 | + // matchs |
| 120 | + final int matchDec = SafeUtils.readShortLE(src, sOff); |
| 121 | + sOff += 2; |
| 122 | + int matchOff = dOff - matchDec; |
92 | 123 |
|
93 | | - matchLen += 4; |
94 | | - int matchCopyEnd = literalCopyEnd + matchLen; |
95 | | - if (matchCopyEnd > destEnd - 8) { |
96 | | - if (matchCopyEnd > destEnd) { |
97 | | - throw new LZ4Exception("Malformed input at " + sOff); |
98 | | - } |
| 124 | + if (matchOff < destOff) { |
| 125 | + throw new LZ4Exception("Malformed input at " + sOff); |
| 126 | + } |
99 | 127 |
|
100 | | - LZ4SafeUtils.safeIncrementalCopy(dest, matchOff, literalCopyEnd, matchLen); |
101 | | - } else { |
102 | | - LZ4SafeUtils.wildIncrementalCopy(dest, matchOff, literalCopyEnd, matchCopyEnd); |
| 128 | + int matchLen = token & LZ4Constants.ML_MASK; |
| 129 | + if (matchLen == LZ4Constants.ML_MASK) { |
| 130 | + byte len = (byte) 0xFF; |
| 131 | + while (sOff < srcEnd && (len = SafeUtils.readByte(src, sOff++)) == (byte) 0xFF) { |
| 132 | + matchLen += 0xFF; |
| 133 | + if (matchLen < 0) { |
| 134 | + throw new LZ4Exception("Too large matchLen"); |
| 135 | + } |
103 | 136 | } |
| 137 | + matchLen += len & 0xFF; |
| 138 | + } |
| 139 | + matchLen += LZ4Constants.MIN_MATCH; |
| 140 | + |
| 141 | + final int matchCopyEnd = dOff + matchLen; |
| 142 | + // Check for overflow |
| 143 | + if (matchCopyEnd < dOff) { |
| 144 | + throw new LZ4Exception("Too large matchLen"); |
| 145 | + } |
104 | 146 |
|
105 | | - dOff = matchCopyEnd; |
| 147 | + if (matchDec == 0) { |
| 148 | + if (matchCopyEnd > destEnd) { |
| 149 | + throw new LZ4Exception("Malformed input at " + sOff); |
| 150 | + } |
| 151 | + // With matchDec == 0, matchOff == dOff, so we'd copy in place. Zero the data instead. (CVE-2025-66566) |
| 152 | + assert matchOff == dOff; // should always hold, but this extra check will trigger during fuzzing if my logic is wrong |
| 153 | + LZ4Utils.zero(dest, dOff, matchCopyEnd); |
| 154 | + } else if (notEnoughSpace(destEnd - matchCopyEnd, COPY_LENGTH)) { |
| 155 | + if (matchCopyEnd > destEnd) { |
| 156 | + throw new LZ4Exception("Malformed input at " + sOff); |
| 157 | + } |
| 158 | + LZ4SafeUtils.safeIncrementalCopy(dest, matchOff, dOff, matchLen); |
| 159 | + } else { |
| 160 | + LZ4SafeUtils.wildIncrementalCopy(dest, matchOff, dOff, matchCopyEnd); |
106 | 161 | } |
| 162 | + dOff = matchCopyEnd; |
107 | 163 | } |
| 164 | + |
| 165 | + return sOff - srcOff; |
| 166 | + |
108 | 167 | } |
109 | 168 |
|
110 | | - public int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, int destLen) { |
| 169 | + @Override |
| 170 | + public int decompress(ByteBuffer src, final int srcOff, ByteBuffer dest, final int destOff, int destLen) { |
| 171 | + |
| 172 | + final int srcEnd = src.capacity(); |
| 173 | + |
| 174 | + return decompress(src, srcOff, srcEnd - srcOff, dest, destOff, destLen); |
| 175 | + } |
| 176 | + |
| 177 | + private int decompress(ByteBuffer src, final int srcOff, final int srcLen, ByteBuffer dest, final int destOff, int destLen) { |
| 178 | + ByteBufferUtils.checkRange(src, srcOff, srcLen); |
| 179 | + ByteBufferUtils.checkRange(dest, destOff, destLen); |
| 180 | + |
111 | 181 | if (src.hasArray() && dest.hasArray()) { |
112 | | - return this.decompress(src.array(), srcOff + src.arrayOffset(), dest.array(), destOff + dest.arrayOffset(), destLen); |
113 | | - } else { |
114 | | - throw new AssertionError("Do not support decompression on direct buffers"); |
| 182 | + return decompress(src.array(), srcOff + src.arrayOffset(), srcLen, dest.array(), destOff + dest.arrayOffset(), destLen); |
115 | 183 | } |
| 184 | + |
| 185 | + // [ES change from upstream]: remove unused code |
| 186 | + throw new AssertionError("Do not support decompression on direct buffers"); |
116 | 187 | } |
117 | 188 | } |
0 commit comments