pcap: prevent crashes when output `FILE *` is null
v3 cover pcap: prevent crashes when output `FILE *` is null
If ulogd2 receives a signal it will attempt to re-open the pcap output file. If this fails (because the permissions or ownership have changed for example), the FILE pointer will be null and when the next packet comes in, the null pointer will be passed to fwrite and ulogd will crash.
The first patch simplifies the logic of the code that opens the output file, and the second avoids closing the existing stream if fopen fails.
Change since v2
* The first patch is new.
* In the second patch, just keep the old stream open, rather than disabling output and trying to reopen at intervals.
Change since v1
* Correct subject-prefix.
Jeremy Sowden (2):
pcap: simplify opening of output file
pcap: prevent crashes when output FILE * is null
output/pcap/ulogd_output_PCAP.c | 50 +++++++++++++-------------------- 1 file changed, 19 insertions(+), 31 deletions(-)
Signature · 1 line
2.39.2
PATCH v3 1/2 pcap: simplify opening of output file
Instead of statting the file, and choosing the mode with which to open it and whether to write the PCAP header based on the result, always open it with mode "a" and _then_ stat it. This simplifies the flow-control and avoids a race between statting and opening.
--- output/pcap/ulogd_output_PCAP.c | 42 ++++++++++++--------------------- 1 file changed, 15 insertions(+), 27 deletions(-)
output/pcap/ulogd_output_PCAP.c +15 -27
@@ -220,33 +220,21 @@ static int append_create_outfile(struct ulogd_pluginstance *upi) {struct pcap_instance *pi = (struct pcap_instance *) &upi->private;char *filename = upi->config_kset->ces[0 ].u.string; -struct stat st_dummy; -int exist =0 ; - -if (stat (filename, &st_dummy) ==0 && st_dummy.st_size >0 ) - exist =1 ; - -if (!exist) { - pi->of =fopen (filename,"w" ); -if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , - filename, -strerror (errno)); -return -EPERM ; - } -if (!write_pcap_header (pi)) { -ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , -strerror (errno)); -return -ENOSPC ; - } - }else { - pi->of =fopen (filename,"a" ); -if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , - filename, -strerror (errno)); -return -EPERM ; - } +struct stat st_of; + + pi->of =fopen (filename,"a" ); +if (!pi->of) { +ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , + filename, +strerror (errno)); +return -EPERM ; + } +if (fstat (fileno (pi->of), &st_of) ==0 && st_of.st_size ==0 ) { +if (!write_pcap_header (pi)) { +ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , +strerror (errno)); +return -ENOSPC ; + } }return 0 ;
Signature · 1 line
2.39.2
Florian Westphal
maintainerRe: [PATCH ulogd2 v3 1/2] pcap: simplify opening of output file
Quoted 21 lines · Jeremy Sowden
Instead of statting the file, and choosing the mode with which to open it and whether to write the PCAP header based on the result, always open it with mode "a" and _then_ stat it. This simplifies the flow-control and avoids a race between statting and opening.
Signed-off-by Jeremy Sowden <[email protected]>--- output/pcap/ulogd_output_PCAP.c | 42 ++++++++++++--------------------- 1 file changed, 15 insertions(+), 27 deletions(-)output/pcap/ulogd_output_PCAP.c +4
-0@@ -220,33 +220,21 @@ static int append_create_outfile(struct ulogd_pluginstance *upi) { +struct stat st_of; + + pi->of =fopen (filename,"a" ); +if (!pi->of) {
output/pcap/ulogd_output_PCAP.c static int append_create_outfile(struct ulogd_pluginstance *upi) · line 235
Quoted 10 lines
output/pcap/ulogd_output_PCAP.c +10
-0+ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , + filename, +strerror (errno)); +return -EPERM ; + } +if (fstat (fileno (pi->of), &st_of) ==0 && st_of.st_size ==0 ) { +if (!write_pcap_header (pi)) { +ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , +strerror (errno)); +return -ENOSPC ;
LGTM, but should that fclose() before -ENOSPC?
Florian Westphal
maintainerRe: [PATCH ulogd2 v3 1/2] pcap: simplify opening of output file
Quoted 18 lines · Florian Westphal
Quoted 16 lines · Jeremy Sowden
Instead of statting the file, and choosing the mode with which to open it and whether to write the PCAP header based on the result, always open it with mode "a" and _then_ stat it. This simplifies the flow-control and avoids a race between statting and opening.
Signed-off-by Jeremy Sowden <[email protected]>--- output/pcap/ulogd_output_PCAP.c | 42 ++++++++++++--------------------- 1 file changed, 15 insertions(+), 27 deletions(-)output/pcap/ulogd_output_PCAP.c +0
-0@@ -220,33 +220,21 @@ static int append_create_outfile(struct ulogd_pluginstance *upi) {
Quoted 16 lines
Quoted 4 lines
output/pcap/ulogd_output_PCAP.c +4
-0+struct stat st_of; + + pi->of =fopen (filename,"a" ); +if (!pi->of) {output/pcap/ulogd_output_PCAP.c static int append_create_outfile(struct ulogd_pluginstance *upi) · line 236
Quoted 10 lines
output/pcap/ulogd_output_PCAP.c +10
-0+ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , + filename, +strerror (errno)); +return -EPERM ; + } +if (fstat (fileno (pi->of), &st_of) ==0 && st_of.st_size ==0 ) { +if (!write_pcap_header (pi)) { +ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , +strerror (errno)); +return -ENOSPC ;LGTM, but should that
fclose()before-ENOSPC?
AFAICS this doesn't really matter, ulogd will exit().
SIGHUP case doesn't handle errors.
Pablo Neira Ayuso
maintainerRe: [PATCH ulogd2 v3 1/2] pcap: simplify opening of output file
In reply to Jeremy SowdenQuoted 46 lines · Jeremy Sowden
Instead of statting the file, and choosing the mode with which to open it and whether to write the PCAP header based on the result, always open it with mode "a" and _then_ stat it. This simplifies the flow-control and avoids a race between statting and opening.
Signed-off-by Jeremy Sowden <[email protected]>--- output/pcap/ulogd_output_PCAP.c | 42 ++++++++++++--------------------- 1 file changed, 15 insertions(+), 27 deletions(-)output/pcap/ulogd_output_PCAP.c +1
-27@@ -220,33 +220,21 @@ static int append_create_outfile(struct ulogd_pluginstance *upi) {struct pcap_instance *pi = (struct pcap_instance *) &upi->private;char *filename = upi->config_kset->ces[0 ].u.string; -struct stat st_dummy; -int exist =0 ; - -if (stat (filename, &st_dummy) ==0 && st_dummy.st_size >0 ) - exist =1 ; - -if (!exist) { - pi->of =fopen (filename,"w" ); -if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , - filename, -strerror (errno)); -return -EPERM ; - } -if (!write_pcap_header (pi)) { -ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , -strerror (errno)); -return -ENOSPC ; - } - }else { - pi->of =fopen (filename,"a" ); -if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , - filename, -strerror (errno)); -return -EPERM ; - } +struct stat st_of;
output/pcap/ulogd_output_PCAP.c static int append_create_outfile(struct ulogd_pluginstance *upi) · line 233
Quoted 10 lines
output/pcap/ulogd_output_PCAP.c +10
-0+ + pi->of =fopen (filename,"a" ); +if (!pi->of) { +ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , + filename, +strerror (errno)); +return -EPERM ; + } +if (fstat (fileno (pi->of), &st_of) ==0 && st_of.st_size ==0 ) { +if (!write_pcap_header (pi)) {
^^^^
coding style nitpick, it can be fixed before applying it.
Quoted 10 lines
output/pcap/ulogd_output_PCAP.c +4
-0+ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , +strerror (errno)); +return -ENOSPC ; + } }return 0 ;Signature · 1 line
2.39.2
Re: [PATCH ulogd2 v3 1/2] pcap: simplify opening of output file
Quoted 58 lines · Pablo Neira Ayuso
Quoted 46 lines · Jeremy Sowden
Instead of statting the file, and choosing the mode with which to open it and whether to write the PCAP header based on the result, always open it with mode "a" and _then_ stat it. This simplifies the flow-control and avoids a race between statting and opening.
Signed-off-by Jeremy Sowden <[email protected]>--- output/pcap/ulogd_output_PCAP.c | 42 ++++++++++++--------------------- 1 file changed, 15 insertions(+), 27 deletions(-)output/pcap/ulogd_output_PCAP.c +1
-27@@ -220,33 +220,21 @@ static int append_create_outfile(struct ulogd_pluginstance *upi) {struct pcap_instance *pi = (struct pcap_instance *) &upi->private;char *filename = upi->config_kset->ces[0 ].u.string; -struct stat st_dummy; -int exist =0 ; - -if (stat (filename, &st_dummy) ==0 && st_dummy.st_size >0 ) - exist =1 ; - -if (!exist) { - pi->of =fopen (filename,"w" ); -if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , - filename, -strerror (errno)); -return -EPERM ; - } -if (!write_pcap_header (pi)) { -ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , -strerror (errno)); -return -ENOSPC ; - } - }else { - pi->of =fopen (filename,"a" ); -if (!pi->of) { -ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , - filename, -strerror (errno)); -return -EPERM ; - } +struct stat st_of;output/pcap/ulogd_output_PCAP.c static int append_create_outfile(struct ulogd_pluginstance *upi) · line 233
Quoted 10 lines
output/pcap/ulogd_output_PCAP.c +10
-0+ + pi->of =fopen (filename,"a" ); +if (!pi->of) { +ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , + filename, +strerror (errno)); +return -EPERM ; + } +if (fstat (fileno (pi->of), &st_of) ==0 && st_of.st_size ==0 ) { +if (!write_pcap_header (pi)) {^^^^
Quoted 1 line
coding style nitpick,
Whoops. Not sure what happened there.
Quoted 1 line
it can be fixed before applying it.
Please.
J.
Quoted 11 lines
Quoted 10 lines
output/pcap/ulogd_output_PCAP.c +4
-0+ulogd_log (ULOGD_ERROR ,"can't write pcap header: %s\n" , +strerror (errno)); +return -ENOSPC ; + } }return 0 ;Signature · 1 line
2.39.2
PATCH v3 2/2 pcap: prevent crashes when output `FILE *` is null
In reply to Jeremy SowdenIf ulogd2 receives a signal it will attempt to re-open the pcap output file. If this fails (because the permissions or ownership have changed for example), the FILE pointer will be null and when the next packet comes in, the null pointer will be passed to fwrite and ulogd will crash.
Instead, assign the return value of fopen to a local variable, and only close the existing stream if fopen succeeded.
--- output/pcap/ulogd_output_PCAP.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
output/pcap/ulogd_output_PCAP.c +6 -6
@@ -221,14 +221,18 @@ static int append_create_outfile(struct ulogd_pluginstance *upi)struct pcap_instance *pi = (struct pcap_instance *) &upi->private;char *filename = upi->config_kset->ces[0 ].u.string;struct stat st_of; +FILE *of; - pi->of =fopen (filename,"a" ); -if (!pi->of) { + of =fopen (filename,"a" ); +if (!of) {ulogd_log (ULOGD_ERROR ,"can't open pcap file %s: %s\n" , filename,strerror (errno));return -EPERM ; } +if (pi->of) +fclose (pi->of); + pi->of = of;if (fstat (fileno (pi->of), &st_of) ==0 && st_of.st_size ==0 ) {if (!write_pcap_header (pi)) { ulogd_log(ULOGD_ERROR, "can't write pcap header: %s\n",
@@ -236,18 +240,14 @@ static int append_create_outfile(struct ulogd_pluginstance *upi)return -ENOSPC ; } } -return 0 ; }static void signal_pcap (struct ulogd_pluginstance *upi,int signal) { -struct pcap_instance *pi = (struct pcap_instance *) &upi->private; -switch (signal) {case SIGHUP :ulogd_log (ULOGD_NOTICE ,"reopening capture file\n" ); -fclose (pi->of);append_create_outfile (upi);break ;default :
Signature · 1 line
2.39.2
Florian Westphal
maintainerQuoted 10 lines · Jeremy Sowden
If ulogd2 receives a signal it will attempt to re-open the pcap output file. If this fails (because the permissions or ownership have changed for example), the FILE pointer will be null and when the next packet comes in, the null pointer will be passed to fwrite and ulogd will crash.
The first patch simplifies the logic of the code that opens the output file, and the second avoids closing the existing stream if
fopenfails.
Quoted 7 lines
Change since v2
* The first patch is new.
* In the second patch, just keep the old stream open, rather than disabling output and trying to reopen at intervals.
LGTM, thanks Jeremy.
Quoted 10 lines · Jeremy Sowden
If ulogd2 receives a signal it will attempt to re-open the pcap output file. If this fails (because the permissions or ownership have changed for example), the FILE pointer will be null and when the next packet comes in, the null pointer will be passed to fwrite and ulogd will crash.
The first patch simplifies the logic of the code that opens the output file, and the second avoids closing the existing stream if
fopenfails.
Quoted 7 lines
Change since v2
* The first patch is new.
* In the second patch, just keep the old stream open, rather than disabling output and trying to reopen at intervals.
Applied, please double-check the mangling done in patch #1 and send a followup fix if needed.
Quoted 13 lines · Florian Westphal
Quoted 12 lines · Jeremy Sowden
If ulogd2 receives a signal it will attempt to re-open the pcap output file. If this fails (because the permissions or ownership have changed for example), the FILE pointer will be null and when the next packet comes in, the null pointer will be passed to fwrite and ulogd will crash.
The first patch simplifies the logic of the code that opens the output file, and the second avoids closing the existing stream if
fopenfails.
Quoted 8 lines
Quoted 5 lines
Change since v2
* The first patch is new.
* In the second patch, just keep the old stream open, rather than disabling output and trying to reopen at intervals.Applied, please double-check the mangling done in patch #1 and send a followup fix if needed.
Thanks, Florian. LGTM.
J.