1 #include <sys/types.h> 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <assert.h> 5 6 int main() 7 { 8 printf("Current uid: %d, euid: %d, gid: %d, egid: %d\n\n", getuid(), geteuid(), getgid(), getegid()); 9 10 // 测试uid 11 printf("Set uid 1000\n"); 12 setuid(1000); 13 int uid = getuid(); 14 assert(uid == 1000); 15 printf("Current uid:%d\n\n", uid); 16 17 // 测试gid 18 printf("Set gid 1000\n"); 19 setgid(1000); 20 int gid = getgid(); 21 assert(gid == 1000); 22 printf("Current gid:%d\n\n", gid); 23 24 // 测试euid 25 printf("Setg euid 1000\n"); 26 seteuid(1000); 27 int euid = geteuid(); 28 assert(euid == 1000); 29 printf("Current euid:%d\n\n", euid); 30 31 // 测试egid 32 printf("Set egid 1000\n"); 33 setegid(1000); 34 int egid = getegid(); 35 assert(egid == 1000); 36 printf("Current egid:%d\n\n", egid); 37 38 // 测试uid在非root用户下无法修改 39 printf("Try to setuid for non_root.\n"); 40 assert(setuid(0) < 0); // 非root用户无法修改uid 41 printf("Current uid: %d, euid: %d, gid: %d, egid: %d\n", getuid(), geteuid(), getgid(), getegid()); 42 }