Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions configs/components/ruby-3.2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@
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"
# 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"
# 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"
# 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"
# 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"
pkg.apply_patch "#{base}/ruby-faster-load_32.patch"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
From: Stan Hu <stanhu@gmail.com>
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,
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
From: Stan Hu <stanhu@gmail.com>
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 */
Loading