From dfb45a7ac75d17c3a8421bfb53840d6db271a121 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Wed, 12 Jun 2013 14:33:43 +0200 Subject: ioops: misc: add dup{,2}_or_die to ioops Bail out if it should ever fail. Detected by coverty in the translate_pcap_to_txf() path. Signed-off-by: Daniel Borkmann --- geoip.c | 8 ++++---- ioops.c | 16 ++++++++++++++++ ioops.h | 2 ++ netsniff-ng.c | 8 ++++---- xmalloc.c | 8 -------- xmalloc.h | 1 - 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/geoip.c b/geoip.c index b403ad1..d027e14 100644 --- a/geoip.c +++ b/geoip.c @@ -348,10 +348,10 @@ static int fdout, fderr; static void geoip_open_prepare(void) { fflush(stdout); - fdout = dup(1); + fdout = dup_or_die(1); fflush(stderr); - fderr = dup(2); + fderr = dup_or_die(2); close(1); close(2); @@ -359,8 +359,8 @@ static void geoip_open_prepare(void) static void geoip_open_restore(void) { - dup2(fdout, 1); - dup2(fderr, 2); + dup2_or_die(fdout, 1); + dup2_or_die(fderr, 2); close(fdout); close(fderr); diff --git a/ioops.c b/ioops.c index 7cec2fd..4ac0f3a 100644 --- a/ioops.c +++ b/ioops.c @@ -12,6 +12,7 @@ #include "dev.h" #include "ioops.h" #include "str.h" +#include "built_in.h" int open_or_die(const char *file, int flags) { @@ -30,6 +31,21 @@ int open_or_die_m(const char *file, int flags, mode_t mode) return ret; } +int dup_or_die(int oldfd) +{ + int newfd = dup(oldfd); + if (unlikely(newfd < 0)) + panic("Cannot dup old file descriptor!\n"); + return newfd; +} + +void dup2_or_die(int oldfd, int newfd) +{ + int ret = dup2(oldfd, newfd); + if (unlikely(ret < 0)) + panic("Cannot dup2 old/new file descriptor!\n"); +} + void create_or_die(const char *file, mode_t mode) { int fd = open_or_die_m(file, O_WRONLY | O_CREAT, mode); diff --git a/ioops.h b/ioops.h index 6de6c62..8e12448 100644 --- a/ioops.h +++ b/ioops.h @@ -5,6 +5,8 @@ extern int open_or_die(const char *file, int flags); extern int open_or_die_m(const char *file, int flags, mode_t mode); +extern int dup_or_die(int oldfd); +extern void dup2_or_die(int oldfd, int newfd); extern void create_or_die(const char *file, mode_t mode); extern int tun_open_or_die(char *name, int type); extern void pipe_or_die(int pipefd[2], int flags); diff --git a/netsniff-ng.c b/netsniff-ng.c index 14ce8f2..2738b9a 100644 --- a/netsniff-ng.c +++ b/netsniff-ng.c @@ -184,7 +184,7 @@ static void pcap_to_xmit(struct ctx *ctx) tx_sock = pf_socket(); if (!strncmp("-", ctx->device_in, strlen("-"))) { - fd = dup(fileno(stdin)); + fd = dup_or_die(fileno(stdin)); close(fileno(stdin)); if (ctx->pcap == PCAP_OPS_MM) ctx->pcap = PCAP_OPS_SG; @@ -544,7 +544,7 @@ static void read_pcap(struct ctx *ctx) bug_on(!__pcap_io); if (!strncmp("-", ctx->device_in, strlen("-"))) { - fd = dup(fileno(stdin)); + fd = dup_or_die(fileno(stdin)); close(fileno(stdin)); if (ctx->pcap == PCAP_OPS_MM) ctx->pcap = PCAP_OPS_SG; @@ -579,7 +579,7 @@ static void read_pcap(struct ctx *ctx) if (ctx->device_out) { if (!strncmp("-", ctx->device_out, strlen("-"))) { - fdo = dup(fileno(stdout)); + fdo = dup_or_die(fileno(stdout)); close(fileno(stdout)); } else { fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT | @@ -769,7 +769,7 @@ static int begin_single_pcap_file(struct ctx *ctx) bug_on(!__pcap_io); if (!strncmp("-", ctx->device_out, strlen("-"))) { - fd = dup(fileno(stdout)); + fd = dup_or_die(fileno(stdout)); close(fileno(stdout)); if (ctx->pcap == PCAP_OPS_MM) ctx->pcap = PCAP_OPS_SG; diff --git a/xmalloc.c b/xmalloc.c index d5805b8..02d6ce4 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -141,11 +141,3 @@ char *xstrndup(const char *str, size_t size) return cp; } - -int xdup(int fd) -{ - int ret = dup(fd); - if (unlikely(ret < 0)) - panic("xdup: dup failed\n"); - return ret; -} diff --git a/xmalloc.h b/xmalloc.h index 21ce84d..e1e4f8f 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -16,7 +16,6 @@ extern void *xrealloc(void *ptr, size_t nmemb, size_t size) __hidden; extern void xfree_func(void *ptr) __hidden; extern char *xstrdup(const char *str) __hidden; extern char *xstrndup(const char *str, size_t size) __hidden; -extern int xdup(int fd) __hidden; static inline void __xfree(void *ptr) { -- cgit v1.2.3-54-g00ecf