logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0008-regex-reject-invalid-digit-back-reference-in-BRE.patch (2112B)


  1. From b6bfb0cbc7e56b560b1a928b6136c3ef89015743 Mon Sep 17 00:00:00 2001
  2. From: Szabolcs Nagy <nsz@port70.net>
  3. Date: Mon, 23 Mar 2026 17:33:20 +0000
  4. Subject: [PATCH] regex: reject invalid \digit back reference in BRE
  5. in BRE \n matches the nth subexpression, but regcomp did not check if
  6. the nth subexpression was complete or not, only that there were more
  7. subexpressions overall than the largest backref.
  8. fix regcomp to error if the referenced subexpression is incomplete.
  9. the bug could cause an infinite loop in regexec:
  10. regcomp(&re, "\\(^a*\\1\\)*", 0);
  11. regexec(&re, "aa", 0, 0, 0);
  12. since BRE has backreferences, any application accepting a BRE from
  13. untrusted sources is already vulnerable to an attacker-controlled
  14. near-infinite (exponential-time) loop, but this particular case where
  15. the loop is actually infinite can and should be avoided.
  16. ERE is not affected since the language an ERE describes is actually
  17. regular.
  18. Reported-by: Simon Resch <simon.resch@code-intelligence.com>
  19. ---
  20. src/regex/regcomp.c | 6 ++++++
  21. 1 file changed, 6 insertions(+)
  22. diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c
  23. index fb24556e..b4b81968 100644
  24. --- a/src/regex/regcomp.c
  25. +++ b/src/regex/regcomp.c
  26. @@ -409,6 +409,8 @@ typedef struct {
  27. int position;
  28. /* The highest back reference or -1 if none seen so far. */
  29. int max_backref;
  30. + /* Bit mask of submatch IDs that can be back referenced. */
  31. + int backref_ok;
  32. /* Compilation flags. */
  33. int cflags;
  34. } tre_parse_ctx_t;
  35. @@ -769,6 +771,8 @@ static reg_errcode_t marksub(tre_parse_ctx_t *ctx, tre_ast_node_t *node, int sub
  36. node->submatch_id = subid;
  37. node->num_submatches++;
  38. ctx->n = node;
  39. + if (subid < 10)
  40. + ctx->backref_ok |= 1<<subid;
  41. return REG_OK;
  42. }
  43. @@ -864,6 +868,8 @@ static reg_errcode_t parse_atom(tre_parse_ctx_t *ctx, const char *s)
  44. if (!ere && (unsigned)*s-'1' < 9) {
  45. /* back reference */
  46. int val = *s - '0';
  47. + if (!(ctx->backref_ok & 1<<val))
  48. + return REG_ESUBREG;
  49. node = tre_ast_new_literal(ctx->mem, BACKREF, val, ctx->position++);
  50. ctx->max_backref = MAX(val, ctx->max_backref);
  51. } else {
  52. --
  53. 2.49.0