|
| 1 | + |
| 2 | +# 说明 |
| 3 | + |
| 4 | +该实验为《奔跑吧Linux入门版》中第六章的实验2的参考代码。 |
| 5 | + |
| 6 | +实验要求添加的系统调用不传递任何参数,一般将`pid`和`uid`直接通过`printk`输出到`dmesg`中,但是这样非常的不优雅。该参考代码添加了一个系统调用 |
| 7 | + |
| 8 | +```c |
| 9 | +long getpuid(pid_t *pid, uid_t *uid); |
| 10 | +``` |
| 11 | +
|
| 12 | +`pid`和`uid`通过参数返回。 |
| 13 | +
|
| 14 | +为了方便进行实验,以下实验基于《奔跑吧Linux入门版》中第一章的实验室2,我们选定了最新的社区稳定版内核来修改添加系统调用,然后编译后,安装到优麒麟Linux机器上。 |
| 15 | +
|
| 16 | +# 实验步骤 |
| 17 | +
|
| 18 | +## 下载最新的社区稳定版内核 |
| 19 | +
|
| 20 | +在完成该实验时,社区最新的稳定版内核是`linux-5.1.16`,下载方法如下: |
| 21 | +``` |
| 22 | +$ wget -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz |
| 23 | +$ xz -d linux-5.1.16.tar.xz |
| 24 | +$ tar -xf linux-5.1.16.tar |
| 25 | +$ cd linux-5.1.16/ |
| 26 | +``` |
| 27 | +
|
| 28 | +
|
| 29 | +## 添加系统调用getpuid |
| 30 | +
|
| 31 | +`0001-x86-add-a-new-syscall-which-called-getpuid.patch`是基于`linux-5.1.16`制作的patch,如果是其它版本,请参考如下该`patch`进行修改: |
| 32 | +
|
| 33 | +``` |
| 34 | +$ cat 0001-x86-add-a-new-syscall-which-called-getpuid.patch |
| 35 | +From e4d3eb5953e8933cad1b51915c917dd7905d7078 Mon Sep 17 00:00:00 2001 |
| 36 | +From: Wang Long <[email protected]> |
| 37 | +Date: Sat, 6 Jul 2019 10:06:03 +0800 |
| 38 | +Subject: [PATCH] x86: add a new syscall which called getpuid |
| 39 | + |
| 40 | +This patch add a new syscall for x86, which name is getpuid. |
| 41 | +getpuid return the current process's pid and uid. |
| 42 | + |
| 43 | +its prototype is: long sys_getpuid(pid_t *pid, uid_t *uid); |
| 44 | + |
| 45 | +Signed-off-by: Wang Long <[email protected]> |
| 46 | +--- |
| 47 | + arch/x86/entry/syscalls/syscall_64.tbl | 1 + |
| 48 | + include/linux/syscalls.h | 1 + |
| 49 | + kernel/sys.c | 14 ++++++++++++++ |
| 50 | + 3 files changed, 16 insertions(+) |
| 51 | + |
| 52 | +diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl |
| 53 | +index 92ee0b437..f8afc50c9 100644 |
| 54 | +--- a/arch/x86/entry/syscalls/syscall_64.tbl |
| 55 | ++++ b/arch/x86/entry/syscalls/syscall_64.tbl |
| 56 | +@@ -343,6 +343,7 @@ |
| 57 | + 332 common statx __x64_sys_statx |
| 58 | + 333 common io_pgetevents __x64_sys_io_pgetevents |
| 59 | + 334 common rseq __x64_sys_rseq |
| 60 | ++350 common getpuid __x64_sys_getpuid |
| 61 | + # don't use numbers 387 through 423, add new calls after the last |
| 62 | + # 'common' entry |
| 63 | + 424 common pidfd_send_signal __x64_sys_pidfd_send_signal |
| 64 | +diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h |
| 65 | +index e446806a5..8f007f00b 100644 |
| 66 | +--- a/include/linux/syscalls.h |
| 67 | ++++ b/include/linux/syscalls.h |
| 68 | +@@ -1390,4 +1390,5 @@ static inline unsigned int ksys_personality(unsigned int personality) |
| 69 | + return old; |
| 70 | + } |
| 71 | + |
| 72 | ++asmlinkage long sys_getpuid(pid_t __user *pid, uid_t __user *uid); |
| 73 | + #endif |
| 74 | +diff --git a/kernel/sys.c b/kernel/sys.c |
| 75 | +index bdbfe8d37..d75d8654d 100644 |
| 76 | +--- a/kernel/sys.c |
| 77 | ++++ b/kernel/sys.c |
| 78 | +@@ -2648,4 +2648,18 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info) |
| 79 | + |
| 80 | + return 0; |
| 81 | + } |
| 82 | ++ |
| 83 | ++SYSCALL_DEFINE2(getpuid, pid_t __user *, pid, uid_t __user *, uid) { |
| 84 | ++ if (pid == NULL && uid == NULL) |
| 85 | ++ return -EINVAL; |
| 86 | ++ |
| 87 | ++ if (pid != NULL) |
| 88 | ++ *pid = task_tgid_vnr(current); |
| 89 | ++ |
| 90 | ++ if (uid != NULL) |
| 91 | ++ *uid = from_kuid_munged(current_user_ns(), current_uid()); |
| 92 | ++ |
| 93 | ++ return 0; |
| 94 | ++} |
| 95 | ++ |
| 96 | + #endif /* CONFIG_COMPAT */ |
| 97 | +-- |
| 98 | +2.17.1 |
| 99 | +``` |
| 100 | +
|
| 101 | +## 编译 |
| 102 | +
|
| 103 | +为了方便,我们直接复制优麒麟Linux系统中自带的配置文件,我的系统上的配置文件为:`/boot/config-4.18.0-16-generic`,相关命令如下: |
| 104 | +
|
| 105 | +``` |
| 106 | +$ cd linux-5.1.16/ |
| 107 | +$ cp /boot/config-4.18.0-16-generic .config |
| 108 | +$ make menuconfig |
| 109 | +$ make -j4 |
| 110 | +``` |
| 111 | +
|
| 112 | +## 安装 |
| 113 | +``` |
| 114 | +$ sudo make modules_install |
| 115 | +$ sudo make install |
| 116 | +``` |
| 117 | +
|
| 118 | +安装完成后,重启电脑,用刚才编译的内核启动,登录系统。 |
| 119 | +
|
| 120 | +## 编写应用程序并编译运行 |
| 121 | +
|
| 122 | +``` |
| 123 | +$ cd ~/runninglinuxkernel_4.0 |
| 124 | +$ cd rlk_lab/rlk_basic/chapter_6/lab2/ |
| 125 | +$ gcc -o test_getpuid_syscall test_getpuid_syscall.c |
| 126 | +$ ./test_getpuid_syscall |
| 127 | +call getpuid success, return pid = 1419, uid = 1000 |
| 128 | +$ ./test_getpuid_syscall |
| 129 | +call getpuid success, return pid = 1420, uid = 1000 |
| 130 | +``` |
0 commit comments