setids.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. setids.c
  9. Abstract:
  10. This module implements the logic to make setuid and friends calls work
  11. across all threads.
  12. Author:
  13. Evan Green 1-May-2015
  14. Environment:
  15. User Mode C Library
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include "pthreadp.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // Define the amount of time to wait for a set ID request to go through, in
  26. // seconds.
  27. //
  28. #define PTHREAD_SETID_TIMEOUT 60
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. /*++
  33. Structure Description:
  34. This structure stores details for a setuid (and friends) request.
  35. Members:
  36. SetGroups - Stores a boolean indicating if this is a set supplementary
  37. groups call (TRUE) or a set thread identity call.
  38. Fields - Stores the fields to set in the thread identity.
  39. Identity - Stores a pointer to the thread identity to set.
  40. Groups - Stores a pointer to an array of supplementary group IDs to set.
  41. GroupCount - Stores the number of elements in the supplementary group ID
  42. array.
  43. Thread - Stores the thread the request is directed to.
  44. Mutex - Stores the mutex guarding the condition.
  45. Condition - Stores the condition variable.
  46. --*/
  47. typedef struct _PTHREAD_SETID_REQUEST {
  48. BOOL SetGroups;
  49. ULONG Fields;
  50. PTHREAD_IDENTITY Identity;
  51. PGROUP_ID Groups;
  52. UINTN GroupCount;
  53. pthread_t Thread;
  54. pthread_mutex_t Mutex;
  55. pthread_cond_t Condition;
  56. } PTHREAD_SETID_REQUEST, *PPTHREAD_SETID_REQUEST;
  57. //
  58. // ----------------------------------------------- Internal Function Prototypes
  59. //
  60. VOID
  61. ClpExecuteSetIdRequest (
  62. PPTHREAD_SETID_REQUEST Request
  63. );
  64. //
  65. // -------------------------------------------------------------------- Globals
  66. //
  67. PPTHREAD_SETID_REQUEST ClSetIdRequest;
  68. //
  69. // ------------------------------------------------------------------ Functions
  70. //
  71. VOID
  72. ClpSetThreadIdentityOnAllThreads (
  73. ULONG Fields,
  74. PTHREAD_IDENTITY Identity
  75. )
  76. /*++
  77. Routine Description:
  78. This routine uses a signal to set the thread identity on all threads
  79. except the current one (which is assumed to have already been set).
  80. Arguments:
  81. Fields - Supplies the bitfield of identity fields to set. See
  82. THREAD_IDENTITY_FIELD_* definitions.
  83. Identity - Supplies a pointer to the thread identity information to set.
  84. Return Value:
  85. None.
  86. --*/
  87. {
  88. PTHREAD_SETID_REQUEST Request;
  89. //
  90. // If threading's not been fired up, nothing needs to be done.
  91. //
  92. if (ClThreadList.Next == NULL) {
  93. return;
  94. }
  95. RtlZeroMemory(&Request, sizeof(PTHREAD_SETID_REQUEST));
  96. Request.SetGroups = FALSE;
  97. Request.Fields = Fields;
  98. Request.Identity = Identity;
  99. ClpExecuteSetIdRequest(&Request);
  100. return;
  101. }
  102. VOID
  103. ClpSetSupplementaryGroupsOnAllThreads (
  104. PGROUP_ID GroupIds,
  105. UINTN GroupIdCount
  106. )
  107. /*++
  108. Routine Description:
  109. This routine uses a signal to set the supplementary groups on all threads
  110. except the current one (which is assumed to have already been set).
  111. Arguments:
  112. GroupIds - Supplies a pointer to the array of group IDs to set.
  113. GroupIdCount - Supplies the number of elements in the group ID array.
  114. Return Value:
  115. None.
  116. --*/
  117. {
  118. PTHREAD_SETID_REQUEST Request;
  119. //
  120. // If threading's not been fired up, nothing needs to be done.
  121. //
  122. if (ClThreadList.Next == NULL) {
  123. return;
  124. }
  125. RtlZeroMemory(&Request, sizeof(PTHREAD_SETID_REQUEST));
  126. Request.SetGroups = TRUE;
  127. Request.Groups = GroupIds;
  128. Request.GroupCount = GroupIdCount;
  129. ClpExecuteSetIdRequest(&Request);
  130. return;
  131. }
  132. void
  133. ClpSetIdSignalHandler (
  134. int Signal
  135. )
  136. /*++
  137. Routine Description:
  138. This routine is a signal handler called to fix up the user identity on a
  139. thread.
  140. Arguments:
  141. Signal - Supplies the signal that caused this handler to be invoked.
  142. Return Value:
  143. None.
  144. --*/
  145. {
  146. PPTHREAD_SETID_REQUEST Request;
  147. Request = ClSetIdRequest;
  148. //
  149. // Ignore spurious requests. Note that this is not foolproof, as the
  150. // request might be set now but be destroyed in just a moment if a request
  151. // is not actually going through.
  152. //
  153. if ((Request == NULL) || (Request->Thread != pthread_self())) {
  154. abort();
  155. }
  156. if (Request->SetGroups != FALSE) {
  157. OsSetSupplementaryGroups(TRUE, Request->Groups, &(Request->GroupCount));
  158. } else {
  159. OsSetThreadIdentity(Request->Fields, Request->Identity);
  160. }
  161. pthread_mutex_lock(&(Request->Mutex));
  162. Request->Thread = 0;
  163. pthread_cond_signal(&(Request->Condition));
  164. pthread_mutex_unlock(&(Request->Mutex));
  165. return;
  166. }
  167. //
  168. // --------------------------------------------------------- Internal Functions
  169. //
  170. VOID
  171. ClpExecuteSetIdRequest (
  172. PPTHREAD_SETID_REQUEST Request
  173. )
  174. /*++
  175. Routine Description:
  176. This routine uses a signal to set the thread identity on all threads
  177. except the current one (which is assumed to have already been set).
  178. Arguments:
  179. Request - Supplies a pointer to the request to execute.
  180. Return Value:
  181. None.
  182. --*/
  183. {
  184. PLIST_ENTRY CurrentEntry;
  185. KSTATUS KernelStatus;
  186. PPTHREAD Self;
  187. int Status;
  188. PPTHREAD Thread;
  189. struct timespec Timeout;
  190. pthread_mutex_init(&(Request->Mutex), NULL);
  191. pthread_mutex_lock(&(Request->Mutex));
  192. Self = (PPTHREAD)pthread_self();
  193. pthread_mutex_lock(&ClThreadListMutex);
  194. ClSetIdRequest = Request;
  195. CurrentEntry = ClThreadList.Next;
  196. while (CurrentEntry != &ClThreadList) {
  197. Thread = LIST_VALUE(CurrentEntry, PTHREAD, ListEntry);
  198. CurrentEntry = CurrentEntry->Next;
  199. if (Thread == Self) {
  200. continue;
  201. }
  202. Request->Thread = (pthread_t)Thread;
  203. //
  204. // Fire off the request. Allow for the possibility that the thread has
  205. // died, which is okay.
  206. //
  207. KernelStatus = OsSendSignal(SignalTargetThread,
  208. Thread->ThreadId,
  209. SIGNAL_SETID,
  210. SIGNAL_CODE_USER,
  211. 0);
  212. if (KernelStatus == STATUS_NO_SUCH_THREAD) {
  213. continue;
  214. }
  215. if (!KSUCCESS(KernelStatus)) {
  216. fprintf(stderr,
  217. "Error: Failed to signal thread %x: %d\n",
  218. Thread,
  219. KernelStatus);
  220. abort();
  221. }
  222. clock_gettime(CLOCK_REALTIME_COARSE, &Timeout);
  223. Timeout.tv_sec += PTHREAD_SETID_TIMEOUT;
  224. while (Request->Thread == (pthread_t)Thread) {
  225. Status = pthread_cond_timedwait(&(Request->Condition),
  226. &(Request->Mutex),
  227. &Timeout);
  228. if (Status == ETIMEDOUT) {
  229. fprintf(stderr,
  230. "Error: Thread %x failed to respond to set ID "
  231. "request.\n",
  232. Thread);
  233. abort();
  234. }
  235. }
  236. }
  237. ClSetIdRequest = NULL;
  238. pthread_mutex_unlock(&ClThreadListMutex);
  239. pthread_mutex_unlock(&(Request->Mutex));
  240. pthread_mutex_destroy(&(Request->Mutex));
  241. return;
  242. }