android安裝核心module,提示Required key not available
最近在調試一個驅動的時候,用insmod載入.ko的時候,提示Required key not available,第一反應是簽名有問題,核心模組也開始使用類似apk的簽名了嗎。查資料後果然是這樣。這個問題可以說不算是android的問題,而應該是linux系統的問題,android本身就是個linux系統。
下來一步一步分析問題的所在。
核心配置
核心從3.7後開始支援模組簽名,這個功能使能以後,核心只允許安裝特定key簽名的模組。
核心配置項
CONFIG_MODULE_SIG=y
表示開啟了簽名機制,但是這時候模組簽名或不簽名都可以使用。
CONFIG_MODULE_SIG_FORCE=y
如果上述配置項使能,則模組必須有正確的簽名才能正常使用。
CONFIG_MODULE_SIG_ALL=y
核心在編譯的時候,並不會主動去給模組簽名,除非你把上述配置項開啟。
查看核心設定檔,發現上面3個配置項確實都開啟了,因此肯定是ko簽名的問題。
核心如何簽名
在核心kernel/kernel下的Makefile中有如下,
signing_key.priv signing_key.x509: x509.genkey
@echo "###"
@echo "### Now generating an X.509 key pair to be used for signing modules."
@echo "###"
@echo "### If this takes a long time, you might wish to run rngd in the"
@echo "### background to keep the supply of entropy topped up. It"
@echo "### needs to be run as root, and uses a hardware random"
@echo "### number generator if one is available."
@echo "###"
openssl req -new -nodes -utf8 -$(CONFIG_MODULE_SIG_HASH) -days 36500 \
-batch -x509 -config x509.genkey \
-outform DER -out signing_key.x509 \
-keyout signing_key.priv 2>&1
@echo "###"
@echo "### Key pair generated."
@echo "###"
x509.genkey:
@echo Generating X.509 key generation config
@echo >x509.genkey "[ req ]"
@echo >>x509.genkey "default_bits = 4096"
@echo >>x509.genkey "distinguished_name = req_distinguished_name"
@echo >>x509.genkey "prompt = no"
@echo >>x509.genkey "string_mask = utf8only"
@echo >>x509.genkey "x509_extensions = myexts"
@echo >>x509.genkey
@echo >>x509.genkey "[ req_distinguished_name ]"
@echo >>x509.genkey "O = Magrathea"
@echo >>x509.genkey "CN = Glacier signing key"
@echo >>x509.genkey "emailAddress = slartibartfast@magrathea.h2g2"
@echo >>x509.genkey
@echo >>x509.genkey "[ myexts ]"
@echo >>x509.genkey "basicConstraints=critical,CA:FALSE"
@echo >>x509.genkey "keyUsage=digitalSignature"
@echo >>x509.genkey "subjectKeyIdentifier=hash"
@echo >>x509.genkey "authorityKeyIdentifier=keyid"
其中,x509.genkey是產生key pair時的配置項,signing_key.priv signing_key.x509分別為private key和數位憑證。數位憑證會打包進核心,裡面有公開金鑰等,用來解密嘛。每編譯一次,雖然設定檔每次都相同,但是產生的key pair是不同的。
查看簽名資訊
利用下面命令查看裝置中的ko檔案資訊,
hexdump -C my_ko.ko |tail
下面是輸出(核心簽名後會把簽名資訊附在模組的最後面),
00538760 d3 48 70 32 1c 36 75 05 5f f2 39 84 7d c8 77 2f |.Hp2.6u._.9.}.w/|
00538770 db 1d b6 1a 18 4b b5 0f 0f 44 5a f9 c3 1d d7 66 |.....K...DZ....f|
00538780 08 d5 22 ab 3e f6 4b 38 81 14 b3 a4 56 ab 22 3d |..".>.K8....V."=|
00538790 55 fe cc 2b 9c 82 28 39 0e 47 df 63 a3 2a bc b4 |U..+..(9.G.c.*..|
005387a0 73 c9 a2 78 6a 6e 4c f7 4f 36 b3 45 1b 64 73 b8 |s..xjnL.O6.E.ds.|
005387b0 1d ca 49 ff 59 6a 99 4b 5b 13 40 75 01 06 01 1e |..I.Yj.K[.@u....|
005387c0 14 00 00 00 00 00 02 02 7e 4d 6f 64 75 6c 65 20 |........~Module |
005387d0 73 69 67 6e 61 74 75 72 65 20 61 70 70 65 6e 64 |signature append|
005387e0 65 64 7e 0a |ed~.|
005387e4
由上面輸出,我們發現這個ko已經有簽名資訊(Module signature appended),為何還是提示key不對。於是我將編譯機中版本的my_ko.ko和裝置中的做比較,發現唯有最後部分不同,我猜一定是兩個ko的簽名不同,這應該就是初步原因。
問題根源
仔細分析後,得到原因:
原來裝置中的核心是後來編譯的,編譯完成後我將核心單獨燒錄進裝置(核心肯定就放在kernel的分區),而未改變檔案系統(這樣會造成新kernel中的數位憑證已經改變,但是檔案系統中的my_ko.ko未改變,而是用以前的核心中private key進行簽名的)。重新完整燒錄版本後,一切功能正常!