
If you use fchown(sock->fd) then you avoid any possible race issues.
Except that it doesn't work. That was the first thing I tried but fchown() doesn't seem to work on unix sockets. The socket will still ended up with root:root ownership regardless on where I put fchown() -- either before bind() to avoid race issues or after it, which wouldn't be any better than chown().
POSIX states that fchown() on pipes and sockets is allowed (but not required) to fail with EINVAL. I think it's a POSIX-compliance bug in the Linux kernel that it silently succeeds but ignores the change request, but to be truly portable, we have to use chown() rather than fchown() to avoid falling foul of the undefined behavior in the first place (whether or not we can convince kernel folks to either make fchown() fail with EINVAL or succeed at doing what we want).
So, I don't see any other alternatives, and your patch looks like the way to go. ACK as-is.
Thanks, pushed. Jirka