# 2024q1 Homework6 (integration)
contributed by < `max890808` >
## 自我檢查清單
- [X] 自己的實體電腦運作 GNU/Linux
- [ ] 閱讀〈Linux 核心模組運作原理〉並對照 Linux 核心原始程式碼 (v6.1+),解釋 insmod 後,Linux 核心模組的符號 (symbol) 如何被 Linux 核心找到 (使用 List API)、MODULE_LICENSE 巨集指定的授權條款又對核心有什麼影響 (GPL 與否對於可用的符號列表有關),以及藉由 strace 追蹤 Linux 核心的掛載,涉及哪些系統呼叫和子系統?
### 解釋 insmod 後,Linux 核心模組的符號 (symbol) 如何被 Linux 核心找到 (使用 List API)
使用 `strace` 追蹤執行 `insmod` 的過程會有哪些系統呼叫被執行,可以發現會呼叫 `finit_module`,接著呼叫 `idempotent_init_module` 該函式當中還會呼叫 `init_module_from_file` ,在 `init_module_from_file` 才真正呼叫到 `load_module`。
在 [kernel/module/main.c](https://elixir.bootlin.com/linux/latest/source/kernel/module/main.c) 可以找到 `find_symbol` 函式,其使用 List API `list_for_each_entry_rcu` 和 `find_exported_symbol_in_section` 來查找模組的符號。
### MODULE_LICENSE 巨集指定的授權條款又對核心有什麼影響 (GPL 與否對於可用的符號列表有關)
在 [include/linux/module.h](https://elixir.bootlin.com/linux/latest/source/include/linux/module.h) 中可以找到以下說明
```c
/*
* The following license idents are currently accepted as indicating free
* software modules
*
* "GPL" [GNU Public License v2]
* "GPL v2" [GNU Public License v2]
* "GPL and additional rights" [GNU Public License v2 rights and more]
* "Dual BSD/GPL" [GNU Public License v2
* or BSD license choice]
* "Dual MIT/GPL" [GNU Public License v2
* or MIT license choice]
* "Dual MPL/GPL" [GNU Public License v2
* or Mozilla license choice]
*
* The following other idents are available
*
* "Proprietary" [Non free products]
*
* Both "GPL v2" and "GPL" (the latter also in dual licensed strings) are
* merely stating that the module is licensed under the GPL v2, but are not
* telling whether "GPL v2 only" or "GPL v2 or later". The reason why there
* are two variants is a historic and failed attempt to convey more
* information in the MODULE_LICENSE string. For module loading the
* "only/or later" distinction is completely irrelevant and does neither
* replace the proper license identifiers in the corresponding source file
* nor amends them in any way. The sole purpose is to make the
* 'Proprietary' flagging work and to refuse to bind symbols which are
* exported with EXPORT_SYMBOL_GPL when a non free module is loaded.
*
* In the same way "BSD" is not a clear license information. It merely
* states, that the module is licensed under one of the compatible BSD
* license variants. The detailed and correct license information is again
* to be found in the corresponding source files.
*
* There are dual licensed components, but when running with Linux it is the
* GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
* is a GPL combined work.
*
* This exists for several reasons
* 1. So modinfo can show license info for users wanting to vet their setup
* is free
* 2. So the community can ignore bug reports including proprietary modules
* 3. So vendors can do likewise based on their own policies
*/
#define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
```
對於模組中使用 `EXPORT_SYMBOL_GPL` 標記的符號,當一個非自由模組被載入時,核心會拒絕綁定這些符號,從而確保這些符號僅被自由(GPL 授權)模組所使用。這個機制旨在保證核心僅與符合自由軟件定義的模組進行交互。
### 藉由 strace 追蹤 Linux 核心的掛載,涉及哪些系統呼叫和子系統?
`execve` : 執行指定的程式
`brk(NULL)` : 獲取 data segment 最後一個位址
`access` : 檢查使用者是否有檔案的權限
`openat` : 打開檔案並回傳 `directory file descriptor` ,如果 `dirfd` 設置為 `AT_FDCWD` ,則 pathname 是相對於當前工作目錄的路徑