extract-sys-certs.pl 3.7 KB

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