From 793526f8884455f43daecd0a2c46772388417a00 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 4 Oct 2015 02:24:29 +0200 Subject: [PATCH] protocols: don't use assert on paths that can be reached Malformed packets should not make lldpd crash. Ensure we can handle them by not using assert() in this part. --- src/daemon/lldpd.c | 1 - src/daemon/protocols/cdp.c | 10 +++++++--- src/daemon/protocols/edp.c | 1 - src/daemon/protocols/lldp.c | 14 ++++++++------ src/daemon/protocols/sonmp.c | 8 +++++--- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 876fcc90..b4716f45 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -198,7 +198,6 @@ lldpd_alloc_mgmt(int family, void *addrptr, size_t addrsize, u_int32_t iface) return NULL; } mgmt->m_family = family; - assert(addrsize <= LLDPD_MGMT_MAXADDRSIZE); memcpy(&mgmt->m_addr, addrptr, addrsize); mgmt->m_addrsize = addrsize; mgmt->m_iface = iface; diff --git a/src/daemon/protocols/cdp.c b/src/daemon/protocols/cdp.c index 5dfa20ea..09a8d2b3 100644 --- a/src/daemon/protocols/cdp.c +++ b/src/daemon/protocols/cdp.c @@ -25,7 +25,6 @@ #include #include #include -#include static int cdp_send(struct lldpd *global, @@ -438,8 +437,13 @@ cdp_decode(struct lldpd *cfg, char *frame, int s, mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4, &addr, sizeof(struct in_addr), 0); if (mgmt == NULL) { - assert(errno == ENOMEM); - log_warn("cdp", "unable to allocate memory for management address"); + if (errno == ENOMEM) + log_warn("cdp", + "unable to allocate memory for management address"); + else + log_warn("cdp", + "too large management address received on %s", + hardware->h_ifname); goto malformed; } TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries); diff --git a/src/daemon/protocols/edp.c b/src/daemon/protocols/edp.c index acbe53dd..9b9fc203 100644 --- a/src/daemon/protocols/edp.c +++ b/src/daemon/protocols/edp.c @@ -25,7 +25,6 @@ #include #include #include -#include static int seq = 0; diff --git a/src/daemon/protocols/lldp.c b/src/daemon/protocols/lldp.c index 431b22fd..deddc3e7 100644 --- a/src/daemon/protocols/lldp.c +++ b/src/daemon/protocols/lldp.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -161,7 +160,7 @@ static int _lldp_send(struct lldpd *global, /* Management addresses */ TAILQ_FOREACH(mgmt, &chassis->c_mgmt, m_entries) { proto = lldpd_af_to_lldp_proto(mgmt->m_family); - assert(proto != LLDP_MGMT_ADDR_NONE); + if (proto == LLDP_MGMT_ADDR_NONE) continue; if (!( POKE_START_LLDP_TLV(LLDP_TLV_MGMT_ADDR) && /* Size of the address, including its type */ @@ -749,10 +748,13 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, iface = 0; mgmt = lldpd_alloc_mgmt(af, addr_ptr, addr_length, iface); if (mgmt == NULL) { - assert(errno == ENOMEM); - log_warn("lldp", "unable to allocate memory " - "for management address"); - goto malformed; + if (errno == ENOMEM) + log_warn("lldp", "unable to allocate memory " + "for management address"); + else + log_warn("lldp", "too large management address " + "received on %s", hardware->h_ifname); + goto malformed; } TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries); break; diff --git a/src/daemon/protocols/sonmp.c b/src/daemon/protocols/sonmp.c index 30930cb3..b55d73b7 100644 --- a/src/daemon/protocols/sonmp.c +++ b/src/daemon/protocols/sonmp.c @@ -24,7 +24,6 @@ #include #include #include -#include static struct sonmp_chassis sonmp_chassis_types[] = { {1, "unknown (via SONMP)"}, @@ -358,8 +357,11 @@ sonmp_decode(struct lldpd *cfg, char *frame, int s, } mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4, &address, sizeof(struct in_addr), 0); if (mgmt == NULL) { - assert(errno == ENOMEM); - log_warn("sonmp", "unable to allocate memory for management address"); + if (errno == ENOMEM) + log_warn("sonmp", "unable to allocate memory for management address"); + else + log_warn("sonmp", "too large management address received on %s", + hardware->h_ifname); goto malformed; } TAILQ_INSERT_TAIL(&chassis->c_mgmt, mgmt, m_entries); -- 2.39.5