From 2d086a831dc16d10729d6fce17bed3ade3efd16c Mon Sep 17 00:00:00 2001
From: Tomasz Torcz <tomek@pipebreaker.pl>
Date: Wed, 28 Nov 2012 13:37:06 +0100
Subject: iscsid,iscsiadm: fix abstract socket length in bind() call
For abstract sockets, the addrlen parameter should be the actual
length of socket's name. Otherwise socket gets padded with some
number of NULs.
---
usr/iscsid_req.c | 10 ++++++----
usr/mgmt_ipc.c | 9 +++++----
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/usr/iscsid_req.c b/usr/iscsid_req.c
index 0902011..1c4678d 100644
--- a/usr/iscsid_req.c
+++ b/usr/iscsid_req.c
@@ -56,7 +56,7 @@ static void iscsid_startup(void)
static int iscsid_connect(int *fd, int start_iscsid)
{
- int nsec;
+ int nsec, addr_len;
struct sockaddr_un addr;
*fd = socket(AF_LOCAL, SOCK_STREAM, 0);
@@ -65,15 +65,17 @@ static int iscsid_connect(int *fd, int start_iscsid)
return ISCSI_ERR_ISCSID_NOTCONN;
}
+ addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(ISCSIADM_NAMESPACE) + 1;
+
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
- memcpy((char *) &addr.sun_path + 1, ISCSIADM_NAMESPACE,
- strlen(ISCSIADM_NAMESPACE));
+ memcpy((char *) &addr.sun_path + 1, ISCSIADM_NAMESPACE, addr_len);
+
/*
* Trying to connect with exponential backoff
*/
for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1) {
- if (connect(*fd, (struct sockaddr *) &addr, sizeof(addr)) == 0)
+ if (connect(*fd, (struct sockaddr *) &addr, addr_len) == 0)
/* Connection established */
return ISCSI_SUCCESS;
diff --git a/usr/mgmt_ipc.c b/usr/mgmt_ipc.c
index 5c39c2e..a1dafc9 100644
--- a/usr/mgmt_ipc.c
+++ b/usr/mgmt_ipc.c
@@ -43,7 +43,7 @@
int
mgmt_ipc_listen(void)
{
- int fd, err;
+ int fd, err, addr_len;
struct sockaddr_un addr;
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
@@ -52,12 +52,13 @@ mgmt_ipc_listen(void)
return fd;
}
+ addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(ISCSIADM_NAMESPACE) + 1;
+
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
- memcpy((char *) &addr.sun_path + 1, ISCSIADM_NAMESPACE,
- strlen(ISCSIADM_NAMESPACE));
+ memcpy((char *) &addr.sun_path + 1, ISCSIADM_NAMESPACE, addr_len);
- if ((err = bind(fd, (struct sockaddr *) &addr, sizeof(addr))) < 0) {
+ if ((err = bind(fd, (struct sockaddr *) &addr, addr_len)) < 0 ) {
log_error("Can not bind IPC socket");
close(fd);
return err;
--
1.7.11.7