From fdeea7c916d591738c9a2f12ab1029e0c04e7b26 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Sat, 4 Feb 2012 12:39:26 +0000 Subject: [PATCH] iscsi-scstd: Follow-up for r4077 - avoid that reading from the netlink socket can cause data corruption git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@4109 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- iscsi-scst/usr/event.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/iscsi-scst/usr/event.c b/iscsi-scst/usr/event.c index 89339233d..6555d5ae0 100644 --- a/iscsi-scst/usr/event.c +++ b/iscsi-scst/usr/event.c @@ -37,6 +37,8 @@ #define ISCSI_ISNS_SYSFS_ACCESS_CONTROL_ENABLED "AccessControl" +#define STATIC_ASSERT(e) ((void)sizeof(int[1-2*!(e)])) + static struct sockaddr_nl src_addr, dest_addr; static int nl_write(int fd, void *data, int len) @@ -158,7 +160,7 @@ static int handle_e_add_target(int fd, const struct iscsi_kern_event *event) /* Params are not 0-terminated */ - size = strlen("Target ") + event->param1_size + 2 + event->param2_size + 1; + size = NLMSG_ALIGN(strlen("Target ") + event->param1_size + 2 + event->param2_size + 1); buf = malloc(size); if (buf == NULL) { @@ -397,7 +399,7 @@ static int handle_e_mgmt_cmd(int fd, const struct iscsi_kern_event *event) /* Params are not 0-terminated */ - size = event->param1_size + 1; + size = NLMSG_ALIGN(event->param1_size + 1); buf = malloc(size); if (buf == NULL) { @@ -486,7 +488,7 @@ static int handle_e_get_attr_value(int fd, const struct iscsi_kern_event *event) /* Params are not 0-terminated */ - size = event->param1_size + 1; + size = NLMSG_ALIGN(event->param1_size + 1); buf = malloc(size); if (buf == NULL) { @@ -757,7 +759,7 @@ static int handle_e_set_attr_value(int fd, const struct iscsi_kern_event *event) } /* Params are not 0-terminated */ - size = event->param1_size + 1 + 1 + event->param2_size + 1; + size = NLMSG_ALIGN(event->param1_size + 1 + 1 + event->param2_size + 1); buf = malloc(size); if (buf == NULL) { @@ -1048,6 +1050,8 @@ int handle_iscsi_events(int fd, bool wait) * but IET developers thought it's OK. ToDo: fix somewhen. */ + STATIC_ASSERT(sizeof(event) % NLMSG_ALIGNTO == 0); + retry: if ((rc = nl_read(fd, &event, sizeof(event), wait)) < 0) { if (errno == EAGAIN)