pastebin - collaborative debugging tool
rovema.kpaste.net RSS


msnfs41client: Patches for impersonation logging and Win32 privileges, 2024-04-24
Posted by Anonymous on Wed 24th Apr 2024 15:55
raw | new post

  1. From 753a66deb92a2b12e0dc1d68889344325207ceb4 Mon Sep 17 00:00:00 2001
  2. From: Roland Mainz <roland.mainz@nrubsig.org>
  3. Date: Wed, 24 Apr 2024 16:35:26 +0200
  4. Subject: [PATCH 1/2] daemon: |logprintf()| should print src of token
  5.  user/group info
  6.  
  7. |logprintf()| should print te source of token user/group info, i.e.
  8. whether this is an impersonation token, anon token or a process token.
  9.  
  10. Signed-off-by: Cedric Blancher <cedric.blancher@gmail.com>
  11. ---
  12. daemon/daemon_debug.c | 33 ++++++++++++++++++++++++++++-----
  13.  1 file changed, 28 insertions(+), 5 deletions(-)
  14.  
  15. diff --git a/daemon/daemon_debug.c b/daemon/daemon_debug.c
  16. index b35bfee..54fb411 100644
  17. --- a/daemon/daemon_debug.c
  18. +++ b/daemon/daemon_debug.c
  19. @@ -90,14 +90,32 @@ void logprintf(LPCSTR format, ...)
  20.      SYSTEMTIME stime;
  21.      char username[UNLEN+1];
  22.      char groupname[GNLEN+1];
  23. +    HANDLE tok;
  24. +    const char *tok_src;
  25. +    bool free_tok = false;
  26.  
  27.      GetLocalTime(&stime);
  28. -    if (!get_token_user_name(GetCurrentThreadEffectiveToken(),
  29. -        username)) {
  30. +
  31. +    if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &tok)) {
  32. +        tok_src = "impersonated_user";
  33. +        free_tok = true;
  34. +    }
  35. +    else {
  36. +        int lasterr = GetLastError();
  37. +        if (lasterr == ERROR_CANT_OPEN_ANONYMOUS) {
  38. +            tok_src = "anon_user";
  39. +        }
  40. +        else {
  41. +            tok_src = "proc_user";
  42. +        }
  43. +
  44. +        tok = GetCurrentProcessToken();
  45. +    }
  46. +
  47. +    if (!get_token_user_name(tok, username)) {
  48.          (void)strcpy(username, "<unknown>");
  49.      }
  50. -    if (!get_token_primarygroup_name(GetCurrentThreadEffectiveToken(),
  51. -        groupname)) {
  52. +    if (!get_token_primarygroup_name(tok, groupname)) {
  53.          (void)strcpy(groupname, "<unknown>");
  54.      }
  55.  
  56. @@ -105,15 +123,20 @@ void logprintf(LPCSTR format, ...)
  57.      va_start(args, format);
  58.      (void)fprintf(dlog_file,
  59.          "# LOG: ts=%04d-%02d-%02d_%02d:%02d:%02d:%04d"
  60. -        " thr=%04x user='%s'/'%s' msg=",
  61. +        " thr=%04x %s='%s'/'%s' msg=",
  62.          (int)stime.wYear, (int)stime.wMonth, (int)stime.wDay,
  63.          (int)stime.wHour, (int)stime.wMinute, (int)stime.wSecond,
  64.          (int)stime.wMilliseconds,
  65.          (int)GetCurrentThreadId(),
  66. +        tok_src,
  67.          username, groupname);
  68.      (void)vfprintf(dlog_file, format, args);
  69.      (void)fflush(dlog_file);
  70.      va_end(args);
  71. +
  72. +    if (free_tok) {
  73. +        (void)CloseHandle(tok);
  74. +    }
  75.  }
  76.  
  77.  void eprintf(LPCSTR format, ...)
  78. --
  79. 2.43.0
  80.  
  81. From a1b83f0e1845fa18710c9561ca92c404c2eede07 Mon Sep 17 00:00:00 2001
  82. From: Roland Mainz <roland.mainz@nrubsig.org>
  83. Date: Wed, 24 Apr 2024 16:37:17 +0200
  84. Subject: [PATCH 2/2] daemon: Enable Win32 priviledges for impersonation
  85.  
  86. Enable Win32 priviledges "SeImpersonatePrivilege" and
  87. "SeDelegateSessionUserImpersonatePrivilege" in case Admins or site
  88. policy have disabled them by default.
  89.  
  90. Signed-off-by: Cedric Blancher <cedric.blancher@gmail.com>
  91. ---
  92. daemon/nfs41_daemon.c | 26 ++++++++++++++++++++++++++
  93.  daemon/util.c         | 40 ++++++++++++++++++++++++++++++++++++++++
  94.  daemon/util.h         |  1 +
  95.  3 files changed, 67 insertions(+)
  96.  
  97. diff --git a/daemon/nfs41_daemon.c b/daemon/nfs41_daemon.c
  98. index d63b65f..78393be 100644
  99. --- a/daemon/nfs41_daemon.c
  100. +++ b/daemon/nfs41_daemon.c
  101. @@ -655,6 +655,29 @@ void init_version_string(void)
  102.          nfs41_dg.nfs41_nii_name));
  103.  }
  104.  
  105. +static
  106. +void set_nfs_daemon_privileges(void)
  107. +{
  108. +    HANDLE proc_token;
  109. +
  110. +    DPRINTF(0, ("Enabling priviledges...\n"));
  111. +
  112. +    if (!OpenProcessToken(GetCurrentProcess(),
  113. +        TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &proc_token)) {
  114. +        eprintf("set_nfs_daemon_privileges: "
  115. +            "cannot open process token\n");
  116. +        exit(1);
  117. +    }
  118. +
  119. +    (void)set_token_privilege(proc_token,
  120. +        "SeImpersonatePrivilege", true);
  121. +    (void)set_token_privilege(proc_token,
  122. +        "SeDelegateSessionUserImpersonatePrivilege", true);
  123. +
  124. +    (void)CloseHandle(proc_token);
  125. +}
  126. +
  127. +
  128.  #ifdef STANDALONE_NFSD
  129.  void __cdecl _tmain(int argc, TCHAR *argv[])
  130.  #else
  131. @@ -685,6 +708,9 @@ VOID ServiceStart(DWORD argc, LPTSTR *argv)
  132.  
  133.      logprintf("NFS client daemon starting...\n");
  134.  
  135. +    /* Enable Win32 privileges */
  136. +    set_nfs_daemon_privileges();
  137. +
  138.      /* acquire and store in global memory current dns domain name.
  139.       * needed for acls */
  140.      if (getdomainname()) {
  141. diff --git a/daemon/util.c b/daemon/util.c
  142. index c9c3310..ba753d9 100644
  143. --- a/daemon/util.c
  144. +++ b/daemon/util.c
  145. @@ -798,3 +798,43 @@ bool get_token_primarygroup_name(HANDLE tok, char *out_buffer)
  146.  
  147.      return true;
  148.  }
  149. +
  150. +bool set_token_privilege(HANDLE tok, const char *seprivname, bool enable_priv)
  151. +{
  152. +    TOKEN_PRIVILEGES tp;
  153. +    LUID luid;
  154. +    bool res;
  155. +
  156. +    if(!LookupPrivilegeValueA(NULL, seprivname, &luid)) {
  157. +        DPRINTF(1, ("set_token_privilege: "
  158. +            "LookupPrivilegeValue(seprivname='%s') failed, "
  159. +            "status=%d\n",
  160. +            seprivname,
  161. +            (int)GetLastError()));
  162. +        res = false;
  163. +        goto out;
  164. +    }
  165. +
  166. +    tp.PrivilegeCount = 1;
  167. +    tp.Privileges[0].Luid = luid;
  168. +    tp.Privileges[0].Attributes = enable_priv?(SE_PRIVILEGE_ENABLED):0;
  169. +
  170. +    if(!AdjustTokenPrivileges(tok,
  171. +        FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
  172. +        NULL, NULL)) {
  173. +        DPRINTF(1, ("set_token_privilege: "
  174. +            "AdjustTokenPrivileges() for '%s' failed, status=%d\n",
  175. +            seprivname,
  176. +            (int)GetLastError()));
  177. +        res = false;
  178. +        goto out;
  179. +    }
  180. +
  181. +    res = true;
  182. +out:
  183. +    DPRINTF(0,
  184. +        ("set_token_privilege(seprivname='%s',enable_priv=%d), res=%d\n",
  185. +        seprivname, (int)enable_priv, (int)res));
  186. +
  187. +    return res;
  188. +}
  189. diff --git a/daemon/util.h b/daemon/util.h
  190. index a09df70..899711d 100644
  191. --- a/daemon/util.h
  192. +++ b/daemon/util.h
  193. @@ -286,6 +286,7 @@ bool getwinntversionnnumbers(DWORD *MajorVersionPtr, DWORD *MinorVersionPtr, DWO
  194.  
  195.  bool get_token_user_name(HANDLE tok, char *out_buffer);
  196.  bool get_token_primarygroup_name(HANDLE tok, char *out_buffer);
  197. +bool set_token_privilege(HANDLE tok, const char *seprivname, bool enable_priv);
  198.  
  199.  
  200.  #endif /* !__NFS41_DAEMON_UTIL_H__ */
  201. --
  202. 2.43.0

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