setuid.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Process.h>
  27. namespace Kernel {
  28. KResultOr<int> Process::sys$seteuid(uid_t new_euid)
  29. {
  30. REQUIRE_PROMISE(id);
  31. if (new_euid != uid() && new_euid != suid() && !is_superuser())
  32. return EPERM;
  33. if (euid() != new_euid)
  34. set_dumpable(false);
  35. ProtectedDataMutationScope scope { *this };
  36. m_euid = new_euid;
  37. return 0;
  38. }
  39. KResultOr<int> Process::sys$setegid(gid_t new_egid)
  40. {
  41. REQUIRE_PROMISE(id);
  42. if (new_egid != gid() && new_egid != sgid() && !is_superuser())
  43. return EPERM;
  44. if (egid() != new_egid)
  45. set_dumpable(false);
  46. ProtectedDataMutationScope scope { *this };
  47. m_egid = new_egid;
  48. return 0;
  49. }
  50. KResultOr<int> Process::sys$setuid(uid_t new_uid)
  51. {
  52. REQUIRE_PROMISE(id);
  53. if (new_uid != uid() && new_uid != euid() && !is_superuser())
  54. return EPERM;
  55. if (euid() != new_uid)
  56. set_dumpable(false);
  57. ProtectedDataMutationScope scope { *this };
  58. m_uid = new_uid;
  59. m_euid = new_uid;
  60. m_suid = new_uid;
  61. return 0;
  62. }
  63. KResultOr<int> Process::sys$setgid(gid_t new_gid)
  64. {
  65. REQUIRE_PROMISE(id);
  66. if (new_gid != gid() && new_gid != egid() && !is_superuser())
  67. return EPERM;
  68. if (egid() != new_gid)
  69. set_dumpable(false);
  70. ProtectedDataMutationScope scope { *this };
  71. m_gid = new_gid;
  72. m_egid = new_gid;
  73. m_sgid = new_gid;
  74. return 0;
  75. }
  76. KResultOr<int> Process::sys$setresuid(uid_t new_ruid, uid_t new_euid, uid_t new_suid)
  77. {
  78. REQUIRE_PROMISE(id);
  79. if (new_ruid == (uid_t)-1)
  80. new_ruid = uid();
  81. if (new_euid == (uid_t)-1)
  82. new_euid = euid();
  83. if (new_suid == (uid_t)-1)
  84. new_suid = suid();
  85. auto ok = [this](uid_t id) { return id == uid() || id == euid() || id == suid(); };
  86. if ((!ok(new_ruid) || !ok(new_euid) || !ok(new_suid)) && !is_superuser())
  87. return EPERM;
  88. if (euid() != new_euid)
  89. set_dumpable(false);
  90. ProtectedDataMutationScope scope { *this };
  91. m_uid = new_ruid;
  92. m_euid = new_euid;
  93. m_suid = new_suid;
  94. return 0;
  95. }
  96. KResultOr<int> Process::sys$setresgid(gid_t new_rgid, gid_t new_egid, gid_t new_sgid)
  97. {
  98. REQUIRE_PROMISE(id);
  99. if (new_rgid == (gid_t)-1)
  100. new_rgid = gid();
  101. if (new_egid == (gid_t)-1)
  102. new_egid = egid();
  103. if (new_sgid == (gid_t)-1)
  104. new_sgid = sgid();
  105. auto ok = [this](gid_t id) { return id == gid() || id == egid() || id == sgid(); };
  106. if ((!ok(new_rgid) || !ok(new_egid) || !ok(new_sgid)) && !is_superuser())
  107. return EPERM;
  108. if (egid() != new_egid)
  109. set_dumpable(false);
  110. ProtectedDataMutationScope scope { *this };
  111. m_gid = new_rgid;
  112. m_egid = new_egid;
  113. m_sgid = new_sgid;
  114. return 0;
  115. }
  116. KResultOr<int> Process::sys$setgroups(ssize_t count, Userspace<const gid_t*> user_gids)
  117. {
  118. REQUIRE_PROMISE(id);
  119. if (count < 0)
  120. return EINVAL;
  121. if (!is_superuser())
  122. return EPERM;
  123. if (!count) {
  124. ProtectedDataMutationScope scope { *this };
  125. m_extra_gids.clear();
  126. return 0;
  127. }
  128. Vector<gid_t> new_extra_gids;
  129. new_extra_gids.resize(count);
  130. if (!copy_n_from_user(new_extra_gids.data(), user_gids, count))
  131. return EFAULT;
  132. HashTable<gid_t> unique_extra_gids;
  133. for (auto& extra_gid : new_extra_gids) {
  134. if (extra_gid != gid())
  135. unique_extra_gids.set(extra_gid);
  136. }
  137. ProtectedDataMutationScope scope { *this };
  138. m_extra_gids.resize(unique_extra_gids.size());
  139. size_t i = 0;
  140. for (auto& extra_gid : unique_extra_gids) {
  141. if (extra_gid == gid())
  142. continue;
  143. m_extra_gids[i++] = extra_gid;
  144. }
  145. return 0;
  146. }
  147. }