threads / discuss / 42730

git svn clone segmentation faul issue

Subject: git svn clone segmentation faul issue

## tl;dr

12 messages between Jun 28, 2016 and Jul 1, 2016.

replies: 11people: 3as markdown or json

ioannis.kappas@rbs.com· Jun 28, 2016, 08:50 UTC · lore
Hello,
Git can fail with a "malformed index nnn" error or cause a segmentation fault when executing the "git svn clone" command. 
This is because the git-svn perl script calling into the external subversion library during fetch, wherein a bug exists that can corrupt the Perl Interpreter's stack maintained on the heap. The bug is in a custom swig typemap around the "result_digest" argument which is not handled in a safe manner: An absolute address on the perl stack is calculated beforehand to store the result of svn_swig_pl_from_md5(), but the function can trigger the stack to be reallocated elsewhere in memory (i.e. when trying to grow it but there is not enough space left) thus invaliding the address calculated earlier, causing memory corruption or segmentation fault.
This call is made once for each file retrieved from subversion. The fault is caused when the free available perl stack size is very small at the call, triggering a realloc() moving it elsewhere in memory. In my test environment --MSYS2 on windows 7--, this was caused when the available stack size left was 24 SV* slots. This means that a subversion repository with many file entries has a higher probability to fail while cloning than one with only a few file entries big.
Perl stack trace to the entry point of the offending code is:
        SVN::TxDelta::apply(GLOB(0x60107ece8), GLOB(0x6011ee3a0), undef, SVN::Pool=REF(0x6011ee268)) called at /usr/share/perl5/site_perl/Git/SVN/Fetcher.pm line 368
        Git::SVN::Fetcher::apply_textdelta(Git::SVN::Fetcher=HASH(0x6010236a0), HASH(0x6011ee2e0), undef, _p_apr_pool_t=SCALAR(0x60113e510)) called at /usr/lib/perl5/vendor_perl/SVN/Ra.pm line 623
        SVN::Ra::Reporter::AUTOLOAD(SVN::Ra::Reporter=ARRAY(0x6010e9ee0), SVN::Pool=REF(0x6010ea228)) called at /usr/share/perl5/site_perl/Git/SVN/Ra.pm line 312
        Git::SVN::Ra::gs_do_update(Git::SVN::Ra=HASH(0x60101f4f0), 384103, 384103, Git::SVN=HASH(0x60101fc58), Git::SVN::Fetcher=HASH(0x6010236a0)) called at /usr/share/perl5/site_perl/Git/SVN.pm line 1205
        Git::SVN::do_fetch(Git::SVN=HASH(0x60101fc58), HASH(0x6010e9b20), 384103) called at /usr/share/perl5/site_perl/Git/SVN/Ra.pm line 475
        Git::SVN::Ra::gs_fetch_loop_common(Git::SVN::Ra=HASH(0x60101f4f0), 384103, 384103, ARRAY(0x600788cc8), ARRAY(0x600788ce0)) called at /usr/share/perl5/site_perl/Git/SVN.pm line 179
        Git::SVN::fetch_all("svn") called at /usr/lib/git-core/git-svn line 522
        main::cmd_clone("http://...", "debug") called at /usr/lib/git-core/git-svn line 386
        eval {...} called at /usr/lib/git-core/git-svn line 384
The solution is to change the swig typemap in subversion to delay calculating the stack address where the result is stored after the call to the md5 function:
In subversion's subversion/subversion/bindings/swig/include/svn_types.swg, one can change
From 
%typemap(argout) unsigned char *result_digest {
    %append_output($1);
}
To
%typemap(argout) unsigned char *result_digest {
    SV* ucrdTemp = svn_swig_pl_from_md5($1);             
    %append_output(ucrdTemp);
}
Swig will translate the above typemap in subversion/subversion/bindings/swig/perl/native/svn_delta.c
From
      if (argvi >= items) EXTEND(sp,1);  ST(argvi) = svn_swig_pl_from_md5(arg3); argvi++  ;
to
      SV* ucrdTemp = svn_swig_pl_from_md5(arg3);             
      if (argvi >= items) EXTEND(sp,1);  ST(argvi) = ucrdTemp; argvi++  ;
