# 2024q1 Homework6 (integration)
contributed by < `yan112388` >
## 開發環境
```shell
$ gcc --version
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 20
On-line CPU(s) list: 0-19
Vendor ID: GenuineIntel
Model name: 13th Gen Intel(R) Core(TM) i7-13700H
CPU family: 6
Model: 186
Thread(s) per core: 2
Core(s) per socket: 14
Socket(s): 1
Stepping: 2
CPU max MHz: 5000.0000
CPU min MHz: 400.0000
BogoMIPS: 5836.80
```
## 自我檢查清單
- [ ] 研讀前述 Linux 效能分析 描述,在自己的實體電腦運作 GNU/Linux,做好必要的設定和準備工作 → 從中也該理解為何不希望在虛擬機器中進行實驗;
設定準備中,`make check` 後遇到警告:
```
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
You are using: gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
```
查閱 [Linux 核心模組運作原理](https://hackmd.io/@sysprog/linux-kernel-module) 中有提及此敘序
>你的編譯器版本與kernel開發套件所用的編譯器版本不一致,建議安裝對應的版本避免後續開發出問題。
目前仍在了解 gcc-12 與 x86_64-linux-gnu-gcc-12 的差異性,進行以下嘗試:
* 更新套件
```
$ sudo apt update
$ sudo apt upgrade
```
此外,還輸出以下錯誤:
```
insmod: ERROR: could not insert module sort.ko: Key was rejected by service
```
查詢 [The Linux Kernel Module Programming Guide](https://unix.stackexchange.com/questions/751517/insmod-causes-key-rejected-by-service),內文提到與 UEFI 安全開機(security boot)有關,最簡單的解決方式為進入 bios 將安全關機功能關閉後,這個錯誤便消失了。
> Failing this, an attempt to insert your first “hello world” module would result in the message: “ERROR: could not insert module”. If this message Lockdown: insmod: unsigned module loading is restricted; see man kernel lockdown.7 appears in the dmesg output, the simplest approach involves disabling UEFI SecureBoot from the boot menu of your PC or laptop, allowing the successful insertion of “hello world” module.
查閱 [UEFI-Secure Boot](https://wiki.debian.org/SecureBoot) 提到
> SB works using cryptographic checksums and signatures. Each program that is loaded by the firmware includes a signature and a checksum, and before allowing execution the firmware will verify that the program is trusted by validating the checksum and the signature. When SB is enabled on a system, any attempt to execute an untrusted program will not be allowed. This stops unexpected / unauthorised code from running in the UEFI environment.
更為恰當的方式應該是對模組進行簽名(待研究細節):
* 生成一個密鑰對(公鑰和私鑰)
* 使用私鑰對模組進行簽名。
* 將公鑰加入 linux 核心的密鑰庫中,使核心可以驗證模組的簽名。
- [ ] 閱讀〈[Linux 核心模組運作原理](https://hackmd.io/@sysprog/linux-kernel-module)〉並對照 Linux 核心原始程式碼 (v6.1+),解釋 insmod 後,Linux 核心模組的符號 (symbol) 如何被 Linux 核心找到 (使用 List API)、MODULE_LICENSE 巨集指定的授權條款又對核心有什麼影響 (GPL 與否對於可用的符號列表有關),以及藉由 strace 追蹤 Linux 核心的掛載,涉及哪些系統呼叫和子系統?
>〈[Linux 核心模組運作原理](https://hackmd.io/@sysprog/linux-kernel-module)〉列出的程式碼較舊,歡迎編輯頁面,更新到 Linux v6.1 以上。