android安裝內核module,提示Required key not


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 = [email protected]"
@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進行簽名的)。重新完整燒錄版本後,一切功能正常!



發佈了369 篇原創文章 · 獲贊 587 · 訪問量 533萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章