63be29f
diff --git a/scripts/Dpkg/Source/Patch.pm b/scripts/Dpkg/Source/Patch.pm
63be29f
--- a/scripts/Dpkg/Source/Patch.pm
63be29f
+++ b/scripts/Dpkg/Source/Patch.pm
63be29f
@@ -322,8 +322,9 @@ sub analyze {
63be29f
 	    error(_g("expected ^--- in line %d of diff `%s'"), $., $diff);
63be29f
 	}
63be29f
         $_ = strip_ts($_);
63be29f
-        if ($_ eq '/dev/null' or s{^(\./)?[^/]+/}{$destdir/}) {
63be29f
+        if ($_ eq '/dev/null' or s{^[^/]+/}{$destdir/}) {
63be29f
             $fn = $_;
63be29f
+	    error(_g("%s contains an insecure path: %s"), $diff, $_) if m{/\.\./};
63be29f
         }
63be29f
 	if (/\.dpkg-orig$/) {
63be29f
 	    error(_g("diff `%s' patches file with name ending .dpkg-orig"), $diff);
63be29f
@@ -336,8 +337,9 @@ sub analyze {
63be29f
 	    error(_g("line after --- isn't as expected in diff `%s' (line %d)"), $diff, $.);
63be29f
 	}
63be29f
         $_ = strip_ts($_);
63be29f
-        if ($_ eq '/dev/null' or s{^(\./)?[^/]+/}{$destdir/}) {
63be29f
+        if ($_ eq '/dev/null' or s{^[^/]+/}{$destdir/}) {
63be29f
             $fn2 = $_;
63be29f
+	    error(_g("%s contains an insecure path: %s"), $diff, $_) if m{/\.\./};
63be29f
         } else {
63be29f
             unless (defined $fn) {
63be29f
                 error(_g("none of the filenames in ---/+++ are relative in diff `%s' (line %d)"),
63be29f
@@ -363,6 +365,17 @@ sub analyze {
63be29f
 	if ($dirname =~ s{/[^/]+$}{} && not -d $dirname) {
63be29f
 	    $dirtocreate{$dirname} = 1;
63be29f
 	}
63be29f
+
63be29f
+	# Sanity check, refuse to patch through a symlink
63be29f
+	$dirname = $fn;
63be29f
+	while (1) {
63be29f
+	    if (-l $dirname) {
63be29f
+		error(_g("diff %s modifies file %s through a symlink: %s"),
63be29f
+		      $diff, $fn, $dirname);
63be29f
+	    }
63be29f
+	    last unless $dirname =~ s{/[^/]+$}{};
63be29f
+	}
63be29f
+
63be29f
 	if (-e $fn and not -f _) {
63be29f
 	    error(_g("diff `%s' patches something which is not a plain file"), $diff);
63be29f
 	}