An application I'm writing requires the execution of potentially malicious code to be executed on a host system. The code only interacts with stdin
, stdout
, and stderr
, and should not attempt to interact with the filesystem or network.
I've restricted network access through a firewall rule, and filesystem access through running the process as an unprivileged user created through NetUserAdd
with CreateProcessWithLogonW
. Finally, I assign the process to a job object that limits memory and active processes.
This works fine on Windows 8, but when I tested it on a Windows 7 machine (the deployment platform), I found that AssignProcessToJobObject
failed with an access denied, despite running as administrator. From this answer, I found that
CreateProcessWithLogonW executes the new process as a child of the Secondary Logon service, which has the outcome of making the process escape any Job Object membership/restrictions even if the Job Object did not allow breakaway.
Furthermore, the Secondary Logon service automatically creates its own new Job Object and assigns the new process into it.
So while this works on Windows 8 which allows nested job objects, it fails on Windows 7 and under.
The same answer suggests spawning an agent process under the Secondary Logon service and using it to spawn the process with the CREATE_BREAKAWAY_FROM_JOB
flag. However, when attempting this, the agent's CreateProcess
call fails with 5 ERROR_ACCESS_DENIED
, because the job Secondary Logon puts the agent in does not allow breakaways.
How can I assign a process created under another user to a job object on Windows 7?