From e1dcc2f0c02a31d12d2455e4ce457ff426b6c6fe Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Tue, 28 Mar 2017 22:44:36 +0200 Subject: [PATCH] build: Detect the required GNU patch This makes sure the perl module is using a directory traversal resistant patch implementation, currently that's only GNU patch. --- configure.ac | 1 + m4/dpkg-progs.m4 | 21 +++++++++++++++++++++ scripts/Dpkg.pm | 13 ++++++++++++- scripts/Dpkg/Source/Patch.pm | 9 +++++---- scripts/Makefile.am | 2 ++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index d5075509c..87c1e9d09 100644 --- a/configure.ac +++ b/configure.ac @@ -66,6 +66,7 @@ AC_PROG_CC DPKG_C_C99 AC_PROG_CXX DPKG_CXX_CXX11 +DPKG_PROG_PATCH AC_CHECK_PROGS([DOXYGEN], [doxygen]) AC_CHECK_PROG([HAVE_DOT], [dot], [YES], [NO]) DPKG_PROG_PO4A diff --git a/m4/dpkg-progs.m4 b/m4/dpkg-progs.m4 index f4a0b5b3a..3a0a8478f 100644 --- a/m4/dpkg-progs.m4 +++ b/m4/dpkg-progs.m4 @@ -70,3 +70,24 @@ AC_DEFUN([DPKG_DEB_PROG_TAR], [ AC_SUBST([TAR], [$ac_cv_path_TAR]) AC_DEFINE_UNQUOTED([TAR], ["$TAR"], [GNU tar program]) ])# DPKG_DEB_PROG_TAR + +# DPKG_PROG_PATCH +# --------------- +# Specify GNU patch program name to use by dpkg-source. On GNU systems this +# is usually simply patch, on BSD systems this is usually gpatch. +# Even though most invocations would work with other patch implementations, +# currently only GNU patch is directory traversal resitant. +AC_DEFUN([DPKG_PROG_PATCH], [ + AC_ARG_VAR([PATCH], [GNU patch program]) + AC_CACHE_CHECK([for GNU patch], [ac_cv_path_PATCH], [ + AC_PATH_PROGS_FEATURE_CHECK([PATCH], [gpatch patch], [ + AS_IF([$ac_path_PATCH --version 2>/dev/null | grep -q '^GNU patch'], [ + ac_cv_path_PATCH=$ac_path_PATCH ac_path_PATCH_found=: + ]) + ], [ + AC_MSG_ERROR([cannot find a GNU patch program]) + ]) + ]) + AC_SUBST([PATCH], [$ac_cv_path_PATCH]) + AC_DEFINE_UNQUOTED([PATCH], ["$PATCH"], [GNU patch program]) +])# DPKG_PROG_PATCH diff --git a/scripts/Dpkg.pm b/scripts/Dpkg.pm index 1b9624c4d..3deb933f9 100644 --- a/scripts/Dpkg.pm +++ b/scripts/Dpkg.pm @@ -29,12 +29,13 @@ this system installation. use strict; use warnings; -our $VERSION = '1.02'; +our $VERSION = '1.03'; our @EXPORT_OK = qw( $PROGNAME $PROGVERSION $PROGMAKE $PROGTAR + $PROGPATCH $CONFDIR $ADMINDIR $LIBDIR @@ -70,6 +71,11 @@ Contains the name of the system GNU make program. Contains the name of the system GNU tar program. +=item $Dpkg::PROGPATCH + +Contains the name of the system GNU patch program (or another implementation +that is directory traversal resistant). + =item $Dpkg::CONFDIR Contains the path to the dpkg system configuration directory. @@ -96,6 +102,7 @@ our ($PROGNAME) = $0 =~ m{(?:.*/)?([^/]*)}; our $PROGVERSION = '1.18.x'; our $PROGMAKE = $ENV{DPKG_PROGMAKE} // 'make'; our $PROGTAR = $ENV{DPKG_PROGTAR} // 'tar'; +our $PROGPATCH = $ENV{DPKG_PROGPATCH} // 'patch'; our $CONFDIR = '/etc/dpkg'; our $ADMINDIR = '/var/lib/dpkg'; @@ -114,6 +121,10 @@ our $pkgdatadir = $DATADIR; =head1 CHANGES +=head2 Version 1.03 (dpkg 1.18.24) + +New variable: $PROGPATCH. + =head2 Version 1.02 (dpkg 1.18.11) New variable: $PROGTAR, $PROGMAKE. diff --git a/scripts/Dpkg/Source/Patch.pm b/scripts/Dpkg/Source/Patch.pm index ee5e114f8..22e9d213d 100644 --- a/scripts/Dpkg/Source/Patch.pm +++ b/scripts/Dpkg/Source/Patch.pm @@ -30,6 +30,7 @@ use File::Compare; use Fcntl ':mode'; use Time::HiRes qw(stat); +use Dpkg; use Dpkg::Gettext; use Dpkg::ErrorHandling; use Dpkg::IPC; @@ -582,7 +583,7 @@ sub apply { $self->ensure_open('r'); my ($stdout, $stderr) = ('', ''); spawn( - exec => [ 'patch', @{$opts{options}} ], + exec => [ $Dpkg::PROGPATCH, @{$opts{options}} ], chdir => $destdir, env => { LC_ALL => 'C', LANG => 'C', PATCH_GET => '0' }, delete_env => [ 'POSIXLY_CORRECT' ], # ensure expected patch behaviour @@ -595,7 +596,7 @@ sub apply { if ($?) { print { *STDOUT } $stdout; print { *STDERR } $stderr; - subprocerr('LC_ALL=C patch ' . join(' ', @{$opts{options}}) . + subprocerr("LC_ALL=C $Dpkg::PROGPATCH " . join(' ', @{$opts{options}}) . ' < ' . $self->get_filename()); } $self->close(); @@ -632,7 +633,7 @@ sub check_apply { # Apply the patch $self->ensure_open('r'); my $patch_pid = spawn( - exec => [ 'patch', @{$opts{options}} ], + exec => [ $Dpkg::PROGPATCH, @{$opts{options}} ], chdir => $destdir, env => { LC_ALL => 'C', LANG => 'C', PATCH_GET => '0' }, delete_env => [ 'POSIXLY_CORRECT' ], # ensure expected patch behaviour @@ -642,7 +643,7 @@ sub check_apply { ); wait_child($patch_pid, nocheck => 1); my $exit = WEXITSTATUS($?); - subprocerr('patch --dry-run') unless WIFEXITED($?); + subprocerr("$Dpkg::PROGPATCH --dry-run") unless WIFEXITED($?); $self->close(); return ($exit == 0); } diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 5583fa960..54a83c044 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -132,6 +132,7 @@ do_perl_subst = $(AM_V_GEN) sed \ -e "s:our \$$DATADIR = .*;:our \$$DATADIR = '$(pkgdatadir)';:" \ -e "s:our \$$PROGMAKE = .*;:our \$$PROGMAKE = '$(MAKE)';:" \ -e "s:our \$$PROGTAR = .*;:our \$$PROGTAR = '$(TAR)';:" \ + -e "s:our \$$PROGPATCH = .*;:our \$$PROGPATCH = '$(PATCH)';:" \ -e "s:our \$$PROGVERSION = .*;:our \$$PROGVERSION = '$(PACKAGE_VERSION)';:" do_shell_subst = $(AM_V_GEN) sed \ @@ -193,6 +194,7 @@ coverage-clean: TEST_ENV_VARS = \ DPKG_PROGTAR=$(TAR) \ + DPKG_PROGPATCH=$(PATCH) \ DPKG_PROGMAKE=$(MAKE) \ DPKG_DATADIR=$(top_srcdir)/data \ DPKG_ORIGINS_DIR=$(srcdir)/t/origins -- 2.12.2.762.g0e3151a226