misc fixes
cover misc fixes
Hello,
Here's a small patchset fixing some issues in nftables. The erec patches are relatively significative (relatively as only applying to debug mode) and the last one is just a small cleaning proposal.
BR,
--
Eric Leblond
PATCH 1/3 erec: fix buffer overflow
A static array was used to read data and to write information in it without checking the limit of the array. The result was a buffer overflow when the line was longer than 1024.
This patch now uses a allocated buffer to avoid the problem.
--- src/erec.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
src/erec.c +14 -5
@@ -12,6 +12,7 @@#include <stdio.h> #include <string.h> #include <stdarg.h> +#include <stdlib.h> #include <netlink.h> #include <gmputil.h>
@@ -82,6 +83,7 @@ void erec_print(FILE *f, const struct error_record *erec)const struct input_descriptor *indesc = loc->indesc, *tmp;const char *line =NULL ;/* silence gcc */ char buf[1024 ]; +char *pbuf =NULL ;unsigned int i, end;int l, ret;
@@ -141,17 +143,24 @@ void erec_print(FILE *f, const struct error_record *erec)if (indesc->type !=INDESC_INTERNAL )fprintf (f,"%s\n" , line); -memset (buf,' ' ,sizeof (buf)); end =0 ;for (l = erec->num_locations -1 ; l >=0 ; l--) { loc = &erec->locations[l]; + end =max (end, loc->last_column); + } + pbuf =malloc (end +1 ); +if (pbuf ==NULL ) +return ; +memset (pbuf,' ' , end +1 ); +for (l = erec->num_locations -1 ; l >=0 ; l--) { + loc = &erec->locations[l];for (i = loc->first_column ? loc->first_column -1 :0 ; i < loc->last_column; i++) - buf[i] = l ?'~' :'^' ; - end =max (end, loc->last_column); + pbuf[i] = l ?'~' :'^' ; } - buf[end] ='\0' ; -fprintf (f,"%s" , buf); + pbuf[end] ='\0' ; +fprintf (f,"%s" , pbuf); +free (pbuf); }fprintf (f,"\n" ); }
Signature · 1 line
2.1.4
Re: [nftables 1/3] erec: fix buffer overflow
Quoted 5 lines · Eric Leblond
A static array was used to read data and to write information in it without checking the limit of the array. The result was a buffer overflow when the line was longer than 1024.
This patch now uses a allocated buffer to avoid the problem.
Applied, thanks Eric.
I have replaced malloc and free by xmalloc and xfree respectively.
PATCH 2/3 erec: fix logic when reading from file
In reply to Eric LeblondIn case we are reading the rules from a file we need to reset the file descriptor to the original position when calling erec_print.
This was not the case in previous code and was leading to valid file to be seen as invalid when treated in debug mode.
--- src/erec.c | 3 +++ 1 file changed, 3 insertions(+)
src/erec.c +3 -0
@@ -86,6 +86,7 @@ void erec_print(FILE *f, const struct error_record *erec)char *pbuf =NULL ;unsigned int i, end;int l, ret; +int orig_offset =0 ;switch (indesc->type) {case INDESC_BUFFER :
@@ -94,11 +95,13 @@ void erec_print(FILE *f, const struct error_record *erec)break ;case INDESC_FILE :memset (buf,0 ,sizeof (buf)); + orig_offset =lseek (indesc->fd,0 ,SEEK_CUR );lseek (indesc->fd, loc->line_offset,SEEK_SET ); ret =read (indesc->fd, buf,sizeof (buf) -1 );if (ret >0 ) *strchrnul (buf,'\n' ) ='\0' ; line = buf; +lseek (indesc->fd, orig_offset,SEEK_SET );break ;case INDESC_INTERNAL :case INDESC_NETLINK :
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerRe: [nftables 2/3] erec: fix logic when reading from file
Quoted 5 lines · Eric Leblond
In case we are reading the rules from a file we need to reset the file descriptor to the original position when calling
erec_print.This was not the case in previous code and was leading to valid file to be seen as invalid when treated in debug mode.
Also applied, thanks.
PATCH 3/3 mnl: improve select timeout logic
In reply to Eric LeblondThis patch explicitely set timeout for select in mnl_batch_talk. Timeout used at the second call was the time spend on first select and this was not looking correct. Instead this patch set a short timeout as we are supposed to dequeue waiting kernel messages.
--- src/mnl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
src/mnl.c +3 -1
@@ -246,7 +246,7 @@ int mnl_batch_talk(struct mnl_socket *nl, struct list_head *err_list)char rcv_buf[MNL_SOCKET_BUFFER_SIZE ];fd_set readfds;struct timeval tv = { - .tv_sec =0 , + .tv_sec =10 , .tv_usec =0 };
@@ -274,6 +274,8 @@ int mnl_batch_talk(struct mnl_socket *nl, struct list_head *err_list)if (ret ==-1 )mnl_err_list_node_add (err_list, errno, nlh->nlmsg_seq); + tv.tv_sec =0 ; + tv.tv_usec =1000 ; ret =select (fd+1 , &readfds,NULL ,NULL , &tv);if (ret ==-1 )return -1 ;
Signature · 1 line
2.1.4
Re: [nftables 3/3] mnl: improve select timeout logic
Hello,
Following live discussion with Pablo, please disregard this patch. Sending a v2 in the following minutes.
BR,
Quoted 35 lines · Eric Leblond
This patch explicitely set timeout for select in
mnl_batch_talk. Timeout used at the second call was the time spend on first select and this was not looking correct. Instead this patch set a short timeout as we are supposed to dequeue waiting kernel messages.Signed-off-by Eric Leblond <[email protected]>--- src/mnl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)src/mnl.c +3
-2@@ -246,7 +246,7 @@ int mnl_batch_talk(struct mnl_socket *nl, struct list_head *err_list)char rcv_buf[MNL_SOCKET_BUFFER_SIZE ];fd_set readfds;struct timeval tv = { - .tv_sec =0 , + .tv_sec =10 , .tv_usec =0 };@@ -274,6 +274,8 @@ int mnl_batch_talk(struct mnl_socket *nl, struct list_head *err_list)if (ret ==-1 )mnl_err_list_node_add (err_list, errno, nlh ->nlmsg_seq); + tv.tv_sec = 0; + tv.tv_usec =1000 ; ret =select (fd+1 , &readfds,NULL ,NULL , &tv);if (ret ==-1 )return -1 ;
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/