Re: [PATCH v4 09/12] unix-socket: disallow chdir() when creating unix domain sockets
- From
Jeff King <peff@peff.net>
- Date
- Mar 5, 2021, 09:25 UTC
- Message-ID
- <YEH5AUxgFxWTxb6u@coredump.intra.peff.net>
- In-Reply-To
- <YEHzmIOYgRtI1Ak1@coredump.intra.peff.net>
On Fri, Mar 05, 2021 at 04:02:16AM -0500, Jeff King wrote:
Show 8 quoted lines
> I don't know offhand if this trick actually works. I can imagine it > does, but it hinges on the subtlety between an integer descriptor and > the underlying "file description" (the term used in POSIX). Does binding > a socket operate on the former (like close() does not close the parent's > descriptor) or the latter (like lseek() impacts other descriptors). > > I'd guess the latter, but I wasn't sure if you were suggesting this from > experience or if you just invented the technique. ;)
I was curious, but this does indeed work:
-- >8 -- #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h>
int main(void)
{
int listen_fd, client_fd;
struct addrinfo *ai;
pid_t pid; getaddrinfo("127.0.0.1", "1234", NULL, &ai);
listen_fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
pid = fork();
if (!pid) {
bind(listen_fd, ai->ai_addr, ai->ai_addrlen);
return 0;
}
waitpid(pid, NULL, 0);listen(listen_fd, 5); client_fd = accept(listen_fd, NULL, NULL); write(client_fd, "foo\n", 4); return 0; } -- >8 --
-Peff