Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 3 | # |
Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 4 | use warnings; |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 5 | use strict; |
| 6 | use Math::BigInt; |
| 7 | use Fcntl "SEEK_SET"; |
| 8 | |
| 9 | die "Format: $0 [-s <systemmap-file>] <vmlinux-file> <keyring-file>\n" |
| 10 | if ($#ARGV != 1 && $#ARGV != 3 || |
| 11 | $#ARGV == 3 && $ARGV[0] ne "-s"); |
| 12 | |
| 13 | my $sysmap = ""; |
| 14 | if ($#ARGV == 3) { |
| 15 | shift; |
| 16 | $sysmap = $ARGV[0]; |
| 17 | shift; |
| 18 | } |
| 19 | |
| 20 | my $vmlinux = $ARGV[0]; |
| 21 | my $keyring = $ARGV[1]; |
| 22 | |
| 23 | # |
| 24 | # Parse the vmlinux section table |
| 25 | # |
| 26 | open FD, "objdump -h $vmlinux |" || die $vmlinux; |
| 27 | my @lines = <FD>; |
| 28 | close(FD) || die $vmlinux; |
| 29 | |
| 30 | my @sections = (); |
| 31 | |
| 32 | foreach my $line (@lines) { |
| 33 | chomp($line); |
| 34 | if ($line =~ /\s*([0-9]+)\s+(\S+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+2[*][*]([0-9]+)/ |
| 35 | ) { |
| 36 | my $seg = $1; |
| 37 | my $name = $2; |
| 38 | my $len = Math::BigInt->new("0x" . $3); |
| 39 | my $vma = Math::BigInt->new("0x" . $4); |
| 40 | my $lma = Math::BigInt->new("0x" . $5); |
| 41 | my $foff = Math::BigInt->new("0x" . $6); |
| 42 | my $align = 2 ** $7; |
| 43 | |
| 44 | push @sections, { name => $name, |
| 45 | vma => $vma, |
| 46 | len => $len, |
| 47 | foff => $foff }; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | print "Have $#sections sections\n"; |
| 52 | |
| 53 | # |
| 54 | # Try and parse the vmlinux symbol table. If the vmlinux file has been created |
| 55 | # from a vmlinuz file with extract-vmlinux then the symbol table will be empty. |
| 56 | # |
| 57 | open FD, "nm $vmlinux 2>/dev/null |" || die $vmlinux; |
| 58 | @lines = <FD>; |
| 59 | close(FD) || die $vmlinux; |
| 60 | |
| 61 | my %symbols = (); |
| 62 | my $nr_symbols = 0; |
| 63 | |
| 64 | sub parse_symbols(@) { |
| 65 | foreach my $line (@_) { |
| 66 | chomp($line); |
| 67 | if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/ |
| 68 | ) { |
| 69 | my $addr = "0x" . $1; |
| 70 | my $type = $2; |
| 71 | my $name = $3; |
| 72 | |
| 73 | $symbols{$name} = $addr; |
| 74 | $nr_symbols++; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | parse_symbols(@lines); |
| 79 | |
| 80 | if ($nr_symbols == 0 && $sysmap ne "") { |
| 81 | print "No symbols in vmlinux, trying $sysmap\n"; |
| 82 | |
| 83 | open FD, "<$sysmap" || die $sysmap; |
| 84 | @lines = <FD>; |
| 85 | close(FD) || die $sysmap; |
| 86 | parse_symbols(@lines); |
| 87 | } |
| 88 | |
| 89 | die "No symbols available\n" |
| 90 | if ($nr_symbols == 0); |
| 91 | |
| 92 | print "Have $nr_symbols symbols\n"; |
| 93 | |
| 94 | die "Can't find system certificate list" |
| 95 | unless (exists($symbols{"__cert_list_start"}) && |
Mehmet Kayaalp | 8e16789 | 2015-11-24 16:19:03 -0500 | [diff] [blame] | 96 | exists($symbols{"system_certificate_list_size"})); |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 97 | |
| 98 | my $start = Math::BigInt->new($symbols{"__cert_list_start"}); |
Mehmet Kayaalp | 8e16789 | 2015-11-24 16:19:03 -0500 | [diff] [blame] | 99 | my $end; |
| 100 | my $size; |
| 101 | my $size_sym = Math::BigInt->new($symbols{"system_certificate_list_size"}); |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 102 | |
Mehmet Kayaalp | 8e16789 | 2015-11-24 16:19:03 -0500 | [diff] [blame] | 103 | open FD, "<$vmlinux" || die $vmlinux; |
| 104 | binmode(FD); |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 105 | |
| 106 | my $s = undef; |
| 107 | foreach my $sec (@sections) { |
| 108 | my $s_name = $sec->{name}; |
| 109 | my $s_vma = $sec->{vma}; |
| 110 | my $s_len = $sec->{len}; |
| 111 | my $s_foff = $sec->{foff}; |
| 112 | my $s_vend = $s_vma + $s_len; |
| 113 | |
| 114 | next unless ($start >= $s_vma); |
| 115 | next if ($start >= $s_vend); |
| 116 | |
Mehmet Kayaalp | 8e16789 | 2015-11-24 16:19:03 -0500 | [diff] [blame] | 117 | die "Certificate list size was not found on the same section\n" |
| 118 | if ($size_sym < $s_vma || $size_sym > $s_vend); |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 119 | |
| 120 | die "Cert object in multiple sections: ", $s_name, " and ", $s->{name}, "\n" |
| 121 | if ($s); |
Mehmet Kayaalp | 8e16789 | 2015-11-24 16:19:03 -0500 | [diff] [blame] | 122 | |
| 123 | my $size_off = $size_sym -$s_vma + $s_foff; |
| 124 | my $packed; |
| 125 | die $vmlinux if (!defined(sysseek(FD, $size_off, SEEK_SET))); |
| 126 | sysread(FD, $packed, 8); |
| 127 | $size = unpack 'L!', $packed; |
| 128 | $end = $start + $size; |
| 129 | |
| 130 | printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start; |
| 131 | |
| 132 | die "Cert object partially overflows section $s_name\n" |
| 133 | if ($end > $s_vend); |
| 134 | |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 135 | $s = $sec; |
| 136 | } |
| 137 | |
| 138 | die "Cert object not inside a section\n" |
| 139 | unless ($s); |
| 140 | |
| 141 | print "Certificate list in section ", $s->{name}, "\n"; |
| 142 | |
| 143 | my $foff = $start - $s->{vma} + $s->{foff}; |
| 144 | |
| 145 | printf "Certificate list at file offset 0x%x\n", $foff; |
| 146 | |
David Howells | 2221a6e | 2015-10-21 14:04:47 +0100 | [diff] [blame] | 147 | die $vmlinux if (!defined(sysseek(FD, $foff, SEEK_SET))); |
| 148 | my $buf = ""; |
| 149 | my $len = sysread(FD, $buf, $size); |
| 150 | die "$vmlinux" if (!defined($len)); |
| 151 | die "Short read on $vmlinux\n" if ($len != $size); |
| 152 | close(FD) || die $vmlinux; |
| 153 | |
| 154 | open FD, ">$keyring" || die $keyring; |
| 155 | binmode(FD); |
| 156 | $len = syswrite(FD, $buf, $size); |
| 157 | die "$keyring" if (!defined($len)); |
| 158 | die "Short write on $keyring\n" if ($len != $size); |
| 159 | close(FD) || die $keyring; |