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