blob: 0e89b02ba090bd2f7593c0537a08c37863653a4e [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001From 7d1985ec82ab00b5a9c88d13f0d2ef9482ede535 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?=E9=AB=98=E5=8D=8E=E5=B3=B00318000169?=
3 <gao.huafeng@sanechips.com.cn>
4Date: Tue, 27 Feb 2024 15:18:54 +0800
5Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=95=85=E4=BA=8B=EF=BC=9A?=
6 =?UTF-8?q?=E6=97=A0=20=E6=95=85=E9=9A=9C=E5=8D=95=E5=8F=B7=EF=BC=9AZX2975?=
7 =?UTF-8?q?20V3-511624=20=20=E5=90=88=E5=85=A5=E5=86=85=E5=AE=B9:=20?=
8 =?UTF-8?q?=E5=B9=BF=E5=B7=9E=E9=80=9A=E5=88=99=E5=BA=B7=E5=A8=81=EF=BC=9A?=
9 =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F=E7=AD=BE=E5=90=8D=E5=8A=A0?=
10 =?UTF-8?q?=E5=AF=86=E9=9C=80=E6=B1=82=E8=AF=84=E4=BC=B0=E3=80=82=E6=B3=A2?=
11 =?UTF-8?q?=E5=8F=8A=E5=88=86=E6=9E=90=EF=BC=9A=E5=90=AF=E5=8A=A8=20=20?=
12 =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=BB=BA=E8=AE=AE=EF=BC=9A=E5=90=AF=E5=8A=A8?=
13 =?UTF-8?q?=E5=92=8C=E5=BA=94=E7=94=A8=E9=AA=8C=E7=AD=BE=E5=8A=9F=E8=83=BD?=
14 =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=AD=A3=E5=B8=B8=20=20=E7=BB=84=E4=BB=B6?=
15 =?UTF-8?q?=E6=A0=87=E7=AD=BE=EF=BC=9A=E6=97=A0=20=E5=BC=95=E5=85=A5?=
16 =?UTF-8?q?=E6=9D=A5=E6=BA=90=EF=BC=9A=E5=85=B6=E5=AE=83?=
17MIME-Version: 1.0
18Content-Type: text/plain; charset=UTF-8
19Content-Transfer-Encoding: 8bit
20
21Change-Id: I9bd1fea056f4a34c7b21421703de5b14523262a7
22---
23 Makefile | 18 +++
24 build/gen_app_hash.sh | 65 +++++++++
25 .../linux-3.4.x/include/linux/verify_app.h | 16 +++
26 os/linux/linux-3.4.x/init/Kconfig | 8 ++
27 os/linux/linux-3.4.x/init/Makefile | 2 +-
28 os/linux/linux-3.4.x/init/verify_app.c | 130 ++++++++++++++++++
29 project/pubconf.mk | 1 +
30 project/zx297520v3/prj_cpe/build/config.mk | 5 +-
31 .../prj_cpe/config/normal/config.linux | 2 +-
32 .../prj_cpe/config/normal/verify_app_list.txt | 3 +
33 10 files changed, 247 insertions(+), 3 deletions(-)
34 create mode 100755 build/gen_app_hash.sh
35 create mode 100755 os/linux/linux-3.4.x/include/linux/verify_app.h
36 create mode 100755 os/linux/linux-3.4.x/init/verify_app.c
37 create mode 100755 project/zx297520v3/prj_cpe/config/normal/verify_app_list.txt
38
39diff --git a/Makefile b/Makefile
40index 97cd38f03..3b5cf553c 100644
41--- a/Makefile
42+++ b/Makefile
43@@ -380,11 +380,29 @@ endif
44 make normal_conf
45 make AP_BUILD_TYPE=normal sys
46 make AP_BUILD_TYPE=normal rootfs
47+ifeq ($(VERIFY_APP_IN_KERNEL),yes)
48+ bash $(BUILD_DIR)/gen_app_hash.sh $(PRJ_CONF_DIR)/normal/verify_app_list.txt $(ROOTFS_DIR) $(LINUX_DIR)
49+ make kernel
50+ifeq ($(CONFIG_SINGLECORE),yes)
51+ifeq ($(USE_CPPS_KO),yes)
52+ make cpko
53+endif
54+endif
55+endif
56 make AP_BUILD_TYPE=normal copybin
57
58 normal2:
59 make AP_BUILD_TYPE=normal sys
60 make AP_BUILD_TYPE=normal rootfs
61+ifeq ($(VERIFY_APP_IN_KERNEL),yes)
62+ bash $(BUILD_DIR)/gen_app_hash.sh $(PRJ_CONF_DIR)/normal/verify_app_list.txt $(ROOTFS_DIR) $(LINUX_DIR)
63+ make kernel
64+ifeq ($(CONFIG_SINGLECORE),yes)
65+ifeq ($(USE_CPPS_KO),yes)
66+ make cpko
67+endif
68+endif
69+endif
70 make AP_BUILD_TYPE=normal copybin
71
72 normalclean:
73diff --git a/build/gen_app_hash.sh b/build/gen_app_hash.sh
74new file mode 100755
75index 000000000..479403809
76--- /dev/null
77+++ b/build/gen_app_hash.sh
78@@ -0,0 +1,65 @@
79+#!/bin/bash
80+
81+echo $#
82+
83+if [ $# -lt 3 ]; then
84+ echo "$0 app_list_file rootfs_dir kernel_dir"
85+ exit 1
86+fi
87+APP_LIST_FILE=$1
88+ROOTFS_DIR=$2
89+KERNEL_DIR=$3
90+
91+if [ ! -f $APP_LIST_FILE ]; then
92+ echo "$APP_LIST_FILE NOT exist"
93+ exit 2
94+fi
95+
96+if [ ! -d $KERNEL_DIR ]; then
97+ echo "$KERNEL_DIR NOT exist"
98+ exit 3
99+fi
100+
101+if [ ! -d $ROOTFS_DIR ]; then
102+ echo "$ROOTFS_DIR NOT exist"
103+ exit 4
104+fi
105+VERIFY_APP_HEADER=$KERNEL_DIR/include/linux/verify_app.h
106+
107+file_array=()
108+hash_array=()
109+file_cnt=0
110+while read file_item; do
111+ if [ -f ${ROOTFS_DIR}${file_item} ]; then
112+ hash_val=`sha256sum ${ROOTFS_DIR}${file_item} | awk '{ print $1 }'`
113+ echo "${file_item} $hash_val ok"
114+ file_array+=("$file_item")
115+ hash_array+=("$hash_val")
116+ let "file_cnt=file_cnt+1"
117+ else
118+ echo "regular file ${ROOTFS_DIR}${file_item} NOT exist"
119+ exit 9
120+ fi
121+done < "${APP_LIST_FILE}"
122+
123+# 打印数组中的数据
124+
125+echo "#ifndef __VERIFY_APP_H" > $VERIFY_APP_HEADER
126+echo "#define __VERIFY_APP_H" >> $VERIFY_APP_HEADER
127+echo "" >> $VERIFY_APP_HEADER
128+
129+echo "const int g_verify_app_cnt = $file_cnt;" >> $VERIFY_APP_HEADER
130+echo "const char *g_verify_file_array[] = {" >> $VERIFY_APP_HEADER
131+for element in "${file_array[@]}"; do
132+ echo " \"$element\"," >> $VERIFY_APP_HEADER
133+done
134+echo "};" >> $VERIFY_APP_HEADER
135+
136+echo "const char *g_verify_hash_array[] = {" >> $VERIFY_APP_HEADER
137+for element in "${hash_array[@]}"; do
138+ echo " \"$element\"," >> $VERIFY_APP_HEADER
139+done
140+echo "};" >> $VERIFY_APP_HEADER
141+
142+echo "" >> $VERIFY_APP_HEADER
143+echo "#endif" >> $VERIFY_APP_HEADER
144diff --git a/os/linux/linux-3.4.x/include/linux/verify_app.h b/os/linux/linux-3.4.x/include/linux/verify_app.h
145new file mode 100755
146index 000000000..90ed58b87
147--- /dev/null
148+++ b/os/linux/linux-3.4.x/include/linux/verify_app.h
149@@ -0,0 +1,16 @@
150+#ifndef __VERIFY_APP_H
151+#define __VERIFY_APP_H
152+
153+const int g_verify_app_cnt = 3;
154+const char *g_verify_file_array[] = {
155+ "/bin/busybox",
156+ "/etc/rc",
157+ "/sbin/zte_mdl",
158+};
159+const char *g_verify_hash_array[] = {
160+ "a071cc29b7bbd47e45a6698e5dde5564995172e7b530314dd375040e0b1d9a41",
161+ "1296e7b4fe520bbc0cdd0b76289afd86318674a99ea2d5faff52baf39854b5d6",
162+ "8a3815c1394f3b343ffc598c245fb356c80071ac3755927f0e7c6b7e8955c070",
163+};
164+
165+#endif
166diff --git a/os/linux/linux-3.4.x/init/Kconfig b/os/linux/linux-3.4.x/init/Kconfig
167index 3db3a515a..8742bb316 100644
168--- a/os/linux/linux-3.4.x/init/Kconfig
169+++ b/os/linux/linux-3.4.x/init/Kconfig
170@@ -1452,3 +1452,11 @@ config PADATA
171 bool
172
173 source "kernel/Kconfig.locks"
174+
175+config VERIFY_APP_IN_KERNEL
176+ bool "Enable verify app in kernel"
177+ default n
178+ help
179+ This option enables support for verify app in kernel. You almost
180+ certainly want to say n here.
181+
182\ No newline at end of file
183diff --git a/os/linux/linux-3.4.x/init/Makefile b/os/linux/linux-3.4.x/init/Makefile
184index 6b473cd16..ab6b944cf 100644
185--- a/os/linux/linux-3.4.x/init/Makefile
186+++ b/os/linux/linux-3.4.x/init/Makefile
187@@ -9,7 +9,7 @@ else
188 obj-$(CONFIG_BLK_DEV_INITRD) += initramfs.o
189 endif
190 obj-$(CONFIG_GENERIC_CALIBRATE_DELAY) += calibrate.o
191-
192+obj-$(CONFIG_VERIFY_APP_IN_KERNEL) += verify_app.o
193 mounts-y := do_mounts.o
194 mounts-$(CONFIG_BLK_DEV_RAM) += do_mounts_rd.o
195 mounts-$(CONFIG_BLK_DEV_INITRD) += do_mounts_initrd.o
196diff --git a/os/linux/linux-3.4.x/init/verify_app.c b/os/linux/linux-3.4.x/init/verify_app.c
197new file mode 100755
198index 000000000..5eccd9c8c
199--- /dev/null
200+++ b/os/linux/linux-3.4.x/init/verify_app.c
201@@ -0,0 +1,130 @@
202+/*
203+ * linux/init/verify_app.c
204+ *
205+ */
206+#include <linux/types.h>
207+#include <linux/kernel.h>
208+#include <linux/syscalls.h>
209+#include <linux/string.h>
210+#include <linux/ctype.h>
211+#include <linux/delay.h>
212+#include <linux/init.h>
213+#include <linux/security.h>
214+#include <linux/kthread.h>
215+#include <linux/crypto.h>
216+#include <crypto/hash.h>
217+#include <crypto/sha.h>
218+#include <linux/verify_app.h>
219+#include <linux/soc/zte/efuse/efuse_zx.h>
220+
221+#define VERIFY_TIMEOUT 30000 //ÑÓ³ÙÑéǩʱ¼ä,½¨Òé´óÓÚ2s£»
222+
223+extern void zDrvEfuse_GetSecureMsg(T_ZDrvEfuse_Secure *secure);
224+
225+u8 *bin2hex(const u8 *old, const size_t oldlen)
226+{
227+ u8 *result = (u8 *)kmalloc((oldlen * 2 + 1), GFP_KERNEL);
228+ size_t i, j;
229+ int b = 0;
230+
231+ for (i = j = 0; i < oldlen; i++)
232+ {
233+ b = old[i] >> 4;
234+ result[j++] = (char)(87 + b + (((b - 10) >> 31) & -39));
235+ b = old[i] & 0xf;
236+ result[j++] = (char)(87 + b + (((b - 10) >> 31) & -39));
237+ }
238+ result[j] = '\0';
239+ return result;
240+}
241+
242+static int verify_app_entry(void *p)
243+{
244+ int i = 0;
245+ int rdlen;
246+ char *buff = NULL;
247+ size_t size;
248+ struct file *fp;
249+ int bufflen = 4096;
250+ u8 *hashstring;
251+ u8 hash[SHA256_DIGEST_SIZE];
252+ struct shash_desc *desc;
253+ struct crypto_shash *sha256;
254+ T_ZDrvEfuse_Secure secure;
255+
256+ msleep(VERIFY_TIMEOUT);
257+
258+ zDrvEfuse_GetSecureMsg(&secure);
259+ if((secure.secureFlag & 0xFF) != 0xFF)
260+ {
261+ printk("verify secure boot don't open \n");
262+ return 0;
263+ }
264+
265+ sha256 = crypto_alloc_shash("sha256", 0, 0);
266+ if (IS_ERR(sha256)) {
267+ panic("verify sha256 error ! \n");
268+ }
269+ size = crypto_shash_descsize(sha256) + sizeof(*desc);
270+ desc = kzalloc(size, GFP_KERNEL);
271+ BUG_ON(desc == NULL);
272+ desc->tfm = sha256;
273+ buff = kmalloc(bufflen, GFP_KERNEL);
274+ BUG_ON(buff == NULL);
275+ for (i = 0; i < g_verify_app_cnt; i++)
276+ {
277+ fp = filp_open(g_verify_file_array[i], O_RDONLY, 0644);
278+ if (IS_ERR(fp))
279+ {
280+ printk("app=%s open fail \n",g_verify_file_array[i]);
281+ panic("verify open fail");
282+ }
283+ else
284+ fp->f_pos = 0;
285+ crypto_shash_init(desc);
286+ while(1)
287+ {
288+ rdlen = kernel_read(fp, fp->f_pos, buff, bufflen);
289+ if (rdlen > 0)
290+ {
291+ fp->f_pos += rdlen;
292+ crypto_shash_update(desc, buff, rdlen);
293+ }
294+ else if (rdlen == 0)
295+ {
296+ filp_close((struct file *)fp, NULL);
297+ crypto_shash_final(desc, hash);
298+ hashstring = bin2hex(hash, SHA256_DIGEST_SIZE);
299+ if (memcmp(hashstring, g_verify_hash_array[i], SHA256_DIGEST_SIZE))
300+ {
301+ printk("verify app=%s hash=%s \n", g_verify_file_array[i], hashstring);
302+ panic("verify hash fail");
303+ }
304+ // printk("verify app=%s success \n", g_verify_file_array[i]);
305+ kfree(hashstring);
306+ break;
307+ }
308+ else
309+ {
310+ printk("verify app=%s rdlen=%d \n", g_verify_file_array[i], rdlen);
311+ panic("verify read fail");
312+ break;
313+ }
314+ }
315+ }
316+
317+ crypto_free_shash(sha256);
318+ kfree(desc);
319+ kfree(buff);
320+ printk("verify app init success \n");
321+ return 0;
322+}
323+
324+static int __init verify_app_init(void)
325+{
326+ kthread_run(verify_app_entry, NULL, "verify_app");
327+ return 0;
328+}
329+
330+late_initcall(verify_app_init);
331+
332diff --git a/project/pubconf.mk b/project/pubconf.mk
333index 43d289bd2..b35f3f3b7 100755
334--- a/project/pubconf.mk
335+++ b/project/pubconf.mk
336@@ -62,4 +62,5 @@ export CONFIG_MIN_8M_VERSION ?= n
337 export USE_ZCAT_MBIM ?= no
338 export USE_RECOVERYFS ?= no
339 export USE_OEM_FS ?= no
340+export VERIFY_APP_IN_KERNEL ?= no
341
342diff --git a/project/zx297520v3/prj_cpe/build/config.mk b/project/zx297520v3/prj_cpe/build/config.mk
343index b7bbac7a0..cbe5621f5 100755
344--- a/project/zx297520v3/prj_cpe/build/config.mk
345+++ b/project/zx297520v3/prj_cpe/build/config.mk
346@@ -77,4 +77,7 @@ endif
347 export ENABLE_PHONECODE_IN_ATCTL=yes
348
349 #¿ØÖÆÊÇ·ñʹÓûìÒô
350-export USE_MIXDATA_SUPPORT=no
351\ No newline at end of file
352+export USE_MIXDATA_SUPPORT=no
353+
354+#ÎļþÑéÇ©¿ª¹Ø
355+#VERIFY_APP_IN_KERNEL := yes
356diff --git a/project/zx297520v3/prj_cpe/config/normal/config.linux b/project/zx297520v3/prj_cpe/config/normal/config.linux
357index d6d117611..bd1d78ba8 100755
358--- a/project/zx297520v3/prj_cpe/config/normal/config.linux
359+++ b/project/zx297520v3/prj_cpe/config/normal/config.linux
360@@ -1916,7 +1916,7 @@ CONFIG_ACCURATE_CPU_PERCENT=y
361 CONFIG_DEFAULT_SECURITY_DAC=y
362 CONFIG_DEFAULT_SECURITY=""
363 CONFIG_CRYPTO=y
364-
365+# CONFIG_VERIFY_APP_IN_KERNEL is not set
366 #
367 # Crypto core or helper
368 #
369diff --git a/project/zx297520v3/prj_cpe/config/normal/verify_app_list.txt b/project/zx297520v3/prj_cpe/config/normal/verify_app_list.txt
370new file mode 100755
371index 000000000..f375abf3f
372--- /dev/null
373+++ b/project/zx297520v3/prj_cpe/config/normal/verify_app_list.txt
374@@ -0,0 +1,3 @@
375+/bin/busybox
376+/etc/rc
377+/sbin/zte_cpe
378--
3792.25.1
380