Protect API socket from unauthorized access

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2016-10-24 00:02:19 +02:00
1 parent c3b7530b98
commit 1b6931eaea
1 file changed
+4
+4
View File
@@ -344,6 +344,7 @@ leave:
int api_init(uev_ctx_t *ctx) int api_init(uev_ctx_t *ctx)
{ {
int sd; int sd;
mode_t oldmask;
struct sockaddr_un sun = { struct sockaddr_un sun = {
.sun_family = AF_UNIX, .sun_family = AF_UNIX,
.sun_path = INIT_SOCKET, .sun_path = INIT_SOCKET,
@@ -356,17 +357,20 @@ int api_init(uev_ctx_t *ctx)
} }
erase(INIT_SOCKET); erase(INIT_SOCKET);
oldmask = umask(0077);
if (-1 == bind(sd, (struct sockaddr*)&sun, sizeof(sun))) if (-1 == bind(sd, (struct sockaddr*)&sun, sizeof(sun)))
goto error; goto error;
if (-1 == listen(sd, 10)) if (-1 == listen(sd, 10))
goto error; goto error;
umask(oldmask);
if (!uev_io_init(ctx, &api_watcher, cb, NULL, sd, UEV_READ)) if (!uev_io_init(ctx, &api_watcher, cb, NULL, sd, UEV_READ))
return 0; return 0;
error: error:
_pe("Failed intializing API socket"); _pe("Failed intializing API socket");
umask(oldmask);
close(sd); close(sd);
return 1; return 1;
} }