extract-sys-certs.pl 3.7 KB

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