2
0
mirror of git://github.com/lxc/lxc synced 2025-08-29 17:49:50 +00:00

make lxc_af_unix_open() safely return error on long pathnames

Signed-off-by: Dwight Engen <dwight.engen@oracle.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Dwight Engen 2013-04-22 11:16:57 -04:00 committed by Stéphane Graber
parent 599d425251
commit ddb17f1f08
No known key found for this signature in database
GPG Key ID: C638974D64792D67

View File

@ -36,6 +36,7 @@ lxc_log_define(lxc_af_unix, lxc);
int lxc_af_unix_open(const char *path, int type, int flags) int lxc_af_unix_open(const char *path, int type, int flags)
{ {
int fd; int fd;
size_t len;
struct sockaddr_un addr; struct sockaddr_un addr;
if (flags & O_TRUNC) if (flags & O_TRUNC)
@ -52,8 +53,16 @@ int lxc_af_unix_open(const char *path, int type, int flags)
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
/* copy entire buffer in case of abstract socket */ /* copy entire buffer in case of abstract socket */
memcpy(addr.sun_path, path, len = sizeof(addr.sun_path);
path[0]?strlen(path):sizeof(addr.sun_path)); if (path[0]) {
len = strlen(path);
if (len >= sizeof(addr.sun_path)) {
close(fd);
errno = ENAMETOOLONG;
return -1;
}
}
memcpy(addr.sun_path, path, len);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) { if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
int tmp = errno; int tmp = errno;
@ -61,7 +70,7 @@ int lxc_af_unix_open(const char *path, int type, int flags)
errno = tmp; errno = tmp;
return -1; return -1;
} }
if (type == SOCK_STREAM && listen(fd, 100)) { if (type == SOCK_STREAM && listen(fd, 100)) {
int tmp = errno; int tmp = errno;
close(fd); close(fd);
@ -76,7 +85,7 @@ int lxc_af_unix_close(int fd)
{ {
struct sockaddr_un addr; struct sockaddr_un addr;
socklen_t addrlen = sizeof(addr); socklen_t addrlen = sizeof(addr);
if (!getsockname(fd, (struct sockaddr *)&addr, &addrlen) && if (!getsockname(fd, (struct sockaddr *)&addr, &addrlen) &&
addr.sun_path[0]) addr.sun_path[0])
unlink(addr.sun_path); unlink(addr.sun_path);