Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null ptr deref -> S_clear_yystack (perly.c:218) #14738

Closed
p5pRT opened this issue Jun 7, 2015 · 15 comments
Closed

null ptr deref -> S_clear_yystack (perly.c:218) #14738

p5pRT opened this issue Jun 7, 2015 · 15 comments

Comments

@p5pRT
Copy link

p5pRT commented Jun 7, 2015

Migrated from rt.perl.org#125350 (status was 'resolved')

Searchable as RT125350$

@p5pRT
Copy link
Author

p5pRT commented Jun 7, 2015

From @geeknik

The attached "script" causes a null ptr deref and seg fault in Perl v5.23.0 (v5.22.0-63-g216b41c)​:

syntax error at test199-min line 1, near "0}"
syntax error at test199-min line 1, near "{]"
Unmatched right curly bracket at test199-min line 1, at end of line
Unrecognized character \x1B; marked by <-- HERE after ]}}}=sub{0<-- HERE near column 25 at test199-min line 1.

Program received signal SIGSEGV, Segmentation fault.
0x000000000065a273 in S_clear_yystack ()
(gdb) bt
#0 0x000000000065a273 in S_clear_yystack ()
#1 0x0000000000af4316 in Perl_leave_scope () at scope.c​:1241
#2 0x000000000050b733 in S_my_exit_jump ()
#3 0x000000000053d289 in Perl_my_failure_exit ()
#4 0x0000000000b522c5 in Perl_die_unwind () at pp_ctl.c​:1694
#5 0x00000000007de26d in Perl_vcroak () at util.c​:1697
#6 0x00000000007de832 in Perl_croak () at util.c​:1742
#7 0x00000000005e9a75 in Perl_yylex () at toke.c​:4642
#8 0x000000000065c875 in Perl_yyparse ()
#9 0x0000000000534d79 in perl_parse ()
#10 0x000000000042ac38 in main () at perlmain.c​:114
(gdb) i r
rax 0x0 0
rbx 0x11f0470 18809968
rcx 0xf9 249
rdx 0x0 0
rsi 0xa3 163
rdi 0x27 39
rbp 0x11f00a0 0x11f00a0
rsp 0x7fffffffd730 0x7fffffffd730
r8 0x0 0
r9 0x11e8070 18776176
r10 0x0 0
r11 0x0 0
r12 0x11e8070 18776176
r13 0x11d85e0 18712032
r14 0x11d3088 18690184
r15 0x13 19
rip 0x65a273 0x65a273 <S_clear_yystack+1123>
eflags 0x10293 [ CF AF SF IF RF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0

syntax error at test199-min line 1, near "0}"
syntax error at test199-min line 1, near "{]"
Unmatched right curly bracket at test199-min line 1, at end of line
Unrecognized character \x1B; marked by <-- HERE after ]}}}=sub{0<-- HERE near column 25 at test199-min line 1.
==27487== Invalid read of size 8
==27487== at 0x65A273​: S_clear_yystack (perly.c​:218)
==27487== by 0xAF4315​: Perl_leave_scope (scope.c​:1241)
==27487== by 0x50B732​: S_my_exit_jump (perl.c​:5043)
==27487== by 0x53D288​: Perl_my_failure_exit (perl.c​:5030)
==27487== by 0xB522C4​: Perl_die_unwind (pp_ctl.c​:1694)
==27487== by 0x7DE26C​: Perl_vcroak (util.c​:1697)
==27487== by 0x7DE831​: Perl_croak (util.c​:1742)
==27487== by 0x5E9A74​: Perl_yylex (toke.c​:4642)
==27487== by 0x65C874​: Perl_yyparse (perly.c​:322)
==27487== by 0x534D78​: perl_parse (perl.c​:2296)
==27487== by 0x42AC37​: main (perlmain.c​:114)
==27487== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==27487==
==27487==
==27487== Process terminating with default action of signal 11 (SIGSEGV)
==27487== Access not within mapped region at address 0x0
==27487== at 0x65A273​: S_clear_yystack (perly.c​:218)
==27487== by 0xAF4315​: Perl_leave_scope (scope.c​:1241)
==27487== by 0x50B732​: S_my_exit_jump (perl.c​:5043)
==27487== by 0x53D288​: Perl_my_failure_exit (perl.c​:5030)
==27487== by 0xB522C4​: Perl_die_unwind (pp_ctl.c​:1694)
==27487== by 0x7DE26C​: Perl_vcroak (util.c​:1697)
==27487== by 0x7DE831​: Perl_croak (util.c​:1742)
==27487== by 0x5E9A74​: Perl_yylex (toke.c​:4642)
==27487== by 0x65C874​: Perl_yyparse (perly.c​:322)
==27487== by 0x534D78​: perl_parse (perl.c​:2296)
==27487== by 0x42AC37​: main (perlmain.c​:114)
==27487== If you believe this happened as a result of a stack
==27487== overflow in your program's main thread (unlikely but
==27487== possible), you can try to increase the size of the
==27487== main thread stack using the --main-stacksize= flag.
==27487== The main thread stack size used in this run was 8388608.
Segmentation fault

