target_dec_fuzzer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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-x86asm
  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 "config.h"
  44. #include "libavutil/avassert.h"
  45. #include "libavutil/imgutils.h"
  46. #include "libavutil/intreadwrite.h"
  47. #include "libavcodec/avcodec.h"
  48. #include "libavcodec/bytestream.h"
  49. #include "libavformat/avformat.h"
  50. //For FF_SANE_NB_CHANNELS, so we dont waste energy testing things that will get instantly rejected
  51. #include "libavcodec/internal.h"
  52. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  53. extern AVCodec * codec_list[];
  54. static void error(const char *err)
  55. {
  56. fprintf(stderr, "%s", err);
  57. exit(1);
  58. }
  59. static AVCodec *c = NULL;
  60. static AVCodec *AVCodecInitialize(enum AVCodecID codec_id)
  61. {
  62. AVCodec *res;
  63. res = avcodec_find_decoder(codec_id);
  64. if (!res)
  65. error("Failed to find decoder");
  66. return res;
  67. }
  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. // Ensure we don't loop forever
  78. const uint32_t maxiteration = 8096;
  79. const uint64_t maxpixels_per_frame = 4096 * 4096;
  80. uint64_t maxpixels;
  81. uint64_t maxsamples_per_frame = 256*1024*32;
  82. uint64_t maxsamples;
  83. static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
  84. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  85. const uint64_t fuzz_tag = FUZZ_TAG;
  86. const uint8_t *last = data;
  87. const uint8_t *end = data + size;
  88. uint32_t it = 0;
  89. uint64_t ec_pixels = 0;
  90. uint64_t nb_samples = 0;
  91. int (*decode_handler)(AVCodecContext *avctx, AVFrame *picture,
  92. int *got_picture_ptr,
  93. const AVPacket *avpkt) = NULL;
  94. AVCodecParserContext *parser = NULL;
  95. uint64_t keyframes = 0;
  96. AVDictionary *opts = NULL;
  97. if (!c) {
  98. #ifdef FFMPEG_DECODER
  99. #define DECODER_SYMBOL0(CODEC) ff_##CODEC##_decoder
  100. #define DECODER_SYMBOL(CODEC) DECODER_SYMBOL0(CODEC)
  101. extern AVCodec DECODER_SYMBOL(FFMPEG_DECODER);
  102. codec_list[0] = &DECODER_SYMBOL(FFMPEG_DECODER);
  103. avcodec_register(&DECODER_SYMBOL(FFMPEG_DECODER));
  104. #if FFMPEG_DECODER == tiff || FFMPEG_DECODER == tdsc
  105. extern AVCodec DECODER_SYMBOL(mjpeg);
  106. codec_list[1] = &DECODER_SYMBOL(mjpeg);
  107. avcodec_register(&DECODER_SYMBOL(mjpeg));
  108. #endif
  109. c = &DECODER_SYMBOL(FFMPEG_DECODER);
  110. #else
  111. avcodec_register_all();
  112. c = AVCodecInitialize(FFMPEG_CODEC); // Done once.
  113. #endif
  114. av_log_set_level(AV_LOG_PANIC);
  115. }
  116. switch (c->type) {
  117. case AVMEDIA_TYPE_AUDIO : decode_handler = avcodec_decode_audio4; break;
  118. case AVMEDIA_TYPE_VIDEO : decode_handler = avcodec_decode_video2; break;
  119. case AVMEDIA_TYPE_SUBTITLE: decode_handler = subtitle_handler ; break;
  120. }
  121. switch (c->id) {
  122. case AV_CODEC_ID_APE: maxsamples_per_frame /= 256; break;
  123. }
  124. maxpixels = maxpixels_per_frame * maxiteration;
  125. maxsamples = maxsamples_per_frame * maxiteration;
  126. switch (c->id) {
  127. case AV_CODEC_ID_BINKVIDEO: maxpixels /= 32; break;
  128. case AV_CODEC_ID_CFHD: maxpixels /= 128; break;
  129. case AV_CODEC_ID_DIRAC: maxpixels /= 8192; break;
  130. case AV_CODEC_ID_DST: maxsamples /= 8192; break;
  131. case AV_CODEC_ID_DXV: maxpixels /= 32; break;
  132. case AV_CODEC_ID_FFWAVESYNTH: maxsamples /= 16384; break;
  133. case AV_CODEC_ID_G2M: maxpixels /= 64; break;
  134. case AV_CODEC_ID_GDV: maxpixels /= 512; break;
  135. case AV_CODEC_ID_GIF: maxpixels /= 16; break;
  136. case AV_CODEC_ID_H264: maxpixels /= 256; break;
  137. case AV_CODEC_ID_HAP: maxpixels /= 128; break;
  138. case AV_CODEC_ID_HEVC: maxpixels /= 16384; break;
  139. case AV_CODEC_ID_HNM4_VIDEO: maxpixels /= 128; break;
  140. case AV_CODEC_ID_IFF_ILBM: maxpixels /= 128; break;
  141. case AV_CODEC_ID_INDEO4: maxpixels /= 128; break;
  142. case AV_CODEC_ID_LSCR: maxpixels /= 16; break;
  143. case AV_CODEC_ID_MMVIDEO: maxpixels /= 256; break;
  144. case AV_CODEC_ID_MOTIONPIXELS:maxpixels /= 256; break;
  145. case AV_CODEC_ID_MP4ALS: maxsamples /= 65536; break;
  146. case AV_CODEC_ID_MSRLE: maxpixels /= 16; break;
  147. case AV_CODEC_ID_MSS2: maxpixels /= 16384; break;
  148. case AV_CODEC_ID_MSZH: maxpixels /= 128; break;
  149. case AV_CODEC_ID_PNG: maxpixels /= 128; break;
  150. case AV_CODEC_ID_APNG: maxpixels /= 128; break;
  151. case AV_CODEC_ID_QTRLE: maxpixels /= 16; break;
  152. case AV_CODEC_ID_RASC: maxpixels /= 16; break;
  153. case AV_CODEC_ID_RTV1: maxpixels /= 16; break;
  154. case AV_CODEC_ID_SANM: maxpixels /= 16; break;
  155. case AV_CODEC_ID_SCPR: maxpixels /= 32; break;
  156. case AV_CODEC_ID_SCREENPRESSO:maxpixels /= 64; break;
  157. case AV_CODEC_ID_SMACKVIDEO: maxpixels /= 64; break;
  158. case AV_CODEC_ID_SNOW: maxpixels /= 128; break;
  159. case AV_CODEC_ID_TGV: maxpixels /= 32; break;
  160. case AV_CODEC_ID_TRUEMOTION2: maxpixels /= 1024; break;
  161. case AV_CODEC_ID_VP7: maxpixels /= 256; break;
  162. case AV_CODEC_ID_VP9: maxpixels /= 4096; break;
  163. case AV_CODEC_ID_ZEROCODEC: maxpixels /= 128; break;
  164. }
  165. AVCodecContext* ctx = avcodec_alloc_context3(c);
  166. AVCodecContext* parser_avctx = avcodec_alloc_context3(NULL);
  167. if (!ctx || !parser_avctx)
  168. error("Failed memory allocation");
  169. if (ctx->max_pixels == 0 || ctx->max_pixels > maxpixels_per_frame)
  170. ctx->max_pixels = maxpixels_per_frame; //To reduce false positive OOM and hangs
  171. ctx->refcounted_frames = 1; //To reduce false positive timeouts and focus testing on the refcounted API
  172. ctx->max_samples = maxsamples_per_frame;
  173. if (size > 1024) {
  174. GetByteContext gbc;
  175. int extradata_size;
  176. int flags;
  177. size -= 1024;
  178. bytestream2_init(&gbc, data + size, 1024);
  179. ctx->width = bytestream2_get_le32(&gbc);
  180. ctx->height = bytestream2_get_le32(&gbc);
  181. ctx->bit_rate = bytestream2_get_le64(&gbc);
  182. ctx->bits_per_coded_sample = bytestream2_get_le32(&gbc);
  183. // Try to initialize a parser for this codec, note, this may fail which just means we test without one
  184. flags = bytestream2_get_byte(&gbc);
  185. if (flags & 1)
  186. parser = av_parser_init(c->id);
  187. if (flags & 2)
  188. ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  189. if (flags & 4) {
  190. ctx->err_recognition = AV_EF_AGGRESSIVE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
  191. if (flags & 8)
  192. ctx->err_recognition |= AV_EF_EXPLODE;
  193. }
  194. if ((flags & 0x10) && c->id != AV_CODEC_ID_H264)
  195. ctx->flags2 |= AV_CODEC_FLAG2_FAST;
  196. if (flags & 0x40)
  197. av_force_cpu_flags(0);
  198. extradata_size = bytestream2_get_le32(&gbc);
  199. ctx->sample_rate = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  200. ctx->channels = (unsigned)bytestream2_get_le32(&gbc) % FF_SANE_NB_CHANNELS;
  201. ctx->block_align = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  202. ctx->codec_tag = bytestream2_get_le32(&gbc);
  203. if (c->codec_tags) {
  204. int n;
  205. for (n = 0; c->codec_tags[n] != FF_CODEC_TAGS_END; n++);
  206. ctx->codec_tag = c->codec_tags[ctx->codec_tag % n];
  207. }
  208. keyframes = bytestream2_get_le64(&gbc);
  209. ctx->request_channel_layout = bytestream2_get_le64(&gbc);
  210. ctx->idct_algo = bytestream2_get_byte(&gbc) % 25;
  211. if (flags & 0x20) {
  212. switch (ctx->codec_id) {
  213. case AV_CODEC_ID_AC3:
  214. case AV_CODEC_ID_EAC3:
  215. av_dict_set_int(&opts, "cons_noisegen", bytestream2_get_byte(&gbc) & 1, 0);
  216. av_dict_set_int(&opts, "heavy_compr", bytestream2_get_byte(&gbc) & 1, 0);
  217. av_dict_set_int(&opts, "target_level", (int)(bytestream2_get_byte(&gbc) % 32) - 31, 0);
  218. av_dict_set_int(&opts, "dmix_mode", (int)(bytestream2_get_byte(&gbc) % 4) - 1, 0);
  219. break;
  220. }
  221. }
  222. if (extradata_size < size) {
  223. ctx->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  224. if (ctx->extradata) {
  225. ctx->extradata_size = extradata_size;
  226. size -= ctx->extradata_size;
  227. memcpy(ctx->extradata, data + size, ctx->extradata_size);
  228. }
  229. }
  230. if (av_image_check_size(ctx->width, ctx->height, 0, ctx))
  231. ctx->width = ctx->height = 0;
  232. }
  233. int res = avcodec_open2(ctx, c, &opts);
  234. if (res < 0) {
  235. avcodec_free_context(&ctx);
  236. av_free(parser_avctx);
  237. av_parser_close(parser);
  238. av_dict_free(&opts);
  239. return 0; // Failure of avcodec_open2() does not imply that a issue was found
  240. }
  241. parser_avctx->codec_id = ctx->codec_id;
  242. int got_frame;
  243. AVFrame *frame = av_frame_alloc();
  244. if (!frame)
  245. error("Failed memory allocation");
  246. // Read very simple container
  247. AVPacket avpkt, parsepkt;
  248. av_init_packet(&avpkt);
  249. av_init_packet(&parsepkt);
  250. while (data < end && it < maxiteration) {
  251. // Search for the TAG
  252. while (data + sizeof(fuzz_tag) < end) {
  253. if (data[0] == (fuzz_tag & 0xFF) && AV_RN64(data) == fuzz_tag)
  254. break;
  255. data++;
  256. }
  257. if (data + sizeof(fuzz_tag) > end)
  258. data = end;
  259. res = av_new_packet(&parsepkt, data - last);
  260. if (res < 0)
  261. error("Failed memory allocation");
  262. memcpy(parsepkt.data, last, data - last);
  263. parsepkt.flags = (keyframes & 1) * AV_PKT_FLAG_DISCARD + (!!(keyframes & 2)) * AV_PKT_FLAG_KEY;
  264. keyframes = (keyframes >> 2) + (keyframes<<62);
  265. data += sizeof(fuzz_tag);
  266. last = data;
  267. while (parsepkt.size > 0) {
  268. if (parser) {
  269. av_init_packet(&avpkt);
  270. int ret = av_parser_parse2(parser, parser_avctx, &avpkt.data, &avpkt.size,
  271. parsepkt.data, parsepkt.size,
  272. parsepkt.pts, parsepkt.dts, parsepkt.pos);
  273. if (avpkt.data == parsepkt.data) {
  274. avpkt.buf = av_buffer_ref(parsepkt.buf);
  275. if (!avpkt.buf)
  276. error("Failed memory allocation");
  277. } else {
  278. if (av_packet_make_refcounted(&avpkt) < 0)
  279. error("Failed memory allocation");
  280. }
  281. parsepkt.data += ret;
  282. parsepkt.size -= ret;
  283. parsepkt.pos += ret;
  284. avpkt.pts = parser->pts;
  285. avpkt.dts = parser->dts;
  286. avpkt.pos = parser->pos;
  287. if ( parser->key_frame == 1 ||
  288. (parser->key_frame == -1 && parser->pict_type == AV_PICTURE_TYPE_I))
  289. avpkt.flags |= AV_PKT_FLAG_KEY;
  290. avpkt.flags |= parsepkt.flags & AV_PKT_FLAG_DISCARD;
  291. } else {
  292. av_packet_move_ref(&avpkt, &parsepkt);
  293. }
  294. // Iterate through all data
  295. while (avpkt.size > 0 && it++ < maxiteration) {
  296. av_frame_unref(frame);
  297. int ret = decode_handler(ctx, frame, &got_frame, &avpkt);
  298. ec_pixels += (ctx->width + 32LL) * (ctx->height + 32LL);
  299. if (it > 20 || ec_pixels > 4 * ctx->max_pixels)
  300. ctx->error_concealment = 0;
  301. if (ec_pixels > maxpixels)
  302. goto maximums_reached;
  303. nb_samples += frame->nb_samples;
  304. if (nb_samples > maxsamples)
  305. goto maximums_reached;
  306. if (ret <= 0 || ret > avpkt.size)
  307. break;
  308. if (ctx->codec_type != AVMEDIA_TYPE_AUDIO)
  309. ret = avpkt.size;
  310. avpkt.data += ret;
  311. avpkt.size -= ret;
  312. }
  313. av_packet_unref(&avpkt);
  314. }
  315. av_packet_unref(&parsepkt);
  316. }
  317. maximums_reached:
  318. av_packet_unref(&avpkt);
  319. do {
  320. got_frame = 0;
  321. av_frame_unref(frame);
  322. decode_handler(ctx, frame, &got_frame, &avpkt);
  323. } while (got_frame == 1 && it++ < maxiteration);
  324. fprintf(stderr, "pixels decoded: %"PRId64", samples decoded: %"PRId64", iterations: %d\n", ec_pixels, nb_samples, it);
  325. av_frame_free(&frame);
  326. avcodec_free_context(&ctx);
  327. avcodec_free_context(&parser_avctx);
  328. av_parser_close(parser);
  329. av_packet_unref(&parsepkt);
  330. av_dict_free(&opts);
  331. return 0;
  332. }