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

Invalid read of size 8 in S_parse_body (perl.c:2166) when argv ends with -- #15647

Closed
p5pRT opened this issue Oct 7, 2016 · 10 comments
Closed

Comments

@p5pRT
Copy link

p5pRT commented Oct 7, 2016

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

Searchable as RT129826$

@p5pRT
Copy link
Author

p5pRT commented Oct 7, 2016

From @niner

Created by @niner

This is a bug report for perl from nine@​detonation.org,
generated with the help of perlbug 1.40 running under perl 5.24.0.

-----------------------------------------------------------------
When perl is run with a command line that ends in "--" as in​:
perl -e 0 --
S_parse_body will do an invalid memory access trying to set
scriptname to argv[0] which by then points after the -- and thus
outside the original arguments list.

Noticed by valgrinding Inline​::Perl5 which embeds perl in Perl 6.
I failed to reproduce the valgrind output with just running perl
because probably valgrind thinks the -- is part of its own command
line.

Perl Info

Flags:
    category=core
    severity=low

This perlbug was built using Perl 5.24.0 - Thu Sep  1 15:14:40 UTC 2016
It is being executed now by  Perl 5.24.0 - Thu Sep  1 15:11:43 UTC 2016.

Site configuration information for perl 5.24.0:

Configured by abuild at Thu Sep  1 15:11:43 UTC 2016.

Summary of my perl5 (revision 5 version 24 subversion 0) configuration:
   
  Platform:
    osname=linux, osvers=4.7.2-1-default, archname=x86_64-linux-thread-multi
    uname='linux lamb13 4.7.2-1-default #1 smp preempt sun aug 21 08:59:11 utc 2016 (34ba8d6) x86_64 x86_64 x86_64 gnulinux '
    config_args='-ds -e -Dprefix=/usr -Dvendorprefix=/usr -Dinstallusrbinperl -Dusethreads -Di_db -Di_dbm -Di_ndbm -Di_gdbm -Dd_dbm_open -Duseshrplib=true -Doptimize=-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -g -Wall -pipe -Accflags=-DPERL_USE_SAFE_PUTENV -Dotherlibdirs=/usr/lib/perl5/site_perl -Dinc_version_list=none'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DPERL_USE_SAFE_PUTENV -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -g -Wall -pipe',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DPERL_USE_SAFE_PUTENV -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong'
    ccversion='', gccversion='6.2.1 20160830 [gcc-6-branch revision 239856]', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib64 -fstack-protector-strong'
    libpth=/usr/local/lib /usr/lib64/gcc/x86_64-suse-linux/6/include-fixed /usr/lib64/gcc/x86_64-suse-linux/6/../../../../x86_64-suse-linux/lib /usr/lib /lib/../lib64 /usr/lib/../lib64 /lib /lib64 /usr/lib64 /usr/local/lib64
    libs=-lm -ldl -lcrypt -lpthread
    perllibs=-lm -ldl -lcrypt -lpthread
    libc=/lib64/libc-2.23.so, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version='2.23'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E -Wl,-rpath,/usr/lib/perl5/5.24.0/x86_64-linux-thread-multi/CORE'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib64 -fstack-protector-strong'



@INC for perl 5.24.0:
    /home/nine/work/lib
    /usr/lib/perl5/site_perl/5.24.0/x86_64-linux-thread-multi
    /usr/lib/perl5/site_perl/5.24.0
    /usr/lib/perl5/vendor_perl/5.24.0/x86_64-linux-thread-multi
    /usr/lib/perl5/vendor_perl/5.24.0
    /usr/lib/perl5/5.24.0/x86_64-linux-thread-multi
    /usr/lib/perl5/5.24.0
    /usr/lib/perl5/site_perl
    .


Environment for perl 5.24.0:
    HOME=/home/nine
    LANG=en_US.UTF-8
    LANGUAGE=
    LC_COLLATE=de_AT.UTF-8
    LC_MEASUREMENT=de_AT.UTF-8
    LC_MONETARY=de_AT.UTF-8
    LC_NUMERIC=en_US.UTF-8
    LC_TIME=de_AT.UTF-8
    LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/osgPlugins
    LOGDIR (unset)
    PATH=/home/nine/perl5/perlbrew/bin:/home/nine/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/opt/kde3/bin:/opt/kde2/bin:/opt/kde/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/sbin:/opt/FlightGear/bin:/home/nine/rakudo/install/bin:/home/nine/rakudo/install/share/perl6/site/bin:/opt/FlightGear/bin:/home/nine/rakudo/install/bin:/home/nine/rakudo/install/share/perl6/site/bin:/usr/sbin:/opt/FlightGear/bin:/home/nine/rakudo/install/bin:/home/nine/rakudo/install/share/perl6/site/bin
    PERL5LIB=/home/nine/work/lib
    PERLBREW_BASHRC_VERSION=0.42
    PERLBREW_HOME=/home/nine/.perlbrew
    PERLBREW_MANPATH=
    PERLBREW_PATH=/home/nine/perl5/perlbrew/bin
    PERLBREW_PERL=
    PERLBREW_ROOT=/home/nine/perl5/perlbrew
    PERLBREW_VERSION=0.42
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Oct 26, 2016

From @tonycoz

On Fri Oct 07 07​:35​:10 2016, nine@​detonation.org wrote​:

When perl is run with a command line that ends in "--" as in​:
perl -e 0 --
S_parse_body will do an invalid memory access trying to set
scriptname to argv[0] which by then points after the -- and thus
outside the original arguments list.

Noticed by valgrinding Inline​::Perl5 which embeds perl in Perl 6.
I failed to reproduce the valgrind output with just running perl
because probably valgrind thinks the -- is part of its own command
line.

