tool_metaflac.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* fuzzer_tool_flac
  2. * Copyright (C) 2023 Xiph.Org Foundation
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h> /* for memcpy */
  34. #define FUZZ_TOOL_METAFLAC
  35. #define fprintf(...)
  36. #define printf(...)
  37. #include "../src/metaflac/main.c"
  38. #include "common.h"
  39. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  40. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
  41. {
  42. size_t size_left = size;
  43. size_t arglen;
  44. char * argv[64];
  45. char exename[] = "metaflac";
  46. char filename[] = "/tmp/fuzzXXXXXX";
  47. char filename_stdin[] = "/tmp/fuzzXXXXXX";
  48. int numarg = 0, maxarg;
  49. int file_to_fuzz;
  50. int tmp_stdout, tmp_stdin;
  51. fpos_t pos_stdout;
  52. bool use_stdin = false;
  53. share__opterr = 0;
  54. share__optind = 0;
  55. if(size < 2)
  56. return 0;
  57. maxarg = data[0] & 15;
  58. use_stdin = data[0] & 16;
  59. size_left--;
  60. argv[0] = exename;
  61. numarg++;
  62. /* Check whether input is zero delimited */
  63. while((arglen = strnlen((char *)data+(size-size_left),size_left)) < size_left && numarg < maxarg) {
  64. argv[numarg++] = (char *)data+(size-size_left);
  65. size_left -= arglen + 1;
  66. }
  67. /* Create file to feed directly */
  68. file_to_fuzz = mkstemp(filename);
  69. if (file_to_fuzz < 0)
  70. abort();
  71. if(use_stdin) {
  72. write(file_to_fuzz,data+(size-size_left),size_left/2);
  73. size_left -= size_left/2;
  74. }
  75. else
  76. write(file_to_fuzz,data+(size-size_left),size_left);
  77. close(file_to_fuzz);
  78. argv[numarg++] = filename;
  79. /* Create file to feed to stdin */
  80. if(use_stdin) {
  81. file_to_fuzz = mkstemp(filename_stdin);
  82. if (file_to_fuzz < 0)
  83. abort();
  84. write(file_to_fuzz,data+(size-size_left),size_left);
  85. close(file_to_fuzz);
  86. }
  87. /* redirect stdout */
  88. fflush(stdout);
  89. fgetpos(stdout,&pos_stdout);
  90. tmp_stdout = dup(fileno(stdout));
  91. freopen("/dev/null","w",stdout);
  92. /* redirect stdin */
  93. tmp_stdin = dup(fileno(stdin));
  94. if(use_stdin)
  95. freopen(filename_stdin,"r",stdin);
  96. else {
  97. freopen("/dev/null","r",stdin);
  98. argv[numarg++] = filename;
  99. }
  100. main_to_fuzz(numarg,argv);
  101. /* restore stdout */
  102. fflush(stdout);
  103. dup2(tmp_stdout, fileno(stdout));
  104. close(tmp_stdout);
  105. clearerr(stdout);
  106. fsetpos(stdout,&pos_stdout);
  107. /* restore stdin */
  108. dup2(tmp_stdin, fileno(stdin));
  109. close(tmp_stdin);
  110. clearerr(stdin);
  111. unlink(filename);
  112. if(use_stdin)
  113. unlink(filename_stdin);
  114. return 0;
  115. }