From 6bc7ae18d384524c5903e42f6c48ec4723ee1f59 Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:08:18 +0000 Subject: [PATCH 1/7] ruby-3.2: Backport fix for GHSA-3p8q-wmhx-vm2w Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 9 + ...regexp-bound-compiled-program-writes.patch | 182 ++++++++++++++++++ ...8-regexp-harden-bbuf-size-arithmetic.patch | 91 +++++++++ ...xp-check-program-size-in-length-pass.patch | 97 ++++++++++ 4 files changed, 379 insertions(+) create mode 100644 resources/patches/ruby_32/0007-regexp-bound-compiled-program-writes.patch create mode 100644 resources/patches/ruby_32/0008-regexp-harden-bbuf-size-arithmetic.patch create mode 100644 resources/patches/ruby_32/0009-regexp-check-program-size-in-length-pass.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index 350bf35c..889cb3ac 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -35,6 +35,15 @@ pkg.apply_patch "#{base}/0005-regexp-free-failed-alternation-branch.patch" pkg.apply_patch "#{base}/0006-regexp-preserve-program-size-error.patch" + # Backport the Ruby 3.3.14 fixes for the Regexp and String case conversion + # advisories below. Keep this order, since the patches build on the ones + # above and on each other. + # + # GHSA-3p8q-wmhx-vm2w, heap buffer overflow in regexp compilation + pkg.apply_patch "#{base}/0007-regexp-bound-compiled-program-writes.patch" + pkg.apply_patch "#{base}/0008-regexp-harden-bbuf-size-arithmetic.patch" + pkg.apply_patch "#{base}/0009-regexp-check-program-size-in-length-pass.patch" + if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" pkg.apply_patch "#{base}/ruby-faster-load_32.patch" diff --git a/resources/patches/ruby_32/0007-regexp-bound-compiled-program-writes.patch b/resources/patches/ruby_32/0007-regexp-bound-compiled-program-writes.patch new file mode 100644 index 00000000..af90eaa9 --- /dev/null +++ b/resources/patches/ruby_32/0007-regexp-bound-compiled-program-writes.patch @@ -0,0 +1,182 @@ +From: Stan Hu +Date: 2026-11-13T22:29:58-08:00 +Subject: [PATCH 1/15] Bound compiled program writes against size accounting + +Backport of Ruby 3.3 commit c588589631cf0d0503be6ae93d75bbc2f6ae1445 +(ruby/ruby#20000, GHSA-3p8q-wmhx-vm2w). + +--- +diff --git a/regcomp.c b/regcomp.c +index e36b9b7d3e..9db32c4383 100644 +--- a/regcomp.c ++++ b/regcomp.c +@@ -221,13 +221,26 @@ unset_addr_list_add(UnsetAddrList* uslist, int offset, struct _Node* node) + #endif /* USE_SUBEXP_CALL */ + + ++/* Reject a write that would push the compiled program past the size its ++ offset type (int) can address. This must run before every write to the ++ program buffer, not only in add_opcode(): the bulk writers below emit ++ class bodies and exact strings in a single BBUF_ADD, which never revisits ++ the per-instruction guard. The arithmetic stays unsigned so a multi-GiB ++ payload cannot wrap the accounting and copy past the allocation. */ + static int +-add_opcode(regex_t* reg, int opcode) ++check_compiled_program_size(regex_t* reg, OnigDistance add_len) + { +- /* Every instruction passes here, so this is where a program that would +- outgrow its offset type is stopped, while it is still being emitted. */ +- if (reg->used > MAX_COMPILED_PROGRAM_SIZE) ++ if (reg->used > MAX_COMPILED_PROGRAM_SIZE || ++ add_len > (OnigDistance )(MAX_COMPILED_PROGRAM_SIZE - reg->used)) + return ONIGERR_TOO_BIG_COMPILED_PROGRAM; ++ return 0; ++} ++ ++static int ++add_opcode(regex_t* reg, int opcode) ++{ ++ int r = check_compiled_program_size(reg, 1); ++ if (r) return r; + BBUF_ADD1(reg, opcode); + return 0; + } +@@ -311,6 +324,8 @@ add_opcode_rel_addr(regex_t* reg, int opcode, int addr) + static int + add_bytes(regex_t* reg, UChar* bytes, OnigDistance len) + { ++ int r = check_compiled_program_size(reg, len); ++ if (r) return r; + BBUF_ADD(reg, bytes, len); + return 0; + } +@@ -318,6 +333,8 @@ add_bytes(regex_t* reg, UChar* bytes, OnigDistance len) + static int + add_bitset(regex_t* reg, BitSetRef bs) + { ++ int r = check_compiled_program_size(reg, SIZE_BITSET); ++ if (r) return r; + BBUF_ADD(reg, bs, SIZE_BITSET); + return 0; + } +@@ -468,21 +485,25 @@ static int + add_compile_string(UChar* s, int mb_len, OnigDistance byte_len, + regex_t* reg, int ignore_case) + { ++ int r; + int op = select_str_opcode(mb_len, byte_len, ignore_case); +- add_opcode(reg, op); ++ r = add_opcode(reg, op); ++ if (r) return r; + +- if (op == OP_EXACTMBN) +- add_length(reg, mb_len); ++ if (op == OP_EXACTMBN) { ++ r = add_length(reg, mb_len); ++ if (r) return r; ++ } + + if (IS_NEED_STR_LEN_OP_EXACT(op)) { + if (op == OP_EXACTN_IC) +- add_length(reg, byte_len); ++ r = add_length(reg, byte_len); + else +- add_length(reg, byte_len / mb_len); ++ r = add_length(reg, byte_len / mb_len); ++ if (r) return r; + } + +- add_bytes(reg, s, byte_len); +- return 0; ++ return add_bytes(reg, s, byte_len); + } + + +@@ -586,21 +607,27 @@ static int + add_multi_byte_cclass(BBuf* mbuf, regex_t* reg) + { + #ifdef PLATFORM_UNALIGNED_WORD_ACCESS +- add_length(reg, mbuf->used); ++ int r = add_length(reg, mbuf->used); ++ if (r) return r; + return add_bytes(reg, mbuf->p, mbuf->used); + #else + int r, pad_size; + UChar* p = BBUF_GET_ADD_ADDRESS(reg) + SIZE_LENGTH; + + GET_ALIGNMENT_PAD_SIZE(p, pad_size); +- add_length(reg, mbuf->used + (WORD_ALIGNMENT_SIZE - 1)); +- if (pad_size != 0) add_bytes(reg, PadBuf, pad_size); ++ r = add_length(reg, mbuf->used + (WORD_ALIGNMENT_SIZE - 1)); ++ if (r) return r; ++ if (pad_size != 0) { ++ r = add_bytes(reg, PadBuf, pad_size); ++ if (r) return r; ++ } + + r = add_bytes(reg, mbuf->p, mbuf->used); ++ if (r) return r; + + /* padding for return value from compile_length_cclass_node() to be fix. */ + pad_size = (WORD_ALIGNMENT_SIZE - 1) - pad_size; +- if (pad_size != 0) add_bytes(reg, PadBuf, pad_size); ++ if (pad_size != 0) r = add_bytes(reg, PadBuf, pad_size); + return r; + #endif + } +@@ -637,26 +664,29 @@ compile_cclass_node(CClassNode* cc, regex_t* reg) + + if (IS_NULL(cc->mbuf)) { + if (IS_NCCLASS_NOT(cc)) +- add_opcode(reg, OP_CCLASS_NOT); ++ r = add_opcode(reg, OP_CCLASS_NOT); + else +- add_opcode(reg, OP_CCLASS); ++ r = add_opcode(reg, OP_CCLASS); ++ if (r) return r; + + r = add_bitset(reg, cc->bs); + } + else { + if (ONIGENC_MBC_MINLEN(reg->enc) > 1 || bitset_is_empty(cc->bs)) { + if (IS_NCCLASS_NOT(cc)) +- add_opcode(reg, OP_CCLASS_MB_NOT); ++ r = add_opcode(reg, OP_CCLASS_MB_NOT); + else +- add_opcode(reg, OP_CCLASS_MB); ++ r = add_opcode(reg, OP_CCLASS_MB); ++ if (r) return r; + + r = add_multi_byte_cclass(cc->mbuf, reg); + } + else { + if (IS_NCCLASS_NOT(cc)) +- add_opcode(reg, OP_CCLASS_MIX_NOT); ++ r = add_opcode(reg, OP_CCLASS_MIX_NOT); + else +- add_opcode(reg, OP_CCLASS_MIX); ++ r = add_opcode(reg, OP_CCLASS_MIX); ++ if (r) return r; + + r = add_bitset(reg, cc->bs); + if (r) return r; +@@ -1120,7 +1150,8 @@ compile_quantifier_node(QtfrNode* qn, regex_t* reg) + r = add_opcode_rel_addr(reg, OP_PUSH_OR_JUMP_EXACT1, + mod_tlen + SIZE_OP_JUMP); + if (r) return r; +- add_bytes(reg, NSTR(qn->head_exact)->s, 1); ++ r = add_bytes(reg, NSTR(qn->head_exact)->s, 1); ++ if (r) return r; + r = compile_tree_empty_check(qn->target, reg, empty_info); + if (r) return r; + r = add_opcode_rel_addr(reg, OP_JUMP, +@@ -1132,7 +1163,8 @@ compile_quantifier_node(QtfrNode* qn, regex_t* reg) + r = add_opcode_rel_addr(reg, OP_PUSH_IF_PEEK_NEXT, + mod_tlen + SIZE_OP_JUMP); + if (r) return r; +- add_bytes(reg, NSTR(qn->next_head_exact)->s, 1); ++ r = add_bytes(reg, NSTR(qn->next_head_exact)->s, 1); ++ if (r) return r; + r = compile_tree_empty_check(qn->target, reg, empty_info); + if (r) return r; + r = add_opcode_rel_addr(reg, OP_JUMP, diff --git a/resources/patches/ruby_32/0008-regexp-harden-bbuf-size-arithmetic.patch b/resources/patches/ruby_32/0008-regexp-harden-bbuf-size-arithmetic.patch new file mode 100644 index 00000000..68eef223 --- /dev/null +++ b/resources/patches/ruby_32/0008-regexp-harden-bbuf-size-arithmetic.patch @@ -0,0 +1,91 @@ +From: Stan Hu +Date: 2026-10-28T10:16:03-07:00 +Subject: [PATCH 2/15] Harden BBuf size arithmetic against 32-bit overflow + +Backport of Ruby 3.3 commit d9f170a93fa8c49f70411f2655477bed863c8d0a +(ruby/ruby#20000, GHSA-3p8q-wmhx-vm2w). + +--- +diff --git a/regint.h b/regint.h +index 0655b1dc12..a55bfb97ff 100644 +--- a/regint.h ++++ b/regint.h +@@ -483,38 +483,52 @@ typedef struct _BBuf { + (buf)->p = tmp;\ + } while (0) + ++/* A BBuf addresses its contents with unsigned int used/alloc fields, so it ++ cannot hold more than this. Every size below is computed in size_t and ++ checked against it, so neither "pos + n" nor the alloc doubling can wrap ++ and send a write or an infinite grow loop past the buffer. */ ++#define ONIG_MAX_BBUF_SIZE ((size_t )INT_MAX_LIMIT) ++ + #define BBUF_EXPAND(buf,low) do{\ + UChar *tmp;\ +- do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\ +- tmp = (UChar* )xrealloc((buf)->p, (buf)->alloc);\ ++ size_t new_alloc = ((buf)->alloc == 0) ? 1 : (size_t )(buf)->alloc;\ ++ if ((size_t )(low) > ONIG_MAX_BBUF_SIZE) return(ONIGERR_MEMORY);\ ++ do { new_alloc *= 2; } while (new_alloc < (size_t )(low));\ ++ if (new_alloc > ONIG_MAX_BBUF_SIZE) new_alloc = ONIG_MAX_BBUF_SIZE;\ ++ tmp = (UChar* )xrealloc((buf)->p, new_alloc);\ + if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\ + (buf)->p = tmp;\ ++ (buf)->alloc = (unsigned int )new_alloc;\ + } while (0) + + #define BBUF_ENSURE_SIZE(buf,size) do{\ +- unsigned int new_alloc = (buf)->alloc;\ +- while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\ +- if ((buf)->alloc != new_alloc) {\ ++ size_t new_alloc = ((buf)->alloc == 0) ? 1 : (size_t )(buf)->alloc;\ ++ if ((size_t )(size) > ONIG_MAX_BBUF_SIZE) return(ONIGERR_MEMORY);\ ++ while (new_alloc < (size_t )(size)) { new_alloc *= 2; }\ ++ if (new_alloc > ONIG_MAX_BBUF_SIZE) new_alloc = ONIG_MAX_BBUF_SIZE;\ ++ if ((buf)->alloc != (unsigned int )new_alloc) {\ + UChar *tmp;\ + tmp = (UChar* )xrealloc((buf)->p, new_alloc);\ + if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\ + (buf)->p = tmp;\ +- (buf)->alloc = new_alloc;\ ++ (buf)->alloc = (unsigned int )new_alloc;\ + }\ + } while (0) + + #define BBUF_WRITE(buf,pos,bytes,n) do{\ +- int used = (pos) + (int )(n);\ +- if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\ ++ size_t used = (size_t )(pos) + (size_t )(n);\ ++ if (used > ONIG_MAX_BBUF_SIZE) return(ONIGERR_MEMORY);\ ++ if ((size_t )(buf)->alloc < used) BBUF_EXPAND((buf),used);\ + xmemcpy((buf)->p + (pos), (bytes), (n));\ +- if ((buf)->used < (unsigned int )used) (buf)->used = used;\ ++ if ((size_t )(buf)->used < used) (buf)->used = (unsigned int )used;\ + } while (0) + + #define BBUF_WRITE1(buf,pos,byte) do{\ +- int used = (pos) + 1;\ +- if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\ ++ size_t used = (size_t )(pos) + 1;\ ++ if (used > ONIG_MAX_BBUF_SIZE) return(ONIGERR_MEMORY);\ ++ if ((size_t )(buf)->alloc < used) BBUF_EXPAND((buf),used);\ + (buf)->p[(pos)] = (UChar )(byte);\ +- if ((buf)->used < (unsigned int )used) (buf)->used = used;\ ++ if ((size_t )(buf)->used < used) (buf)->used = (unsigned int )used;\ + } while (0) + + #define BBUF_ADD(buf,bytes,n) BBUF_WRITE((buf),(buf)->used,(bytes),(n)) +@@ -524,9 +538,11 @@ typedef struct _BBuf { + + /* from < to */ + #define BBUF_MOVE_RIGHT(buf,from,to,n) do {\ +- if ((unsigned int )((to)+(n)) > (buf)->alloc) BBUF_EXPAND((buf),(to) + (n));\ ++ size_t end = (size_t )(to) + (size_t )(n);\ ++ if (end > ONIG_MAX_BBUF_SIZE) return(ONIGERR_MEMORY);\ ++ if (end > (size_t )(buf)->alloc) BBUF_EXPAND((buf),end);\ + xmemmove((buf)->p + (to), (buf)->p + (from), (n));\ +- if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\ ++ if (end > (size_t )(buf)->used) (buf)->used = (unsigned int )end;\ + } while (0) + + /* from > to */ diff --git a/resources/patches/ruby_32/0009-regexp-check-program-size-in-length-pass.patch b/resources/patches/ruby_32/0009-regexp-check-program-size-in-length-pass.patch new file mode 100644 index 00000000..b81cdaa4 --- /dev/null +++ b/resources/patches/ruby_32/0009-regexp-check-program-size-in-length-pass.patch @@ -0,0 +1,97 @@ +From: Yusuke Endoh +Date: 2026-11-18T13:00:28+09:00 +Subject: [PATCH 3/15] Check the compiled program size while computing the length + +Backport of Ruby 3.3 commit 8e7de382840f0b792c610872be4823acde060b94 +(ruby/ruby#20000, GHSA-3p8q-wmhx-vm2w). +Adapted for Ruby 3.2, which lacks ruby/ruby#19505. + +--- +diff --git a/regcomp.c b/regcomp.c +index 9db32c4383..07933e4298 100644 +--- a/regcomp.c ++++ b/regcomp.c +@@ -1661,23 +1661,24 @@ compile_length_tree(Node* node, regex_t* reg) + r = compile_length_tree(NCAR(node), reg); + if (r < 0) return r; + len += r; ++ if (len > (int )MAX_COMPILED_PROGRAM_SIZE) ++ return ONIGERR_TOO_BIG_COMPILED_PROGRAM; + } while (IS_NOT_NULL(node = NCDR(node))); + r = len; + break; + + case NT_ALT: +- { +- int n = 0; +- len = 0; +- do { +- r = compile_length_tree(NCAR(node), reg); +- if (r < 0) return r; +- len += r; +- n++; +- } while (IS_NOT_NULL(node = NCDR(node))); +- r = len; +- r += (SIZE_OP_PUSH + SIZE_OP_JUMP) * (n - 1); +- } ++ len = 0; ++ do { ++ r = compile_length_tree(NCAR(node), reg); ++ if (r < 0) return r; ++ len += r; ++ if (IS_NOT_NULL(NCDR(node))) ++ len += SIZE_OP_PUSH + SIZE_OP_JUMP; ++ if (len > (int )MAX_COMPILED_PROGRAM_SIZE) ++ return ONIGERR_TOO_BIG_COMPILED_PROGRAM; ++ } while (IS_NOT_NULL(node = NCDR(node))); ++ r = len; + break; + + case NT_STR: +@@ -1740,6 +1741,8 @@ compile_length_tree(Node* node, regex_t* reg) + break; + } + ++ if (r > (int )MAX_COMPILED_PROGRAM_SIZE) ++ return ONIGERR_TOO_BIG_COMPILED_PROGRAM; + return r; + } + +@@ -1765,6 +1768,8 @@ compile_tree(Node* node, regex_t* reg) + if (NCDR(x) != NULL) { + len += SIZE_OP_PUSH + SIZE_OP_JUMP; + } ++ if (len > (int )MAX_COMPILED_PROGRAM_SIZE) ++ return ONIGERR_TOO_BIG_COMPILED_PROGRAM; + } while (IS_NOT_NULL(x = NCDR(x))); + pos = reg->used + len; /* goal position */ + +@@ -5789,7 +5794,11 @@ onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, + UnsetAddrList uslist; + #endif + +- if (IS_NOT_NULL(einfo)) einfo->par = (UChar* )NULL; ++ if (IS_NOT_NULL(einfo)) { ++ einfo->enc = reg->enc; ++ einfo->par = (UChar* )NULL; ++ einfo->par_end = (UChar* )NULL; ++ } + + #ifdef RUBY + scan_env.sourcefile = sourcefile; +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 8f8473fe03..575ec65fbb 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1885,4 +1885,11 @@ def test_nested_repeat_expansion_overflow + end + end; + end ++ ++ def test_too_big_compiled_program_length ++ k, rest = (2**32 - 217).divmod(100_001) ++ assert_raise_with_message(RegexpError, /too big compiled program/) do ++ Regexp.new("(?:#{'.{100000,}' * k}.{#{rest - 1},})?") ++ end ++ end + end From 0e452f6fd099d02c90a710ea306a256077b75e41 Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:08:24 +0000 Subject: [PATCH 2/7] ruby-3.2: Backport fix for GHSA-7x2q-68qv-6p8x Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 4 ++ ...-fix-cclass-intersection-double-free.patch | 39 +++++++++++++ ...egexp-fix-cclass-property-error-leak.patch | 48 ++++++++++++++++ ...xp-fix-match-cache-stack-double-free.patch | 56 +++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 resources/patches/ruby_32/0010-regexp-fix-cclass-intersection-double-free.patch create mode 100644 resources/patches/ruby_32/0011-regexp-fix-cclass-property-error-leak.patch create mode 100644 resources/patches/ruby_32/0012-regexp-fix-match-cache-stack-double-free.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index 889cb3ac..1c8d7552 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -43,6 +43,10 @@ pkg.apply_patch "#{base}/0007-regexp-bound-compiled-program-writes.patch" pkg.apply_patch "#{base}/0008-regexp-harden-bbuf-size-arithmetic.patch" pkg.apply_patch "#{base}/0009-regexp-check-program-size-in-length-pass.patch" + # GHSA-7x2q-68qv-6p8x, double free in regexp compilation + pkg.apply_patch "#{base}/0010-regexp-fix-cclass-intersection-double-free.patch" + pkg.apply_patch "#{base}/0011-regexp-fix-cclass-property-error-leak.patch" + pkg.apply_patch "#{base}/0012-regexp-fix-match-cache-stack-double-free.patch" if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" diff --git a/resources/patches/ruby_32/0010-regexp-fix-cclass-intersection-double-free.patch b/resources/patches/ruby_32/0010-regexp-fix-cclass-intersection-double-free.patch new file mode 100644 index 00000000..732c21f1 --- /dev/null +++ b/resources/patches/ruby_32/0010-regexp-fix-cclass-intersection-double-free.patch @@ -0,0 +1,39 @@ +From: Yusuke Endoh +Date: 2026-10-29T00:48:17+09:00 +Subject: [PATCH 4/15] Fix a double free in the && handler of parse_char_class + +Backport of Ruby 3.3 commit 0941ceda29041d48db145cfd5eadf9e9019bb566 +(ruby/ruby#20001, GHSA-7x2q-68qv-6p8x). + +--- +diff --git a/regparse.c b/regparse.c +index 59287880d2..d247fc96af 100644 +--- a/regparse.c ++++ b/regparse.c +@@ -4849,6 +4849,7 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + r = and_cclass(prev_cc, cc, env); + if (r != 0) goto err; + bbuf_free(cc->mbuf); ++ cc->mbuf = NULL; + if (IS_NOT_NULL(asc_cc)) { + r = and_cclass(asc_prev_cc, asc_cc, env); + if (r != 0) goto err; +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 575ec65fbb..a042826ff1 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1288,6 +1288,14 @@ def test_posix_bracket + assert_no_match(/[[:ascii:]]/, "\x80\xFF") + end + ++ def test_too_many_multibyte_ranges_in_intersection ++ chars = (0...9999).map {|i| (0x4e00 + 2 * i).chr("UTF-8") }.join ++ cc = "[[:^ascii:][^#{chars}]&&[^\u9fff]&&x]" ++ assert_raise(RegexpError) { Regexp.new(cc, Regexp::IGNORECASE) } ++ assert_raise(RegexpError) { Regexp.new("[x&&#{cc}]", Regexp::IGNORECASE) } ++ assert_raise(RegexpError) { Regexp.new(cc.encode("UTF-16LE"), Regexp::IGNORECASE) } ++ end ++ + def test_cclass_R + assert_match(/\A\R\z/, "\r") + assert_match(/\A\R\z/, "\n") diff --git a/resources/patches/ruby_32/0011-regexp-fix-cclass-property-error-leak.patch b/resources/patches/ruby_32/0011-regexp-fix-cclass-property-error-leak.patch new file mode 100644 index 00000000..d4c30659 --- /dev/null +++ b/resources/patches/ruby_32/0011-regexp-fix-cclass-property-error-leak.patch @@ -0,0 +1,48 @@ +From: Yusuke Endoh +Date: 2026-10-30T16:39:52+09:00 +Subject: [PATCH 5/15] Fix a memory leak on property errors in parse_char_class + +Backport of Ruby 3.3 commit 6894481ee2428dc65739675be10b5f601c60cbdb +(ruby/ruby#20001, GHSA-7x2q-68qv-6p8x). +Without the upstream test. + +--- +diff --git a/regparse.c b/regparse.c +index d247fc96af..eed959aaab 100644 +--- a/regparse.c ++++ b/regparse.c +@@ -4724,12 +4724,12 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + case TK_CHAR_TYPE: + r = add_ctype_to_cc(cc, tok->u.prop.ctype, tok->u.prop.not, + IS_ASCII_RANGE(env->option), env); +- if (r != 0) return r; ++ if (r != 0) goto err; + if (IS_NOT_NULL(asc_cc)) { + if (tok->u.prop.ctype != ONIGENC_CTYPE_WORD) + r = add_ctype_to_cc(asc_cc, tok->u.prop.ctype, tok->u.prop.not, + IS_ASCII_RANGE(env->option), env); +- if (r != 0) return r; ++ if (r != 0) goto err; + } + + next_class: +@@ -4742,13 +4742,16 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + int ctype; + + ctype = fetch_char_property_to_ctype(&p, end, env); +- if (ctype < 0) return ctype; ++ if (ctype < 0) { ++ r = ctype; ++ goto err; ++ } + r = add_ctype_to_cc(cc, ctype, tok->u.prop.not, 0, env); +- if (r != 0) return r; ++ if (r != 0) goto err; + if (IS_NOT_NULL(asc_cc)) { + if (ctype != ONIGENC_CTYPE_ASCII) + r = add_ctype_to_cc(asc_cc, ctype, tok->u.prop.not, 0, env); +- if (r != 0) return r; ++ if (r != 0) goto err; + } + goto next_class; + } diff --git a/resources/patches/ruby_32/0012-regexp-fix-match-cache-stack-double-free.patch b/resources/patches/ruby_32/0012-regexp-fix-match-cache-stack-double-free.patch new file mode 100644 index 00000000..8d5e82e1 --- /dev/null +++ b/resources/patches/ruby_32/0012-regexp-fix-match-cache-stack-double-free.patch @@ -0,0 +1,56 @@ +From: Yusuke Endoh +Date: 2026-10-30T16:42:53+09:00 +Subject: [PATCH 6/15] Fix a double free of the match stack on match cache errors + +Backport of Ruby 3.3 commit ab4a26435653382426f7dec4f16f72d4e7686e73 +(ruby/ruby#20001, GHSA-7x2q-68qv-6p8x). +Adapted to the older match cache of Ruby 3.2. + +--- +diff --git a/regexec.c b/regexec.c +index c45cc54d66..8238326b93 100644 +--- a/regexec.c ++++ b/regexec.c +@@ -3906,7 +3906,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, + if (msa->cache_index_table == NULL) { + OnigCacheIndex *table = (OnigCacheIndex *)xmalloc(msa->num_cache_table * sizeof(OnigCacheIndex)); + if (table == NULL) { +- return ONIGERR_MEMORY; ++ goto memory_error; + } + OnigPosition r = init_cache_index_table(reg, table); + if (r < 0) { +@@ -3919,16 +3919,16 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, + size_t match_cache_size8 = (size_t)msa->num_cache_opcode * len; + /* overflow check */ + if (match_cache_size8 / len != (size_t)msa->num_cache_opcode) { +- return ONIGERR_MEMORY; ++ goto memory_error; + } + /* Currently, int is used for the key of match_cache */ + if (match_cache_size8 >= LONG_MAX_LIMIT) { +- return ONIGERR_MEMORY; ++ goto memory_error; + } + size_t match_cache_size = (match_cache_size8 >> 3) + (match_cache_size8 & 7 ? 1 : 0); + msa->match_cache = (uint8_t*)xmalloc(match_cache_size * sizeof(uint8_t)); + if (msa->match_cache == NULL) { +- return ONIGERR_MEMORY; ++ goto memory_error; + } + xmemset(msa->match_cache, 0, match_cache_size * sizeof(uint8_t)); + } +@@ -3971,6 +3971,13 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, + STACK_SAVE; + if (xmalloc_base) xfree(xmalloc_base); + return ONIGERR_UNEXPECTED_BYTECODE; ++ ++#ifdef USE_CACHE_MATCH_OPT ++ memory_error: ++ STACK_SAVE; ++ if (xmalloc_base) xfree(xmalloc_base); ++ return ONIGERR_MEMORY; ++#endif + } + + From 30fed8b9c805df859173713e657684044473a8f4 Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:08:32 +0000 Subject: [PATCH 3/7] ruby-3.2: Backport fix for GHSA-wpqw-5qxg-m68r Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 6 ++ ...regexp-check-ignore-case-fold-length.patch | 66 +++++++++++++++ ...egexp-reject-incomplete-byte-escapes.patch | 84 +++++++++++++++++++ ...gexp-keep-comment-as-token-separator.patch | 66 +++++++++++++++ ...gexp-fix-emacs-mule-case-fold-length.patch | 64 ++++++++++++++ ...xp-reject-invalid-code-point-escapes.patch | 79 +++++++++++++++++ 6 files changed, 365 insertions(+) create mode 100644 resources/patches/ruby_32/0013-regexp-check-ignore-case-fold-length.patch create mode 100644 resources/patches/ruby_32/0014-regexp-reject-incomplete-byte-escapes.patch create mode 100644 resources/patches/ruby_32/0015-regexp-keep-comment-as-token-separator.patch create mode 100644 resources/patches/ruby_32/0016-regexp-fix-emacs-mule-case-fold-length.patch create mode 100644 resources/patches/ruby_32/0017-regexp-reject-invalid-code-point-escapes.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index 1c8d7552..0948b0c5 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -47,6 +47,12 @@ pkg.apply_patch "#{base}/0010-regexp-fix-cclass-intersection-double-free.patch" pkg.apply_patch "#{base}/0011-regexp-fix-cclass-property-error-leak.patch" pkg.apply_patch "#{base}/0012-regexp-fix-match-cache-stack-double-free.patch" + # GHSA-wpqw-5qxg-m68r, out-of-bounds read in case-insensitive regexp matching + pkg.apply_patch "#{base}/0013-regexp-check-ignore-case-fold-length.patch" + pkg.apply_patch "#{base}/0014-regexp-reject-incomplete-byte-escapes.patch" + pkg.apply_patch "#{base}/0015-regexp-keep-comment-as-token-separator.patch" + pkg.apply_patch "#{base}/0016-regexp-fix-emacs-mule-case-fold-length.patch" + pkg.apply_patch "#{base}/0017-regexp-reject-invalid-code-point-escapes.patch" if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" diff --git a/resources/patches/ruby_32/0013-regexp-check-ignore-case-fold-length.patch b/resources/patches/ruby_32/0013-regexp-check-ignore-case-fold-length.patch new file mode 100644 index 00000000..60b19dc8 --- /dev/null +++ b/resources/patches/ruby_32/0013-regexp-check-ignore-case-fold-length.patch @@ -0,0 +1,66 @@ +From: Yusuke Endoh +Date: 2026-10-29T00:39:31+09:00 +Subject: [PATCH 7/15] Fix an out-of-bounds read in the case-insensitive exact match + +Backport of Ruby 3.3 commit d0e82b6b854046d54d722fb455e76471dcf316b6 +(ruby/ruby#20002, GHSA-wpqw-5qxg-m68r). + +--- +diff --git a/regexec.c b/regexec.c +index 8238326b93..e7f7276990 100644 +--- a/regexec.c ++++ b/regexec.c +@@ -2494,6 +2494,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, + case_fold_flag, + &s, end, lowbuf); + DATA_ENSURE(0); ++ if (len != enclen(encode, p, reg->p + reg->used)) goto fail; + q = lowbuf; + while (len-- > 0) { + if (*p != *q) { +@@ -2583,6 +2584,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, + case_fold_flag, + &s, end, lowbuf); + DATA_ENSURE(0); ++ if (len > endp - p) goto fail; + q = lowbuf; + while (len-- > 0) { + if (*p != *q) goto fail; +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index a042826ff1..4aa5988b95 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1413,6 +1413,33 @@ def test_unicode + assert_match(/^\u03b9\u0308\u0301$/i, "\u0390") + end + ++ def test_ignorecase_fold_longer_than_operand ++ %w[UTF-16LE UTF-16BE UTF-32LE UTF-32BE].each do |enc| ++ tail = "\u0103\u3042\u3044" ++ s = ("x" * 16 + "\u1E98" + "y" * 8).encode(enc) ++ re = Regexp.new(("x" * 16 + "(?i:w)" + tail).encode(enc)) ++ assert_match(re, ("x" * 16 + "W" + tail).encode(enc)) ++ assert_no_match(re, s) ++ re = Regexp.new(("(?i:" + "x" * 16 + "w)" + tail).encode(enc)) ++ assert_match(re, ("x" * 16 + "W" + tail).encode(enc)) ++ assert_no_match(re, s) ++ re = Regexp.new("(?-i:xyz)t(?-i:\u0103)".encode(enc), Regexp::IGNORECASE) ++ assert_match(re, "xyzT\u0103".encode(enc)) ++ assert_no_match(re, "xyz\u1E97".encode(enc)) ++ end ++ ++ assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") ++ begin; ++ enc = Encoding::UTF_16LE ++ re = Regexp.new("(?i:w)\u6203AB".encode(enc)) ++ assert_match(re, "W\u6203AB".encode(enc)) ++ assert_no_match(re, "\u1E98\u6203AB".encode(enc)) ++ re = Regexp.new("(?i:xw)\u6203AB".encode(enc)) ++ assert_match(re, "xW\u6203AB".encode(enc)) ++ assert_no_match(re, "x\u1E98\u6203AB".encode(enc)) ++ end; ++ end ++ + def test_unicode_age + assert_match(/^\p{Age=6.0}$/u, "\u261c") + assert_match(/^\p{Age=1.1}$/u, "\u261c") diff --git a/resources/patches/ruby_32/0014-regexp-reject-incomplete-byte-escapes.patch b/resources/patches/ruby_32/0014-regexp-reject-incomplete-byte-escapes.patch new file mode 100644 index 00000000..94634c35 --- /dev/null +++ b/resources/patches/ruby_32/0014-regexp-reject-incomplete-byte-escapes.patch @@ -0,0 +1,84 @@ +From: Yusuke Endoh +Date: 2026-11-03T12:47:19+09:00 +Subject: [PATCH 8/15] Reject incomplete characters written as byte escapes in regexps + +Backport of Ruby 3.3 commit c1232cb11fffa32fc9526628a0c54e0a56d490e0 +(ruby/ruby#20002, GHSA-wpqw-5qxg-m68r). + +--- +diff --git a/regparse.c b/regparse.c +index eed959aaab..a8f91d3436 100644 +--- a/regparse.c ++++ b/regparse.c +@@ -4640,7 +4640,6 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + /* tok->base != 0 : octal or hexadec. */ + if (! ONIGENC_IS_SINGLEBYTE(env->enc) && tok->base != 0) { + UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN]; +- UChar* bufe = buf + ONIGENC_CODE_TO_MBC_MAXLEN; + UChar* psave = p; + int i, base = tok->base; + +@@ -4660,9 +4659,10 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + goto err; + } + +- len = enclen(env->enc, buf, buf + i); +- if (i < len) { +- r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING; ++ len = ONIGENC_PRECISE_MBC_ENC_LEN(env->enc, buf, buf + i); ++ if (! ONIGENC_MBCLEN_CHARFOUND_P(len)) { ++ r = ONIGENC_MBCLEN_NEEDMORE_P(len) ? ++ ONIGERR_TOO_SHORT_MULTI_BYTE_STRING : ONIGERR_INVALID_CODE_POINT_VALUE; + goto err; + } + else if (i > len) { /* fetch back */ +@@ -4679,7 +4679,7 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + goto raw_single; + } + else { +- v = ONIGENC_MBC_TO_CODE(env->enc, buf, bufe); ++ v = ONIGENC_MBC_TO_CODE(env->enc, buf, buf + len); + in_type = CCV_CODE_POINT; + } + } +@@ -6227,11 +6227,15 @@ parse_exp(Node** np, OnigToken* tok, int term, + len = 1; + while (1) { + if (len >= ONIGENC_MBC_MINLEN(env->enc)) { +- if (len == enclen(env->enc, NSTR(*np)->s, NSTR(*np)->end)) { ++ int n = ONIGENC_IS_SINGLEBYTE(env->enc) ? 1 : ++ ONIGENC_PRECISE_MBC_ENC_LEN(env->enc, NSTR(*np)->s, NSTR(*np)->end); ++ if (n == len) { + r = fetch_token(tok, src, end, env); + NSTRING_CLEAR_RAW(*np); + goto string_end; + } ++ if (! ONIGENC_MBCLEN_NEEDMORE_P(n)) ++ return ONIGERR_INVALID_CODE_POINT_VALUE; + } + + r = fetch_token(tok, src, end, env); +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 4aa5988b95..32a5a4bf6a 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1220,6 +1220,19 @@ def test_parse_comment + failcheck('(?#') + end + ++ def test_invalid_raw_byte_escape_in_utf16 ++ {"UTF-16LE" => ['\x00\xD8', '\x00\xD8\x00\xDC'], ++ "UTF-16BE" => ['\xD8\x00', '\xD8\x00\xDC\x00']}.each do |enc, (lone, pair)| ++ assert_raise(RegexpError) { Regexp.new(lone.encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("(?i)#{lone}".encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("[#{lone}]".encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("[#{lone}a]".encode(enc)) } ++ s = "\u{10000}".encode(enc) ++ assert_match(Regexp.new(pair.encode(enc)), s) ++ assert_match(Regexp.new("[#{pair}]".encode(enc)), s) ++ end ++ end ++ + def test_char_type + check(/\u3042\d/, ["\u30421", "\u30422"]) + diff --git a/resources/patches/ruby_32/0015-regexp-keep-comment-as-token-separator.patch b/resources/patches/ruby_32/0015-regexp-keep-comment-as-token-separator.patch new file mode 100644 index 00000000..1ddb1bbf --- /dev/null +++ b/resources/patches/ruby_32/0015-regexp-keep-comment-as-token-separator.patch @@ -0,0 +1,66 @@ +From: Yusuke Endoh +Date: 2026-10-28T23:01:38+09:00 +Subject: [PATCH 9/15] Keep an empty comment as a token separator in regexp preprocessing + +Backport of Ruby 3.3 commit ae33ea2a8e6fd2f83f52ed892df805e03c5d74e5 +(ruby/ruby#20002, GHSA-wpqw-5qxg-m68r). + +--- +diff --git a/re.c b/re.c +index f357d38c63..2eb941b3d2 100644 +--- a/re.c ++++ b/re.c +@@ -2931,6 +2931,7 @@ escape_asis: + *encp = enc; + } + } ++ rb_str_buf_cat(buf, "(?#)", 4); + break; + } + rb_str_buf_cat(buf, (char *)&c, 1); +@@ -2989,6 +2990,9 @@ escape_asis: + c = '('; + rb_str_buf_cat(buf, (char *)&c, 1); + } ++ else { ++ rb_str_buf_cat(buf, "(?#)", 4); ++ } + break; + } + else { +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 32a5a4bf6a..8bd69c3abf 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1233,6 +1233,31 @@ def test_invalid_raw_byte_escape_in_utf16 + end + end + ++ def test_comment_separates_tokens ++ assert_raise(RegexpError) { Regexp.new('\3(?#)43') } ++ assert_raise(RegexpError) { Regexp.new("\\3#c\n43", Regexp::EXTENDED) } ++ assert_raise(RegexpError) { Regexp.new("(?x:\\3#c\n43)") } ++ re = Regexp.new('(a)(b)(c)\3(?#)43') ++ assert_match(re, "abcc43") ++ assert_not_match(re, "abc\u00e3") ++ re = Regexp.new("(a)(b)(c)\\3#c\n43", Regexp::EXTENDED) ++ assert_match(re, "abcc43") ++ assert_not_match(re, "abc\u00e3") ++ assert_match(/a(?#x)+b/, "aaab") ++ assert_match(/a #x ++ +b/x, "aaab") ++ ++ assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") ++ begin; ++ assert_raise(RegexpError) { ++ Regexp.new('(?~.*\3(?#)43.|(?i:\u{3380}))').match?("a\u3042") ++ } ++ assert_raise(RegexpError) { ++ Regexp.new("(?~.*(?x:\\3#c\n43).|(?i:\\u{3380}))").match?("a\u3042") ++ } ++ end; ++ end ++ + def test_char_type + check(/\u3042\d/, ["\u30421", "\u30422"]) + diff --git a/resources/patches/ruby_32/0016-regexp-fix-emacs-mule-case-fold-length.patch b/resources/patches/ruby_32/0016-regexp-fix-emacs-mule-case-fold-length.patch new file mode 100644 index 00000000..a1b5eb43 --- /dev/null +++ b/resources/patches/ruby_32/0016-regexp-fix-emacs-mule-case-fold-length.patch @@ -0,0 +1,64 @@ +From: Yusuke Endoh +Date: 2026-10-28T23:33:28+09:00 +Subject: [PATCH 10/15] Never return a negative length from the Emacs-Mule case-fold callback + +Backport of Ruby 3.3 commit a479eb156def4b55d502c99b090429479ede2931 +(ruby/ruby#20002, GHSA-wpqw-5qxg-m68r). + +--- +diff --git a/enc/emacs_mule.c b/enc/emacs_mule.c +index f92eb183cf..c7d93d2f53 100644 +--- a/enc/emacs_mule.c ++++ b/enc/emacs_mule.c +@@ -272,24 +272,7 @@ mbc_case_fold(OnigCaseFoldType flag, + const UChar** pp, const UChar* end, UChar* lower, + OnigEncoding enc) + { +- int len; +- const UChar* p = *pp; +- +- if (ONIGENC_IS_MBC_ASCII(p)) { +- *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p); +- (*pp)++; +- return 1; +- } +- else { +- int i; +- +- len = mbc_enc_len(p, end, enc); +- for (i = 0; i < len; i++) { +- *lower++ = *p++; +- } +- (*pp) += len; +- return len; /* return byte length of converted char to lower */ +- } ++ return onigenc_mbn_mbc_case_fold(enc, flag, pp, end, lower); + } + + static UChar* +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 8bd69c3abf..dc573c815f 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1258,6 +1258,21 @@ def test_comment_separates_tokens + end; + end + ++ def test_emacs_mule_case_fold_at_mid_character ++ assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") ++ begin; ++ %w[Emacs-Mule stateless-ISO-2022-JP stateless-ISO-2022-JP-KDDI].each do |name| ++ enc = Encoding.find(name) ++ re = Regexp.new("(?i)(?!x)\x81\xa1{2}".b.force_encoding(enc)) ++ assert_nil(re.match("\x81\xa1\x9c\xf0\xa1\xa1".b.force_encoding(enc))) ++ assert_equal(0, re =~ "\x81\xa1\x81\xa1".b.force_encoding(enc)) ++ assert_equal(1, re =~ "\x81\xa2\x81\xa1\x81\xa1".b.force_encoding(enc)) ++ re = Regexp.new("(?i)\x81\xa1".b.force_encoding(enc)) ++ assert_equal(1, re =~ "\x9c\xf0\xa1\xa1\x81\xa1".b.force_encoding(enc)) ++ end ++ end; ++ end ++ + def test_char_type + check(/\u3042\d/, ["\u30421", "\u30422"]) + diff --git a/resources/patches/ruby_32/0017-regexp-reject-invalid-code-point-escapes.patch b/resources/patches/ruby_32/0017-regexp-reject-invalid-code-point-escapes.patch new file mode 100644 index 00000000..6720e84e --- /dev/null +++ b/resources/patches/ruby_32/0017-regexp-reject-invalid-code-point-escapes.patch @@ -0,0 +1,79 @@ +From: Yusuke Endoh +Date: 2026-10-29T14:04:57+09:00 +Subject: [PATCH 11/15] Reject surrogates and out-of-range code points in regexp code point escapes + +Backport of Ruby 3.3 commit bdc1368e514ac0d87ffec028adff00a8e51a38cc +(ruby/ruby#20002, GHSA-wpqw-5qxg-m68r). + +--- +diff --git a/regparse.c b/regparse.c +index a8f91d3436..84d202e7bd 100644 +--- a/regparse.c ++++ b/regparse.c +@@ -1443,6 +1443,15 @@ node_str_cat_codepoint(Node* node, OnigEncoding enc, OnigCodePoint c) + return onig_node_str_cat(node, buf, buf + num); + } + ++static int ++check_code_point(OnigCodePoint code, OnigEncoding enc) ++{ ++ if (ONIGENC_IS_UNICODE(enc) && ++ (code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff))) ++ return ONIGERR_INVALID_CODE_POINT_VALUE; ++ return 0; ++} ++ + #if 0 + extern void + onig_node_conv_to_str_node(Node* node, int flag) +@@ -4694,6 +4703,8 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e + + case TK_CODE_POINT: + v = tok->u.code; ++ r = check_code_point(v, env->enc); ++ if (r != 0) goto err; + in_israw = 1; + val_entry: + len = ONIGENC_CODE_TO_MBCLEN(env->enc, v); +@@ -6204,6 +6215,8 @@ parse_exp(Node** np, OnigToken* tok, int term, + } + #ifndef NUMBERED_CHAR_IS_NOT_CASE_AMBIG + else if (r == TK_CODE_POINT) { ++ r = check_code_point(tok->u.code, env->enc); ++ if (r != 0) return r; + r = node_str_cat_codepoint(*np, env->enc, tok->u.code); + } + #endif +@@ -6266,6 +6279,8 @@ parse_exp(Node** np, OnigToken* tok, int term, + + case TK_CODE_POINT: + { ++ r = check_code_point(tok->u.code, env->enc); ++ if (r != 0) return r; + *np = node_new_empty(); + CHECK_NULL_RETURN_MEMERR(*np); + r = node_str_cat_codepoint(*np, env->enc, tok->u.code); +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index dc573c815f..0eaa90007f 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1273,6 +1273,19 @@ def test_emacs_mule_case_fold_at_mid_character + end; + end + ++ def test_invalid_code_point_escape_in_utf16 ++ %w[UTF-16LE UTF-16BE UTF-32LE UTF-32BE].each do |enc| ++ assert_raise(RegexpError) { Regexp.new("\\x{D801}".encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("a\\x{D801}".encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("a\\x{110000}".encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("[\\x{D801}]".encode(enc)) } ++ assert_raise(RegexpError) { Regexp.new("\\x{110000}".encode(enc)) } ++ s = "\u{1F600}".encode(enc) ++ assert_match(Regexp.new("\\x{1F600}".encode(enc)), s) ++ assert_match(Regexp.new("[\\x{1F600}]".encode(enc)), s) ++ end ++ end ++ + def test_char_type + check(/\u3042\d/, ["\u30421", "\u30422"]) + From 0cd920e6ce86a067600494b42094aba89118cbbc Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:08:38 +0000 Subject: [PATCH 4/7] ruby-3.2: Backport fix for GHSA-pvgc-g7gj-pv6h Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 2 + ...-regexp-check-subject-end-in-backref.patch | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 resources/patches/ruby_32/0018-regexp-check-subject-end-in-backref.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index 0948b0c5..fd3e4d79 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -53,6 +53,8 @@ pkg.apply_patch "#{base}/0015-regexp-keep-comment-as-token-separator.patch" pkg.apply_patch "#{base}/0016-regexp-fix-emacs-mule-case-fold-length.patch" pkg.apply_patch "#{base}/0017-regexp-reject-invalid-code-point-escapes.patch" + # GHSA-pvgc-g7gj-pv6h, out-of-bounds read in case-insensitive backreferences + pkg.apply_patch "#{base}/0018-regexp-check-subject-end-in-backref.patch" if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" diff --git a/resources/patches/ruby_32/0018-regexp-check-subject-end-in-backref.patch b/resources/patches/ruby_32/0018-regexp-check-subject-end-in-backref.patch new file mode 100644 index 00000000..8db3e1be --- /dev/null +++ b/resources/patches/ruby_32/0018-regexp-check-subject-end-in-backref.patch @@ -0,0 +1,37 @@ +From: Hiroya Fujinami +Date: 2026-10-30T16:30:19+09:00 +Subject: [PATCH 12/15] Check the end of the subject in string_cmp_ic + +Backport of Ruby 3.3 commit 1184052e023c95a7883a4f68c0b22883109a5a0d +(ruby/ruby#20003, GHSA-pvgc-g7gj-pv6h). + +--- +diff --git a/regexec.c b/regexec.c +index e7f7276990..be4f870d7f 100644 +--- a/regexec.c ++++ b/regexec.c +@@ -1782,6 +1782,7 @@ static int string_cmp_ic(OnigEncoding enc, int case_fold_flag, + s2 = *ps2; + end1 = s1 + mblen; + while (s1 < end1) { ++ if (s2 >= text_end) return 0; + len1 = ONIGENC_MBC_CASE_FOLD(enc, case_fold_flag, &s1, text_end, buf1); + len2 = ONIGENC_MBC_CASE_FOLD(enc, case_fold_flag, &s2, text_end, buf2); + if (len1 != len2) return 0; +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 0eaa90007f..0df6da9647 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -1506,6 +1506,12 @@ def test_ignorecase_fold_longer_than_operand + end; + end + ++ def test_backref_ignorecase_at_end_of_string ++ assert_no_match(/(k\0)\1/i, "k\0\u212a") ++ assert_match(/(k)\1/i, "k\u212a") ++ assert_match(/(k\0)\1/i, "k\0\u212a\0") ++ end ++ + def test_unicode_age + assert_match(/^\p{Age=6.0}$/u, "\u261c") + assert_match(/^\p{Age=1.1}$/u, "\u261c") From 2927687c51b3f6de7a71e9941f74391422fb2310 Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:08:44 +0000 Subject: [PATCH 5/7] ruby-3.2: Backport fix for GHSA-7whj-cfh7-xgm4 Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 2 + ...019-regexp-preprocess-source-in-to-s.patch | 79 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 resources/patches/ruby_32/0019-regexp-preprocess-source-in-to-s.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index fd3e4d79..617058e4 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -55,6 +55,8 @@ pkg.apply_patch "#{base}/0017-regexp-reject-invalid-code-point-escapes.patch" # GHSA-pvgc-g7gj-pv6h, out-of-bounds read in case-insensitive backreferences pkg.apply_patch "#{base}/0018-regexp-check-subject-end-in-backref.patch" + # GHSA-7whj-cfh7-xgm4, heap buffer overflow in Regexp#to_s + pkg.apply_patch "#{base}/0019-regexp-preprocess-source-in-to-s.patch" if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" diff --git a/resources/patches/ruby_32/0019-regexp-preprocess-source-in-to-s.patch b/resources/patches/ruby_32/0019-regexp-preprocess-source-in-to-s.patch new file mode 100644 index 00000000..0f9cfcb2 --- /dev/null +++ b/resources/patches/ruby_32/0019-regexp-preprocess-source-in-to-s.patch @@ -0,0 +1,79 @@ +From: Yusuke Endoh +Date: 2026-11-09T03:27:00+09:00 +Subject: [PATCH 13/15] Preprocess the source before Regexp#to_s checks it with Onigmo + +Backport of Ruby 3.3 commit e6011259c9387ba925b90a15434107d2eef5bfa1 +(ruby/ruby#20004, GHSA-7whj-cfh7-xgm4). + +--- +diff --git a/re.c b/re.c +index 2eb941b3d2..dd2d62e106 100644 +--- a/re.c ++++ b/re.c +@@ -561,6 +561,10 @@ rb_reg_to_s(VALUE re) + return rb_reg_str_with_term(re, '/'); + } + ++static VALUE ++rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc, ++ rb_encoding **fixed_enc, onig_errmsg_buffer err, int options); ++ + static VALUE + rb_reg_str_with_term(VALUE re, int term) + { +@@ -616,14 +620,23 @@ rb_reg_str_with_term(VALUE re, int term) + if (*ptr == ':' && ptr[len-1] == ')') { + Regexp *rp; + VALUE verbose = ruby_verbose; +- ruby_verbose = Qfalse; ++ rb_encoding *fixed_enc = 0; ++ onig_errmsg_buffer errmsg = ""; ++ VALUE unescaped; + + ++ptr; + len -= 2; +- err = onig_new(&rp, ptr, ptr + len, options, +- enc, OnigDefaultSyntax, NULL); +- onig_free(rp); +- ruby_verbose = verbose; ++ unescaped = rb_reg_preprocess((const char *)ptr, (const char *)ptr + len, ++ enc, &fixed_enc, errmsg, options); ++ if (!NIL_P(unescaped)) { ++ ruby_verbose = Qfalse; ++ err = onig_new(&rp, (UChar *)RSTRING_PTR(unescaped), ++ (UChar *)RSTRING_END(unescaped), options, ++ enc, OnigDefaultSyntax, NULL); ++ onig_free(rp); ++ ruby_verbose = verbose; ++ RB_GC_GUARD(unescaped); ++ } + } + if (err) { + options = RREGEXP_PTR(re)->options; +@@ -1530,10 +1543,6 @@ rb_reg_fixed_encoding_p(VALUE re) + return RBOOL(FL_TEST(re, KCODE_FIXED)); + } + +-static VALUE +-rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc, +- rb_encoding **fixed_enc, onig_errmsg_buffer err, int options); +- + NORETURN(static void reg_enc_error(VALUE re, VALUE str)); + + static void +diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb +index 0df6da9647..f0587b2b89 100644 +--- a/test/ruby/test_regexp.rb ++++ b/test/ruby/test_regexp.rb +@@ -85,6 +85,11 @@ def test_to_s + end + end + ++ def test_to_s_comment_in_char_class ++ re = EnvUtil.suppress_warning { Regexp.new('(?:[](?#\x{7FFFFFFF})])') } ++ assert_equal('(?-mix:[](?#\x{7FFFFFFF})])', EnvUtil.suppress_warning { re.to_s }) ++ end ++ + def test_to_s_extended_subexp + re = /#\g#{"\n"}/x + re = /#{re}/ From a08c857093af2ba2d88f62b5bacdcc6c03ce548d Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:08:51 +0000 Subject: [PATCH 6/7] ruby-3.2: Backport fix for GHSA-34m4-m9fh-gqp5 Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 2 + ...string-fix-sharp-s-case-map-overflow.patch | 222 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 resources/patches/ruby_32/0020-string-fix-sharp-s-case-map-overflow.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index 617058e4..2c564801 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -57,6 +57,8 @@ pkg.apply_patch "#{base}/0018-regexp-check-subject-end-in-backref.patch" # GHSA-7whj-cfh7-xgm4, heap buffer overflow in Regexp#to_s pkg.apply_patch "#{base}/0019-regexp-preprocess-source-in-to-s.patch" + # GHSA-34m4-m9fh-gqp5, heap buffer overflow in case conversion of strings + pkg.apply_patch "#{base}/0020-string-fix-sharp-s-case-map-overflow.patch" if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" diff --git a/resources/patches/ruby_32/0020-string-fix-sharp-s-case-map-overflow.patch b/resources/patches/ruby_32/0020-string-fix-sharp-s-case-map-overflow.patch new file mode 100644 index 00000000..960ca399 --- /dev/null +++ b/resources/patches/ruby_32/0020-string-fix-sharp-s-case-map-overflow.patch @@ -0,0 +1,222 @@ +From: Yusuke Endoh +Date: 2026-10-28T22:55:06+09:00 +Subject: [PATCH 14/15] Fix a heap buffer overflow in case mapping of the sharp s + +Backport of Ruby 3.3 commit d8d4c02fff5a3f6418ed3c331b0fe116727b8eac +(ruby/ruby#20005, GHSA-34m4-m9fh-gqp5). + +--- +diff --git a/enc/iso_8859_1.c b/enc/iso_8859_1.c +index 7af0888c3e..5535358bb7 100644 +--- a/enc/iso_8859_1.c ++++ b/enc/iso_8859_1.c +@@ -264,7 +264,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_10.c b/enc/iso_8859_10.c +index cae4be2db0..33dbab68ef 100644 +--- a/enc/iso_8859_10.c ++++ b/enc/iso_8859_10.c +@@ -233,7 +233,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_13.c b/enc/iso_8859_13.c +index fe1ddd7065..05862533f1 100644 +--- a/enc/iso_8859_13.c ++++ b/enc/iso_8859_13.c +@@ -226,7 +226,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_14.c b/enc/iso_8859_14.c +index 647514a016..be347d7592 100644 +--- a/enc/iso_8859_14.c ++++ b/enc/iso_8859_14.c +@@ -235,7 +235,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_15.c b/enc/iso_8859_15.c +index 377a3afc7b..6ccb5f906d 100644 +--- a/enc/iso_8859_15.c ++++ b/enc/iso_8859_15.c +@@ -229,7 +229,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_16.c b/enc/iso_8859_16.c +index 135630eb73..47aff1e120 100644 +--- a/enc/iso_8859_16.c ++++ b/enc/iso_8859_16.c +@@ -231,7 +231,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_2.c b/enc/iso_8859_2.c +index 3a05c6320d..3e3d506d51 100644 +--- a/enc/iso_8859_2.c ++++ b/enc/iso_8859_2.c +@@ -229,7 +229,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_3.c b/enc/iso_8859_3.c +index 2a343eac63..8c78fdd64d 100644 +--- a/enc/iso_8859_3.c ++++ b/enc/iso_8859_3.c +@@ -231,7 +231,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_4.c b/enc/iso_8859_4.c +index e2134e8c0b..24f78bb7c4 100644 +--- a/enc/iso_8859_4.c ++++ b/enc/iso_8859_4.c +@@ -232,7 +232,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/iso_8859_9.c b/enc/iso_8859_9.c +index 004eec310f..4670823283 100644 +--- a/enc/iso_8859_9.c ++++ b/enc/iso_8859_9.c +@@ -224,7 +224,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/windows_1250.c b/enc/windows_1250.c +index daf23e9d1e..62a3cbb1ed 100644 +--- a/enc/windows_1250.c ++++ b/enc/windows_1250.c +@@ -199,7 +199,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/windows_1252.c b/enc/windows_1252.c +index b685878d3f..a8a9b205b9 100644 +--- a/enc/windows_1252.c ++++ b/enc/windows_1252.c +@@ -190,7 +190,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/windows_1254.c b/enc/windows_1254.c +index 5e6d92d3d2..b9fe1bef76 100644 +--- a/enc/windows_1254.c ++++ b/enc/windows_1254.c +@@ -232,7 +232,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/enc/windows_1257.c b/enc/windows_1257.c +index ada03b72bf..350c90ecdd 100644 +--- a/enc/windows_1257.c ++++ b/enc/windows_1257.c +@@ -236,7 +236,7 @@ case_map(OnigCaseFoldType* flagP, const OnigUChar** pp, + OnigUChar *to_start = to; + OnigCaseFoldType flags = *flagP; + +- while (*pp < end && to < to_end) { ++ while (*pp < end && to_end - to > 1) { + code = *(*pp)++; + if (code == SHARP_s) { + if (flags & ONIGENC_CASE_UPCASE) { +diff --git a/test/ruby/enc/test_case_mapping.rb b/test/ruby/enc/test_case_mapping.rb +index 31acdc4331..d1edfefa5d 100644 +--- a/test/ruby/enc/test_case_mapping.rb ++++ b/test/ruby/enc/test_case_mapping.rb +@@ -215,6 +215,27 @@ def test_shift_jis_downcase_ascii + assert_equal s, s.downcase(:ascii) + end + ++ SHARP_S_SINGLE_BYTE_ENCODINGS = %w[ ++ ISO-8859-1 ISO-8859-2 ISO-8859-3 ISO-8859-4 ISO-8859-9 ISO-8859-10 ++ ISO-8859-13 ISO-8859-14 ISO-8859-15 ISO-8859-16 ++ Windows-1250 Windows-1252 Windows-1254 Windows-1257 ++ ] ++ ++ def test_single_byte_sharp_s_expansion ++ SHARP_S_SINGLE_BYTE_ENCODINGS.each do |name| ++ enc = Encoding.find(name) ++ (1..64).each do |n| ++ s = ("\xDF".b * n).force_encoding(enc) ++ assert_equal "SS" * n, s.upcase, "#{name} upcase #{n}" ++ assert_equal "ss" * n, s.downcase(:fold), "#{name} fold #{n}" ++ assert_equal "SS" * n, s.swapcase, "#{name} swapcase #{n}" ++ assert_equal ("Ss" + "\xDF".b * (n - 1)).force_encoding(enc), s.capitalize, "#{name} capitalize #{n}" ++ assert_equal "SS" * n, ("a".b + "\xDF".b * n).force_encoding(enc).upcase[1..], "#{name} upcase a+#{n}" ++ assert_equal true, s.casecmp?(("ss" * n).force_encoding(enc)), "#{name} casecmp? #{n}" ++ end ++ end ++ end ++ + def test_shift_jis_upcase_ascii + s = ("a".."z").map {|c| "\x89#{c}"}.join("").force_encoding("Shift_JIS") + assert_equal s, s.upcase(:ascii) From 29a678a9b3d01e1a2b7bf7dc066f5e6e8e8b79d4 Mon Sep 17 00:00:00 2001 From: nmburgan <13688219+nmburgan@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:09:00 +0000 Subject: [PATCH 7/7] ruby-3.2: Backport fix for GHSA-87fm-7w84-j2px Signed-off-by: nmburgan <13688219+nmburgan@users.noreply.github.com> --- configs/components/ruby-3.2.rb | 2 + .../0021-string-check-case-map-lengths.patch | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 resources/patches/ruby_32/0021-string-check-case-map-lengths.patch diff --git a/configs/components/ruby-3.2.rb b/configs/components/ruby-3.2.rb index 2c564801..c205e442 100644 --- a/configs/components/ruby-3.2.rb +++ b/configs/components/ruby-3.2.rb @@ -59,6 +59,8 @@ pkg.apply_patch "#{base}/0019-regexp-preprocess-source-in-to-s.patch" # GHSA-34m4-m9fh-gqp5, heap buffer overflow in case conversion of strings pkg.apply_patch "#{base}/0020-string-fix-sharp-s-case-map-overflow.patch" + # GHSA-87fm-7w84-j2px, integer overflow in case conversion of strings + pkg.apply_patch "#{base}/0021-string-check-case-map-lengths.patch" if platform.is_windows? pkg.apply_patch "#{base}/windows_mingw32_mkmf.patch" diff --git a/resources/patches/ruby_32/0021-string-check-case-map-lengths.patch b/resources/patches/ruby_32/0021-string-check-case-map-lengths.patch new file mode 100644 index 00000000..4ccf73dc --- /dev/null +++ b/resources/patches/ruby_32/0021-string-check-case-map-lengths.patch @@ -0,0 +1,59 @@ +From: Yusuke Endoh +Date: 2026-11-09T14:37:21+09:00 +Subject: [PATCH 15/15] Check the lengths in rb_str_casemap + +Backport of Ruby 3.3 commit 0cc06da5269013f2c2b6fed6e3383816f8a8ca2b +(ruby/ruby#20006, GHSA-87fm-7w84-j2px). + +--- +diff --git a/string.c b/string.c +index b9ffd19b8d..ea51f91602 100644 +--- a/string.c ++++ b/string.c +@@ -7313,7 +7313,7 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc) + VALUE target; + + const OnigUChar *source_current, *source_end; +- int target_length = 0; ++ long target_length = 0; + VALUE buffer_anchor; + mapping_buffer *current_buffer = 0; + mapping_buffer **pre_buffer; +@@ -7321,6 +7321,8 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc) + int buffer_length_or_invalid; + + if (RSTRING_LEN(source) == 0) return str_duplicate(rb_cString, source); ++ if (RSTRING_LEN(source) > INT_MAX - CASE_MAPPING_ADDITIONAL_LENGTH) ++ rb_raise(rb_eArgError, "string size too big"); + + source_current = (OnigUChar*)RSTRING_PTR(source); + source_end = (OnigUChar*)RSTRING_END(source); +@@ -7329,7 +7331,11 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc) + pre_buffer = (mapping_buffer **)&DATA_PTR(buffer_anchor); + while (source_current < source_end) { + /* increase multiplier using buffer count to converge quickly */ +- size_t capa = (size_t)(source_end-source_current)*++buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH; ++ size_t capa = (size_t)(source_end-source_current); ++ if (capa > (INT_MAX - CASE_MAPPING_ADDITIONAL_LENGTH) / ++buffer_count) ++ capa = INT_MAX; ++ else ++ capa = capa * buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH; + if (CASEMAP_DEBUG) { + fprintf(stderr, "Buffer allocation, capa is %"PRIuSIZE"\n", capa); /* for tuning */ + } +@@ -7343,11 +7349,13 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc) + current_buffer->space, + current_buffer->space+current_buffer->capa, + enc); +- if (buffer_length_or_invalid < 0) { ++ if (buffer_length_or_invalid < 0 || buffer_length_or_invalid > LONG_MAX - target_length) { + current_buffer = DATA_PTR(buffer_anchor); + DATA_PTR(buffer_anchor) = 0; + mapping_buffer_free(current_buffer); +- rb_raise(rb_eArgError, "input string invalid"); ++ if (buffer_length_or_invalid < 0) ++ rb_raise(rb_eArgError, "input string invalid"); ++ rb_raise(rb_eArgError, "string size too big"); + } + target_length += current_buffer->used = buffer_length_or_invalid; + }