2020/11/20

讓 uboot envtools 禁止變更重要的變數

uboot envtools 提供很方便的途徑來存取 uboot 環境變數,但是方便的工具卻也帶來很大的風險,如果被惡意使用,變更了重要的 uboot 環境變數,例如 bootcmd,可能會導致無法預期的後果,所以真的有必要禁止使用 uboot envtools 改變這些重要的變數內容。

以下內容是以 uboot 2014.10 為基礎, 更新的版本應該也不會有太大的差異.

檔案 uboot/tools/env/fw_env.c

最主要的寫入 function 是:
    int fw_env_write(char *name, char *value)

要做的事情很單純:
  1. 增加一個檢查要寫入的環境變數名稱的 function, 可以被更改的傳回 0, 不能被更改的傳回非0.
  2. 在 fw_env_wrire 裡面呼叫 1. 加入的 function 進行檢查.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int fw_env_check_unmodifiable_name(char *name) {
	int i, cnt;
	char *unmodi[] = {
		"bootcmd",
		"baudrate",
		"ethaddr",
		"country",
		"regdomain",
		};
        cnt = sizeof(unmodi)/sizeof(unmodi[0]);
	for (i=0; i<cnt; i++) {
		if (0 == strcmp(unmodi[i], name)) {
			fprintf(stderr, "## Warning: "
						"environment \"%s\" can't be modified.\n", name);
			errno = EINVAL;
			return -EINVAL;
		}
	}
	return 0;
}

/*
 * Set/Clear a single variable in the environment.
 * This is called in sequence to update the environment
 * in RAM without updating the copy in flash after each set
 */
int fw_env_write(char *name, char *value)
{
	int len;
	char *env, *nxt;
	char *oldval = NULL;
	int deleting, creating, overwriting;

	if (fw_env_check_unmodifiable_name(name)) {
		return 0;
	}


沒有留言: