Blame SOURCES/openssl-1.1.1-cve-2022-2068.patch

6e1574
From 9639817dac8bbbaa64d09efad7464ccc405527c7 Mon Sep 17 00:00:00 2001
6e1574
From: Daniel Fiala <daniel@openssl.org>
6e1574
Date: Sun, 29 May 2022 20:11:24 +0200
6e1574
Subject: [PATCH] Fix file operations in c_rehash.
6e1574
6e1574
CVE-2022-2068
6e1574
6e1574
Reviewed-by: Matt Caswell <matt@openssl.org>
6e1574
Reviewed-by: Richard Levitte <levitte@openssl.org>
6e1574
Upstream-Status: Backport [https://github.com/openssl/openssl/commit/9639817dac8bbbaa64d09efad7464ccc405527c7]
6e1574
---
6e1574
 tools/c_rehash.in | 216 +++++++++++++++++++++++-----------------------
6e1574
 1 file changed, 107 insertions(+), 109 deletions(-)
6e1574
6e1574
diff --git a/tools/c_rehash.in b/tools/c_rehash.in
6e1574
index cfd18f5da110..9d2a6f6db73b 100644
6e1574
--- a/tools/c_rehash.in
6e1574
+++ b/tools/c_rehash.in
6e1574
@@ -104,52 +104,78 @@ foreach (@dirlist) {
6e1574
 }
6e1574
 exit($errorcount);
6e1574
 
6e1574
+sub copy_file {
6e1574
+    my ($src_fname, $dst_fname) = @_;
6e1574
+
6e1574
+    if (open(my $in, "<", $src_fname)) {
6e1574
+        if (open(my $out, ">", $dst_fname)) {
6e1574
+            print $out $_ while (<$in>);
6e1574
+            close $out;
6e1574
+        } else {
6e1574
+            warn "Cannot open $dst_fname for write, $!";
6e1574
+        }
6e1574
+        close $in;
6e1574
+    } else {
6e1574
+        warn "Cannot open $src_fname for read, $!";
6e1574
+    }
6e1574
+}
6e1574
+
6e1574
 sub hash_dir {
6e1574
-	my %hashlist;
6e1574
-	print "Doing $_[0]\n";
6e1574
-	chdir $_[0];
6e1574
-	opendir(DIR, ".");
6e1574
-	my @flist = sort readdir(DIR);
6e1574
-	closedir DIR;
6e1574
-	if ( $removelinks ) {
6e1574
-		# Delete any existing symbolic links
6e1574
-		foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
6e1574
-			if (-l $_) {
6e1574
-				print "unlink $_" if $verbose;
6e1574
-				unlink $_ || warn "Can't unlink $_, $!\n";
6e1574
-			}
6e1574
-		}
6e1574
-	}
6e1574
-	FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
6e1574
-		# Check to see if certificates and/or CRLs present.
6e1574
-		my ($cert, $crl) = check_file($fname);
6e1574
-		if (!$cert && !$crl) {
6e1574
-			print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
6e1574
-			next;
6e1574
-		}
6e1574
-		link_hash_cert($fname) if ($cert);
6e1574
-		link_hash_crl($fname) if ($crl);
6e1574
-	}
6e1574
+    my $dir = shift;
6e1574
+    my %hashlist;
6e1574
+
6e1574
+    print "Doing $dir\n";
6e1574
+
6e1574
+    if (!chdir $dir) {
6e1574
+        print STDERR "WARNING: Cannot chdir to '$dir', $!\n";
6e1574
+        return;
6e1574
+    }
6e1574
+
6e1574
+    opendir(DIR, ".") || print STDERR "WARNING: Cannot opendir '.', $!\n";
6e1574
+    my @flist = sort readdir(DIR);
6e1574
+    closedir DIR;
6e1574
+    if ( $removelinks ) {
6e1574
+        # Delete any existing symbolic links
6e1574
+        foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
6e1574
+            if (-l $_) {
6e1574
+                print "unlink $_\n" if $verbose;
6e1574
+                unlink $_ || warn "Can't unlink $_, $!\n";
6e1574
+            }
6e1574
+        }
6e1574
+    }
6e1574
+    FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
6e1574
+        # Check to see if certificates and/or CRLs present.
6e1574
+        my ($cert, $crl) = check_file($fname);
6e1574
+        if (!$cert && !$crl) {
6e1574
+            print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
6e1574
+            next;
6e1574
+        }
6e1574
+        link_hash_cert($fname) if ($cert);
6e1574
+        link_hash_crl($fname) if ($crl);
6e1574
+    }
6e1574
+
6e1574
+    chdir $pwd;
6e1574
 }
