mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-21 12:26:34 +00:00
feat(cred): 初步实现Cred (#846)
* 初步实现Cred * 添加seteuid和setegid * 添加cred测试程序 * 修改Cred::fscmp返回结果为CredFsCmp枚举 * 完善root用户相关信息
This commit is contained in:
1
user/apps/test_cred/.gitignore
vendored
Normal file
1
user/apps/test_cred/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
test_cred
|
20
user/apps/test_cred/Makefile
Normal file
20
user/apps/test_cred/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
ifeq ($(ARCH), x86_64)
|
||||
CROSS_COMPILE=x86_64-linux-musl-
|
||||
else ifeq ($(ARCH), riscv64)
|
||||
CROSS_COMPILE=riscv64-linux-musl-
|
||||
endif
|
||||
|
||||
CC=$(CROSS_COMPILE)gcc
|
||||
|
||||
.PHONY: all
|
||||
all: main.c
|
||||
$(CC) -static -o test_cred main.c
|
||||
|
||||
.PHONY: install clean
|
||||
install: all
|
||||
mv test_cred $(DADK_CURRENT_BUILD_DIR)/test_cred
|
||||
|
||||
clean:
|
||||
rm test_cred *.o
|
||||
|
||||
fmt:
|
42
user/apps/test_cred/main.c
Normal file
42
user/apps/test_cred/main.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Current uid: %d, euid: %d, gid: %d, egid: %d\n\n", getuid(), geteuid(), getgid(), getegid());
|
||||
|
||||
// 测试uid
|
||||
printf("Set uid 1000\n");
|
||||
setuid(1000);
|
||||
int uid = getuid();
|
||||
assert(uid == 1000);
|
||||
printf("Current uid:%d\n\n", uid);
|
||||
|
||||
// 测试gid
|
||||
printf("Set gid 1000\n");
|
||||
setgid(1000);
|
||||
int gid = getgid();
|
||||
assert(gid == 1000);
|
||||
printf("Current gid:%d\n\n", gid);
|
||||
|
||||
// 测试euid
|
||||
printf("Setg euid 1000\n");
|
||||
seteuid(1000);
|
||||
int euid = geteuid();
|
||||
assert(euid == 1000);
|
||||
printf("Current euid:%d\n\n", euid);
|
||||
|
||||
// 测试egid
|
||||
printf("Set egid 1000\n");
|
||||
setegid(1000);
|
||||
int egid = getegid();
|
||||
assert(egid == 1000);
|
||||
printf("Current egid:%d\n\n", egid);
|
||||
|
||||
// 测试uid在非root用户下无法修改
|
||||
printf("Try to setuid for non_root.\n");
|
||||
assert(setuid(0) < 0); // 非root用户无法修改uid
|
||||
printf("Current uid: %d, euid: %d, gid: %d, egid: %d\n", getuid(), geteuid(), getgid(), getegid());
|
||||
}
|
23
user/dadk/config/test_cred-0.1.0.dadk
Normal file
23
user/dadk/config/test_cred-0.1.0.dadk
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "test_cred",
|
||||
"version": "0.1.0",
|
||||
"description": "测试cred",
|
||||
"task_type": {
|
||||
"BuildFromSource": {
|
||||
"Local": {
|
||||
"path": "apps/test_cred"
|
||||
}
|
||||
}
|
||||
},
|
||||
"depends": [],
|
||||
"build": {
|
||||
"build_command": "make install"
|
||||
},
|
||||
"clean": {
|
||||
"clean_command": "make clean"
|
||||
},
|
||||
"install": {
|
||||
"in_dragonos_path": "/bin"
|
||||
},
|
||||
"target_arch": ["x86_64"]
|
||||
}
|
@ -0,0 +1 @@
|
||||
root::0:
|
@ -0,0 +1 @@
|
||||
root:::
|
@ -0,0 +1 @@
|
||||
root::0:0:root:/root:/bin/NovaShell
|
@ -0,0 +1 @@
|
||||
root:::0:99999:7:::
|
Reference in New Issue
Block a user