You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Process] Start process as non-elevated user #128378
Today I've been studying some MSFT source code and I got to the conclusion that we most likely need to add the ability to start process as non-elevated user when running as elevated user.
One way of doing that on Windows would be to use following sys-calls:
On Unix, you can run as a different user through ProcessStartInfo.UserName.
If you elevate through something like sudo,getuid does not return the original user (the original user is available via the SUDO_USER environment variable). getuid would only be useful for +s binaries. Those are typically very minimal, security-sensitive apps.
I do not think we should be adding this as an API.
I think we should focus on better interop to address these scenarios. There are many ways to launch a process in custom way on Windows. What's required to allow people to launch a process in a custom way using these Safer APIs and still allow them to take advantage of the goodness in the managed process type like the output streaming, etc.?
The related use case I've seen is mostly about UAC. Specifically, launch a process (often self) with UAC prompt, and launch non UAC-elevated process from elevated one.
I do not think we should be adding this as an API.
I think we should focus on better interop to address these scenarios. There are many ways to launch a process in custom way on Windows. What's required to allow people to launch a process in a custom way using these Safer APIs and still allow them to take advantage of the goodness in the managed process type like the output streaming, etc.?
I don't think I follow you. This can be said about basically anything because P/Invoke exists.
The problem is that the moment you have a need to use one of the options that are not exposed as a managed API on System.Diagnostics.Process, you have to roll your own implementation from scratch. For example, let's say that I want to launch a process with a custom PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES and consume its output using IAsyncEnumerable<System.Diagnostics.ProcessOutputLine>. How do I do that?
Currently, I have to copy & paste thousands (?) of lines of System.Diagnostics.Process implementation. Can we do better than that? It is what I meant by a better interop.
My preference would be for #94127 to be handled by the "better interop" solution as well.
I do not think we want to introduce a first-class managed APIs on System.Diagnostics.Process for all of them.
I agree that we should not be doing this for all of them, but there are some that are quite frequently implemented by our customers and foremost hard to get right. And in my opinion we should be recognizing such patterns and exposing them as first-class managed APIs. For now I am going to change the milestone to Future and wait for more feedback from our security champions (@GrabYourPitchforks@vcsjones).
Currently, I have to copy & paste thousands (?) of lines of System.Diagnostics.Process implementation. Can we do better than that? It is what I meant by a better interop.
The async implementation is very simple, so it would be less than a hundred lines of code for ReadAllLinesAsync.
But I agree that we need a better interop story and also implement #71595 (I've just set the milestone to 11 to ensure it happens).
My preference would be for #94127 to be handled by the "better interop" solution as well.
We could expose the ability to pass the creation flags (#71515), but in case of CREATE_SUSPENDED we also need to be able the resume the process. And for that I am afraid we have to store the main thread handle.
On Unix, fork+exec always require the users to provide a native library to be able to customize process creation. This is really hard to get right. Right now I see two ways to improve it:
implement an ability to specify a custom unmanaged delegate (I am sorry I might be using the wrong naming). The downside is that users would still need to ship custom native library with their app. We have discussed it offline in the past.
implement StartSuspended and Resume and let the users run custom logic for the suspended process. The downside is that the users can use only sys-calls that accept pid to set something:
there are some that are quite frequently implemented by our customers and foremost hard to get right. And in my opinion we should be recognizing such patterns and exposing them as first-class managed APIs.
It depends where you set the bar for frequently implemented. I think we are in the long tail where you can find some hits for each of the missing niche options when you do deep enough search. I doubt that any of these niche options are used in 100 or more distinct .NET libraries or projects (arbitrary bar for frequently implemented).
The async implementation is very simple, so it would be less than a hundred lines of code for ReadAllLinesAsync.
I think ReadAllLinesAsync is smaller part of the problem. I would expect most of the lines to be in the rest of the scaffolding. Here is a more precise spec for my motivating example:
// I want to pass `PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES` to Windows CreateProcess API and otherwise do exactly// what regular Process.Start does.usingProcessprocess=Process.Start(newProcessStartInfo("myapp.exe")){RedirectStandardOutput=true,RedirectStandardError=true})!;awaitforeach(ProcessOutputLinelineinprocess.ReadAllLinesAsync()){...line.Content...}
It may be insightful to actually produce a working code for this.
implement StartSuspended and Resume and let the users run custom logic for the suspended process. The downside is that the users can use only sys-calls that accept pid to set something
It depends where you set the bar for frequently implemented.
I agree, the problem is that it's very subjective.
It may be insightful to actually produce a working code for this.
I'll do some prototyping and get back to you.
Also, the suspend API is proposed as windows and macOS only currently, so it would not work on Linux where it is perhaps most interesting.
There is nothing that stops us from implementing it on Linux, it's just a bit more complicated. And I've not included this in the initial proposal as in the past I've received pushback that claimed that starting a suspended process is a Windows-only concept.
Activity
dotnet-policy-service commentedon May 19, 2026
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.
adamsitnik commentedon May 19, 2026
On Unix, we would need to obtain the real uid/gid and also restore the groups:
adamsitnik commentedon May 19, 2026
cc @jkotas @tmds
tmds commentedon May 19, 2026
On Unix, you can run as a different user through
ProcessStartInfo.UserName.If you elevate through something like
sudo,getuiddoes not return the original user (the original user is available via theSUDO_USERenvironment variable).getuidwould only be useful for+sbinaries. Those are typically very minimal, security-sensitive apps.jkotas commentedon May 19, 2026
I do not think we should be adding this as an API.
I think we should focus on better interop to address these scenarios. There are many ways to launch a process in custom way on Windows. What's required to allow people to launch a process in a custom way using these Safer APIs and still allow them to take advantage of the goodness in the managed process type like the output streaming, etc.?
huoyaoyuan commentedon May 20, 2026
The related use case I've seen is mostly about UAC. Specifically, launch a process (often self) with UAC prompt, and launch non UAC-elevated process from elevated one.
ZzZombo commentedon May 20, 2026
I don't think I follow you. This can be said about basically anything because P/Invoke exists.
jkotas commentedon May 20, 2026
Windows exposes many different options for launching new processes. For example, https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute lists 14 different options and it is not a complete list. My estimate is that we can find 30+ options for customizing process launch on Windows. I do not think we want to introduce a first-class managed APIs on System.Diagnostics.Process for all of them.
The problem is that the moment you have a need to use one of the options that are not exposed as a managed API on System.Diagnostics.Process, you have to roll your own implementation from scratch. For example, let's say that I want to launch a process with a custom
PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIESand consume its output usingIAsyncEnumerable<System.Diagnostics.ProcessOutputLine>. How do I do that?Currently, I have to copy & paste thousands (?) of lines of System.Diagnostics.Process implementation. Can we do better than that? It is what I meant by a better interop.
My preference would be for #94127 to be handled by the "better interop" solution as well.
adamsitnik commentedon May 21, 2026
I agree that we should not be doing this for all of them, but there are some that are quite frequently implemented by our customers and foremost hard to get right. And in my opinion we should be recognizing such patterns and exposing them as first-class managed APIs. For now I am going to change the milestone to Future and wait for more feedback from our security champions (@GrabYourPitchforks @vcsjones).
adamsitnik commentedon May 21, 2026
The async implementation is very simple, so it would be less than a hundred lines of code for
ReadAllLinesAsync.But I agree that we need a better interop story and also implement #71595 (I've just set the milestone to 11 to ensure it happens).
We could expose the ability to pass the creation flags (#71515), but in case of
CREATE_SUSPENDEDwe also need to be able the resume the process. And for that I am afraid we have to store the main thread handle.On Unix, fork+exec always require the users to provide a native library to be able to customize process creation. This is really hard to get right. Right now I see two ways to improve it:
StartSuspendedandResumeand let the users run custom logic for the suspended process. The downside is that the users can use only sys-calls that acceptpidto set something:tmds commentedon May 21, 2026
To make it more clear: my earlier comment was meant to say that I don't think we should enable/encourage implementing setuid binaries using .NET.
I have no opinion on the Windows side.
jkotas commentedon May 21, 2026
It depends where you set the bar for frequently implemented. I think we are in the long tail where you can find some hits for each of the missing niche options when you do deep enough search. I doubt that any of these niche options are used in 100 or more distinct .NET libraries or projects (arbitrary bar for frequently implemented).
I think ReadAllLinesAsync is smaller part of the problem. I would expect most of the lines to be in the rest of the scaffolding. Here is a more precise spec for my motivating example:
It may be insightful to actually produce a working code for this.
Yep, example of the scenario close to home that it does not solve: https://github.com/mitchdenny/hex1b/blob/ccbaf06159f1b397c605be552c879e929aa4eb19/src/Hex1b/native/hex1binterop.c#L50
Also, the suspend API is proposed as windows and macOS only currently, so it would not work on Linux where it is perhaps most interesting.
adamsitnik commentedon May 22, 2026
I agree, the problem is that it's very subjective.
I'll do some prototyping and get back to you.
There is nothing that stops us from implementing it on Linux, it's just a bit more complicated. And I've not included this in the initial proposal as in the past I've received pushback that claimed that starting a suspended process is a Windows-only concept.