6e1574
 
6e1574
 sub check_file {
6e1574
-	my ($is_cert, $is_crl) = (0,0);
6e1574
-	my $fname = $_[0];
6e1574
-	open IN, $fname;
6e1574
-	while(<IN>) {
6e1574
-		if (/^-----BEGIN (.*)-----/) {
6e1574
-			my $hdr = $1;
6e1574
-			if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
6e1574
-				$is_cert = 1;
6e1574
-				last if ($is_crl);
6e1574
-			} elsif ($hdr eq "X509 CRL") {
6e1574
-				$is_crl = 1;
6e1574
-				last if ($is_cert);
6e1574
-			}
6e1574
-		}
6e1574
-	}
6e1574
-	close IN;
6e1574
-	return ($is_cert, $is_crl);
6e1574
+    my ($is_cert, $is_crl) = (0,0);
6e1574
+    my $fname = $_[0];
6e1574
+
6e1574
+    open(my $in, "<", $fname);
6e1574
+    while(<$in>) {
6e1574
+        if (/^-----BEGIN (.*)-----/) {
6e1574
+            my $hdr = $1;
6e1574
+            if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
6e1574
+                $is_cert = 1;
6e1574
+                last if ($is_crl);
6e1574
+            } elsif ($hdr eq "X509 CRL") {
6e1574
+                $is_crl = 1;
6e1574
+                last if ($is_cert);
6e1574
+            }
6e1574
+        }
6e1574
+    }
6e1574
+    close $in;
6e1574
+    return ($is_cert, $is_crl);
6e1574
 }
6e1574
 
