isaacpittman-hitachi / rpms / openssl

Forked from rpms/openssl 2 years ago
Clone

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

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