ãLinuxã«ã¼ãã«2.6解èªå®¤ãï¼ä»¥éãæ§çï¼åºçå¾ãLinuxã«ã¯å¤ãã®æ©è½ã追å ãããã¨ã³ã¿ã¼ãã©ã¤ãºé åãã¯ããã¨ããæ§ã
ãªå ´æã§ä½¿ãããããã«ãªãã¾ããã
ããã«ä¼´ãã³ã¼ããè¥å¤§ãã¤è¤éåããå¤ãã®ã¨ã³ã¸ãã¢ã«ã¨ã£ã¦è§£èªä¸è½ãªãã©ãã¯ããã¯ã¹ã¨ãªã£ã¦ãã¾ãã
ä¸çä¸ã®ãããã¨ã³ã¸ãã¢éã®åä½ã§ããLinuxã«ã¼ãã«ã«ã¡ã¹ãå
¥ãããã©ãã¯ããã¯ã¹ãããéãã¦ãæã«å¥½å¥å¿ã®èµ´ãã¾ã¾ã«ã«ã¼ãã«ã®ä¸çã解èªãããæ°Linuxã«ã¼ãã«è§£èªå®¤ãããã¸ã§ã¯ãã
æ¬ç¨¿ã§ã¯ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«æ©è½å®è£
ã®æ¦è¦ã«ã¤ãã¦èª¬æãã¾ãã
- ã¯ããã«
- 1. åããã¦ã¿ãã
- 2. åä½ã®æ¦è¦
- 3. ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«èµ·åã®åä½ããã¼
- 4. ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®åé¤
- æå¾ã«
- ãã¾ã ï½ ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«çµã¿è¾¼ã¿æ©æ§ã®æ´å²
- ãã¾ã ï½ ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¸ã®ç½²å
å·çè : é«æ©ã浩å
â»ãæ°Linuxã«ã¼ãã«è§£èªå®¤ãé£è¼è¨äºä¸è¦§ã¯ãã¡ã â» æ¬ç¨¿ã®ãµã³ãã«ã³ã¼ã㯠github ã«ã¦å ¬éãã¦ãã¾ãã
ã¯ããã«
Linuxã«ã¼ãã«ã¯ä¸é¨æ©è½ãå¥ãªãã¸ã§ã¯ãã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¨ãã¦çæããLinuxã«ã¼ãã«æ¬ä½èµ·åå¾ã«ãå¿ è¦ã«å¿ãã¦ãããã¢ã¸ã¥ã¼ã«ãçµè¾¼ãã¨ããæ¹å¼ãæ¡ç¨ãã¦ãã¾ãããã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«æ©è½ã¯ãLinuxã«ã¼ãã«ã®éçºãå§ã¾ã£ãåæãã使ããã¦ããå¤ãæ©è½ã§ãã
å ã ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãçµè¾¼ãå¦çã®æ®ã©ã¯ã¦ã¼ã¶ã©ã³ãinsmodã³ãã³ãã®ä¸ã§è¡ããã¦ãã¾ãããç¾å¨ã®Linuxã«ã¼ãã«(kernel 6.8)ã§ã¯ããããã®æ©è½å ¨ã¦ãã«ã¼ãã«æ©è½ã¨ãã¦å®ç¾ããã¦ãã¾ãã
ä»åã¯ããã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«æ©è½ãè¦ã¦è¡ãã¾ãã
1. åããã¦ã¿ãã
解説ç¨ã®ä¾é¡ã¨ãã¦ç°¡åãªã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãç¨æãã¾ãããgithub.comãããã¦ã³ãã¼ããã¦ãã ãããkmoduleãã£ã¬ã¯ããªé
ä¸ã«å°ããªã«ã¼ãã«ã¢ã¸ã¥ã¼ã«kaidockç¨ã®Cã½ã¼ã¹ãã¡ã¤ã«1ã¤ã¨ããã«ãç¨ã®Makefileãç½®ãã¦ããã¾ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«kaidockã¯ã1ç§æ¯ã«jiffies
å¤æ°*1ã®å¤ã表示ãã¾ãã
kaidockã¯é常ã«çããããã°ã©ã ã§ããkaidockã¢ã¸ã¥ã¼ã«ãçµè¾¼ãã¨init_module
é¢æ°ãå¼ã³ã ããããã®ä¸ã§ã¿ã¤ãkaidock_timer
ãHZæéå¾ã«æºäºæéãè¿ããããã«ç»é²ãã¦ãã¾ã(add_timeré¢æ°)ãHZæéçµéå¾ã«å¼ã³åºãããkaidock_timer
ã¯kaidock_func
é¢æ°ãå®è¡ããjiffies
ã®å¤ãã³ã³ã½ã¼ã«ã«è¡¨ç¤ºãã¾ãã
kaidock_func
é¢æ°ã¯ãHZæéå¾ã«å度ã¿ã¤ãkaidock_timer
ãæºäºæéãè¿ããããã«ç»é²ä»ç´ãã¾ã(mod_timeré¢æ°)ã
ã¿ã¤ãkaidock_timer
ã®èµ·ååæ°ã¯ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«èµ·åå¼æ°loop
ã«ã¦å¤æ´ã§ãã¾ã(module_paramé¢æ°)ãã©ã¤ã»ã³ã¹ã¯GPLã¨ãã¾ããã
#include <linux/module.h> #include <linux/kernel.h> static int loop = INT_MAX; module_param(loop, int, 0644); MODULE_PARM_DESC(loop, "specify the loop count"); static void kaidock_func(struct timer_list *timer); DEFINE_TIMER(kaidock_timer, kaidock_func); static void kaidock_func(struct timer_list *timer) { printk(KERN_ERR "%lx\n", jiffies); if (--loop > 0) { do { mod_timer(timer, timer->expires + HZ); } while ((long)(timer->expires - jiffies) <= 0L); } } int init_module(void) { kaidock_timer.expires = jiffies + HZ; add_timer(&kaidock_timer); return 0; } void cleanup_module(void) { del_timer_sync(&kaidock_timer); } MODULE_AUTHOR("taka"); MODULE_LICENSE("GPL");
1.1 ãã«ããã
Ubuntu 24.04 LTSç°å¢ã§ã®åä½ç¢ºèªããã¦ãã¾ããkaidockããã¦ã³ãã¼ããããã£ã¬ã¯ããªã«ã¦ãã«ããã¾ããç¾å¨åä½ãã¦ããã«ã¼ãã«ç¨ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¨ãã¦ã³ã³ãã¤ã«ããã¾ãã*2
$ cd kmodule $ make -C /lib/modules/`uname -r`/build M=`pwd` modules
1.2 çµè¾¼ã
kaidockã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãçµè¾¼ã¿ã¾ããåãã£ã¦å©ç¨ãã¦ããPCã®ã»ãã¥ã¢ãã¼ãæ©è½ãåã£ã¦ãããã¨ãå¿ è¦ã§ãããããã¯ãkaidockã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã«ç½²åãä»ããå¿ è¦ãããã¾ããç½²åæé ã¯ãããã¾ã ï½ ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¸ã®ç½²åããåç §ãã¦ãã ããã
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«èµ·åå¼æ°loop
ã«ã¦ãjiffies
ã®è¡¨ç¤ºåæ°ãæå®ãããã¨ãã§ãã¾ããã³ã³ã½ã¼ã«ç»é¢ã®å³ä¸ã®æ¹ã«jiffies
ã®å¤ãç·è²ã®æåã§å¨æ表示ããã¾ãã
$ sudo insmod kaidock.ko loop=100 jiffies:0x1113dac00
insmodãè¡ãªã£ã端æ«ã«è¡¨ç¤ºãããªãå ´åãã©ããã«ããã³ã³ã½ã¼ã«ã«è¡¨ç¤ºããã¦ãã¾ããä¸è¨ã®ããã«syslogã®å 容ã表示ãã¦ãã ããã
$ sudo tail -f /var/log/syslog
çµè¾¼ã¾ãã¦ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã表示ãã¾ããkaidockã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãçµè¾¼ã¾ãã¦ãããã¨ã確èªã§ãã¾ãã
$ lsmod Module Size Used by kaidock 12288 0 cpuid 12288 0 tls 151552 0 xt_conntrack 12288 1 nft_chain_nat 12288 3 xt_MASQUERADE 16384 1 nf_nat 65536 2 nft_chain_nat,xt_MASQUERADE nf_conntrack 200704 3 xt_conntrack,nf_nat,xt_MASQUERADE nf_defrag_ipv6 24576 1 nf_conntrack nf_defrag_ipv4 12288 1 nf_conntrack : :
1.3 åæ¢ããã
kaidockã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãåæ¢ããã¾ãã
$ sudo rmmod kaidock
2. åä½ã®æ¦è¦
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¯ããªãã±ã¼ã¿ãã«ãªãã¸ã§ã¯ã(åé ç½®å¯è½ãªãã¸ã§ã¯ã)ã§ããxyz.cã®ã³ã³ãã¤ã«éä¸ã«çæããã xyz.o ãã¡ã¤ã«ã¨åºæ¬çã«åãã§ãã
- å²ãä»ãã¢ãã¬ã¹ãæªè§£æ±ºãã¢ã¸ã¥ã¼ã«å ã®é¢æ°ãå¤æ°ã®å²ãä»ãã¢ãã¬ã¹ã決å®ãã¦ããªãã
- ã·ã³ãã«ã®å¤é¨åç §ãæªè§£æ±ºã ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®å ´åãã«ã¼ãã«æ¬ä½ãä»ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ã·ã³ãã«(é¢æ°ãå¤æ°)ãåç §ããå½ä»¤ããæªè§£æ±ºç¶æ ã«ãªã£ã¦ããã
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãçµè¾¼ãããã«ã¯ä¸è¨ã®æä½ãå¿ è¦ã§ãã
- ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãçµã¿è¾¼ãããã®ã«ã¼ãã«ã¡ã¢ãªé åã確ä¿ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãèªã¿è¾¼ã
- ã«ã¼ãã«ã¨æ¢ã«åä½ä¸ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ã·ã³ãã«æ å ±åå¾(ã·ã³ãã«åã¨ã¢ãã¬ã¹ãçµã«ãªã£ãæ å ±)
- 2.ã®æ å ±ãå ã«ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®æªè§£æ±ºã¢ãã¬ã¹ãæªè§£æ±ºã·ã³ãã«ã解決ãã
- çµã¿è¾¼ãã ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãæå¹ã«ãã
ç¾å¨ã®Linuxã§ã¯ããããã®å¦çã¯å ¨ã¦ã«ã¼ãã«ãæ ã£ã¦ãã¾ãããªãã¨ãã«ã¼ãã«ã«ãªã³ã«ã®æ©è½ã¾ã§çµè¾¼ã¾ãã¦ãã¾ãã
ãªã³ã¯å¦çã®çµæãã©ããªããããªãã±ã¼ãåã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãªãã¸ã§ã¯ã(kaidock.ko)ã®ã³ã¼ãã¨ãªã³ã¯å¾(ã«ã¼ãã«ã¸ã®çµã¿è¾¼ã¿å¾)ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ã³ã¼ããæ¯è¼ãã¾ãã
ãªãã±ã¼ãåã®ãªãã¸ã§ã¯ãkaidock.koã®kaidoc_func
é¢æ°ã¯ä¸è¨ã®ããã«ã¢ãã¬ã¹ã解決ããã¦ããªãç¶æ
ã«ããã¾ãã
$ objdump -d kaidock.ko : : 000000000000010 <kaidock_func>: (x) 10: e8 00 00 00 00 call 15 <kaidock_func+0x5> 15: 55 push %rbp (a) 16: 48 8b 35 00 00 00 00 mov 0x0(%rip),%rsi # 1d <kaidock_func+0xd> 1d: 48 89 e5 mov %rsp,%rbp 20: 53 push %rbx 21: 48 89 fb mov %rdi,%rbx 24: 48 c7 c7 00 00 00 00 mov $0x0,%rdi 2b: e8 00 00 00 00 call 30 <kaidock_func+0x20> (b) 30: 8b 05 00 00 00 00 mov 0x0(%rip),%eax # 36 <kaidock_func+0x26> 36: 83 e8 01 sub $0x1,%eax (b) 39: 89 05 00 00 00 00 mov %eax,0x0(%rip) # 3f <kaidock_func+0x2f> 3f: 85 c0 test %eax,%eax 41: 7e 29 jle 6c <kaidock_func+0x5c> 43: 48 8b 73 10 mov 0x10(%rbx),%rsi 47: 48 81 c6 e8 03 00 00 add $0x3e8,%rsi 4e: 48 89 df mov %rbx,%rdi (c) 51: e8 00 00 00 00 call 56 <kaidock_func+0x46> 56: 48 8b 73 10 mov 0x10(%rbx),%rsi (a) 5a: 48 8b 15 00 00 00 00 mov 0x0(%rip),%rdx # 61 <kaidock_func+0x51> 61: 48 89 f0 mov %rsi,%rax 64: 48 29 d0 sub %rdx,%rax 67: 48 85 c0 test %rax,%rax 6a: 7e db jle 47 <kaidock_func+0x37> 6c: 48 8b 5d f8 mov -0x8(%rbp),%rbx 70: c9 leave 71: 31 c0 xor %eax,%eax 73: 31 d2 xor %edx,%edx 75: 31 f6 xor %esi,%esi 77: 31 ff xor %edi,%edi (d) 79: e9 00 00 00 00 jmp 7e <kaidock_func+0x6e> 7e: 66 90 xchg %ax,%ax
ã«ã¼ãã«æ¬ä½ã¸çµã¿è¾¼ã¿å¾ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãcrashã³ãã³ããç¨ãã¦è¦ãã¦ã¿ã¾ããããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®kaidck_func
é¢æ°ãéã¢ã»ã³ãã«ãã¦ã¿ã¾ããçµã¿è¾¼ã¿åã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®kaidck_func
é¢æ°ã¯0x10çªå°ã«å²ãä»ããããªã³ã¼ãã«ãªã£ã¦ãã¾ããããã«ã¼ãã«ã¸ã®çµã¿è¾¼ã¿å¾ã¯0xffffffffc0d4c010ã«å²ãä»ãã¦ãããã¨ã確èªã§ãã¾ãã
å¤æ°jiffies
(a)(A)ãloop
(b)(B)ãå¼åºãé¢æ°modtime
(c)(C)ã®ã¢ãã¬ã¹ã解決ããã¦ãã¾ãã
é¢æ°ããã®å¾©å¸°å½ä»¤(d)(D)ã解決ããã¦ãã¾ãã
kaidock_func
é¢æ°ã®å
é ã«ããcall
å½ä»¤(x)ã¯nopl
å½ä»¤(X)ã«ç½®ãæãããã¦ãã¾ããé¢æ°ã®å
é 5ãã¤ãã¯ãftrace
æ©è½ãkprobe
æ©è½ãªã©ãå¹ççã«ããã¯ãä»æããããããã«é åãäºç´ãã¦ãã¾ã*3ã
$ sudo crash /usr/lib/debug/boot/vmlinux-`uname -r` crash> dis kaidock_func (X) 0xffffffffc0d4c010 <kaidock_func>: nopl 0x0(%rax,%rax,1) [FTRACE NOP] 0xffffffffc0d4c015 <kaidock_func+5>: push %rbp (A) 0xffffffffc0d4c016 <kaidock_func+6>: mov -0xeb4465d(%rip),%rsi # 0xffffffffb22079c0 <jiffies> 0xffffffffc0d4c01d <kaidock_func+13>: mov %rsp,%rbp 0xffffffffc0d4c020 <kaidock_func+16>: push %rbx 0xffffffffc0d4c021 <kaidock_func+17>: mov %rdi,%rbx 0xffffffffc0d4c024 <kaidock_func+20>: mov $0xffffffffc0d7b058,%rdi 0xffffffffc0d4c02b <kaidock_func+27>: call 0xffffffffaffb0040 <_printk> (B) 0xffffffffc0d4c030 <kaidock_func+32>: mov 0x2012(%rip),%eax # 0xffffffffc0d4e048 <loop> 0xffffffffc0d4c036 <kaidock_func+38>: sub $0x1,%eax (B) 0xffffffffc0d4c039 <kaidock_func+41>: mov %eax,0x2009(%rip) # 0xffffffffc0d4e048 <loop> 0xffffffffc0d4c03f <kaidock_func+47>: test %eax,%eax 0xffffffffc0d4c041 <kaidock_func+49>: jle 0xffffffffc0d4c06c <kaidock_func+92> 0xffffffffc0d4c043 <kaidock_func+51>: mov 0x10(%rbx),%rsi 0xffffffffc0d4c047 <kaidock_func+55>: add $0x3e8,%rsi 0xffffffffc0d4c04e <kaidock_func+62>: mov %rbx,%rdi (C) 0xffffffffc0d4c051 <kaidock_func+65>: call 0xffffffffb0004b60 <mod_timer> 0xffffffffc0d4c056 <kaidock_func+70>: mov 0x10(%rbx),%rsi (A) 0xffffffffc0d4c05a <kaidock_func+74>: mov -0xeb446a1(%rip),%rdx # 0xffffffffb22079c0 <jiffies> 0xffffffffc0d4c061 <kaidock_func+81>: mov %rsi,%rax 0xffffffffc0d4c064 <kaidock_func+84>: sub %rdx,%rax 0xffffffffc0d4c067 <kaidock_func+87>: test %rax,%rax 0xffffffffc0d4c06a <kaidock_func+90>: jle 0xffffffffc0d4c047 <kaidock_func+55> 0xffffffffc0d4c06c <kaidock_func+92>: mov -0x8(%rbp),%rbx 0xffffffffc0d4c070 <kaidock_func+96>: leave 0xffffffffc0d4c071 <kaidock_func+97>: xor %eax,%eax 0xffffffffc0d4c073 <kaidock_func+99>: xor %edx,%edx 0xffffffffc0d4c075 <kaidock_func+101>: xor %esi,%esi 0xffffffffc0d4c077 <kaidock_func+103>: xor %edi,%edi (D) 0xffffffffc0d4c079 <kaidock_func+105>: ret 0xffffffffc0d4c07a <kaidock_func+106>: int3 0xffffffffc0d4c07b <kaidock_func+107>: int3 0xffffffffc0d4c07c <kaidock_func+108>: int3 0xffffffffc0d4c07d <kaidock_func+109>: int3 0xffffffffc0d4c07e <kaidock_func+110>: xchg %ax,%ax
3. ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«èµ·åã®åä½ããã¼
Linuxã«ã¼ãã«ã¯ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«çµè¾¼ã¿ãè¦æ±ããfinit_module
ã·ã¹ãã ã³ã¼ã«ãç¨æãã¦ãã¾ããinsmodã³ãã³ãã¯finit_module
ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãã ãã§ããã¨ã¯finit_module
ãå
¨ã¦ã®å¦çãè¡ãã¾ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®çµã¿è¾¼ã¿å¦çã¯é次å¦çã§ãã¢ã«ã´ãªãºã çã«è¤éãªå ´æã¯ããã¾ããã
int finit_module(int fd, const char *param_values, int flags);
å¼æ° | 説æ |
---|---|
fd | ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ãªãã¸ã§ã¯ãã示ããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã |
param_values | ã¢ã¸ã¥ã¼ã«èµ·åå¼æ°ã渡ããä»åã®ä¾ã§ã¯ã"loop=100"ãªã©ã渡ãã |
flags | MODULE_INIT_IGNORE_MODVERSIONSã¨MODULE_INIT_IGNORE_VERMAGICã¯ããã¼ã¸ã§ã³ã®åããªãã¢ã¸ã¥ã¼ã«ãå¼·å¶çã«çµè¾¼ãå ´åã«æå®ãããMODULE_INIT_COMPRESSED_FILE ã¯ãã¢ã¸ã¥ã¼ã«ãå§ç¸®ããã¦ããå ´åã«æå®ããã |
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¤ã¡ã¼ã¸ãå§ç¸®ããã¦ããå ´åãã¤ã¡ã¼ã¸ã解åãã¾ãã ç¾å¨ã®Linuxã«ã¼ãã«ã¯ãå§ç¸®å½¢å¼ã¨ã㦠gzip/xz/zst ã®ãããã1ã¤ã ããã«ã¼ãã«ã®ãã«ãæã«é¸æã§ãã¾ããUbuntu 24.04 LTSã®ã«ã¼ãã«ã¯zstãæ¡ç¨ãã¦ãã¾ãã (module_decompressé¢æ°)
ç½²åã®ç¢ºèªãè¡ãã¾ããPKCS#7ã§ç½²åããã¦ãããã¨ãåæã¨ãã¦ãã¾ã*4ã ãã®ç½²åã¯ELFã®ã»ã¯ã·ã§ã³ã«ã¯ç½®ããã¦ããããELFãã¡ã¤ã«ã®å¾ãã«æ·»ä»ããå½¢ã«ãªã£ã¦ãã¾ã*5ã (module_sig_checké¢æ°)
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãªã¹ã(
modules
ã¨ããååã®å¤æ°ããããã¨ãããªã¹ã)ã«æ¬ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«(ã管çãããã¼ã¿æ§é )ãç»é²ãã¾ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ç¶æ ã¯åæåä¸(MODULE_STATE_UNFORMED)ã¨ãã¦ããã¾ãã(add_unformed_moduleé¢æ°)ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãé ç½®ããããã®ã«ã¼ãã«ã¡ã¢ãªé åã
vmalloc
ç³»ã®é¢æ°ãç¨ãã¦ç¢ºä¿ãã¾ãã (layout_and_allocateé¢æ°)æªè§£æ±ºã·ã³ãã«æ å ±ãåéãã¾ãã çµã¿è¾¼ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«å ã®æªè§£æ±ºã·ã³ãã«ããã«ã¼ãã«æ¬ä½ãå ¬éãã¦ããã·ã³ãã«ãçµã¿è¾¼ã¿æ¸ã¿ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãå ¬éãã¦ããã·ã³ãã«ã®æ å ±ããè¦ã¤ãåºãã¾ãã çµã¿è¾¼ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãGPL以å¤ã®ã©ã¤ã»ã³ã¹ãæã¤æãEXPORT_SYMBOL_GPL(ã·ã³ãã«å)ã§å ¬éããã¦ããã·ã³ãã«ã¯ãªã³ã¯ã§ããªãããã«å¶å¾¡ãã¦ãã¾ã*6ã (simplify_symbolsé¢æ°ãresolve_symbolé¢æ°)
åé ç½®å¯è½ã»ã¯ã·ã§ã³ã解決ãã¾ãã çµè¾¼ã¿åã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ã»ã¯ã·ã§ã³æ å ±ãè¦ãã¨RELAã¨ããã¿ã¤ãã®ã»ã¯ã·ã§ã³ãããã¾ã*7ã ãããã®ã»ã¯ã·ã§ã³ä¸ã®ã¢ãã¬ã¹æªè§£æ±ºã®å½ä»¤ãæ¸ãæãã¾ã(ãã®æ å ±ã¯relocationã»ã¯ã·ã§ã³ã«ããã¾ã)ã ããã¯ã¢ã¼ããã¯ãã£ä¾åã®å¦çã«ãªãã¾ãã (apply_relocationsé¢æ°)
Elfãã¡ã¤ã«ã®ã»ã¯ã·ã§ã³æ å ±ã¨ãªãã±ã¼ã·ã§ã³æ å ±ãåç §ãã解決ãã¦ããã¾ãããªãã±ã¼ã·ã§ã³æ å ±ã«ã¯ãå½ä»¤ä¸ã®ã¢ãã¬ã¹æ å ±ãæ ¼ç´ããã¦ããå ´æ(Offset)ãããã³ãã®ã¢ãã¬ã¹æ å ±ã®å(Type)ãããã¾ã(R_X86_64_PLT32ãR_X86_64_32Sãªã©)ããã®ã¢ãã¬ã¹æ å ±ã®é¨å(å½ä»¤ã®ä¸é¨ã®ãã£ã¼ã«ã)ãç·¨éãã¦ããã¾ãã
$ readelf -S kaidock.ko There are 43 section headers, starting at offset 0x494c0: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .note.gnu.bu[...] NOTE 0000000000000000 00000040 0000000000000024 0000000000000000 A 0 0 4 [ 2] .note.Linux NOTE 0000000000000000 00000064 0000000000000030 0000000000000000 A 0 0 4 [ 3] .text PROGBITS 0000000000000000 000000a0 000000000000010f 0000000000000000 AX 0 0 16 [ 4] .rela.text RELA 0000000000000000 00027978 0000000000000210 0000000000000018 I 40 3 8 : :
$ readelf -r kaidock.ko Relocation section '.rela.text' at offset 0x27950 contains 19 entries: Offset Info Type Sym. Value Sym. Name + Addend 000000000011 003100000004 R_X86_64_PLT32 0000000000000000 __fentry__ - 4 000000000019 003900000002 R_X86_64_PC32 0000000000000000 jiffies - 4 000000000027 00040000000b R_X86_64_32S 0000000000000000 .rodata.str1.8 + 0 00000000002c 003300000004 R_X86_64_PLT32 0000000000000000 _printk - 4 000000000032 000d00000002 R_X86_64_PC32 0000000000000000 .data + 24 00000000003b 000d00000002 R_X86_64_PC32 0000000000000000 .data + 24 000000000052 003600000004 R_X86_64_PLT32 0000000000000000 mod_timer - 4 00000000005d 003900000002 R_X86_64_PC32 0000000000000000 jiffies - 4 00000000007a 003800000004 R_X86_64_PLT32 0000000000000000 __x86_return_thunk - 4 000000000091 003100000004 R_X86_64_PLT32 0000000000000000 __fentry__ - 4 000000000099 003900000002 R_X86_64_PC32 0000000000000000 jiffies - 4 0000000000a0 003a0000000b R_X86_64_32S 0000000000000000 kaidock_timer + 0 0000000000b0 003a00000002 R_X86_64_PC32 0000000000000000 kaidock_timer + c 0000000000b5 003500000004 R_X86_64_PLT32 0000000000000000 add_timer - 4 0000000000bf 003800000004 R_X86_64_PLT32 0000000000000000 __x86_return_thunk - 4 0000000000e1 003100000004 R_X86_64_PLT32 0000000000000000 __fentry__ - 4 0000000000e9 003a0000000b R_X86_64_32S 0000000000000000 kaidock_timer + 0 0000000000f1 003000000004 R_X86_64_PLT32 0000000000000000 timer_delete_sync - 4 0000000000fb 003800000004 R_X86_64_PLT32 0000000000000000 __x86_return_thunk - 4 : :
çµã¿è¾¼ãã ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ã·ã³ãã«(EXPORT_SYMBOLãEXPORT_SYMBOL_GPLãªã©ã§æå®ãããã®)ãå ¬éãã¾ãã(add_kallsymsé¢æ°)
CPUã¢ãã«ã«åããå½ä»¤ã¸ã®æ¸ãæããè¡ãã¾ãã(module_finalizeé¢æ°)
é¢æ°ã®å é ã«ãã
call
å½ä»¤ãnopl
å½ä»¤ã«ç½®ãæãã¾ãã(ftrace_module_inité¢æ°)sysãã¡ã¤ã«(
/sys/module/kaidock
)ãä½æãã¾ãã(mod_sysfs_setupé¢æ°)ã¢ã¸ã¥ã¼ã«ãinit_moduleé¢æ°ãå¼ã³åºãæºåãæ´ã£ããã¨ã示ãMODULE_STATE_COMINGç¶æ ã«ãã¾ãã(complete_formationé¢æ°)
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã«å®ç¾©ãã
init_module()
ãå¼ã³åºãããã®å¾ã§ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ç¶æ ãå®è¡ä¸(MODULE_STATE_LIVE)ã«ãã¾ãã(do_init_moduleé¢æ°)
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«èµ·å(insmod
)å¦çã¯ããã§çµããã§ããå¾ã¯ã«ã¼ãã«æ¬ä½æ©è½ã¨åºå¥ãªãåä½ãç¶ãã¾ãã
4. ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®åé¤
delete_moduleã·ã¹ãã ã³ã¼ã«ã«ã¦å®ç¾ããã¦ãã¾ããrmmodã³ãã³ãã¯delete_module
ãå¼ã³åºãã ãã§ãã
åé¤å¯¾è±¡ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãä»ã¢ã¸ã¥ã¼ã«ããåç
§ããã¦ããå ´åã«ã¨ã©ã¼ã¨ãã¦ã¯ããã ãã§ããã®å¾ã¯åé¤å¦çãå§ãã¾ããåé¤ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®cleanup_module
é¢æ°ãå¼ã³åºããå¾ãfinit_module
ã§ç¢ºä¿ããè³æºã解æ¾ãã¾ãã(free_moduleé¢æ°)
æå¾ã«
ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¯ãã©ã¤ãããã(Live Patch)æ©è½ã®å®ç¾ã®ããã«ãå©ç¨ããã¦ãã¾ããåé¡ã®ããé¢æ°ã®ä¿®æ£çãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¨ãã¦ã«ã¼ãã«æ¬ä½ã«çµã¿è¾¼ãã å¾ãftrace
ã®ä»çµã¿ã使ã£ã¦åé¡ã®ããé¢æ°ã®å
é ã«ä¿®æ£çã®é¢æ°ã®å
é ã«ã¸ã£ã³ãããå½ä»¤ãåãè¾¼ããã¨ãåºæ¬çæ¦ç¥ã§ãã
ã¾ããæè¿ãLinuxã«ã¼ãã«ãRust対å¿ãããã¨ãã話ãèãããã¨ãããã¨æãã¾ãããããã«ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®è¨è¿°è¨èªãRustã«ãããã¨ãã§ããããã«ãªãã¾ãããinsmod
å¦çããããã¨ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã*.ko
å½¢å¼ã®ãªãã±ã¼ã¿ãã«ãªãã¸ã§ã¯ãã§ããã°ãå
ã®è¨è¿°è¨èªãCã§ããããRustã§ããããé¢ä¿ããã¾ãããä¸æ¹ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«éçºè
ã«ã¨ã£ã¦ã¯ãRustã使ããã¨ã§ã¡ã¢ãªæä½é¢é£ã®ãã°ãå
¥ãã«ãããªãã¨ããã¡ãªãããããã¾ãã
ç¾æç¹ã§ã®èª²é¡ã¯ãã«ã¼ãã«æ©è½ãå©ç¨ããããã®RuståãAPIãã¾ã æã£ã¦ããªã*8ãã¨ã¨ãCè¨èªè¨è¿°ã®ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ããå°ããªã¼ãããã(Rustè¨èªä»æ§ä¸ãããããã®)ã大ãããªããããæ§è½ãæ±ããããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã§ã¯ä½¿ãã«ãããã¨ã§ããããããã®ãããã®è©³ç´°ã«ã¤ãã¦ã¯ãè¤ç°ããã®è¨äºãåç §ãããã¨è¯ãã¨æãã¾ããRustæãä¼ãã£ã¦ãã¾ãã
ãã¾ã ï½ ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«çµã¿è¾¼ã¿æ©æ§ã®æ´å²
å¤ãinsmodã³ãã³ãã¯ä¸è¨ã«ç¤ºãæµãã§ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãçµã¿è¾¼ãã§ãã¾ããã
- query_moduleã·ã¹ãã ã³ã¼ã«ã§ã«ã¼ãã«ã®ã·ã³ãã«æ å ±åå¾
- create_moduleã·ã¹ãã ã³ã¼ã«ã§ã¡ã¢ãªé å確ä¿
- ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ã¢ãã¬ã¹è§£æ±º
- init_moduleã·ã¹ãã ã³ã¼ã«ã§ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«çµã¿è¾¼ã¿
ã«ã¼ãã«v2.6ã®æ代ã«ã¯æ¢ã«query_moduleãcreate_moduleã·ã¹ãã ã³ã¼ã«ã¯å»æ¢ããã¦ãããinit_moduleã·ã¹ãã ã³ã¼ã«ãã¢ã¸ã¥ã¼ã«çµã¿è¾¼ã¿ã®ãã¹ã¦ã®å¦çãè¡ãªãããã«å¤æ´ããã¦ãã¾ãã
ãã®å¾ã«ã¼ãã«v3.8ã«ã¦ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¤ã¡ã¼ã¸ãç´æ¥æ¸¡ãinit_moduleã·ã¹ãã ã³ã¼ã«ã«ä»£ãããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ãªãã±ã¼ã¿ãã«ãªãã¸ã§ã¯ã(*.ko
)ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã渡ãfinit_moduleã·ã¹ãã ã³ã¼ã«ã追å ããã¾ããããã¡ã¤ã«ã·ã¹ãã èªä½ã®æ©è½ãã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®æ£å½æ§ã®ç¢ºèªã«å½¹ç«ã¦ããã¨ãããã¨ã§ãã2ã¤ã®ã·ã¹ãã ã³ã¼ã«ã¯ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®æ¸¡ãæ¹ãç°ãªãã®ã¿ã§ãã«ã¼ãã«å
ã§ã¯åãåãããã¾ãã
ãã¾ã ï½ ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¸ã®ç½²å
/var/lib/shim-signed/mok/é ä¸ã«ãä¸è¨ã®ãã¡ã¤ã«ãããæã¯æ¢ã«ç½²åç¨ã®éµãç¨æããã¦ãã¾ãã
$ ls /var/lib/shim-signed/mok/
MOK.der MOK.priv
ç½²åç¨ã®éµãããå ´å
kmodsignã³ãã³ããç¨ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã«ç½²åãæ·»ä»ãã¾ãã ããã·ã¥ã¢ã«ã´ãªãºã ã¯ãUbuntu 24.04 LTSã®ä»ã®ã¢ã¸ã¥ã¼ã«ã¨åãsha512ã«ãã¦ããã¾ãã
$ sudo kmodsign sha512 /var/lib/shim-signed/mok/MOK.priv /var/lib/shim-signed/mok/MOK.der kaidock.ko
ããã§ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¸ã®ç½²åã¯å®äºã§ãã念ã®ããã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã®ç½²åã確èªãã¦ããã¾ãããã
$ modinfo kaidock.ko filename: /home/riscv/kaidoku/kmodule/kaidock.ko license: GPL author: taka srcversion: FCA95EBA12E11A29AD336FF depends: retpoline: Y name: kaidock vermagic: 6.8.0-40-generic SMP preempt mod_unload modversions sig_id: PKCS#7 signer: new-linux-kaidokusitu-pj Secure Boot Module Signature key sig_key: 3A:AB:5A:1E:A0:B8:B8:E7:86:68:E7:21:58:2C:39:1B:65:0B:BD:F5 sig_hashalgo: sha512 signature: EF:A8:73:6D:28:96:27:A5:63:74:09:F6:03:7B:D1:53:F6:3F:08:02: DF:0B:86:FB:14:1A:AC:21:CF:61:87:CB:15:BB:F2:8A:29:33:BD:8B: 6A:34:5F:5A:88:67:BC:9F:11:52:9E:76:48:64:BA:9B:4C:D7:73:4C: D7:B0:B4:E1:5B:84:49:58:98:36:C5:D6:03:F3:D8:8F:1E:F6:B6:56: 7F:07:D3:00:D8:FC:E5:57:5E:46:CF:06:92:72:5A:CC:C5:3D:B5:F3: 32:1B:02:01:80:69:35:64:75:1C:80:A1:7D:34:FF:74:32:04:55:0A: 42:90:6B:59:44:78:7C:FA:26:2C:B6:ED:5E:73:D4:62:D4:B2:1E:54: 8F:FC:5B:D3:95:24:AC:F8:96:E2:53:02:17:82:67:1C:81:B1:F5:80: BA:94:6B:07:4C:09:DE:A6:E7:8F:F5:07:38:FD:56:3B:B2:FD:A5:05: 8B:10:6D:94:6F:E5:A2:D8:24:11:57:91:FA:FB:79:EB:70:48:37:EF: 69:73:1E:25:18:CE:34:B1:1F:B6:6F:5E:D8:D6:64:D1:C6:2B:78:A9: 27:59:A0:A2:0E:EC:82:74:A8:AC:8F:57:40:E5:5F:2F:86:DE:5E:15: 17:A5:B3:D6:C9:B2:27:4D:68:35:EF:D7:3F:92:80:68 parm: loop:specify the loop count (int)
ç½²åç¨ã®éµããªãå ´å
ã¾ããç½²åç¨ã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ sudo apt install shim-singned
éµã®çæããã¾ãã
$ sudo update-secureboot-policy --new-key Generating a new Secure Boot signing key: Can't load /var/lib/shim-signed/mok/.rnd into RNG 40779766D3700000:error:12000079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:106:Filename=/var/lib/shim-signed/mok/.rnd .+..........+...............+.....+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.........+.......+...+...........+.+.........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.....+....+.........+........+...+...............+....+.....+.......+........+............+.+.....+...+..........+..+.......+.................+...+...+...+.+......+.....+....+...+.....+.+...........+....+.........+........+.........+.+............+..+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ........+.+..+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*....+....+......+..+.......+........+.+...........+.+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.......+..+.+......+...........+.......+...+..+.+.....+....+..+.........+................+........+......+....+..+....+..+.......+..+....+.........+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -----
éµãçæããã¾ããã
$ ls /var/lib/shim-signed/mok/
MOK.der MOK.priv
次ã«ããã®éµãUEFIã»ãã¥ã¢ãã¼ãã®éµãã¼ã¿ãã¼ã¹ã«ç»é²ãã¾ããä¸è¨ã³ãã³ãã¯ãéµç»é²ãä¾é ¼ããã³ãã³ãã§ãã次ã®ã¹ãããã§å©ç¨ãããã¹ã¯ã¼ãè¨å®ãæ±ãããã¾ãã
$ sudo mokutil --import /var/lib/shim-signed/mok/MOK.der
ã·ã¹ãã ãåèµ·åãã¾ãã
- Shim UEFI key managerãèµ·åãã¾ãããPress any key to perform MOK managementãã¨è¡¨ç¤ºãããã®ã§ãä»»æã®ãã¼ãæ¼ãã¦MOK管çãéå§ãã¾ãã
- ç»é¢ã«è¡¨ç¤ºããããEnroll MOKããé¸æãã¾ãã
- ãView key 0ã é¸æããã¨å ã»ã©çæããéµã®æ å ±ã表示ããã¾ãã
- ä»»æã®ãã¼ãæ¼ãã¨ãEnroll MOKãã®ã¡ãã¥ã¼ã«æ»ãã¾ãã
- Continueãé¸æããã¨ããEnroll the key(s)?ãã¨èãããã®ã§ãYesãé¸æãã¾ãã
- ãã¹ã¯ã¼ãå ¥åãæ±ããããã®ã§ãå ã»ã©ã®éµçæã®æã«è¨å®ãããã¹ã¯ã¼ããå ¥åãã¾ãã
- ãRebootã ãé¸æããã·ã¹ãã ãåèµ·åãã¾ãã
ã·ã¹ãã ãåèµ·åããããç½²åç¨ã®éµãããå ´åã«é²ã¿ã¾ãã
*1:jiffiesã¯ãã«ã¼ãã«èµ·åæããã®çµéæéã表ãå¤æ°ã§ãã
*2:ã«ã¼ãã«ãã³ã³ãã¤ã«ããæã®gccã¨ã¤ã³ã¹ãã¼ã«ããã¦ããgccã®ãã¼ã¸ã§ã³ãç°ãªãæã¯ãã«ãå¦çãã¨ã©ã¼ã¨ãªãã¾ãããã®å ´åããã¼ã¸ã§ã³çªå·ä»ãã®gccãæå®ãã¦makeãã¦ãã ãããUbuntu 24.04ã®å ´åã¯ã$ make -C /lib/modules/`uname -r`/build M=`pwd` modules MAKEFLAGS="CC=gcc-13" ã¨ãªãã¾ãã/boot/config-`uname -r`ãã¡ã¤ã«ãåç §ããã¨ã«ã¼ãã«ãã¼ã¸ã§ã³ã¨gccã®ãã¼ã¸ã§ã³ã®å¯¾å¿ãåããã¾ãã
*3:ããããé åãäºç´ããããã®ã³ã³ãã¤ã©ãªãã·ã§ã³ãããã¾ã
*4:ã«ã¼ãã«ããããè¦ãã¨ç½²åæ¹å¼ã¨ãã¦OpenPGPãX.509ãPKCS#7ãé¸æã§ãããã«è¦ãã¾ãããå®éã«ã¯PKCS#7ç¨ä»¥å¤ã®ã³ã¼ãã¯å®è£ ããã¦ããããã¨ã©ã¼ã«ãªãã¾ãã
*5:ãã¼ã«ã«ãã£ã¦ã¯ãå£ããelfãã¡ã¤ã«ã¨èªèãããã
*6:OSSã§ãã£ã¦ãGPL以å¤ã®ã©ã¤ã»ã³ã¹(MITãApacheãªã©)ãæã¤ã«ã¼ãã«ã¢ã¸ã¥ã¼ã«ã¯ãå®è£ ãã«ãããªã£ã¦ãã¾ãã
*7:ã¢ã¼ããã¯ãã£ã®ç¨®é¡ã«ãã£ã¦ã¯SHT_REL
*8:Rustã®ã³ã¼ããããCé¢æ°ãç´æ¥å¼ã³åºããã¨ã¯å¾¡æ³åº¦ã¨ããã¦ããã