It also causes a null ptr deref and seg fault in Perl v5.21.7 (v5.21.6-602-ge9d2bd8), albeit in a different location and file​:

Program received signal SIGSEGV, Segmentation fault.
S_pad_findlex (namepv=namepv@​entry=0x7fffffffdb30 "&sub", namelen=namelen@​entry=4, flags=flags@​entry=0, cv=0x0, seq=5,
  Perl_warn=Perl_warn@​entry=1, out_capture=out_capture@​entry=0x0, out_name=out_name@​entry=0x7fffffffd8f0,
  out_flags=out_flags@​entry=0x7fffffffd8e0) at pad.c​:1141
1141 pad.c​: No such file or directory.
(gdb) bt
#0 S_pad_findlex (namepv=namepv@​entry=0x7fffffffdb30 "&sub", namelen=namelen@​entry=4, flags=flags@​entry=0, cv=0x0, seq=5,
  Perl_warn=Perl_warn@​entry=1, out_capture=out_capture@​entry=0x0, out_name=out_name@​entry=0x7fffffffd8f0,
  out_flags=out_flags@​entry=0x7fffffffd8e0) at pad.c​:1141
#1 0x00000000005e217d in Perl_pad_findmy_pvn (namepv=0x7fffffffdb30 "&sub", namelen=4, flags=<optimized out>) at pad.c​:962
#2 0x0000000000579950 in Perl_yylex () at toke.c​:6430
#3 0x00000000005c60b5 in Perl_yyparse (gramtype=<optimized out>) at perly.c​:322
#4 0x00000000004f0875 in S_parse_body (xsinit=0x42ac70 <xs_init>, env=0x0) at perl.c​:2271
#5 perl_parse (my_perl=<optimized out>, xsinit=0x42ac70 <xs_init>, argc=<optimized out>, argv=<optimized out>, env=0x0) at perl.c​:1605
#6 0x000000000042a87c in main (argc=2, argv=0x7fffffffe3a8, env=0x7fffffffe3c0) at perlmain.c​:114
#7 0x00007ffff6f98ead in __libc_start_main (main=<optimized out>, argc=<optimized out>, ubp_av=<optimized out>, init=<optimized out>,
  fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe398) at libc-start.c​:244
#8 0x000000000042ab95 in _start ()
(gdb) i r
rax 0x7fffffffd8e0 140737488345312
rbx 0x0 0
rcx 0x0 0
rdx 0x0 0
rsi 0x0 0
rdi 0x7fffffffdb30 140737488345904
rbp 0x4 0x4
rsp 0x7fffffffd7f0 0x7fffffffd7f0
r8 0x5 5
r9 0x1 1
r10 0x3 3
r11 0x7ffff70cf4f0 140737338209520
r12 0x7fffffffdb31 140737488345905
r13 0x0 0
r14 0x4 4
r15 0xe46416 14967830
rip 0x5dea2f 0x5dea2f <S_pad_findlex+55>
eflags 0x10246 [ PF ZF IF RF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0

==60285== Invalid read of size 8
==60285== at 0x5DEA2F​: S_pad_findlex (pad.c​:1141)
==60285== by 0x5E217C​: Perl_pad_findmy_pvn (pad.c​:962)
==60285== by 0x57994F​: Perl_yylex (toke.c​:6430)
==60285== by 0x5C60B4​: Perl_yyparse (perly.c​:322)
==60285== by 0x4F0874​: perl_parse (perl.c​:2271)
==60285== by 0x42A87B​: main (perlmain.c​:114)
==60285== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==60285==
==60285==
==60285== Process terminating with default action of signal 11 (SIGSEGV)
==60285== Access not within mapped region at address 0x0
==60285== at 0x5DEA2F​: S_pad_findlex (pad.c​:1141)
==60285== by 0x5E217C​: Perl_pad_findmy_pvn (pad.c​:962)
==60285== by 0x57994F​: Perl_yylex (toke.c​:6430)
==60285== by 0x5C60B4​: Perl_yyparse (perly.c​:322)
==60285== by 0x4F0874​: perl_parse (perl.c​:2271)
==60285== by 0x42A87B​: main (perlmain.c​:114)
==60285== If you believe this happened as a result of a stack
==60285== overflow in your program's main thread (unlikely but
==60285== possible), you can try to increase the size of the
==60285== main thread stack using the --main-stacksize= flag.
==60285== The main thread stack size used in this run was 8388608.
Segmentation fault