I haven't been able to reproduce this.

I tried​:

- a default + -DDEBUGGING build of blead (system gcc, 4.9.2)

- a default + -DDEBUGGING build of 5.24.0 (system gcc)

- a build of 5.24.0 with your switches. (system gcc)

- a build of 5.24.0 with your switches. (gcc 5.2.0)

Could you please provide a backtrace of the failure?

I also checked whether valgrind is passing the -- through, first I started
valgrind with debugging enabled​:

  $ LD_LIBRARY_PATH=. valgrind --vgdb=full --vgdb-stop-at=startup ./perl -e 0 --
  ...
  ==16307== target remote | /usr/lib/valgrind/../../bin/vgdb --pid=16307
  ...

Then connected to it with the debugger
  $ gdb ./perl
  ...
  (gdb) target remote | /usr/lib/valgrind/../../bin/vgdb --pid=16307
  Remote debugging using | /usr/lib/valgrind/../../bin/vgdb --pid=16307
  ...
  (gdb) b main
  Breakpoint 1 at 0x400d50​: file perlmain.c, line 62.
  (gdb) c
  Continuing.
 
  Breakpoint 1, main (argc=4, argv=0xfff0007b8, env=0xfff0007e0) at perlmain.c​:62
  62 {
  (gdb) p argv[0]
  $1 = 0xfff000a73 "./perl"
  (gdb) p argv[1]
  $2 = 0xfff000a7a "-e"
  (gdb) p argv[2]
  $3 = 0xfff000a7d "0"
  (gdb) p argv[3]
  $4 = 0xfff000a7f "--"
  (gdb) c
  Continuing.
  [Inferior 1 (Remote target) exited normally]
  (gdb)

and valgrind finished with no errors​:

  ==16307== ERROR SUMMARY​: 0 errors from 0 contexts (suppressed​: 0 from 0)

Tony

@p5pRT
Copy link
Author

p5pRT commented Oct 26, 2016

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

@p5pRT
Copy link
Author

p5pRT commented Oct 27, 2016

From @tonycoz

Noticed by valgrinding Inline​::Perl5 which embeds perl in Perl 6.
I failed to reproduce the valgrind output with just running perl
because probably valgrind thinks the -- is part of its own command
line.

Looking at this a bit more closely, I suspect this is a bug in Inline​::Perl5

For perl -e 0 --, argv and argv contain the following​:

argc = 4

argv[0] = "perl" # or something similar
argv[1] = "-e"
argv[2] = "0"
argv[3] = "--"
argv[4] = NULL # Inline​::Perl5 doesn't appear to do this bit

Tony

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 2016

From @niner

Hi!

On Mittwoch, 26. Oktober 2016 17​:18​:55 CET Tony Cook via RT wrote​:

Looking at this a bit more closely, I suspect this is a bug in Inline​::Perl5

For perl -e 0 --, argv and argv contain the following​:

argc = 4

argv[0] = "perl" # or something similar
argv[1] = "-e"
argv[2] = "0"
argv[3] = "--"
argv[4] = NULL # Inline​::Perl5 doesn't appear to do this bit

Oh, thank you for pointing that out and sorry for wasting your time. Valgrind
is indeed happy when I NULL terminate argv. I just didn't know that was
required, which is why I propose the attached doc patch.

Thanks again!
Stefan

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 2016

From @niner

argv_null_termination.diff
diff --git a/pod/perlembed.pod b/pod/perlembed.pod
index 7bc7a8c..1c971c3 100644
--- a/pod/perlembed.pod
+++ b/pod/perlembed.pod
@@ -211,6 +211,9 @@ Also notice that no matter what arguments you pass to perl_parse(),
 PERL_SYS_INIT3() must be invoked on the C main() argc, argv and env and
 only once.
 
+Mind that argv[argc] must be NULL, same as those passed to a main
+function in C.
+
 Now compile this program (I'll call it I<interp.c>) into an executable:
 
     % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`

@p5pRT
Copy link
Author

p5pRT commented Nov 9, 2016

From @tonycoz

On Thu, 03 Nov 2016 12​:07​:00 -0700, nine@​detonation.org wrote​:

Hi!

On Mittwoch, 26. Oktober 2016 17​:18​:55 CET Tony Cook via RT wrote​:

Looking at this a bit more closely, I suspect this is a bug in
Inline​::Perl5

For perl -e 0 --, argv and argv contain the following​:

argc = 4

argv[0] = "perl" # or something similar
argv[1] = "-e"
argv[2] = "0"
argv[3] = "--"
argv[4] = NULL # Inline​::Perl5 doesn't appear to do this bit

Oh, thank you for pointing that out and sorry for wasting your time.
Valgrind
is indeed happy when I NULL terminate argv. I just didn't know that
was
required, which is why I propose the attached doc patch.

Applied as 54c85bb and added your
name to AUTHORS in 6645d2e.

Tony

@p5pRT
Copy link
Author

p5pRT commented Nov 9, 2016

@tonycoz - Status changed from 'open' to 'pending release'

@p5pRT
Copy link
Author

p5pRT commented May 30, 2017

From @khwilliamson

Thank you for filing this report. You have helped make Perl better.

With the release today of Perl 5.26.0, this and 210 other issues have been
resolved.

Perl 5.26.0 may be downloaded via​:
https://metacpan.org/release/XSAWYERX/perl-5.26.0

If you find that the problem persists, feel free to reopen this ticket.

@p5pRT
Copy link
Author

p5pRT commented May 30, 2017

@khwilliamson - Status changed from 'pending release' 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