From 30e1aafbae487e63a1f1f294391bbdc993808c4b Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Sat, 16 Mar 2013 13:40:19 +0100 Subject: netsniff-ng: do not leak file descriptors on exit Covertiy detected that when we redirect sdtin/stdout either via ``-i -'' or ``-o -'', we also need to properly close it when it goes out of scope. Signed-off-by: Daniel Borkmann --- netsniff-ng.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'netsniff-ng.c') diff --git a/netsniff-ng.c b/netsniff-ng.c index ad5fd04..94fb313 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -289,10 +289,9 @@ static void pcap_to_xmit(struct ctx *ctx) if (__pcap_io->prepare_close_pcap) __pcap_io->prepare_close_pcap(fd, PCAP_MODE_RD); - if (strncmp("-", ctx->device_in, strlen("-"))) - close(fd); - else + if (!strncmp("-", ctx->device_in, strlen("-"))) dup2(fd, fileno(stdin)); + close(fd); close(tx_sock); @@ -619,16 +618,14 @@ static void read_pcap(struct ctx *ctx) printf("\r%12lu bytes outgoing\n", ctx->tx_bytes); printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec); - if (strncmp("-", ctx->device_in, strlen("-"))) - close(fd); - else + if (!strncmp("-", ctx->device_in, strlen("-"))) dup2(fd, fileno(stdin)); + close(fd); if (ctx->device_out) { - if (strncmp("-", ctx->device_out, strlen("-"))) - close(fdo); - else + if (!strncmp("-", ctx->device_out, strlen("-"))) dup2(fdo, fileno(stdout)); + close(fdo); } } -- cgit v1.2.3-54-g00ecf