fix: harden the per-user IPC endpoint
This commit is contained in:
46
ipc.c
46
ipc.c
@@ -1,7 +1,10 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include "ipc.h"
|
||||
|
||||
#if !defined(MSG_NOSIGNAL) && !defined(SO_NOSIGPIPE)
|
||||
@@ -21,6 +24,49 @@ getlen(const unsigned char p[Ipclensz])
|
||||
return p[0] | (p[1] << 8);
|
||||
}
|
||||
|
||||
int
|
||||
ipcpath(char *dst, size_t cap)
|
||||
{
|
||||
const char *dir, *sep;
|
||||
int n;
|
||||
|
||||
if(dst == NULL || cap == 0)
|
||||
return -1;
|
||||
dir = getenv("XDG_RUNTIME_DIR");
|
||||
if(dir != NULL && dir[0] == '/'){
|
||||
sep = dir[strlen(dir)-1] == '/' ? "" : "/";
|
||||
n = snprintf(dst, cap, "%s%sstrans.sock", dir, sep);
|
||||
}else
|
||||
n = snprintf(dst, cap, "/tmp/strans.%lu",
|
||||
(unsigned long)getuid());
|
||||
if(n < 0 || (size_t)n >= cap)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
ipcconnect(void)
|
||||
{
|
||||
struct sockaddr_un addr;
|
||||
int e, fd;
|
||||
|
||||
memset(&addr, 0, sizeof addr);
|
||||
addr.sun_family = AF_UNIX;
|
||||
if(ipcpath(addr.sun_path, sizeof addr.sun_path) < 0){
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if(fd < 0)
|
||||
return -1;
|
||||
if(connect(fd, (struct sockaddr*)&addr, sizeof addr) == 0)
|
||||
return fd;
|
||||
e = errno;
|
||||
close(fd);
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
ipcpackreq(unsigned char req[Ipcreqsz], int want, unsigned int mod,
|
||||
unsigned int key)
|
||||
|
||||
Reference in New Issue
Block a user