exercise_9_1.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Assume in each of the following cases that the initial set of process user IDs is
  3. real=1000 effective=0 saved=0 file-system=0. What would be the state of the user IDs
  4. after the following calls?
  5. a) setuid(2000);
  6. b) setreuid(–1, 2000);
  7. c) seteuid(2000);
  8. d) setfsuid(2000);
  9. e) setresuid(–1, 2000, 3000);
  10. */
  11. // TODO: boring topic exercises undone
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. // -D_GNU_SOURCE add feature_test_macro
  15. #include <unistd.h>
  16. #include <sys/fsuid.h>
  17. void
  18. print_process_credentials(void)
  19. {
  20. uid_t ruid, euid, suid, fsuid;
  21. fsuid = setfsuid(0);
  22. if (getresuid(&ruid, &euid, &suid) == -1)
  23. return;
  24. printf("ruid: %ld, euid: %ld, suid %ld, fsuid %ld\n",
  25. ruid, euid, suid, fsuid);
  26. gid_t rgid, egid, sgid, fsgid;
  27. fsgid = setfsgid(0);
  28. if (getresuid(&rgid, &egid, &sgid) == -1)
  29. return;
  30. printf("rgid: %ld, egid: %ld, sgid %ld, fsgid %ld\n",
  31. rgid, egid, sgid, fsgid);
  32. long ngroups = sysconf(_SC_NGROUPS_MAX) + 1;
  33. gid_t supp_groups[ngroups];
  34. ssize_t curr_ngroups = getgroups(ngroups, supp_groups);
  35. printf("supplementary groups:\n");
  36. for (int i = 0; i < curr_ngroups; ++i)
  37. printf("%ld ", supp_groups[i]);
  38. printf("\n");
  39. }
  40. int
  41. main(void)
  42. {
  43. print_process_credentials();
  44. // -- setuid(2000);
  45. /*
  46. ruid: 2000, euid: 2000, suid 2000, fsuid 2000
  47. rgid: 2000, egid: 2000, sgid 2000, fsgid 0
  48. */
  49. // -- printf("%d\n", setreuid(-1, 2000));
  50. // ruid: 1000, euid: 2000, suid 2000, fsuid 2000
  51. // seteuid(2000);
  52. // -- ruid: 1000, euid: 2000, suid 0, fsuid 2000
  53. // setfsuid(2000);
  54. // -- ruid: 1000, euid: 0, suid 0, fsuid 2000
  55. // setresuid(-1, 2000, 3000);
  56. // -- ruid: 1000, euid: 2000, suid 3000, fsuid 2000
  57. print_process_credentials();
  58. return EXIT_SUCCESS;
  59. }