I'm writing a daemon which needs to accept unnamed pipe connections identified by a PID from potentially untrusted applications. With Unix domain sockets, this is not a complex task: create a socket on the filesystem, start listening, accept incoming connections, receive the file descriptors for the reading end of the unnamed pipes via ancillary data, make sure that the application sending the file descriptor is what it pretends to be (only the PID is needed to figure out the rest, which is handy), then start receiving data from the application using the private unnamed pipe connection while still listening to the socket for new connections.
On Windows, however, the only IPC method which can be recognizable by third-party applications (and thus can be used for establishing connections via other methods of IPC without having a parent-child relationship) is named pipes (known in Unix terminology as FIFO files). Thanks to DuplicateHandle()
, there's no need to use some kind of ancillary data transmission system to get a file descriptor from an application any process can send any of its handles to any other process, provided that the target process knows the handle value via some form of IPC, which is, in my case, a named pipe. However, a named pipe does not identify writers, i.e. if an application writes a handle x
there and pretends to have a certain PID a
, there's no way to know that whoever is writing on the unnamed pipe on the handle x
is actually the process with PID a
and not PID b
trying to mess up records management in the daemon.
TL;DR how can I create an unnamed pipe connection between a daemon and a non-child process on Windows and be absolutely sure that the pipe handle sent by that process indeed belongs to it and not to an imposter process?