I have a situation in which I intend to communicate with a service through a command interface made available via a UNIX-domain socket on the file system. I am able to successfully send it commands, but for a while sat perplexed as to why I could not receive any response to my queries.
As it turns out, the service did not have sufficient permissions to write to the address I (or the OS) provided for it. However, I realized that if I explicitly bind
to an address on the file system then I could adjust the file permissions by leveraging chmod
.
Something like:
int mySocket;
struct sockaddr_un local_addr;
mySocket = socket(AF_UNIX, SOCK_DGRAM, 0);
local_addr.sun_family = AF_UNIX;
snprintf(local_addr.sun_path, 108 "/path/to/mySocket");
bind(mySocket, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_un));
chmod("/path/to/mySocket", 777);
That is to say, without the final chmod
step, the service is unable to write to mySocket
because it does not have the appropriate write permissions. Obviously, this is an even harder problem to spot if one does not explicitly bind
to a specific address, since the underlying OS will implicitly generate this socket for the user - but it still exists and still will have the same access problems.
My question, then, is with respect to this final step. Is there a way to allow the OS to implicitly generate the socket for my endpoint (i.e. the address to which the service will respond) but request that it be given certain permissions?
The Explanation
The reason this issue is becoming a problem is due to the requirement that other portions of the program be executed as root. As such, when I, as root, attempt to connect
/send
to the background service, the OS will implicitly create an address to which replies will be directed. However, this leads to the problem that my socket-file, whether implicit or created with bind
, will have permissions like srw- --- ---
, so the other endpoint can only reply if they, too, elevate themselves.
Thus, the problem goes away if I first bind
and then chmod
the permissions as I showed above.