(The stack address calculation is the results of calling the macro ST(argvi) ).
I am not a Perl expert, but ST(n) = any_function() seems to be a dangerous idiom because of the potential of any_function() to cause a Perl stack reallocation invaliding the address calculated beforehand by ST(n).
The above patch has been tested to work in my codebase both on windows 7 (with MSYS2) and on Arch Linux, where it would have caused a segmentation fault otherwise.
Fortunately, a patch has already been submitted to subversion with (github) revision a074af86c8764404b28ce99d0bedcb668a321408 (at https://github.com/apache/subversion/commit/a074af86c8764404b28ce99d0bedcb668a321408 ) on the trunk to handle this and a couple of other similar cases. But by the looks of it has not been picked up yet in the latest subversion 1.9.4 release or the 1.9.x branch, perhaps because this patch was identified in sanity checks rather than coming out from a perceivable production issue?
Although this issue is not in the Git source code, I thought reporting this error here first since it is Git that stresses the subversion perl bindings in such a level to cause the failure. Perhaps the patch can be applied first in those git binary releases that include subversion (I believe Git for windows is such a case) while in the mean time I submit this case for consideration in the subversion bug tracking system.
Yannis
*********************************************************************************** 
The Royal Bank of Scotland plc. Registered in Scotland No 90312. 
Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. 
Authorised by the Prudential Regulation Authority and regulated 
by the Financial Conduct Authority and Prudential Regulation Authority. 
The Royal Bank of Scotland N.V. is authorised and regulated by the 
De Nederlandsche Bank and has its seat at Amsterdam, the 
Netherlands, and is registered in the Commercial Register under 
number 33002587. Registered Office: Gustav Mahlerlaan 350, 
Amsterdam, The Netherlands. The Royal Bank of Scotland N.V. and 
The Royal Bank of Scotland plc are authorised to act as agent for each 
other in certain jurisdictions. 
  
This e-mail message is confidential and for use by the addressee only. 
If the message is received by anyone other than the addressee, please 
return the message to the sender by replying to it and then delete the 
message from your computer. Internet e-mails are not necessarily 
secure. The Royal Bank of Scotland plc and The Royal Bank of Scotland 
N.V. including its affiliates ("RBS group") does not accept responsibility 
for changes made to this message after it was sent. For the protection
of RBS group and its clients and customers, and in compliance with
regulatory requirements, the contents of both incoming and outgoing
e-mail communications, which could include proprietary information and
Non-Public Personal Information, may be read by authorised persons
within RBS group other than the intended recipient(s). 

Whilst all reasonable care has been taken to avoid the transmission of viruses, it is the responsibility of the recipient to ensure that the onward transmission, opening or use of this message and any attachments will not adversely affect its systems or data. No responsibility is accepted by the RBS group in this regard and the recipient should carry out such virus and other checks as it considers appropriate.

Visit our website at www.rbs.com ***********************************************************************************

Johannes Schindelin· Jun 28, 2016, 11:47 UTC · re: ioannis.kappas@rbs.com · lore

Re: git svn clone segmentation faul issue

Hi Ioannis,
On Tue, 28 Jun 2016, Ioannis.Kappas@rbs.com wrote:
Show 12 quoted lines
> Git can fail with a "malformed index nnn" error or cause a segmentation
> fault when executing the "git svn clone" command. 
>
> [...]
>
> Fortunately, a patch has already been submitted to subversion with
> (github) revision a074af86c8764404b28ce99d0bedcb668a321408 (at
> https://github.com/apache/subversion/commit/a074af86c8) on the trunk to
> handle this and a couple of other similar cases. But by the looks of it
> has not been picked up yet in the latest subversion 1.9.4 release or the
> 1.9.x branch, perhaps because this patch was identified in sanity checks
> rather than coming out from a perceivable production issue?

This is an excellent analysis and a silver lining on the horizon to resolve those vexing git svn issues we keep having in Git for Windows.

Do you have a test case that is reliably reproducing the issue?
Show 6 quoted lines
> Although this issue is not in the Git source code, I thought reporting
> this error here first since it is Git that stresses the subversion perl
> bindings in such a level to cause the failure. Perhaps the patch can be
> applied first in those git binary releases that include subversion (I
> believe Git for windows is such a case) while in the mean time I submit
> this case for consideration in the subversion bug tracking system.
Yes, absolutely. Thank you for reporting this!
> The Royal Bank of Scotland plc. Registered in Scotland No 90312. 
Oh. I am sorry!
> This e-mail message is confidential and for use by the addressee only. 

This footer does not make much sense when sending a mail to a public mailing list ;-)

Ciao, Johannes

Johannes Schindelin· Jun 28, 2016, 13:32 UTC · re: Johannes Schindelin · lore

Re: git svn clone segmentation faul issue

Hi Ioannis,
On Tue, 28 Jun 2016, Johannes Schindelin wrote:
Show 19 quoted lines
> On Tue, 28 Jun 2016, Ioannis.Kappas@rbs.com wrote:
> 
> > Git can fail with a "malformed index nnn" error or cause a segmentation
> > fault when executing the "git svn clone" command. 
> >
> > [...]
> >
> > Fortunately, a patch has already been submitted to subversion with
> > (github) revision a074af86c8764404b28ce99d0bedcb668a321408 (at
> > https://github.com/apache/subversion/commit/a074af86c8) on the trunk to
> > handle this and a couple of other similar cases. But by the looks of it
> > has not been picked up yet in the latest subversion 1.9.4 release or the
> > 1.9.x branch, perhaps because this patch was identified in sanity checks
> > rather than coming out from a perceivable production issue?
> 
> This is an excellent analysis and a silver lining on the horizon to
> resolve those vexing git svn issues we keep having in Git for Windows.
> 
> Do you have a test case that is reliably reproducing the issue?

I hope you do! I patched the MSYS2 build script to apply a074af86c8 before compiling, and uploaded the resulting packages for i686 and x86_64 architectures to

	https://github.com/dscho/MSYS2-packages/releases/tag/subversion-1.9.4-2
Would you mind giving them a whirl?

Thanks, Johannes

Ioannis Kappas· Jun 28, 2016, 16:13 UTC · re: Johannes Schindelin · lore

Re: git svn clone segmentation faul issue

Hi Johanes,
Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
Show 8 quoted lines
> 
> Hi Ioannis,
> 
> On Tue, 28 Jun 2016, Johannes Schindelin wrote:
> 
> > On Tue, 28 Jun 2016, Ioannis.Kappas <at> rbs.com wrote:
> > 
> > > Git can fail with a "malformed index nnn" error or cause a 
segmentation
Show 7 quoted lines
> > > fault when executing the "git svn clone" command. 
> > >
> > > [...]
> > >
> > > Fortunately, a patch has already been submitted to subversion with
> > > (github) revision a074af86c8764404b28ce99d0bedcb668a321408 (at
> > > https://github.com/apache/subversion/commit/a074af86c8) on the trunk 
to
> > > handle this and a couple of other similar cases. But by the looks of 
it
> > > has not been picked up yet in the latest subversion 1.9.4 release or 
the
> > > 1.9.x branch, perhaps because this patch was identified in sanity 
checks
Show 8 quoted lines
> > > rather than coming out from a perceivable production issue?
> > 
> > This is an excellent analysis and a silver lining on the horizon to
> > resolve those vexing git svn issues we keep having in Git for Windows.
> > 
> > Do you have a test case that is reliably reproducing the issue?
> 
> I hope you do! 
Yes, my codebase always fails on git svn clone with something like:

fatal: malformed index info 100644 a6d6543f625fb4a7200e error: git-svn died of signal 11

Show 5 quoted lines
> I patched the MSYS2 build script to apply a074af86c8 before
> compiling, and uploaded the resulting packages for i686 and x86_64
> architectures to
> 
> 	https://github.com/dscho/MSYS2-
packages/releases/tag/subversion-1.9.4-2
> 
> Would you mind giving them a whirl?

The patch in "source code (zip)" seems to be missing the line in the prepare () section of PKGBUILD to actually apply the fix:

diff --git a/subversion/PKGBUILD b/subversion/PKGBUILD
--- a/subversion/PKGBUILD
+++ b/subversion/PKGBUILD

@@ -101,6 +103,7 @@ prepare() {
   patch -p1 -i ${srcdir}/17-fix-test-link.patch
   patch -p1 -i ${srcdir}/18-fix-serf-config.patch
   patch -p1 -i ${srcdir}/19-remove-contrib-from-configure.patch
+  patch -p1 -i ${srcdir}/20-fix-stack-corruption-in-swig-wrappers.patch
   patch -p1 -i ${srcdir}/subversion-1.9.1-msys2.patch
   patch -p1 -i ${srcdir}/remove-checking-symlink.patch
   patch -p1 -i ${srcdir}/90-use-copy-instead-symlink.patch


Would you be so kind to add the above and rebuild. 

I have just tested it locally to work successfully, but I do not mind 
retesting if you wish to provide another build :)

Thanks,
Yannis

> 
> Thanks,
> Johannes
> 
Johannes Schindelin· Jun 28, 2016, 16:27 UTC · re: Ioannis Kappas · lore

Re: git svn clone segmentation faul issue

Hi Yannis,
On Tue, 28 Jun 2016, Ioannis Kappas wrote:
> Hi Johanes,
Almost ;-)
Show 22 quoted lines
> Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
> 
> > Would you mind giving them a whirl?
> 
> The patch in "source code (zip)" seems to be missing the line in the prepare
> () section of PKGBUILD to actually apply the fix:
> 
> diff --git a/subversion/PKGBUILD b/subversion/PKGBUILD
> --- a/subversion/PKGBUILD
> +++ b/subversion/PKGBUILD
> 
> @@ -101,6 +103,7 @@ prepare() {
>    patch -p1 -i ${srcdir}/17-fix-test-link.patch
>    patch -p1 -i ${srcdir}/18-fix-serf-config.patch
>    patch -p1 -i ${srcdir}/19-remove-contrib-from-configure.patch
> +  patch -p1 -i ${srcdir}/20-fix-stack-corruption-in-swig-wrappers.patch
>    patch -p1 -i ${srcdir}/subversion-1.9.1-msys2.patch
>    patch -p1 -i ${srcdir}/remove-checking-symlink.patch
>    patch -p1 -i ${srcdir}/90-use-copy-instead-symlink.patch
> 
> 
> Would you be so kind to add the above and rebuild. 
Oh bummer. Sorry for that! I will fix it and rebuild tomorrow.
> I have just tested it locally to work successfully, but I do not mind 
> retesting if you wish to provide another build :)

Thanks, that would be much appreciated! I'll send a reply when I rebuilt the packages.

Ciao, Johannes

Johannes Schindelin· Jun 29, 2016, 07:13 UTC · re: Johannes Schindelin · lore

Re: git svn clone segmentation faul issue

Hi Yannis,
On Tue, 28 Jun 2016, Johannes Schindelin wrote:
Show 32 quoted lines
> On Tue, 28 Jun 2016, Ioannis Kappas wrote:
> 
> > Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
> > 
> > > Would you mind giving them a whirl?
> > 
> > The patch in "source code (zip)" seems to be missing the line in the prepare
> > () section of PKGBUILD to actually apply the fix:
> > 
> > diff --git a/subversion/PKGBUILD b/subversion/PKGBUILD
> > --- a/subversion/PKGBUILD
> > +++ b/subversion/PKGBUILD
> > 
> > @@ -101,6 +103,7 @@ prepare() {
> >    patch -p1 -i ${srcdir}/17-fix-test-link.patch
> >    patch -p1 -i ${srcdir}/18-fix-serf-config.patch
> >    patch -p1 -i ${srcdir}/19-remove-contrib-from-configure.patch
> > +  patch -p1 -i ${srcdir}/20-fix-stack-corruption-in-swig-wrappers.patch
> >    patch -p1 -i ${srcdir}/subversion-1.9.1-msys2.patch
> >    patch -p1 -i ${srcdir}/remove-checking-symlink.patch
> >    patch -p1 -i ${srcdir}/90-use-copy-instead-symlink.patch
> > 
> > 
> > Would you be so kind to add the above and rebuild. 
> 
> Oh bummer. Sorry for that! I will fix it and rebuild tomorrow.
> 
> > I have just tested it locally to work successfully, but I do not mind 
> > retesting if you wish to provide another build :)
> 
> Thanks, that would be much appreciated! I'll send a reply when I rebuilt
> the packages.

I just re-uploaded new packages, after rebuilding them and making sure that the patch was actually applied.

May I ask you to re-test, just to make extra sure that the bug in question is fixed?

Ciao, Johannes

Ioannis Kappas· Jun 29, 2016, 09:00 UTC · re: Johannes Schindelin · lore

Re: git svn clone segmentation faul issue

Hi Johannes,
Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
Show 10 quoted lines
> 
> Hi Yannis,
> 
> [...]
> 
> I just re-uploaded new packages, after rebuilding them and making sure
> that the patch was actually applied.
> 
> May I ask you to re-test, just to make extra sure that the bug in question
> is fixed?

I confirm the latest version, built from the 'source files (zip)' package, has resolved the issue.

Thanks for the prompt response! Next task is to get the patch through to the next subversion official release.

> 
> Ciao,
> Johannes
> 
Johannes Schindelin· Jun 29, 2016, 11:43 UTC · re: Ioannis Kappas · lore

Re: git svn clone segmentation faul issue

Hi Yannis,
On Wed, 29 Jun 2016, Ioannis Kappas wrote:
Show 10 quoted lines
> Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
> 
> > I just re-uploaded new packages, after rebuilding them and making sure
> > that the patch was actually applied.
> > 
> > May I ask you to re-test, just to make extra sure that the bug in
> > question is fixed?
> 
> I confirm the latest version, built from the 'source files (zip)'
> package, has resolved the issue.
Thank you so much!
> Thanks for the prompt response! Next task is to get the patch through to
> the next subversion official release.

My more immediate concern is MSYS2 (the POSIX-emulating basis of Git for Windows), and I opened a Pull Request there:

	https://github.com/Alexpux/MSYS2-packages/pull/647

If this Pull Request is not picked up in time for the next Git for Windows version, I plan to use the packages that you tested.

Thanks, Dscho

Ioannis Kappas· Jul 1, 2016, 10:09 UTC · re: Johannes Schindelin · lore

Re: git svn clone segmentation faul issue

Hi Johannes,
Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
> [...]
> 
> My more immediate concern is MSYS2 (the POSIX-emulating basis of Git 
for
Show 5 quoted lines
> Windows), and I opened a Pull Request there:
> 
> 	https://github.com/Alexpux/MSYS2-packages/pull/647
> 
> If this Pull Request is not picked up in time for the next Git for 
Windows
> version, I plan to use the packages that you tested.

The patch has also been accepted for inclusion in the forthcoming subversion 1.9.5 release ("barring unexpectancies"):

http://thread.gmane.org/gmane.comp.version- control.subversion.user/120144/focus=120146

Thanks, Yannis

> 
> Thanks,
> Dscho
> 
Johannes Schindelin· Jul 1, 2016, 13:41 UTC · re: Ioannis Kappas · lore

Re: git svn clone segmentation faul issue

Hi Yannis,
On Fri, 1 Jul 2016, Ioannis Kappas wrote:
Show 17 quoted lines
> Johannes Schindelin <Johannes.Schindelin <at> gmx.de> writes:
> 
> > [...]
> > 
> > My more immediate concern is MSYS2 (the POSIX-emulating basis of Git
> > for Windows), and I opened a Pull Request there:
> > 
> > 	https://github.com/Alexpux/MSYS2-packages/pull/647
> > 
> > If this Pull Request is not picked up in time for the next Git for
> > Windows version, I plan to use the packages that you tested.
> 
> The patch has also been accepted for inclusion in the forthcoming 
> subversion 1.9.5 release ("barring unexpectancies"):
> 
> http://thread.gmane.org/gmane.comp.version-
> control.subversion.user/120144/focus=120146
Perfect. Thanks for being diligent and keeping us informed!

Ciao, Dscho

Eric Wong· Jun 28, 2016, 19:48 UTC · re: ioannis.kappas@rbs.com · lore

Re: git svn clone segmentation faul issue

Ioannis.Kappas@rbs.com wrote:
Show 9 quoted lines
> Fortunately, a patch has already been submitted to subversion
> with (github) revision
> a074af86c8764404b28ce99d0bedcb668a321408 (at
> https://github.com/apache/subversion/commit/a074af86c8764404b28ce99d0bedcb668a321408
> ) on the trunk to handle this and a couple of other similar
> cases. But by the looks of it has not been picked up yet in
> the latest subversion 1.9.4 release or the 1.9.x branch,
> perhaps because this patch was identified in sanity checks
> rather than coming out from a perceivable production issue?

Thank you for documenting this. Curious, does this affect older SVN versions or only 1.9.x?

I don't know Perl internals well or SWIG at all; so reports like these are very much appreciated.

Ioannis Kappas· Jun 29, 2016, 08:11 UTC · re: Eric Wong · lore

Re: git svn clone segmentation faul issue

Hi Eric,
Eric Wong <e <at> 80x24.org> writes:
Show 6 quoted lines
> 
> Ioannis.Kappas <at> rbs.com wrote:
> > Fortunately, a patch has already been submitted to subversion
> > with (github) revision
> > a074af86c8764404b28ce99d0bedcb668a321408 (at
> > 

https://github.com/apache/subversion/commit/a074af86c8764404b28ce99d0bedcb66 8a321408

Show 8 quoted lines
> > ) on the trunk to handle this and a couple of other similar
> > cases. But by the looks of it has not been picked up yet in
> > the latest subversion 1.9.4 release or the 1.9.x branch,
> > perhaps because this patch was identified in sanity checks
> > rather than coming out from a perceivable production issue?
> 
> Thank you for documenting this.  Curious, does this affect older
> SVN versions or only 1.9.x?

It does not appear so, at least not in the degree of failures that I am seeing it now, i.e. failing consistently. It only came to my attention after I upgraded to the latest git/perl/subversion release from a version that was more than a year's old.

Given that the issue is with corruption in the perl interpreter stack, I suspect is due to one, or combination, of the following:

- git in latest releases makes additional perl subversion calls, thus 
affecting the perl stack usage.
- perl in latest releases affected the stack allocation profile.
- subversion perl module in latest releases refactored/added new calls that 
has changed the perl stack usage.
> 
> I don't know Perl internals well or SWIG at all; so reports
> like these are very much appreciated.
> 
Thanks

← back to recent threads