6e1574
 sub compute_hash {
6e1574
@@ -177,76 +203,48 @@ sub compute_hash {
6e1574
 # certificate fingerprints
6e1574
 
6e1574
 sub link_hash_cert {
6e1574
-		my $fname = $_[0];
6e1574
-		my ($hash, $fprint) = compute_hash($openssl, "x509", $x509hash,
6e1574
-						   "-fingerprint", "-noout",
6e1574
-						   "-in", $fname);
6e1574
-		chomp $hash;
6e1574
-		chomp $fprint;
6e1574
-		return if !$hash;
6e1574
-		$fprint =~ s/^.*=//;
6e1574
-		$fprint =~ tr/://d;
6e1574
-		my $suffix = 0;
6e1574
-		# Search for an unused hash filename
6e1574
-		while(exists $hashlist{"$hash.$suffix"}) {
6e1574
-			# Hash matches: if fingerprint matches its a duplicate cert
6e1574
-			if ($hashlist{"$hash.$suffix"} eq $fprint) {
6e1574
-				print STDERR "WARNING: Skipping duplicate certificate $fname\n";
6e1574
-				return;
6e1574
-			}
6e1574
-			$suffix++;
6e1574
-		}
6e1574
-		$hash .= ".$suffix";
6e1574
-		if ($symlink_exists) {
6e1574
-			print "link $fname -> $hash\n" if $verbose;
6e1574
-			symlink $fname, $hash || warn "Can't symlink, $!";
6e1574
-		} else {
6e1574
-			print "copy $fname -> $hash\n" if $verbose;
6e1574
-                        if (open($in, "<", $fname)) {
6e1574
-                            if (open($out,">", $hash)) {
6e1574
-                                print $out $_ while (<$in>);
6e1574
-                                close $out;
6e1574
-                            } else {
6e1574
-                                warn "can't open $hash for write, $!";
6e1574
-                            }
6e1574
-                            close $in;
6e1574
-                        } else {
6e1574
-                            warn "can't open $fname for read, $!";
6e1574
-                        }
6e1574
-		}
6e1574
-		$hashlist{$hash} = $fprint;
6e1574
+    link_hash($_[0], 'cert');
6e1574
 }
6e1574
 
6e1574
 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
6e1574
 
6e1574
 sub link_hash_crl {
6e1574
-		my $fname = $_[0];
6e1574
-		my ($hash, $fprint) = compute_hash($openssl, "crl", $crlhash,
6e1574
-						   "-fingerprint", "-noout",
6e1574
-						   "-in", $fname);
6e1574
-		chomp $hash;
6e1574
-		chomp $fprint;
6e1574
-		return if !$hash;
6e1574
-		$fprint =~ s/^.*=//;
6e1574
-		$fprint =~ tr/://d;
6e1574
-		my $suffix = 0;
6e1574
-		# Search for an unused hash filename
6e1574
-		while(exists $hashlist{"$hash.r$suffix"}) {
6e1574
-			# Hash matches: if fingerprint matches its a duplicate cert
6e1574
-			if ($hashlist{"$hash.r$suffix"} eq $fprint) {
6e1574
-				print STDERR "WARNING: Skipping duplicate CRL $fname\n";
6e1574
-				return;
6e1574
-			}
6e1574
-			$suffix++;
6e1574
-		}
6e1574
-		$hash .= ".r$suffix";
6e1574
-		if ($symlink_exists) {
6e1574
-			print "link $fname -> $hash\n" if $verbose;
6e1574
-			symlink $fname, $hash || warn "Can't symlink, $!";
6e1574
-		} else {
6e1574
-			print "cp $fname -> $hash\n" if $verbose;
6e1574
-			system ("cp", $fname, $hash);
6e1574
-                        warn "Can't copy, $!" if ($? >> 8) != 0;
6e1574
-		}
6e1574
-		$hashlist{$hash} = $fprint;
6e1574
+    link_hash($_[0], 'crl');
6e1574
+}
6e1574
+
6e1574
+sub link_hash {
6e1574
+    my ($fname, $type) = @_;
6e1574
+    my $is_cert = $type eq 'cert';
6e1574
+
6e1574
+    my ($hash, $fprint) = compute_hash($openssl,
6e1574
+                                       $is_cert ? "x509" : "crl",
6e1574
+                                       $is_cert ? $x509hash : $crlhash,
6e1574
+                                       "-fingerprint", "-noout",
6e1574
+                                       "-in", $fname);
6e1574
+    chomp $hash;
6e1574
+    chomp $fprint;
6e1574
+    return if !$hash;
6e1574
+    $fprint =~ s/^.*=//;
6e1574
+    $fprint =~ tr/://d;
6e1574
+    my $suffix = 0;
6e1574
+    # Search for an unused hash filename
6e1574
+    my $crlmark = $is_cert ? "" : "r";
6e1574
+    while(exists $hashlist{"$hash.$crlmark$suffix"}) {
6e1574
+        # Hash matches: if fingerprint matches its a duplicate cert
6e1574
+        if ($hashlist{"$hash.$crlmark$suffix"} eq $fprint) {
6e1574
+            my $what = $is_cert ? 'certificate' : 'CRL';
6e1574
+            print STDERR "WARNING: Skipping duplicate $what $fname\n";
6e1574
+            return;
6e1574
+        }
6e1574
+        $suffix++;
6e1574
+    }
6e1574
+    $hash .= ".$crlmark$suffix";
6e1574
+    if ($symlink_exists) {
6e1574
+        print "link $fname -> $hash\n" if $verbose;
6e1574
+        symlink $fname, $hash || warn "Can't symlink, $!";
6e1574
+    } else {
6e1574
+        print "copy $fname -> $hash\n" if $verbose;
6e1574
+        copy_file($fname, $hash);
6e1574
+    }
6e1574
+    $hashlist{$hash} = $fprint;
6e1574
 }