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