|
| 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.tika.parser.mp4; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | + |
| 22 | +import com.drew.imaging.mp4.Mp4Handler; |
| 23 | +import com.drew.lang.StreamReader; |
| 24 | +import com.drew.metadata.mp4.Mp4BoxHandler; |
| 25 | +import com.drew.metadata.mp4.Mp4Context; |
| 26 | +import com.drew.metadata.mp4.Mp4MediaHandler; |
| 27 | + |
| 28 | +/** |
| 29 | + * A size-bounded reimplementation of com.drew.imaging.mp4.Mp4Reader. |
| 30 | + * <p> |
| 31 | + * The metadata-extractor reader eagerly does {@code new byte[(int) boxSize - 8]} |
| 32 | + * for every box a handler accepts, with {@code boxSize} attacker-controlled and |
| 33 | + * capped only at {@code Integer.MAX_VALUE} (~2GB), and {@code StreamReader.getBytes} |
| 34 | + * allocates before checking how much data is actually present. A single crafted |
| 35 | + * box header therefore forces a multi-GB allocation. This reader is identical to |
| 36 | + * the library's box walk except that an accepted box whose payload exceeds |
| 37 | + * {@code maxBoxSize} is skipped (a lazy stream advance, no allocation) instead of |
| 38 | + * being read. Boxes the handler does not accept were already skipped by the |
| 39 | + * library, so this only bounds the boxes we opt into. See TIKA-4812. |
| 40 | + */ |
| 41 | +final class TikaMp4Reader { |
| 42 | + |
| 43 | + private TikaMp4Reader() { |
| 44 | + } |
| 45 | + |
| 46 | + //MP4 containers nest (moov/trak/mdia/minf/stbl/udta/meta); cap the recursion so a |
| 47 | + //crafted chain of nested container headers cannot overflow the stack (an uncaught |
| 48 | + //Error, caught by neither the IOException handler below nor CompositeParser). Real |
| 49 | + //files nest well under this. |
| 50 | + private static final int MAX_BOX_DEPTH = 100; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param inputLength total input length in bytes, or -1 if unknown. When known, a box |
| 54 | + * that declares more payload than the input holds is skipped rather |
| 55 | + * than allocated (StreamReader.getBytes allocates before reading). |
| 56 | + */ |
| 57 | + static void extract(InputStream inputStream, Mp4BoxHandler handler, long maxBoxSize, |
| 58 | + long inputLength) { |
| 59 | + StreamReader reader = new StreamReader(inputStream); |
| 60 | + reader.setMotorolaByteOrder(true); |
| 61 | + processBoxes(reader, -1, handler, new Mp4Context(), maxBoxSize, inputLength, 0); |
| 62 | + } |
| 63 | + |
| 64 | + private static void processBoxes(StreamReader reader, long atomEnd, Mp4Handler<?> handler, |
| 65 | + Mp4Context context, long maxBoxSize, long inputLength, |
| 66 | + int depth) { |
| 67 | + if (depth > MAX_BOX_DEPTH) { |
| 68 | + handler.addError("MP4 box nesting exceeds the maximum depth of " + MAX_BOX_DEPTH); |
| 69 | + return; |
| 70 | + } |
| 71 | + try { |
| 72 | + while (atomEnd == -1 || reader.getPosition() < atomEnd) { |
| 73 | + long boxSize = reader.getUInt32(); |
| 74 | + String boxType = reader.getString(4); |
| 75 | + //4 bytes size + 4 bytes type, plus 8 more when a 64-bit largesize follows |
| 76 | + int headerSize = boxSize == 1 ? 16 : 8; |
| 77 | + if (headerSize == 16) { |
| 78 | + boxSize = reader.getInt64(); |
| 79 | + } |
| 80 | + if (boxSize > Integer.MAX_VALUE) { |
| 81 | + handler.addError("Box size too large."); |
| 82 | + break; |
| 83 | + } |
| 84 | + if (boxSize < headerSize) { |
| 85 | + handler.addError("Box size too small."); |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + long payloadLength = boxSize - headerSize; |
| 90 | + if (acceptContainer(handler, boxType)) { |
| 91 | + processBoxes(reader, reader.getPosition() + payloadLength, |
| 92 | + processBox(handler, boxType, null, boxSize, context), context, |
| 93 | + maxBoxSize, inputLength, depth + 1); |
| 94 | + } else if (acceptBox(handler, boxType)) { |
| 95 | + //StreamReader.getBytes allocates the whole payload up front, so skip |
| 96 | + //(a lazy stream advance) any box over the cap, or one that claims more |
| 97 | + //than the input holds, instead of allocating it. Skip-and-continue is |
| 98 | + //deliberate: unlike the TikaMemoryLimitException other parsers throw, |
| 99 | + //this keeps the remaining boxes' metadata; the skip is recorded as a |
| 100 | + //warning via the directory's error list. |
| 101 | + boolean tooLarge = payloadLength > maxBoxSize; |
| 102 | + boolean beyondInput = inputLength >= 0 |
| 103 | + && reader.getPosition() + payloadLength > inputLength; |
| 104 | + if (tooLarge || beyondInput) { |
| 105 | + handler.addError("MP4 box '" + boxType + "' payload (" + payloadLength |
| 106 | + + " bytes) exceeds the " |
| 107 | + + (tooLarge ? "maximum of " + maxBoxSize + " bytes" : "input size") |
| 108 | + + "; skipping."); |
| 109 | + reader.skip(payloadLength); |
| 110 | + } else { |
| 111 | + handler = processBox(handler, boxType, |
| 112 | + reader.getBytes((int) payloadLength), boxSize, context); |
| 113 | + } |
| 114 | + } else { |
| 115 | + reader.skip(payloadLength); |
| 116 | + } |
| 117 | + } |
| 118 | + } catch (IOException e) { |
| 119 | + handler.addError(e.getMessage() == null ? "IOException reading MP4 boxes" |
| 120 | + : e.getMessage()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + //the box walk holds handlers as Mp4Handler, whose accept/process methods are |
| 125 | + //protected; every concrete handler in play (Mp4BoxHandler-rooted, or an |
| 126 | + //Mp4MediaHandler track handler swapped in on 'hdlr') widens them to public, |
| 127 | + //so dispatch through whichever of the two families the instance belongs to. |
| 128 | + //A container's handler is obtained with processBox(type, null, ...), which is |
| 129 | + //exactly what the library's protected processContainer does. |
| 130 | + |
| 131 | + private static boolean acceptContainer(Mp4Handler<?> handler, String type) { |
| 132 | + return handler instanceof Mp4BoxHandler |
| 133 | + ? ((Mp4BoxHandler) handler).shouldAcceptContainer(type) |
| 134 | + : ((Mp4MediaHandler<?>) handler).shouldAcceptContainer(type); |
| 135 | + } |
| 136 | + |
| 137 | + private static boolean acceptBox(Mp4Handler<?> handler, String type) { |
| 138 | + return handler instanceof Mp4BoxHandler |
| 139 | + ? ((Mp4BoxHandler) handler).shouldAcceptBox(type) |
| 140 | + : ((Mp4MediaHandler<?>) handler).shouldAcceptBox(type); |
| 141 | + } |
| 142 | + |
| 143 | + private static Mp4Handler<?> processBox(Mp4Handler<?> handler, String type, byte[] payload, |
| 144 | + long boxSize, Mp4Context context) throws IOException { |
| 145 | + return handler instanceof Mp4BoxHandler |
| 146 | + ? ((Mp4BoxHandler) handler).processBox(type, payload, boxSize, context) |
| 147 | + : ((Mp4MediaHandler<?>) handler).processBox(type, payload, boxSize, context); |
| 148 | + } |
| 149 | +} |
0 commit comments