alfred: argument-parsing: random fixes
cover alfred: argument-parsing: random fixes
Similar to the batctl fixes, I have now compiled a list of potential fixes for alfred. Instead of posting everything at once, I will be posted in smaller chunks. They are just various (mostly minor) problems which should be tackled at some point.
---
Sven Eckelmann (6):
alfred: keep interface names always in heap
alfred: free interface parameters before assigning new
alfred: check strdup() failures during option parsing
alfred: reject invalid synchronization periods
alfred: reject too long unix socket paths
alfred: validate the IPv4 multicast group argument
Makefile | 2 +- main.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++--------- unix_sock.c | 10 ++++++++++ 3 files changed, 60 insertions(+), 10 deletions(-)
---
base-commit: 462a0cf40615f62e0912c36d72a75accbb26f5ee
change-id: 20260730-bugfixes-parsing-c2406b2251dd
Best regards,
Signature · 1 line
Sven Eckelmann <[email protected]>
PATCH 1/6 alfred: keep interface names always in heap
The interface pointer in globals was used to store an address in .rodata or in on the heap. This makes the handling of the allocation/deallocation hard. At the same time, nothing checked for allocations problems. An allocation error could cause an hard-to-debug problem later when trying to calculate the ifindex.
Adjust the code to always allocate the string as an heap object to simplify the handling.
--- main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
main.c +6 -1
@@ -191,7 +191,6 @@ static struct globals *alfred_init(int argc, char *argv[]) globals->clientmode =CLIENT_NONE ; globals->best_server =NULL ; globals->clientmode_version =0 ; - globals->mesh_iface ="bat0" ; globals->unix_path =ALFRED_SOCK_PATH_DEFAULT ; globals->verbose = false; globals->ipv4mode = false;
@@ -201,6 +200,12 @@ static struct globals *alfred_init(int argc, char *argv[]) globals->sync_period.tv_nsec =0 ;bitmap_zero (globals->changed_data_types,ALFRED_NUM_TYPES ); + globals->mesh_iface =strdup ("bat0" ); +if (!globals->mesh_iface) { +perror ("strdup" ); +return NULL ; + } +time_random_seed (); while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:B:SEu:dc:p:4:f", long_options,
Signature · 1 line
2.47.3
PATCH 2/6 alfred: free interface parameters before assigning new
In reply to Sven EckelmannWhen an interface specific parameter is given multiple times then it is necessary to free the already allocated interface string buffer.
--- main.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
main.c +6 -6
@@ -235,12 +235,6 @@ static struct globals *alfred_init(int argc, char *argv[])case 'm' : globals->opmode =OPMODE_PRIMARY ;break ; -case 'i' : - globals->net_iface =strdup (optarg); -break ; -case 'b' : - globals->mesh_iface =strdup (optarg); -break ;case 'V' : i =atoi (optarg);if (i <0 || i >255 ) {
@@ -264,10 +258,16 @@ static struct globals *alfred_init(int argc, char *argv[])break ;case 'I' : globals->clientmode =CLIENT_CHANGE_INTERFACE ; +/* fall-through */ +case 'i' : +free (globals->net_iface); globals->net_iface =strdup (optarg);break ;case 'B' : globals->clientmode =CLIENT_CHANGE_BAT_IFACE ; +/* fall-through */ +case 'b' : +free (globals->mesh_iface); globals->mesh_iface =strdup (optarg);break ;case 'S' :
Signature · 1 line
2.47.3
PATCH 3/6 alfred: check strdup() failures during option parsing
In reply to Sven Eckelmannalfred_init() duplicates the default batman-adv interface name and the interface names passed via -i/-I and -b/-B with strdup() but never checked the result. On allocation failure globals->mesh_iface or globals->net_iface is left NULL and later used unconditionally and crashes.
The allocation should be checked to present a user readable error instead of crashing at a later point.
--- main.c | 8 ++++++++ 1 file changed, 8 insertions(+)
main.c +8 -0
@@ -262,6 +262,10 @@ static struct globals *alfred_init(int argc, char *argv[])case 'i' :free (globals->net_iface); globals->net_iface =strdup (optarg); +if (!globals->net_iface) { +perror ("strdup" ); +return NULL ; + }break ;case 'B' : globals->clientmode =CLIENT_CHANGE_BAT_IFACE ;
@@ -269,6 +273,10 @@ static struct globals *alfred_init(int argc, char *argv[])case 'b' :free (globals->mesh_iface); globals->mesh_iface =strdup (optarg); +if (!globals->mesh_iface) { +perror ("strdup" ); +return NULL ; + }break ;case 'S' : globals->clientmode =CLIENT_SERVER_STATUS ;
Signature · 1 line
2.47.3
PATCH 4/6 alfred: reject invalid synchronization periods
In reply to Sven EckelmannThe -p option argument was parsed with strtod() without any validation. An unparsable argument yields 0.0 and a zero it_value disarms a timerfd instead of arming it. 'alfred -p 0' or a mistyped period therefore started a daemon whose periodic timer never fired - no primary announcements, no data synchronization and no purging of stale data - while everything looked fine at startup.
Reject non-finite, zero and negative synchronization periods. And also reject parse errors.
--- Makefile | 2 +- main.c | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-)
Makefile +1 -1
@@ -25,7 +25,7 @@ MANPAGE = man/alfred.8 # alfred flags and options CFLAGS += -pedantic -Wall -W -std=gnu99 -fno-strict-aliasing -MD -MP CPPFLAGS += -D_GNU_SOURCE -LDLIBS += -lrt +LDLIBS += -lrt -lm # Turn on alfred capability dropping by default - set this to n if you don't want/need it export CONFIG_ALFRED_CAPABILITIES=y
main.c +24 -1
@@ -7,7 +7,10 @@*/ #include <arpa/inet.h> +#include <errno.h> #include <getopt.h> +#include <limits.h> +#include <math.h> #include <signal.h> #include <stdbool.h> #include <stdio.h>
@@ -172,6 +175,7 @@ static struct globals *alfred_init(int argc, char *argv[]) };double sync_period =0.0 ;struct globals *globals; +char *endptr;int opt_ind;int opt;int ret;
@@ -298,9 +302,28 @@ static struct globals *alfred_init(int argc, char *argv[])printf ("A.L.F.R.E.D. - Almighty Lightweight Remote Fact Exchange Daemon\n" );return NULL ;case 'p' : - sync_period =strtod (optarg,NULL ); + errno =0 ; + sync_period =strtod (optarg, &endptr); +if (errno || *endptr !='\0' || endptr == optarg || + !isfinite (sync_period) || sync_period <=0 || + sync_period > (double )INT_MAX ) { +fprintf (stderr,"bad sync period argument\n" ); +return NULL ; + } + globals->sync_period.tv_sec = (int )sync_period; globals->sync_period.tv_nsec = (double )(sync_period - (int )sync_period) *1e9 ; + +/* a period which rounds down to an all zero timespec +* would disarm the timer in timerfd_settime() instead +* of triggering it +*/ +if (globals->sync_period.tv_sec ==0 && + globals->sync_period.tv_nsec ==0 ) { +fprintf (stderr,"sync period argument too small\n" ); +return NULL ; + } +printf (" ** Setting sync interval to: %.9f seconds (%lld.%09u)\n" , sync_period, (long long )globals->sync_period.tv_sec, (unsigned int )globals->sync_period.tv_nsec);
Signature · 1 line
2.47.3
PATCH 5/6 alfred: reject too long unix socket paths
In reply to Sven EckelmannThe unix socket path needs to be stored in sockaddr_un::sun_path. But strncpy() is silently truncating the string when the target buffer is too small.
Reject such paths with a clear error message.
--- unix_sock.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
unix_sock.c +10 -0
@@ -35,6 +35,11 @@ int unix_sock_open_daemon(struct globals *globals)struct sockaddr_un addr;struct epoll_event ev; +if (strlen (globals->unix_path) >=sizeof (addr.sun_path)) { +fprintf (stderr,"unix socket path too long\n" ); +return -1 ; + } +unlink (globals->unix_path); globals->unix_sock =socket (AF_LOCAL ,SOCK_STREAM ,0 );
@@ -76,6 +81,11 @@ int unix_sock_open_client(struct globals *globals) {struct sockaddr_un addr; +if (strlen (globals->unix_path) >=sizeof (addr.sun_path)) { +fprintf (stderr,"unix socket path too long\n" ); +return -1 ; + } + globals->unix_sock =socket (AF_LOCAL ,SOCK_STREAM ,0 );if (globals->unix_sock <0 ) {perror ("can't create unix socket" );
Signature · 1 line
2.47.3
PATCH 6/6 alfred: validate the IPv4 multicast group argument
In reply to Sven EckelmannThe return value of inet_pton() was ignored when parsing the -4 option. A mistyped group address left alfred_mcast untouched, so the daemon continued with the first bytes of the IPv6 default group (255.2.0.0) as IPv4 multicast group. Instead of a clear parse error the user only got confusing 'can't add multicast membership' failures at runtime.
Reject the option argument when it is not a valid IPv4 address.
--- main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
main.c +5 -1
@@ -330,7 +330,11 @@ static struct globals *alfred_init(int argc, char *argv[])break ;case '4' : globals->ipv4mode = true; -inet_pton (AF_INET , optarg, &alfred_mcast.ipv4); + ret =inet_pton (AF_INET , optarg, &alfred_mcast.ipv4); +if (ret !=1 ) { +fprintf (stderr,"bad IPv4 multicast group argument\n" ); +return NULL ; + }printf (" ** IPv4 Multicast Mode: %x\n" , alfred_mcast.ipv4.s_addr);break ;case 'f' :
Signature · 1 line
2.47.3