pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


mount |DuplicateTokenEx()| experiment
Posted by Anonymous on Wed 24th Apr 2024 14:34
raw | new post

  1. diff --git a/daemon/daemon_debug.c b/daemon/daemon_debug.c
  2. index b35bfee..54fb411 100644
  3. --- a/daemon/daemon_debug.c
  4. +++ b/daemon/daemon_debug.c
  5. @@ -90,14 +90,32 @@ void logprintf(LPCSTR format, ...)
  6.      SYSTEMTIME stime;
  7.      char username[UNLEN+1];
  8.      char groupname[GNLEN+1];
  9. +    HANDLE tok;
  10. +    const char *tok_src;
  11. +    bool free_tok = false;
  12.  
  13.      GetLocalTime(&stime);
  14. -    if (!get_token_user_name(GetCurrentThreadEffectiveToken(),
  15. -        username)) {
  16. +
  17. +    if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &tok)) {
  18. +        tok_src = "impersonated_user";
  19. +        free_tok = true;
  20. +    }
  21. +    else {
  22. +        int lasterr = GetLastError();
  23. +        if (lasterr == ERROR_CANT_OPEN_ANONYMOUS) {
  24. +            tok_src = "anon_user";
  25. +        }
  26. +        else {
  27. +            tok_src = "proc_user";
  28. +        }
  29. +
  30. +        tok = GetCurrentProcessToken();
  31. +    }
  32. +
  33. +    if (!get_token_user_name(tok, username)) {
  34.          (void)strcpy(username, "<unknown>");
  35.      }
  36. -    if (!get_token_primarygroup_name(GetCurrentThreadEffectiveToken(),
  37. -        groupname)) {
  38. +    if (!get_token_primarygroup_name(tok, groupname)) {
  39.          (void)strcpy(groupname, "<unknown>");
  40.      }
  41.  
  42. @@ -105,15 +123,20 @@ void logprintf(LPCSTR format, ...)
  43.      va_start(args, format);
  44.      (void)fprintf(dlog_file,
  45.          "# LOG: ts=%04d-%02d-%02d_%02d:%02d:%02d:%04d"
  46. -        " thr=%04x user='%s'/'%s' msg=",
  47. +        " thr=%04x %s='%s'/'%s' msg=",
  48.          (int)stime.wYear, (int)stime.wMonth, (int)stime.wDay,
  49.          (int)stime.wHour, (int)stime.wMinute, (int)stime.wSecond,
  50.          (int)stime.wMilliseconds,
  51.          (int)GetCurrentThreadId(),
  52. +        tok_src,
  53.          username, groupname);
  54.      (void)vfprintf(dlog_file, format, args);
  55.      (void)fflush(dlog_file);
  56.      va_end(args);
  57. +
  58. +    if (free_tok) {
  59. +        (void)CloseHandle(tok);
  60. +    }
  61.  }
  62.  
  63.  void eprintf(LPCSTR format, ...)
  64. diff --git a/dll/nfs41_np.c b/dll/nfs41_np.c
  65. index dfd7b88..1070c8f 100644
  66. --- a/dll/nfs41_np.c
  67. +++ b/dll/nfs41_np.c
  68. @@ -24,10 +24,18 @@
  69.  #error Code requires ISO C17
  70.  #endif
  71.  
  72. +#if 0
  73. +#define NP_PRINT_TOKEN_USER 1
  74. +#endif
  75. +
  76.  #include <windows.h>
  77.  #include <npapi.h>
  78.  #include <devioctl.h>
  79.  #include <strsafe.h>
  80. +#ifdef NP_PRINT_TOKEN_USER
  81. +#include <stdbool.h>
  82. +#include <Lmcons.h>
  83. +#endif /* NP_PRINT_TOKEN_USER */
  84.  
  85.  #include "nfs41_build_features.h"
  86.  #include "nfs41_driver.h"
  87. @@ -44,13 +52,120 @@
  88.  #define TRACE_TAG   L"[NFS41_NP] "
  89.  #define WNNC_DRIVER(major, minor) ((major * 0x00010000) + (minor))
  90.  
  91. +#ifdef NP_PRINT_TOKEN_USER
  92. +/*
  93. + * Performance hack:
  94. + * GETTOKINFO_EXTRA_BUFFER - extra space for more data
  95. + * |GetTokenInformation()| for |TOKEN_USER| and |TOKEN_PRIMARY_GROUP|
  96. + * always fails in Win10 with |ERROR_INSUFFICIENT_BUFFER| if you
  97. + * just pass the |sizeof(TOKEN_*)| value. Instead of calling
  98. + * |GetTokenInformation()| with |NULL| arg to obtain the size to
  99. + * allocate we just provide 512 bytes of extra space after the
  100. + * |TOKEN_*| size, and pray it is enough
  101. + */
  102. +#define GETTOKINFO_EXTRA_BUFFER (512)
  103. +
  104. +static
  105. +bool get_token_user_name(HANDLE tok, char *out_buffer)
  106. +{
  107. +    DWORD tokdatalen;
  108. +    PTOKEN_USER ptuser;
  109. +    PSID pusid;
  110. +    DWORD namesize = UNLEN+1;
  111. +    char domainbuffer[UNLEN+1];
  112. +    DWORD domainbuffer_size = sizeof(domainbuffer);
  113. +    SID_NAME_USE name_use;
  114. +
  115. +    tokdatalen = sizeof(TOKEN_USER)+GETTOKINFO_EXTRA_BUFFER;
  116. +    ptuser = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tokdatalen));
  117. +    if (!GetTokenInformation(tok, TokenUser, ptuser,
  118. +        tokdatalen, &tokdatalen)) {
  119. +        return false;
  120. +    }
  121. +
  122. +    pusid = ptuser->User.Sid;
  123. +
  124. +    if (!LookupAccountSidA(NULL, pusid, out_buffer, &namesize,
  125. +        domainbuffer, &domainbuffer_size, &name_use)) {
  126. +        return false;
  127. +    }
  128. +
  129. +    return true;
  130. +}
  131. +
  132. +static
  133. +bool get_token_primarygroup_name(HANDLE tok, char *out_buffer)
  134. +{
  135. +    DWORD tokdatalen;
  136. +    PTOKEN_PRIMARY_GROUP ptpgroup;
  137. +    PSID pgsid;
  138. +    DWORD namesize = GNLEN+1;
  139. +    char domainbuffer[UNLEN+1];
  140. +    DWORD domainbuffer_size = sizeof(domainbuffer);
  141. +    SID_NAME_USE name_use;
  142. +
  143. +    tokdatalen = sizeof(TOKEN_PRIMARY_GROUP)+GETTOKINFO_EXTRA_BUFFER;
  144. +    ptpgroup = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(tokdatalen));
  145. +    if (!GetTokenInformation(tok, TokenPrimaryGroup, ptpgroup,
  146. +        tokdatalen, &tokdatalen)) {
  147. +        return false;
  148. +    }
  149. +
  150. +    pgsid = ptpgroup->PrimaryGroup;
  151. +
  152. +    if (!LookupAccountSidA(NULL, pgsid, out_buffer, &namesize,
  153. +        domainbuffer, &domainbuffer_size, &name_use)) {
  154. +        return false;
  155. +    }
  156. +
  157. +    return true;
  158. +}
  159. +#endif /* NP_PRINT_TOKEN_USER */
  160. +
  161.  
  162.  ULONG _cdecl NFS41DbgPrint(__in LPTSTR fmt, ...)
  163.  {
  164.      ULONG rc = 0;
  165. -#define SZBUFFER_SIZE 1024
  166. +#define SZBUFFER_SIZE 512
  167.      wchar_t szbuffer[SZBUFFER_SIZE+1];
  168.      wchar_t *szbp = szbuffer;
  169. +#ifdef NP_PRINT_TOKEN_USER
  170. +    char username[UNLEN+1];
  171. +    char groupname[GNLEN+1];
  172. +    HANDLE tok;
  173. +    const char *tok_src;
  174. +    bool free_tok = false;
  175. +
  176. +    if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &tok)) {
  177. +        tok_src = "impersonated_user";
  178. +        free_tok = true;
  179. +    }
  180. +    else {
  181. +        int lasterr = GetLastError();
  182. +        if (lasterr == ERROR_CANT_OPEN_ANONYMOUS) {
  183. +            tok_src = "anon_user";
  184. +        }
  185. +        else {
  186. +            tok_src = "proc_user";
  187. +        }
  188. +
  189. +        tok = GetCurrentProcessToken();
  190. +    }
  191. +
  192. +#pragma warning( push )
  193. +    /*
  194. +     * Disable "'strcpy': This function or variable may be unsafe",
  195. +     * in this context it is safe to use
  196. +     */
  197. +#pragma warning (disable : 4996)
  198. +    if (!get_token_user_name(tok, username)) {
  199. +        (void)strcpy(username, "<unknown>");
  200. +    }
  201. +    if (!get_token_primarygroup_name(tok, groupname)) {
  202. +        (void)strcpy(groupname, "<unknown>");
  203. +    }
  204. +#pragma warning( pop )
  205. +#endif /* NP_PRINT_TOKEN_USER */
  206.  
  207.      va_list marker;
  208.      va_start(marker, fmt);
  209. @@ -61,7 +176,12 @@ ULONG _cdecl NFS41DbgPrint(__in LPTSTR fmt, ...)
  210.       * in this context it is safe to use
  211.       */
  212.  #pragma warning (disable : 4996)
  213. +#ifdef NP_PRINT_TOKEN_USER
  214. +    (void)swprintf(szbp, SZBUFFER_SIZE, L"%s%S='%S'/'%S': ",
  215. +        TRACE_TAG, tok_src, username, groupname);
  216. +#else
  217.      (void)wcscpy(szbp, TRACE_TAG);
  218. +#endif
  219.  #pragma warning( pop )
  220.      szbp += wcslen(szbp);
  221.  
  222. @@ -72,6 +192,12 @@ ULONG _cdecl NFS41DbgPrint(__in LPTSTR fmt, ...)
  223.  
  224.      va_end(marker);
  225.  
  226. +#ifdef NP_PRINT_TOKEN_USER
  227. +    if (free_tok) {
  228. +        (void)CloseHandle(tok);
  229. +    }
  230. +#endif /* NP_PRINT_TOKEN_USER */
  231. +
  232.      return rc;
  233.  }
  234.  
  235. diff --git a/mount/mount.c b/mount/mount.c
  236. index d480d05..16efe44 100644
  237. --- a/mount/mount.c
  238. +++ b/mount/mount.c
  239. @@ -612,6 +612,44 @@ static DWORD DoMount(
  240.      TCHAR szRemoteName[NFS41_SYS_MAX_PATH_LEN];
  241.      DWORD dwLength;
  242.  
  243. +#if 1
  244. +    HANDLE process_handle;
  245. +    HANDLE proc_tok;
  246. +    HANDLE thr_tok;
  247. +
  248. +    process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
  249. +    if (process_handle == NULL) {
  250. +        (void)_tprintf(TEXT("OpenProcess() failed, status=%d\n"),
  251. +            GetLastError());
  252. +        exit(1);
  253. +    }
  254. +    if (!OpenProcessToken(process_handle, TOKEN_IMPERSONATE|TOKEN_DUPLICATE, &proc_tok)) {
  255. +        (void)_tprintf(TEXT("OpenProcessToken() failed, status=%d\n"),
  256. +            GetLastError());
  257. +        exit(1);
  258. +    }
  259. +
  260. +    if (!DuplicateTokenEx(proc_tok,
  261. +        MAXIMUM_ALLOWED/*TOKEN_DUPLICATE|TOKEN_IMPERSONATE*/,
  262. +        NULL,
  263. +        SecurityDelegation,
  264. +        TokenImpersonation,
  265. +        &thr_tok)) {
  266. +        (void)_tprintf(TEXT("DuplicateTokenEx() failed, status=%d\n"),
  267. +            GetLastError());
  268. +        exit(1);
  269. +    }
  270. +    
  271. +    if (!SetThreadToken(NULL, thr_tok)) {
  272. +        (void)_tprintf(TEXT("SetThreadToken() failed, status=%d\n"),
  273. +            GetLastError());
  274. +        exit(1);
  275. +    }
  276. +#endif
  277. +
  278. +    (void)_tprintf(TEXT("MARK\n"));
  279. +//    exit(2);
  280. +
  281.      *szRemoteName = TEXT('\0');
  282.      result = ParseRemoteName(pRemoteName, pOptions, szParsedRemoteName, szRemoteName, NFS41_SYS_MAX_PATH_LEN);
  283.      if (result)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at