Crashes with SIGSEGV due to undefined behaviour when calling perl_parse

Bug #1915959 reported by lordaro
12
This bug affects 1 person
Affects Status Importance Assigned to Milestone
libapache2-mod-perl2 (Ubuntu)
Fix Released
Undecided
Unassigned
Focal
Fix Released
Medium
Utkarsh Gupta

Bug Description

[Impact]
========

While setting up a perl web application with mod_perl & apache, apache keeps segfaulting.

Broke out gdb, and found that it was segfaulting within perl itself

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7358ff5 in perl_parse () from /lib/x86_64-linux-gnu/libperl.so.5.30
(gdb) bt
#0 0x00007ffff7358ff5 in perl_parse () from /lib/x86_64-linux-gnu/libperl.so.5.30
#1 0x00007ffff764cd0c in modperl_startup () from /usr/lib/apache2/modules/mod_perl.so
#2 0x00007ffff764cc97 in modperl_startup () from /usr/lib/apache2/modules/mod_perl.so
#3 0x00007ffff764d0fa in modperl_init () from /usr/lib/apache2/modules/mod_perl.so
#4 0x00007ffff764d27b in modperl_hook_init () from /usr/lib/apache2/modules/mod_perl.so
#5 0x00005555555b23d4 in ap_run_open_logs ()
#6 0x000055555558c440 in main ()

 # valgrind apache2 -k start -X
