From 368ab1b710aeaadf9bce79ebb675053ea4990e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Fri, 1 Dec 2017 10:55:43 +0000 Subject: [PATCH] Add some checks in mud_set_path() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They are useless in theory but as we do not define the macros CMSG_FIRSTHDR and CMSG_NXTHDR.. Signed-off-by: Adrien Gallouët --- mud.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/mud.c b/mud.c index 5f91b31..877eff9 100644 --- a/mud.c +++ b/mud.c @@ -411,7 +411,7 @@ mud_cmp_addr(struct sockaddr *a, struct sockaddr *b) return 1; } -static void +static int mud_set_path(struct mud_path *path, struct mud_ipaddr *local_addr, struct sockaddr *addr) { @@ -422,6 +422,9 @@ mud_set_path(struct mud_path *path, struct mud_ipaddr *local_addr, struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); + if (!cmsg) + return -1; + memset(path->ctrl.data, 0, sizeof(path->ctrl.data)); memmove(&path->local_addr, local_addr, sizeof(struct mud_ipaddr)); @@ -438,6 +441,9 @@ mud_set_path(struct mud_path *path, struct mud_ipaddr *local_addr, cmsg = CMSG_NXTHDR(&msg, cmsg); + if (!cmsg) + return -1; + cmsg->cmsg_level = IPPROTO_IP; cmsg->cmsg_type = IP_TOS; cmsg->cmsg_len = CMSG_LEN(sizeof(int)); @@ -460,6 +466,9 @@ mud_set_path(struct mud_path *path, struct mud_ipaddr *local_addr, cmsg = CMSG_NXTHDR(&msg, cmsg); + if (!cmsg) + return -1; + cmsg->cmsg_level = IPPROTO_IPV6; cmsg->cmsg_type = IPV6_TCLASS; cmsg->cmsg_len = CMSG_LEN(sizeof(int)); @@ -468,14 +477,18 @@ mud_set_path(struct mud_path *path, struct mud_ipaddr *local_addr, path->ctrl.size = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)); } + + return 0; } static struct mud_path * mud_path(struct mud *mud, struct mud_ipaddr *local_addr, struct sockaddr *addr, int create) { - if (local_addr->family != addr->sa_family) + if (local_addr->family != addr->sa_family) { + errno = EINVAL; return NULL; + } struct mud_path *path; @@ -497,7 +510,11 @@ mud_path(struct mud *mud, struct mud_ipaddr *local_addr, if (!path) return NULL; - mud_set_path(path, local_addr, addr); + if (mud_set_path(path, local_addr, addr)) { + free(path); + errno = EINVAL; + return NULL; + } path->conf.mtu.local = mud->mtu; // XXX @@ -538,10 +555,8 @@ mud_peer(struct mud *mud, const char *name, const char *host, int port, struct mud_path *path = mud_path(mud, &local_addr, (struct sockaddr *)&addr, 1); - if (!path) { - errno = ENOMEM; + if (!path) return -1; - } path->state.active = 1; path->state.backup = !!backup;