fuzzer-unserializehash.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. */
  14. #include "fuzzer.h"
  15. #include "Zend/zend.h"
  16. #include "main/php_config.h"
  17. #include "main/php_main.h"
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include "fuzzer-sapi.h"
  22. #include "ext/standard/php_var.h"
  23. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t FullSize) {
  24. const uint8_t *Start = memchr(Data, '|', FullSize);
  25. if (!Start) {
  26. return 0;
  27. }
  28. ++Start;
  29. size_t Size = (Data + FullSize) - Start;
  30. unsigned char *orig_data = malloc(Size+1);
  31. memcpy(orig_data, Start, Size);
  32. orig_data[Size] = '\0';
  33. if (fuzzer_request_startup() == FAILURE) {
  34. return 0;
  35. }
  36. fuzzer_setup_dummy_frame();
  37. {
  38. const unsigned char *data = orig_data;
  39. zval result;
  40. ZVAL_UNDEF(&result);
  41. php_unserialize_data_t var_hash;
  42. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  43. php_var_unserialize(&result, (const unsigned char **) &data, data + Size, &var_hash);
  44. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  45. if (Z_TYPE(result) == IS_OBJECT
  46. && zend_string_equals_literal(Z_OBJCE(result)->name, "HashContext")) {
  47. zval args[2];
  48. ZVAL_COPY_VALUE(&args[0], &result);
  49. ZVAL_STRINGL(&args[1], (char *) Data, (Start - Data) - 1);
  50. fuzzer_call_php_func_zval("hash_update", 2, args);
  51. zval_ptr_dtor(&args[1]);
  52. fuzzer_call_php_func_zval("hash_final", 1, args);
  53. }
  54. zval_ptr_dtor(&result);
  55. }
  56. free(orig_data);
  57. fuzzer_request_shutdown();
  58. return 0;
  59. }
  60. int LLVMFuzzerInitialize(int *argc, char ***argv) {
  61. fuzzer_init_php();
  62. /* fuzzer_shutdown_php(); */
  63. return 0;
  64. }