This bug was found with AFL (http​://lcamtuf.coredump.cx/afl/).

@p5pRT
Copy link
Author

p5pRT commented Jun 7, 2015

From @geeknik

test199-min

@p5pRT
Copy link
Author

p5pRT commented Aug 19, 2015

From @geeknik

Confirmed to still affect Perl v5.23.2 (v5.23.1-27-g5cce15f).

@p5pRT
Copy link
Author

p5pRT commented Aug 19, 2015

From [Unknown Contact. See original ticket]

Confirmed to still affect Perl v5.23.2 (v5.23.1-27-g5cce15f).

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 2015

From @rjbs

Anybody had a chance to look into this yet?

--
rjbs

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 2015

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 2015

@rjbs - Status changed from 'open' to 'resolved'

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 2015

From @shlomif

On Thu Sep 03 18​:46​:38 2015, rjbs wrote​:

Anybody had a chance to look into this yet?

I've taken a look now, and here's a tentative, symptomatic patch, which adds a test and passes the new test and all existing tests. I'm not sure it's the proper way to fix it.

Regards,

— Shlomi

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 2015

From @shlomif

0001-Tentative-fix-for-RT-125350-AFL-detected-crash.patch
From 0226391da24e773e63b8c7044c7069bd713e7d13 Mon Sep 17 00:00:00 2001
From: Shlomi Fish <shlomif@shlomifish.org>
Date: Fri, 4 Sep 2015 22:26:12 +0300
Subject: [PATCH] Tentative fix for RT#125350 - AFL detected crash.

---
 perly.c      | 2 +-
 t/base/lex.t | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/perly.c b/perly.c
index abb4d4e..91b4c79 100644
--- a/perly.c
+++ b/perly.c
@@ -213,7 +213,7 @@ S_clear_yystack(pTHX_  const yy_parser *parser)
 	if (yy_type_tab[yystos[ps->state]] == toketype_opval
 	    && ps->val.opval)
 	{
-	    if (ps->compcv != PL_compcv) {
+	    if (ps->compcv && (ps->compcv != PL_compcv)) {
 		PL_compcv = ps->compcv;
 		PAD_SET_CUR_NOSAVE(CvPADLIST(PL_compcv), 1);
 		PL_comppad_name = PadlistNAMES(CvPADLIST(PL_compcv));
diff --git a/t/base/lex.t b/t/base/lex.t
index 47c6be8..981b2e5 100644
--- a/t/base/lex.t
+++ b/t/base/lex.t
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..103\n";
+print "1..104\n";
 
 $x = 'x';
 
@@ -517,3 +517,9 @@ eval q|s##[}#e|;
  eval '0; qq{@{sub{]]}}}}}';
  print "ok $test - 124385\n"; $test++;
 }
+
+{
+ # Used to crash [perl #125350]
+ eval ('qq{@{[0}*sub{]]}}}=sub{0' . "\c[");
+ print "ok $test - 125350\n"; $test++;
+}
-- 
2.4.5

@p5pRT
Copy link
Author

p5pRT commented Sep 6, 2015

From @rurban

On Fri Sep 04 12​:33​:34 2015, shlomif wrote​:

On Thu Sep 03 18​:46​:38 2015, rjbs wrote​:

Anybody had a chance to look into this yet?

I've taken a look now, and here's a tentative, symptomatic patch,
which adds a test and passes the new test and all existing tests. I'm
not sure it's the proper way to fix it.

LGTM
A good and cheap place to avoid this kind of nonsense.

--
Reini Urban

@p5pRT
Copy link
Author

p5pRT commented Sep 17, 2015

From @jkeenan

On Fri Sep 04 12​:33​:34 2015, shlomif wrote​:

On Thu Sep 03 18​:46​:38 2015, rjbs wrote​:

Anybody had a chance to look into this yet?

I've taken a look now, and here's a tentative, symptomatic patch,
which adds a test and passes the new test and all existing tests. I'm
not sure it's the proper way to fix it.

Regards,

— Shlomi

Discussed on #p5p this afternoon. The patch looks good, includes a test, appears to address the immediate problem. We figure the only way we'll flush out other problems is by pushing it to blead and letting the world have at it.

Pushed to blead in commit a293d0f.

I'll take the ticket for the purpose of closing it within 7 days unless there is objection or bad test results.

Thank you very much.
Jim Keenan

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Sep 19, 2015

From @jkeenan

Re-opening pending completion of the 7-day period specified in previous post. (The ticket was marked Resolved earlier this month, but that appears to have been an error.)

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Sep 19, 2015

@jkeenan - Status changed from 'resolved' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Sep 24, 2015

From @jkeenan

Resolved as per schedule described in previous post.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Sep 24, 2015

@jkeenan - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant