You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.1 KiB

  1. From c509265c5f6ae7264f7b8a8aae1cfa5fc59d108c Mon Sep 17 00:00:00 2001
  2. From: "K.Kosako" <kosako@sofnec.co.jp>
  3. Date: Thu, 27 Jun 2019 14:11:55 +0900
  4. Subject: [PATCH] Fix CVE-2019-13225: problem in converting if-then-else
  5. pattern to bytecode.
  6. diff --git a/src/regcomp.c b/src/regcomp.c
  7. index c2c04a4..ff3431f 100644
  8. --- a/src/regcomp.c
  9. +++ b/src/regcomp.c
  10. @@ -1307,8 +1307,9 @@ compile_length_bag_node(BagNode* node, regex_t* reg)
  11. len += tlen;
  12. }
  13. + len += SIZE_OP_JUMP + SIZE_OP_ATOMIC_END;
  14. +
  15. if (IS_NOT_NULL(Else)) {
  16. - len += SIZE_OP_JUMP;
  17. tlen = compile_length_tree(Else, reg);
  18. if (tlen < 0) return tlen;
  19. len += tlen;
  20. @@ -1455,7 +1456,7 @@ compile_bag_node(BagNode* node, regex_t* reg, ScanEnv* env)
  21. case BAG_IF_ELSE:
  22. {
  23. - int cond_len, then_len, jump_len;
  24. + int cond_len, then_len, else_len, jump_len;
  25. Node* cond = NODE_BAG_BODY(node);
  26. Node* Then = node->te.Then;
  27. Node* Else = node->te.Else;
  28. @@ -1472,8 +1473,7 @@ compile_bag_node(BagNode* node, regex_t* reg, ScanEnv* env)
  29. else
  30. then_len = 0;
  31. - jump_len = cond_len + then_len + SIZE_OP_ATOMIC_END;
  32. - if (IS_NOT_NULL(Else)) jump_len += SIZE_OP_JUMP;
  33. + jump_len = cond_len + then_len + SIZE_OP_ATOMIC_END + SIZE_OP_JUMP;
  34. r = add_op(reg, OP_PUSH);
  35. if (r != 0) return r;
  36. @@ -1490,11 +1490,20 @@ compile_bag_node(BagNode* node, regex_t* reg, ScanEnv* env)
  37. }
  38. if (IS_NOT_NULL(Else)) {
  39. - int else_len = compile_length_tree(Else, reg);
  40. - r = add_op(reg, OP_JUMP);
  41. - if (r != 0) return r;
  42. - COP(reg)->jump.addr = else_len + SIZE_INC_OP;
  43. + else_len = compile_length_tree(Else, reg);
  44. + if (else_len < 0) return else_len;
  45. + }
  46. + else
  47. + else_len = 0;
  48. + r = add_op(reg, OP_JUMP);
  49. + if (r != 0) return r;
  50. + COP(reg)->jump.addr = SIZE_OP_ATOMIC_END + else_len + SIZE_INC_OP;
  51. +
  52. + r = add_op(reg, OP_ATOMIC_END);
  53. + if (r != 0) return r;
  54. +
  55. + if (IS_NOT_NULL(Else)) {
  56. r = compile_tree(Else, reg, env);
  57. }
  58. }