diff options
author | Tony Cook <tony@develop-help.com> | 2017-08-08 09:32:58 +1000 |
---|---|---|
committer | Tony Cook <tony@develop-help.com> | 2018-04-16 11:49:20 +1000 |
commit | f17fed5006177dce8ac48229c424a2da0d6ba492 (patch) | |
tree | 1c056fa075840c57430ee606b148c6f3d9d55929 /t/op/pack.t | |
parent | a684213360784beeff22665b3da443d7b86e26cc (diff) | |
download | perl-f17fed5006177dce8ac48229c424a2da0d6ba492.tar.gz |
(perl #131844) fix various space calculation issues in pp_pack.c
- for the originally reported case, if the start/cur pointer is in the
top 75% of the address space the add (cur) + glen addition would
overflow, resulting in the condition failing incorrectly.
- the addition of the existing space used to the space needed could
overflow, resulting in too small an allocation and a buffer overflow.
- the scaling for UTF8 could overflow.
- the multiply to calculate the space needed for many items could
overflow.
For the first case, do a space calculation without making new pointers.
For the other cases, detect the overflow and croak if there's an
overflow.
Originally this used Size_t_MAX as the maximum size of a memory
allocation, but for -DDEBUGGING builds realloc() throws a panic for
allocations over half the address space in size, changing the error
reported for the allocation.
For non-DEBUGGING builds the Size_t_MAX limit has the small chance
of finding a system that has 3GB of contiguous space available, and
allocating that space, which could be a denial of servce in some cases.
Unfortunately changing the limit to half the address space means that
the exact case with the original issue can no longer occur, so the
test is no longer testing against the address + length issue that
caused the original problem, since the allocation is failing earlier.
One option would be to change the test so the size request by pack is
just under 2GB, but this has a higher (but still low) probability that
the system has the address space available, and will actually try to
allocate the memory, so let's not do that.
Diffstat (limited to 't/op/pack.t')
-rw-r--r-- | t/op/pack.t | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/t/op/pack.t b/t/op/pack.t index 664aaaf1b0..cf0e286509 100644 --- a/t/op/pack.t +++ b/t/op/pack.t @@ -12,7 +12,7 @@ my $no_endianness = $] > 5.009 ? '' : my $no_signedness = $] > 5.009 ? '' : "Signed/unsigned pack modifiers not available on this perl"; -plan tests => 14713; +plan tests => 14717; use strict; use warnings qw(FATAL all); @@ -2059,3 +2059,25 @@ print pack("ucW", "0000", 0, 140737488355327) eq "\$,#`P,```\n\0\x{7fffffffffff} ? "ok\n" : "not ok\n"; EOS } + +SKIP: +{ + # [perl #131844] pointer addition overflow + $Config{ptrsize} == 4 + or skip "[perl #131844] need 32-bit build for this test", 4; + # prevent ASAN just crashing on the allocation failure + local $ENV{ASAN_OPTIONS} = $ENV{ASAN_OPTIONS}; + $ENV{ASAN_OPTIONS} .= ",allocator_may_return_null=1"; + fresh_perl_like('pack "f999999999"', qr/Out of memory during pack/, { stderr => 1 }, + "pointer addition overflow"); + + # integer (STRLEN) overflow from addition of glen to current length + fresh_perl_like('pack "c10f1073741823"', qr/Out of memory during pack/, { stderr => 1 }, + "integer overflow calculating allocation (addition)"); + + fresh_perl_like('pack "W10f536870913", 256', qr/Out of memory during pack/, { stderr => 1 }, + "integer overflow calculating allocation (utf8)"); + + fresh_perl_like('pack "c10f1073741824"', qr/Out of memory during pack/, { stderr => 1 }, + "integer overflow calculating allocation (multiply)"); +} |