From f664b97beeb660f0eb0f03930a6b01b41ce33283 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 26 Nov 2019 11:19:03 +0100 Subject: Move packet size overflow checking around --- jawol.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'jawol.c') diff --git a/jawol.c b/jawol.c index b301153..d604455 100644 --- a/jawol.c +++ b/jawol.c @@ -70,6 +70,11 @@ static const char usage_msg[] = #include #include +#define PACKET_SIZE (200) +#define CHECK_PACKET_SIZE(x) do { \ + if ( (x) >= PACKET_SIZE ) { fprintf( stderr, "Packet size overflow.\n" ); abort(); } \ + } while(0) + static struct { const char *ifname; struct in_addr dest; @@ -160,7 +165,7 @@ int main(int argc, char **argv) static int etherwake(int count, char **maclist) { - unsigned char outpack[200]; + unsigned char outpack[PACKET_SIZE]; int s = rawsocket(); if ( s == -1 ) return 2; @@ -200,6 +205,7 @@ static int etherwake(int count, char **maclist) memcpy( outpack + packet_size, options.wolpw, options.wolpwlen ); packet_size += options.wolpwlen; } + CHECK_PACKET_SIZE( packet_size ); if ( options.debug ) { printf( "The final packet for %s is:", maclist[idx] ); for ( int i = 0; i < packet_size; i++ ) printf( " %2.2x", outpack[i] ); @@ -278,7 +284,7 @@ int udpsocket() static int wakeonlan(int count, char **maclist) { struct ether_addr dest_ether; - unsigned char outpack[200]; + unsigned char outpack[PACKET_SIZE]; int sock = udpsocket(); if ( sock == -1 ) return 2; @@ -297,6 +303,7 @@ static int wakeonlan(int count, char **maclist) memcpy( outpack + packet_size, options.wolpw, options.wolpwlen ); packet_size += options.wolpwlen; } + CHECK_PACKET_SIZE( packet_size ); if ( options.debug ) { printf( "The final packet for %s is:", maclist[idx] ); for ( int i = 0; i < packet_size; i++ ) printf( " %2.2x", outpack[i] ); @@ -343,16 +350,7 @@ static int get_fill(unsigned char *pkt, const uint8_t *bindestmac, bool with_hea memcpy(pkt + offset, bindestmac, 6); offset += 6; } - if ( offset > 180 ) { - fprintf( stderr, "Packet buffer overflow.\n" ); - abort(); - } - if (options.debug) { - printf("Packet is "); - for (i = 0; i < offset; i++) - printf(" %2.2x", pkt[i]); - printf(".\n"); - } + CHECK_PACKET_SIZE( offset ); return offset; } -- cgit v1.2.3-55-g7522