Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f68ad34

Browse files
Wang Longfigozhang
authored andcommitted
add lab2 for chapter6
Signed-off-by: Wang Long <[email protected]>
1 parent 4f79d77 commit f68ad34

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
From e4d3eb5953e8933cad1b51915c917dd7905d7078 Mon Sep 17 00:00:00 2001
2+
From: Wang Long <[email protected]>
3+
Date: Sat, 6 Jul 2019 10:06:03 +0800
4+
Subject: [PATCH] x86: add a new syscall which called getpuid
5+
6+
This patch add a new syscall for x86, which name is getpuid.
7+
getpuid return the current process's pid and uid.
8+
9+
its prototype is: long sys_getpuid(pid_t *pid, uid_t *uid);
10+
11+
Signed-off-by: Wang Long <[email protected]>
12+
---
13+
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
14+
include/linux/syscalls.h | 1 +
15+
kernel/sys.c | 14 ++++++++++++++
16+
3 files changed, 16 insertions(+)
17+
18+
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
19+
index 92ee0b437..f8afc50c9 100644
20+
--- a/arch/x86/entry/syscalls/syscall_64.tbl
21+
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
22+
@@ -343,6 +343,7 @@
23+
332 common statx __x64_sys_statx
24+
333 common io_pgetevents __x64_sys_io_pgetevents
25+
334 common rseq __x64_sys_rseq
26+
+350 common getpuid __x64_sys_getpuid
27+
# don't use numbers 387 through 423, add new calls after the last
28+
# 'common' entry
29+
424 common pidfd_send_signal __x64_sys_pidfd_send_signal
30+
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
31+
index e446806a5..8f007f00b 100644
32+
--- a/include/linux/syscalls.h
33+
+++ b/include/linux/syscalls.h
34+
@@ -1390,4 +1390,5 @@ static inline unsigned int ksys_personality(unsigned int personality)
35+
return old;
36+
}
37+
38+
+asmlinkage long sys_getpuid(pid_t __user *pid, uid_t __user *uid);
39+
#endif
40+
diff --git a/kernel/sys.c b/kernel/sys.c
41+
index bdbfe8d37..d75d8654d 100644
42+
--- a/kernel/sys.c
43+
+++ b/kernel/sys.c
44+
@@ -2648,4 +2648,18 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
45+
46+
return 0;
47+
}
48+
+
49+
+SYSCALL_DEFINE2(getpuid, pid_t __user *, pid, uid_t __user *, uid) {
50+
+ if (pid == NULL && uid == NULL)
51+
+ return -EINVAL;
52+
+
53+
+ if (pid != NULL)
54+
+ *pid = task_tgid_vnr(current);
55+
+
56+
+ if (uid != NULL)
57+
+ *uid = from_kuid_munged(current_user_ns(), current_uid());
58+
+
59+
+ return 0;
60+
+}
61+
+
62+
#endif /* CONFIG_COMPAT */
63+
--
64+
2.17.1
65+
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <errno.h>
5+
6+
int main(int argc, char **argv)
7+
{
8+
long pid, uid;
9+
int ret;
10+
11+
pid = uid = 0;
12+
ret = (int)syscall(350, &pid, &uid);
13+
if (ret != 0) {
14+
printf("call getpuid failed\n");
15+
return 1;
16+
}
17+
18+
printf("call getpuid success, return pid = %ld, uid = %ld\n", pid, uid);
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)