CreateProcess is not inheriting security descriptor

From: hammett (dashhammett@hotmail.com)
Date: 08/06/02


From: "hammett" <dashhammett@hotmail.com>
Date: Tue, 6 Aug 2002 15:48:57 -0300


I have a web service that call unmanaged CreateProcess(). The problem is
that CreateProcess() runs in behalf of an different, and more restrictive,
user account.
I used the following code to find out wich user is executing the code:

WindowsIdentity mWI1 = WindowsIdentity.GetCurrent();
String name = mWI1.Name;
IntPtr token = mWI1.Token;

It is using the administrator account, as I specified. Then I call
CreateProcess. The declaration follows:

  public static Process CreateProcess(Win32Handle token, String name, String
cmdLine, String Environment, String curDirectory, CreationFlags
creationFlags)
  {
   StartupInfo stupInfo = new StartupInfo();
   ProcessInfo prInfo = new ProcessInfo();
   Int32 CreationFlag = Int32.Parse(creationFlags.ToString("D"));

   stupInfo.cb = Marshal.SizeOf(stupInfo);
   Int32 retcode = 0;

   SecurityAttributes SecAttrs = new SecurityAttributes();
   SecAttrs.nLen = (UInt32) Marshal.SizeOf(typeof(SecurityAttributes));

   if (token == null)
   {
    retcode = CreateProcessW(name, cmdLine,
     ref SecAttrs, ref SecAttrs,
     1, CreationFlag, IntPtr.Zero,
     curDirectory,
     stupInfo, prInfo);
   }
   else
   {
    retcode = CreateProcessAsUserW(token.ToIntPtr(), name, cmdLine,
     ref SecAttrs, ref SecAttrs,
     1, CreationFlag, IntPtr.Zero,
     curDirectory,
     stupInfo, prInfo);
   }

   if (retcode == 0)
   {
    Int32 errCode = Marshal.GetLastWin32Error();
    throw new Exception( String.Format("Could not start process. Last error
{0}", errCode) );
   }

   return new Process(prInfo);
  }

And the p/invoke is like this:

  [Flags]
  public enum CreationFlags
  {
   CREATE_NEW_CONSOLE = 0x00000010,
   CREATE_SUSPENDED = 0x00000004,
   CREATE_NEW_PROCESS_GROUP = 0x00000200,
   CREATE_UNICODE_ENVIRONMENT = 0x00000400,
   CREATE_SEPARATE_WOW_VDM = 0x00000800,
   CREATE_SHARED_WOW_VDM = 0x00001000,
   CREATE_FORCEDOS = 0x00002000,
   CREATE_DEFAULT_ERROR_MODE = 0x04000000,
   CREATE_NO_WINDOW = 0x08000000,
   DEBUG_PROCESS = 0x00000001,
   DEBUG_ONLY_THIS_PROCESS = 0x00000002,
   DETACHED_PROCESS = 0x00000008,

   NORMAL_PRIORITY_CLASS = 0x00000020,
   IDLE_PRIORITY_CLASS = 0x00000040,
   HIGH_PRIORITY_CLASS = 0x00000080,
   REALTIME_PRIORITY_CLASS = 0x00000100
  }

  [StructLayout( LayoutKind.Sequential, CharSet=CharSet.Unicode )]
  protected class SecurityAttributes
  {
   public UInt32 nLen;
   // [ MarshalAs( UnmanagedType.CustomMarshaler,
MarshalTypeRef=typeof(void) )]
   public IntPtr lpSecDesc = IntPtr.Zero;
   public Int32 bInheritHandle = 1;
  }

  [StructLayout( LayoutKind.Sequential, CharSet=CharSet.Unicode )]
  protected class StartupInfo
  {
   public Int32 cb = 0;
   public String lpReserved;
   public String lpDesktop;
   public String lpTitle;
   public Int32 dwX = 0;
   public Int32 dwY = 0;
   public Int32 dwXSize = 0;
   public Int32 dwYSize = 0;
   public Int32 dwXCountChars = 0;
   public Int32 dwYCountChars = 0;
   public Int32 dwFillAttribute = 0;
   public Int32 dwFlags = 0;
   public Int16 wShowWindow = 0;
   public Int16 cbReserved2 = 0;
   public Int32 lpReserved2 = 0;
   public Int32 hStdInput = 0;
   public Int32 hStdOutput = 0;
   public Int32 hStdError = 0;
  }

  [StructLayout( LayoutKind.Sequential, CharSet=CharSet.Unicode )]
  protected class ProcessInfo
  {
   public IntPtr hProcess;
   public IntPtr hThread;
   public Int32 dwProcessId;
   public Int32 dwThreadId;
  }

  [DllImport("Advapi32.dll", EntryPoint="CreateProcessAsUserW",
CharSet=CharSet.Unicode, SetLastError=true)]
  protected static extern Int32 CreateProcessAsUserW(IntPtr handle, String
appName, String cmdLine,
   ref SecurityAttributes lpProcessAttrs, ref SecurityAttributes
lpThreadAttrs,
   Int32 bInheritHandles,
   Int32 dwCreatingFlags, IntPtr lpEnvironment, String curDir,
   StartupInfo info, [Out] ProcessInfo pinfo);

  [DllImport("kernel32.dll", EntryPoint="CreateProcessW",
CharSet=CharSet.Unicode, SetLastError=true)]
  protected static extern Int32 CreateProcessW(String appName, String
cmdLine,
   ref SecurityAttributes lpProcessAttrs, ref SecurityAttributes
lpThreadAttrs,
   Int32 bInheritHandles,
   Int32 dwCreatingFlags, IntPtr lpEnvironment, String curDir,
   StartupInfo info, [Out] ProcessInfo pinfo);



Relevant Pages

  • Re: Migrating from VB6 and Printer object
    ... You can use the GetPrinter API call to get all the information (such as Port name) - e.g. ... ByRef phPrinter As Int32, _ ... Public pServerName As String ... Dim dmOut As New DEVMODE ...
    (microsoft.public.dotnet.languages.vb)
  • Not enough storage is available to process this command.
    ... String schemaClassName) ... identityType, String userName, SecureString password, TimeSpan ... idleTimeout, TimeSpan periodicRestartTime) ... oProvisionIisApplicationPool(String name, Int32 identityType, String ...
    (microsoft.public.sharepoint.portalserver)
  • Load registry hive (AdjustTokenPrivileges error)
    ... Public PrivilegeCount As Int32 ... Public Function RegLoadKey(ByVal hKey As Int32, ... String, ByVal lpFile As String) As Int32 ... Dim strKeyName As String ...
    (microsoft.public.vb.winapi)
  • Load registry hive (AdjustTokenPrivileges error)
    ... Public PrivilegeCount As Int32 ... Public Function RegLoadKey(ByVal hKey As Int32, ... String, ByVal lpFile As String) As Int32 ... Dim strKeyName As String ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Run app as specified user from ASP.NET 2.0
    ... const int SecurityImpersonation = 2; ... string commandLinePath; ... public uint dwX; ... public IntPtr lpReserved2; ...
    (microsoft.public.dotnet.framework.aspnet)

Loading