file permissions - How to securely set the group ownership of a unix domain socket? -
i want create unix domain socket restricted particular group. i'd ideally (ignoring error checking) like:
// set "address" (ie filesystem path) struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = af_unix; strcpy(addr.sun_path, "./my.sock"); int fd = socket(af_unix, sock_dgram, 0); // create socket // set group owner , permissions fchmod(fd, 0770); // seems succeed fchown(fd, -1, wanted_group_id); // silently fails // create filesystem entry bind(fd, (struct sockaddr *)&addr, sizeof(addr));
however, fchown
on socket has no effect, seems chown
afterwards way set group. want avoid having socket temporarily accessible processes shouldn't permitted access it.
the best idea have is:
int fd = socket(...); fchmod(fd, 0700); // remove group permissions bind(fd, ...); // create fs entry chown("./my.sock", -1, wanted_group_id); // set correct group owner fchmod(fd, 0770); // , restore group permissions
surely common thing want unix sockets, , there's canonical way of achieving this, haven't found clear answers.
i'm interested in linux, bonus points relies on posix.
the way put socket directory correct permissions. directories can created (or renamed place) atomically, , once directory present permissions on socket not important. works on unixes permissions on socket aren't honoured.
Comments
Post a Comment