target_dec_fuzzer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* Targeted fuzzer that targets specific codecs depending on two
  19. compile-time flags.
  20. INSTRUCTIONS:
  21. * Get the very fresh clang, e.g. see http://libfuzzer.info#versions
  22. * Get and build libFuzzer:
  23. svn co http://llvm.org/svn/llvm-project/llvm/trunk/lib/Fuzzer
  24. ./Fuzzer/build.sh
  25. * build ffmpeg for fuzzing:
  26. FLAGS="-fsanitize=address -fsanitize-coverage=trace-pc-guard,trace-cmp -g" CC="clang $FLAGS" CXX="clang++ $FLAGS" ./configure --disable-yasm
  27. make clean && make -j
  28. * build the fuzz target.
  29. Choose the value of FFMPEG_CODEC (e.g. AV_CODEC_ID_DVD_SUBTITLE) and
  30. choose one of FUZZ_FFMPEG_VIDEO, FUZZ_FFMPEG_AUDIO, FUZZ_FFMPEG_SUBTITLE.
  31. clang -fsanitize=address -fsanitize-coverage=trace-pc-guard,trace-cmp tools/target_dec_fuzzer.c -o target_dec_fuzzer -I. -DFFMPEG_CODEC=AV_CODEC_ID_MPEG1VIDEO -DFUZZ_FFMPEG_VIDEO ../../libfuzzer/libFuzzer.a -Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavresample -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -Wl,--as-needed -Wl,-z,noexecstack -Wl,--warn-common -Wl,-rpath-link=libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil:libavresample -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil -ldl -lxcb -lxcb-shm -lxcb -lxcb-xfixes -lxcb -lxcb-shape -lxcb -lX11 -lasound -lm -lbz2 -lz -pthread
  32. * create a corpus directory and put some samples there (empty dir is ok too):
  33. mkdir CORPUS && cp some-files CORPUS
  34. * Run fuzzing:
  35. ./target_dec_fuzzer -max_len=100000 CORPUS
  36. More info:
  37. http://libfuzzer.info
  38. http://tutorial.libfuzzer.info
  39. https://github.com/google/oss-fuzz
  40. http://lcamtuf.coredump.cx/afl/
  41. https://security.googleblog.com/2016/08/guided-in-process-fuzzing-of-chrome.html
  42. */
  43. #include "libavutil/avassert.h"
  44. #include "libavutil/intreadwrite.h"
  45. #include "libavcodec/avcodec.h"
  46. #include "libavformat/avformat.h"
  47. static void error(const char *err)
  48. {
  49. fprintf(stderr, "%s", err);
  50. exit(1);
  51. }
  52. static AVCodec *c = NULL;
  53. static AVCodec *AVCodecInitialize(enum AVCodecID codec_id)
  54. {
  55. AVCodec *res;
  56. av_register_all();
  57. av_log_set_level(AV_LOG_PANIC);
  58. res = avcodec_find_decoder(codec_id);
  59. if (!res)
  60. error("Failed to find decoder");
  61. return res;
  62. }
  63. #if defined(FUZZ_FFMPEG_VIDEO)
  64. #define decode_handler avcodec_decode_video2
  65. #elif defined(FUZZ_FFMPEG_AUDIO)
  66. #define decode_handler avcodec_decode_audio4
  67. #elif defined(FUZZ_FFMPEG_SUBTITLE)
  68. static int subtitle_handler(AVCodecContext *avctx, void *frame,
  69. int *got_sub_ptr, AVPacket *avpkt)
  70. {
  71. AVSubtitle sub;
  72. int ret = avcodec_decode_subtitle2(avctx, &sub, got_sub_ptr, avpkt);
  73. if (ret >= 0 && *got_sub_ptr)
  74. avsubtitle_free(&sub);
  75. return ret;
  76. }
  77. #define decode_handler subtitle_handler
  78. #else
  79. #error "Specify encoder type" // To catch mistakes
  80. #endif
  81. // Class to handle buffer allocation and resize for each frame
  82. typedef struct FuzzDataBuffer {
  83. size_t size_;
  84. uint8_t *data_;
  85. } FuzzDataBuffer;
  86. void FDBCreate(FuzzDataBuffer *FDB) {
  87. FDB->size_ = 0x1000;
  88. FDB->data_ = av_malloc(FDB->size_);
  89. if (!FDB->data_)
  90. error("Failed memory allocation");
  91. }
  92. void FDBDesroy(FuzzDataBuffer *FDB) { av_free(FDB->data_); }
  93. void FDBRealloc(FuzzDataBuffer *FDB, size_t size) {
  94. size_t needed = size + FF_INPUT_BUFFER_PADDING_SIZE;
  95. av_assert0(needed > size);
  96. if (needed > FDB->size_) {
  97. av_free(FDB->data_);
  98. FDB->size_ = needed;
  99. FDB->data_ = av_malloc(FDB->size_);
  100. if (!FDB->data_)
  101. error("Failed memory allocation");
  102. }
  103. }
  104. void FDBPrepare(FuzzDataBuffer *FDB, AVPacket *dst, const uint8_t *data,
  105. size_t size)
  106. {
  107. FDBRealloc(FDB, size);
  108. memcpy(FDB->data_, data, size);
  109. size_t padd = FDB->size_ - size;
  110. if (padd > FF_INPUT_BUFFER_PADDING_SIZE)
  111. padd = FF_INPUT_BUFFER_PADDING_SIZE;
  112. memset(FDB->data_ + size, 0, padd);
  113. av_init_packet(dst);
  114. dst->data = FDB->data_;
  115. dst->size = size;
  116. }
  117. // Ensure we don't loop forever
  118. const uint32_t maxiteration = 8096;
  119. static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
  120. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  121. const uint64_t fuzz_tag = FUZZ_TAG;
  122. FuzzDataBuffer buffer;
  123. const uint8_t *last = data;
  124. const uint8_t *end = data + size;
  125. uint32_t it = 0;
  126. if (!c)
  127. c = AVCodecInitialize(FFMPEG_CODEC); // Done once.
  128. AVCodecContext* ctx = avcodec_alloc_context3(NULL);
  129. if (!ctx)
  130. error("Failed memory allocation");
  131. ctx->max_pixels = 4096 * 4096; //To reduce false positive OOM and hangs
  132. int res = avcodec_open2(ctx, c, NULL);
  133. if (res < 0)
  134. return res;
  135. FDBCreate(&buffer);
  136. int got_frame;
  137. AVFrame *frame = av_frame_alloc();
  138. if (!frame)
  139. error("Failed memory allocation");
  140. // Read very simple container
  141. AVPacket avpkt;
  142. while (data < end && it < maxiteration) {
  143. // Search for the TAG
  144. while (data + sizeof(fuzz_tag) < end) {
  145. if (data[0] == (fuzz_tag & 0xFF) && AV_RN64(data) == fuzz_tag)
  146. break;
  147. data++;
  148. }
  149. if (data + sizeof(fuzz_tag) > end)
  150. data = end;
  151. FDBPrepare(&buffer, &avpkt, last, data - last);
  152. data += sizeof(fuzz_tag);
  153. last = data;
  154. // Iterate through all data
  155. while (avpkt.size > 0 && it++ < maxiteration) {
  156. av_frame_unref(frame);
  157. int ret = decode_handler(ctx, frame, &got_frame, &avpkt);
  158. if (it > 20)
  159. ctx->error_concealment = 0;
  160. if (ret <= 0 || ret > avpkt.size)
  161. break;
  162. if (ctx->codec_type != AVMEDIA_TYPE_AUDIO)
  163. ret = avpkt.size;
  164. avpkt.data += ret;
  165. avpkt.size -= ret;
  166. }
  167. }
  168. av_init_packet(&avpkt);
  169. avpkt.data = NULL;
  170. avpkt.size = 0;
  171. do {
  172. got_frame = 0;
  173. decode_handler(ctx, frame, &got_frame, &avpkt);
  174. } while (got_frame == 1 && it++ < maxiteration);
  175. av_frame_free(&frame);
  176. avcodec_free_context(&ctx);
  177. av_freep(&ctx);
  178. FDBDesroy(&buffer);
  179. return 0;
  180. }