Commit 83ed732f01 for perl
commit 83ed732f01153660bad298b3ff71183534de7303
Author: David Mitchell <davem@iabyn.nospamdeletethisbit.com>
Date: Thu Nov 5 21:28:10 2026 +0000
pp_multiconcat: avoid overflow on 32-bit systems
CVE-2026-144431
On in a 32-bit system with an expression like $a = "$b$c$d$e$f", the new
size of $a's PVX buffer can't exceed the sum of the memory already
allocated for $b, $c etc. So the calculation for the new size can't
exceed 4Gb and so can't wrap.
However, for something like $a = "$a$a$a$a$a", or $a = "$b$b$b$b$b",
then if the existing string(s) are very large, it *is* possible for
the size calculation to wrap.
This commit fixes that.
Note that the fix is only compiled-in on 32-bit systems. On 64-bit
systems, since pp_multiconcat() allows a maximum of 64 variables, a
string would need to be 2<<58 bytes long for there to be any possibility
of wrapping.
diff --git a/pp_hot.c b/pp_hot.c
index 0f078ffd39..d36a12abdd 100644
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -626,6 +626,35 @@ PP(pp_concat)
}
+/* Helper for pp_multiconcat: return grow + size*n with overflow checking
+ */
+PERL_STATIC_INLINE STRLEN
+S_add_size_n(STRLEN grow, STRLEN size, UV n)
+{
+#if PTRSIZE < 8
+ if (n > 1) {
+ if (size >= STRLEN_MAX / n)
+ goto bad;
+ size *= n;
+ }
+
+ {
+ STRLEN g = grow + size;
+ if (g < grow)
+ goto bad;
+ return g;
+ }
+
+ bad:
+ /* diag_listed_as: Out of memory during %s extend */
+ Perl_croak_nocontext("Out of memory during string extend");
+ NOT_REACHED; /* NOTREACHED */
+#else
+ return grow + size * n;
+#endif
+}
+
+
/* pp_multiconcat()
Concatenate one or more args, possibly interleaved with constant string
@@ -833,7 +862,7 @@ PP(pp_multiconcat)
dst_utf8 |= utf8;
ASSUME(len < SSize_t_MAX);
svpv_end->len = utf8 ? -(SSize_t)len : (SSize_t)len;
- grow += len;
+ grow = S_add_size_n(grow, len, 1);
}
/* --------------------------------------------------------------
@@ -864,7 +893,7 @@ PP(pp_multiconcat)
dst_utf8 |= targ_utf8;
targ_len = SvCUR(targ);
- grow += targ_len * (targ_count + is_append);
+ grow = S_add_size_n(grow, targ_len, targ_count + is_append);
goto phase3;
}
else if (ckWARN(WARN_UNINITIALIZED))
@@ -962,7 +991,7 @@ PP(pp_multiconcat)
len = aux[dst_utf8 ? PERL_MULTICONCAT_IX_UTF8_LEN
: PERL_MULTICONCAT_IX_PLAIN_LEN].ssize;
slow_concat = cBOOL(len);
- grow += len;
+ grow = S_add_size_n(grow, len, 1);
}
const_lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
diff --git a/t/op/concat2.t b/t/op/concat2.t
index da9fadb4f8..6893527eff 100644
--- a/t/op/concat2.t
+++ b/t/op/concat2.t
@@ -14,6 +14,7 @@ BEGIN {
use warnings;
use strict;
+use Config;
# This test is in the file because overload.pm uses concatenation.
{ package o; use overload '""' => sub { $_[0][0] } }
@@ -78,4 +79,32 @@ end
is($s, 'FOOX', 'local $magic_var = "...."');
}
+# Check for wrapping of size calculations on 32-bit systems
+
+{
+ SKIP: {
+ skip "ptrsize not 32-bit", 4
+ if $Config{ptrsize} != 4;
+
+ # Should be same value as in perl.h
+ my $PERL_MULTICONCAT_MAXARG = 64;
+ # slightly more than 1/64th of 4Gb
+ my $n = int(1.01 *((4096/$PERL_MULTICONCAT_MAXARG)*1024*1024));
+
+ my $long = 'x' x $n;
+ my $cmd = 'my $x = "'
+ . join('-', ('$long') x $PERL_MULTICONCAT_MAXARG)
+ . '";';
+ ok(!eval($cmd), "extend x n");
+ like($@, qr/Out of memory during string extend/, "extend x n");
+
+ $long = 'x' x $n;
+ $cmd = '$long = "'
+ . join('-', ('$long') x $PERL_MULTICONCAT_MAXARG)
+ . '";';
+ ok(!eval($cmd), "extend targ x n");
+ like($@, qr/Out of memory during string extend/, "extend targ x n");
+ }
+}
+
done_testing();