0001-Add-secureboot-pre-signing-to-the-kernel.patch 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. From 43a7e8759162fa25aea203ce732f177bc6f15cdb Mon Sep 17 00:00:00 2001
  2. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3. Date: Sun, 22 Sep 2019 22:44:16 +0200
  4. Subject: [PATCH] Add secureboot pre-signing to the kernel
  5. If it detects a secure boot certificate at `keys/MOK.key` and `keys/MOK.cer`,
  6. the kernel Makefile will automatically sign the vmlinux / bzImage file that
  7. gets generated, and that is then used in packaging.
  8. By integrating it into the kernel build system directly, it is fully integrated
  9. with targets like `make deb-pkg` (opposed to `make all`, sign, `make bindeb-pkg`)
  10. and it gets added to every tree by the same mechanism that is used to apply the
  11. other surface patches anyways.
  12. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  13. ---
  14. .gitignore | 3 +++
  15. arch/x86/Makefile | 1 +
  16. scripts/sign_kernel.sh | 30 ++++++++++++++++++++++++++++++
  17. 3 files changed, 34 insertions(+)
  18. create mode 100755 scripts/sign_kernel.sh
  19. diff --git a/.gitignore b/.gitignore
  20. index 7f86e0837909..04aaae490610 100644
  21. --- a/.gitignore
  22. +++ b/.gitignore
  23. @@ -152,6 +152,9 @@ signing_key.priv
  24. signing_key.x509
  25. x509.genkey
  26. +# Secureboot certificate
  27. +/keys/
  28. +
  29. # Kconfig presets
  30. /all.config
  31. /alldef.config
  32. diff --git a/arch/x86/Makefile b/arch/x86/Makefile
  33. index fdc2e3abd615..c7a374c7ceaf 100644
  34. --- a/arch/x86/Makefile
  35. +++ b/arch/x86/Makefile
  36. @@ -283,6 +283,7 @@ endif
  37. $(Q)$(MAKE) $(build)=$(boot) $(KBUILD_IMAGE)
  38. $(Q)mkdir -p $(objtree)/arch/$(UTS_MACHINE)/boot
  39. $(Q)ln -fsn ../../x86/boot/bzImage $(objtree)/arch/$(UTS_MACHINE)/boot/$@
  40. + $(Q)$(srctree)/scripts/sign_kernel.sh $(objtree)/arch/$(UTS_MACHINE)/boot/$@
  41. $(BOOT_TARGETS): vmlinux
  42. $(Q)$(MAKE) $(build)=$(boot) $@
  43. diff --git a/scripts/sign_kernel.sh b/scripts/sign_kernel.sh
  44. new file mode 100755
  45. index 000000000000..d2526a279254
  46. --- /dev/null
  47. +++ b/scripts/sign_kernel.sh
  48. @@ -0,0 +1,30 @@
  49. +#!/bin/sh
  50. +# SPDX-License-Identifier: GPL-2.0
  51. +
  52. +# The path to the compiled kernel image is passed as the first argument
  53. +BUILDDIR=$(dirname $(dirname $0))
  54. +VMLINUX=$1
  55. +
  56. +# Keys are stored in a toplevel directory called keys
  57. +# The following files need to be there:
  58. +# * MOK.priv (private key)
  59. +# * MOK.pem (public key)
  60. +#
  61. +# If the files don't exist, this script will do nothing.
  62. +if [ ! -f "$BUILDDIR/keys/MOK.key" ]; then
  63. + exit 0
  64. +fi
  65. +if [ ! -f "$BUILDDIR/keys/MOK.crt" ]; then
  66. + exit 0
  67. +fi
  68. +
  69. +# Both required certificates were found. Check if sbsign is installed.
  70. +echo "Keys for automatic secureboot signing found."
  71. +if [ ! -x "$(command -v sbsign)" ]; then
  72. + echo "ERROR: sbsign not found!"
  73. + exit -2
  74. +fi
  75. +
  76. +# Sign the kernel
  77. +sbsign --key $BUILDDIR/keys/MOK.key --cert $BUILDDIR/keys/MOK.crt \
  78. + --output $VMLINUX $VMLINUX
  79. --
  80. 2.41.0