==22529== Memcheck, a memory error detector
==22529== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22529== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==22529== Command: apache2 -k start -X
==22529==
==22529== Invalid read of size 8
==22529== at 0x564AFF5: perl_parse (in /usr/lib/x86_64-linux-gnu/libperl.so.5.30.0)
==22529== by 0x55A8D0B: modperl_startup (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x55A8C96: modperl_startup (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x55A90F9: modperl_init (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x55A927A: modperl_hook_init (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x1663D3: ap_run_open_logs (in /usr/sbin/apache2)
==22529== by 0x14043F: main (in /usr/sbin/apache2)
==22529== Address 0x5a44000 is not stack'd, malloc'd or (recently) free'd
==22529==
==22529==
==22529== Process terminating with default action of signal 11 (SIGSEGV)
==22529== Access not within mapped region at address 0x5A44000
==22529== at 0x564AFF5: perl_parse (in /usr/lib/x86_64-linux-gnu/libperl.so.5.30.0)
==22529== by 0x55A8D0B: modperl_startup (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x55A8C96: modperl_startup (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x55A90F9: modperl_init (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x55A927A: modperl_hook_init (in /usr/lib/apache2/modules/mod_perl.so)
==22529== by 0x1663D3: ap_run_open_logs (in /usr/sbin/apache2)
==22529== by 0x14043F: main (in /usr/sbin/apache2)

gdb indicated that it was erroring in very early in perl's runtime, before it had got to any perl code. When using debug symbols, the exact line it was failing on was `scriptname = argv[0];` (perl.c:2365) It wasn't possible to reason beyond that as stepping through optimised code even with debug symbols is next to impossible to make any sense of.

I did find that building an unoptimised perl made the error go away.

I found the following closed issue: https://github.com/Perl/perl5/issues/15806 which describes the same issue I was having.

Looking at the source for mod_perl, I found that the argv array passed to perl_parse() is not NULL terminated as is required by perl - ( documentation: https://perldoc.perl.org/perlembed#Adding-a-Perl-interpreter-to-your-C-program )

After patching this, the problem went away and didn't come back. Patch is attached.

[Test Plan]
===========

 # ls
libapache2-mod-perl2_2.0.11-2_amd64.clean.deb libapache2-mod-perl2_2.0.11-2_amd64.patched.deb

 # dpkg -i libapache2-mod-perl2_2.0.11-2_amd64.clean.deb
(Reading database ... 33224 files and directories currently installed.)
Preparing to unpack libapache2-mod-perl2_2.0.11-2_amd64.clean.deb ...
Unpacking libapache2-mod-perl2 (2.0.11-2) over (2.0.11-2) ...
Setting up libapache2-mod-perl2 (2.0.11-2) ...
apache2_invoke perl: already enabled

 # source /etc/apache2/envvars

 # apache2 -k start -X
Segmentation fault (core dumped)

 # dpkg -i libapache2-mod-perl2_2.0.11-2_amd64.patched.deb
(Reading database ... 33224 files and directories currently installed.)
Preparing to unpack libapache2-mod-perl2_2.0.11-2_amd64.patched.deb ...
Unpacking libapache2-mod-perl2 (2.0.11-2) over (2.0.11-2) ...
Setting up libapache2-mod-perl2 (2.0.11-2) ...
apache2_invoke perl: already enabled

 # apache2 -k start -X
<success>^C

 # dpkg -i libapache2-mod-perl2_2.0.11-2_amd64.clean.deb
(Reading database ... 33224 files and directories currently installed.)
Preparing to unpack libapache2-mod-perl2_2.0.11-2_amd64.clean.deb ...
Unpacking libapache2-mod-perl2 (2.0.11-2) over (2.0.11-2) ...
Setting up libapache2-mod-perl2 (2.0.11-2) ...
apache2_invoke perl: already enabled

 # apache2 -k start -X
Segmentation fault (core dumped)

So after the SRU is performed, apache should no longer segfault.

[Where problems could occur]
============================

The argument parsing code is being changed (taking in NULL terminator now), so edge case failures are likely to be in that area. Should be trivial to handle, though.

Related branches

Revision history for this message
lordaro (charlespigott) wrote :
Revision history for this message
lordaro (charlespigott) wrote :

Oops, looks like my gdb/valgrind output formatting got messed up. Should still be broadly readable

Revision history for this message
Ubuntu Foundations Team Bug Bot (crichton) wrote :

The attachment "Proposed patch fixing the issue" seems to be a patch. If it isn't, please remove the "patch" flag from the attachment, remove the "patch" tag, and if you are a member of the ~ubuntu-reviewers, unsubscribe the team.

[This is an automated message performed by a Launchpad user owned by ~brian-murray, for any issues please contact him.]

tags: added: patch
Revision history for this message
Paride Legovini (paride) wrote :

Hello and thanks for all the debugging work here. I agree with your analysis, I think that we have a bug here and the fix you proposed LGTM. However despite my efforts I couldn't reproduce the segfault. I specifically tried with ubuntu-minimal:focal containers with just apache2 and libapache2-mod-perl2 installed, enabled the perl module, stopped apache2 and re-started it manually like you did but to no avail, even after trying several times in different containers or environments.

I understand the very nature of the bug may make it difficult to reproduce, but a reproducer will make the whole process of including the fix and shipping it in existing Ubuntu stable releases both easier and more solid. Even "obvious fixes" carry a regression potential, which has to be weighted against the benefits of shipping the fix, and for some reason users hit the issue you described quite rarely. Can you think of a way to force the crash to happen, or at least make it much more probable?

Also it may be worth submitting your patch upstream. Upstreamed patches are always a win-win: it's easier for downstream distributions to cherry-pick and later drop patches included upstream, package maintenance is easier, the patch gets broader testing and the wider ecosystems benefits from it. Would you consider submitting your patch to the Apache mod_perl project [1]?

I'm marking this bug report as Incomplete for the moment, which simply means that we're waiting for further comments/information.

[1] https://perl.apache.org/contribute/index.html

Changed in libapache2-mod-perl2 (Ubuntu):
status: New → Incomplete
Revision history for this message
lordaro (charlespigott) wrote :

The crash is bizarre. I have plenty of other hosts that are making use of mod_perl with no issues at all. It can't be (directly) application specific, as it's crashing before it even gets to running any of the perl. If I uninstall mariadb (required for this application), it starts working again. If I reinstall mariadb, it keeps on working. If I recreate the container from scratch, it reliably starts segfaulting. When I fiddle with it some amount with debug packages and so on, it starts working again and I can't make it break again.

Such is undefined behaviour and invalid memory accesses, I suppose.

You might be able to try debug mod_perl + debug perl - perhaps that will reliably show up the invalid read error in valgrind?

And yes, I did look into submitting the patch upstream but frankly the email system and everything else scared me off. The project doesn't look particularly active either. However, I'll look a bit deeper to see what I can I can do.

Revision history for this message
lordaro (charlespigott) wrote :

Success!

https://svn.apache.org/viewvc?view=revision&revision=1886793

Consider this a backport request :)

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package libapache2-mod-perl2 - 2.0.11-4

---------------
libapache2-mod-perl2 (2.0.11-4) unstable; urgency=medium

  * Add a patch from upstream SVN to fix a SIGSEGV crash due to wrong use
    of perl_parse(). (LP: #1915959)
  * Update years of packaging copyright.

 -- gregor herrmann <email address hidden> Mon, 22 Feb 2021 19:00:30 +0100

Changed in libapache2-mod-perl2 (Ubuntu):
status: Incomplete → Fix Released
Revision history for this message
lordaro (charlespigott) wrote :

Thank you. Is there anything that needs to be done/can be done to get this backported to focal?

Revision history for this message
gregor herrmann (gregoa) wrote : Re: [Pkg-perl-maintainers] [Bug 1915959] Re: Crashes with SIGSEGV due to undefined behaviour when calling perl_parse

On Fri, 26 Feb 2021 10:00:44 -0000, lordaro wrote:

> Thank you. Is there anything that needs to be done/can be done to get
> this backported to focal?

Probably, but I'm not familiar with Ubuntu processes (I uploaded the
fix to Debian/unstable from where it migrated to Ubuntu/hirsute).

Maybe https://wiki.ubuntu.com/StableReleaseUpdates has the answer, or
someone else will chime in here.

Cheers,
gregor, Debian Perl Group

--
 .''`. https://info.comodo.priv.at -- Debian Developer https://www.debian.org
 : :' : OpenPGP fingerprint D1E1 316E 93A7 60A8 104D 85FA BB3A 6801 8649 AA06
 `. `' Member VIBE!AT & SPI Inc. -- Supporter Free Software Foundation Europe
   `- NP: Mark Knopfler: Wanderlust

Revision history for this message
Robie Basak (racb) wrote :

Yes, this can be backported to Focal. It will need somebody affected to commit to doing the necessary QA after the update is prepared (without that QA, we won't be able to land the update).

The process is documented at https://wiki.ubuntu.com/StableReleaseUpdates#Procedure as gregor correctly pointed out. I'll add this task to the server team's backlog. If you'd like to do it sooner, you are welcome to prepare the update yourself following the documented process.

The commit that needs to be cherry-picked is here: https://salsa.debian.org/perl-team/modules/packages/libapache2-mod-perl2/-/commit/ad28961ec0e3ac3e450eb47c53c6d8cc114fb17d

Changed in libapache2-mod-perl2 (Ubuntu Focal):
status: New → Triaged
importance: Undecided → Medium
tags: added: bitesize server-next
lordaro (charlespigott)
description: updated
Revision history for this message
lordaro (charlespigott) wrote :

Not to nag or anything, but any sort of timescale on this? I'd happily help out, but having read through the process I'm not actually sure what needs doing...

Revision history for this message
Utkarsh Gupta (utkarsh) wrote :

Hi Charles,

I am almost done preparing an update. This should land very soon! I'll keep you updated here.

Changed in libapache2-mod-perl2 (Ubuntu Focal):
assignee: nobody → Utkarsh Gupta (utkarsh)
Utkarsh Gupta (utkarsh)
description: updated
Utkarsh Gupta (utkarsh)
description: updated
Utkarsh Gupta (utkarsh)
Changed in libapache2-mod-perl2 (Ubuntu Focal):
status: Triaged → Fix Committed
Revision history for this message
Robie Basak (racb) wrote : Please test proposed package

Hello lordaro, or anyone else affected,

Accepted libapache2-mod-perl2 into focal-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/libapache2-mod-perl2/2.0.11-2ubuntu0.20.04.1 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested, what testing has been performed on the package and change the tag from verification-needed-focal to verification-done-focal. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-focal. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

tags: added: verification-needed verification-needed-focal
Revision history for this message
lordaro (charlespigott) wrote :

Thanks very much!

2.0.11-2ubuntu0.20.04.1 fixes the bug for me.

tags: added: verification-done-focal
removed: verification-needed-focal
Revision history for this message
Utkarsh Gupta (utkarsh) wrote :

Awesome, thanks, Charles.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package libapache2-mod-perl2 - 2.0.11-2ubuntu0.20.04.1

---------------
libapache2-mod-perl2 (2.0.11-2ubuntu0.20.04.1) focal; urgency=medium

  * Fix a SIGSEGV crash. (LP: #1915959)
    - d/p/Fix_SIGSEGV_perl_parse.patch: Add a patch from upstream
      SVN to fix a SIGSEGV crash due to wrong use of perl_parse().
      + Thanks, Charles Pigott, for the patch.

 -- Utkarsh Gupta <email address hidden> Fri, 19 Mar 2021 19:00:24 +0530

Changed in libapache2-mod-perl2 (Ubuntu Focal):
status: Fix Committed → Fix Released
Revision history for this message
Brian Murray (brian-murray) wrote : Update Released

The verification of the Stable Release Update for libapache2-mod-perl2 has completed successfully and the package is now being released to -updates. Subsequently, the Ubuntu Stable Release Updates Team is being unsubscribed and will not receive messages about this bug report. In the event that you encounter a regression using the package from -updates please report a new bug using ubuntu-bug and tag the bug report regression-update so we can easily find any regressions.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.