From 7f719d2f9f39dc541da85ff3cfc709058952d837 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 8 Feb 2017 14:40:04 +0100 Subject: llmnr-query: Extract LLMNR packet data in an alignment-safe way Follow commit 9bec6fb9 ("Fix misaligned memory access") for llmnr-query and extract values from the response packets in a way that doesn't cause unaligned memory access on architectures that don't support it. Signed-off-by: Tobias Klauser --- llmnr-query.c | 16 ++++++++++------ pkt.h | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/llmnr-query.c b/llmnr-query.c index 5de2c6b..996723d 100644 --- a/llmnr-query.c +++ b/llmnr-query.c @@ -297,7 +297,6 @@ int main(int argc, char **argv) FD_ZERO(&rfds); FD_SET(sock, &rfds); - /* wait up to one second for a response */ tv.tv_sec = timeout_ms / 1000; tv.tv_usec = (timeout_ms % 1000) * 1000; @@ -347,10 +346,14 @@ int main(int argc, char **argv) name = (char *)pkt_put(p, nl + 1); } - type = htons(*(uint16_t *)pkt_put(p, sizeof(type))); - clss = htons(*(uint16_t *)pkt_put(p, sizeof(clss))); - ttl = htonl(*(uint32_t *)pkt_put(p, sizeof(ttl))); - addr_size = htons(*(uint16_t *)pkt_put(p, sizeof(addr_size))); + type = htons(pkt_put_extract_u16(p)); + clss = htons(pkt_put_extract_u16(p)); + + if (clss != LLMNR_CLASS_IN) + log_warn("Unexpected response class received: %d\n", clss); + + ttl = htonl(pkt_put_extract_u32(p)); + addr_size = htons(pkt_put_extract_u16(p)); if (addr_size == sizeof(struct in_addr)) { af = AF_INET; @@ -361,7 +364,8 @@ int main(int argc, char **argv) break; } - if (!inet_ntop(af, pkt_put(p, addr_size), addr, ARRAY_SIZE(addr))) + memcpy(&sst, pkt_put(p, addr_size), addr_size); + if (!inet_ntop(af, &sst, addr, ARRAY_SIZE(addr))) strncpy(addr, "", sizeof(addr)); addr[INET6_ADDRSTRLEN] = '\0'; diff --git a/pkt.h b/pkt.h index bb2b717..70591b5 100644 --- a/pkt.h +++ b/pkt.h @@ -1,7 +1,7 @@ /* * Packet buffer structure and utilities. * - * Copyright (C) 2015 Tobias Klauser + * Copyright (C) 2015-2017 Tobias Klauser * * Based on pkt_buff.h from the netsniff-ng toolkit which is: * @@ -108,4 +108,17 @@ DEFINE_PKT_PUT(8) DEFINE_PKT_PUT(16) DEFINE_PKT_PUT(32) +/* extract values from struct pkt in an alignment-safe way */ +#define DEFINE_PKT_PUT_EXTRACT(__bitwidth) \ +static inline uint##__bitwidth##_t pkt_put_extract_u##__bitwidth(struct pkt *p) \ +{ \ + uint##__bitwidth##_t val; \ + memcpy(&val, pkt_put(p, sizeof(val)), sizeof(val)); \ + return val; \ +} + +DEFINE_PKT_PUT_EXTRACT(8) +DEFINE_PKT_PUT_EXTRACT(16) +DEFINE_PKT_PUT_EXTRACT(32) + #endif /* PKT_H */ -- cgit v1.2.3-54-g00ecf