main.c 1.1 KB

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