|
| 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 | + |
| 18 | +package org.apache.spark.sql.execution.datasources.parquet |
| 19 | + |
| 20 | +import java.io.ByteArrayOutputStream |
| 21 | +import java.nio.ByteBuffer |
| 22 | + |
| 23 | +import org.apache.parquet.bytes.ByteBufferInputStream |
| 24 | +import org.apache.parquet.io.ParquetDecodingException |
| 25 | + |
| 26 | +import org.apache.spark.SparkFunSuite |
| 27 | + |
| 28 | +/** |
| 29 | + * Group headers in a Parquet page body are attacker-controlled if the file is, so |
| 30 | + * `readNextGroup` validates them against the page's declared value count and remaining |
| 31 | + * byte budget before a count can size an allocation or drive loop control. |
| 32 | + */ |
| 33 | +class VectorizedRleValuesReaderSuite extends SparkFunSuite { |
| 34 | + |
| 35 | + import VectorizedRleValuesReaderSuite._ |
| 36 | + |
| 37 | + test("invalid PACKED header: overflowing value count is rejected") { |
| 38 | + // numGroups = 2^28 overflows `numGroups * 8` to a negative Int currentCount, which |
| 39 | + // would send readBatch's leftInPage accounting backwards. Assert on the message: |
| 40 | + // without the validation this also throws, but as a wrapped end-of-stream failure. |
| 41 | + val e = intercept[ParquetDecodingException] { |
| 42 | + packedPageReader(numGroups = 1 << 28, pageValueCount = 1024).readInteger() |
| 43 | + } |
| 44 | + assert(e.getMessage.contains("Invalid bit-packed run")) |
| 45 | + } |
| 46 | + |
| 47 | + test("invalid PACKED header: count beyond the page byte budget is rejected pre-allocation") { |
| 48 | + // numGroups = 2^27 would allocate a ~4GB int buffer for a page with only a handful |
| 49 | + // of bytes left. |
| 50 | + val e = intercept[ParquetDecodingException] { |
| 51 | + packedPageReader(numGroups = 1 << 27, pageValueCount = 1024).readInteger() |
| 52 | + } |
| 53 | + assert(e.getMessage.contains("Invalid bit-packed run")) |
| 54 | + } |
| 55 | + |
| 56 | + test("invalid RLE header: run longer than the page's value count is rejected") { |
| 57 | + val out = new ByteArrayOutputStream() |
| 58 | + writeUnsignedVarInt(out, 1L << 21) // RLE header (LSB 0): run of 2^20 values |
| 59 | + out.write(1) // the repeated value (bitWidth 1 -> one byte) |
| 60 | + val reader = new VectorizedRleValuesReader(1, false) |
| 61 | + reader.initFromPage(1024, ByteBufferInputStream.wrap(ByteBuffer.wrap(out.toByteArray))) |
| 62 | + val e = intercept[ParquetDecodingException] { |
| 63 | + reader.readInteger() |
| 64 | + } |
| 65 | + assert(e.getMessage.contains("Invalid RLE run")) |
| 66 | + } |
| 67 | + |
| 68 | + test("truncated varint header is rejected") { |
| 69 | + // A lone continuation byte: read() returns -1 at end of input, whose set 0x80 bit |
| 70 | + // would otherwise keep the varint continuation loop alive. |
| 71 | + val out = new ByteArrayOutputStream() |
| 72 | + out.write(0x80) |
| 73 | + val reader = new VectorizedRleValuesReader(1, false) |
| 74 | + reader.initFromPage(1024, ByteBufferInputStream.wrap(ByteBuffer.wrap(out.toByteArray))) |
| 75 | + intercept[ParquetDecodingException] { |
| 76 | + reader.readInteger() |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + test("PACKED: value count not a multiple of 8 is accepted (final group is zero-padded)") { |
| 81 | + // The last bit-packed group is padded to a multiple of 8 values, so a page of 1023 |
| 82 | + // values legitimately carries a run counting 1024 -- the validation's +7 allowance. |
| 83 | + // Guards against the bound being too tight to read valid files. |
| 84 | + val numGroups = 128 |
| 85 | + val out = new ByteArrayOutputStream() |
| 86 | + writeUnsignedVarInt(out, (numGroups.toLong << 1) | 1L) |
| 87 | + out.write(Array.fill[Byte](numGroups)(0x55)) // bitWidth 1 -> one byte per group |
| 88 | + val reader = new VectorizedRleValuesReader(1, false) |
| 89 | + reader.initFromPage(1023, ByteBufferInputStream.wrap(ByteBuffer.wrap(out.toByteArray))) |
| 90 | + reader.readInteger() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +private object VectorizedRleValuesReaderSuite { |
| 95 | + |
| 96 | + /** |
| 97 | + * Builds a reader over a page whose body starts with a PACKED header (LSB 1) declaring |
| 98 | + * `numGroups` bit-packed groups of 8 values each, followed by far fewer bytes than the |
| 99 | + * numGroups * bitWidth the run requires. |
| 100 | + */ |
| 101 | + private def packedPageReader( |
| 102 | + numGroups: Int, |
| 103 | + pageValueCount: Int): VectorizedRleValuesReader = { |
| 104 | + val out = new ByteArrayOutputStream() |
| 105 | + writeUnsignedVarInt(out, (numGroups.toLong << 1) | 1L) |
| 106 | + out.write(Array[Byte](0, 0, 0, 0)) |
| 107 | + val reader = new VectorizedRleValuesReader(1, false) |
| 108 | + reader.initFromPage( |
| 109 | + pageValueCount, ByteBufferInputStream.wrap(ByteBuffer.wrap(out.toByteArray))) |
| 110 | + reader |
| 111 | + } |
| 112 | + |
| 113 | + private def writeUnsignedVarInt(out: ByteArrayOutputStream, value: Long): Unit = { |
| 114 | + var v = value |
| 115 | + while ((v & ~0x7fL) != 0) { |
| 116 | + out.write(((v & 0x7f) | 0x80).toInt) |
| 117 | + v >>>= 7 |
| 118 | + } |
| 119 | + out.write(v.toInt) |
| 120 | + } |
| 121 | +} |
0 commit comments