]> The Tcpdump Group git mirrors - libpcap/commitdiff
Don't test errno after calls involving snprintf().
authorGuy Harris <[email protected]>
Sun, 2 Dec 2018 01:28:38 +0000 (17:28 -0800)
committerGuy Harris <[email protected]>
Sun, 2 Dec 2018 01:28:38 +0000 (17:28 -0800)
snprintf() can modify errno; do all errno tests before calling
pcap_fmt_errmsg_for_errno() to generate an error message.  (Yes, we want
to set the error string even for PCAP_WARNING_PROMISC_NOTSUP,
PCAP_ERROR_NO_SUCH_DEVICE, and PCAP_ERROR_PERM_DENIED; the
pcap_activate() man page says

If PCAP_WARNING_PROMISC_NOTSUP, PCAP_ERROR_NO_SUCH_DEVICE, or
PCAP_ERROR_PERM_DENIED is returned, pcap_geterr() or
pcap_perror() may be called with p as an argument to fetch or
display an message giving additional details about the problem
that might be useful for debugging the problem if it's
unexpected.

pcap-linux.c
pcap-usb-linux.c

index f0862d7facd21015d2456c94e4ca364548a67e0c..813e1187e21815760cdfe622ba70524c714a7327 100644 (file)
@@ -3633,7 +3633,7 @@ activate_new(pcap_t *handle)
        const char              *device = handle->opt.device;
        int                     is_any_device = (strcmp(device, "any") == 0);
        int                     protocol = pcap_protocol(handle);
-       int                     sock_fd = -1, arptype;
+       int                     sock_fd = -1, arptype, ret;
 #ifdef HAVE_PACKET_AUXDATA
        int                     val;
 #endif
@@ -3662,21 +3662,21 @@ activate_new(pcap_t *handle)
                         */
                        return 0;
                }
-
-               pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
-                   errno, "socket");
                if (errno == EPERM || errno == EACCES) {
                        /*
                         * You don't have permission to open the
                         * socket.
                         */
-                       return PCAP_ERROR_PERM_DENIED;
+                       ret = PCAP_ERROR_PERM_DENIED;
                } else {
                        /*
                         * Other error.
                         */
-                       return PCAP_ERROR;
+                       ret = PCAP_ERROR;
                }
+               pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
+                   errno, "socket");
+               return ret;
        }
 
        /* It seems the kernel supports the new interface. */
@@ -3771,20 +3771,21 @@ activate_new(pcap_t *handle)
                        }
                        sock_fd = socket(PF_PACKET, SOCK_DGRAM, protocol);
                        if (sock_fd == -1) {
-                               pcap_fmt_errmsg_for_errno(handle->errbuf,
-                                   PCAP_ERRBUF_SIZE, errno, "socket");
                                if (errno == EPERM || errno == EACCES) {
                                        /*
                                         * You don't have permission to
                                         * open the socket.
                                         */
-                                       return PCAP_ERROR_PERM_DENIED;
+                                       ret = PCAP_ERROR_PERM_DENIED;
                                } else {
                                        /*
                                         * Other error.
                                         */
-                                       return PCAP_ERROR;
+                                       ret = PCAP_ERROR;
                                }
+                               pcap_fmt_errmsg_for_errno(handle->errbuf,
+                                   PCAP_ERRBUF_SIZE, errno, "socket");
+                               return ret;
                        }
                        handlep->cooked = 1;
 
@@ -5822,6 +5823,7 @@ static int
 has_wext(int sock_fd, const char *device, char *ebuf)
 {
        struct iwreq ireq;
+       int ret;
 
        if (is_bonding_device(sock_fd, device))
                return 0;       /* bonding device, so don't even try */
@@ -5830,11 +5832,13 @@ has_wext(int sock_fd, const char *device, char *ebuf)
            sizeof ireq.ifr_ifrn.ifrn_name);
        if (ioctl(sock_fd, SIOCGIWNAME, &ireq) >= 0)
                return 1;       /* yes */
+       if (errno == ENODEV)
+               ret = PCAP_ERROR_NO_SUCH_DEVICE;
+       else
+               ret = 0;
        pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
            "%s: SIOCGIWNAME", device);
-       if (errno == ENODEV)
-               return PCAP_ERROR_NO_SUCH_DEVICE;
-       return 0;
+       return ret;
 }
 
 /*
@@ -7114,20 +7118,22 @@ static int
 iface_get_arptype(int fd, const char *device, char *ebuf)
 {
        struct ifreq    ifr;
+       int             ret;
 
        memset(&ifr, 0, sizeof(ifr));
        pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
 
        if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
-               pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
-                   errno, "SIOCGIFHWADDR");
                if (errno == ENODEV) {
                        /*
                         * No such device.
                         */
-                       return PCAP_ERROR_NO_SUCH_DEVICE;
-               }
-               return PCAP_ERROR;
+                       ret = PCAP_ERROR_NO_SUCH_DEVICE;
+               } else
+                       ret = PCAP_ERROR;
+               pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
+                   errno, "SIOCGIFHWADDR");
+               return ret;
        }
 
        return ifr.ifr_hwaddr.sa_family;
index 9787eab088b7bc65bd1c41a8e08d30f7963c0308..5df1ecc72d2f5c7acb60f48e42e11fcc07271203 100644 (file)
@@ -515,6 +515,7 @@ usb_activate(pcap_t* handle)
 {
        struct pcap_usb_linux *handlep = handle->priv;
        char            full_path[USB_LINE_LEN];
+       int             ret;
 
        /*
         * Turn a negative snapshot value (invalid), a snapshot value of
@@ -647,38 +648,38 @@ usb_activate(pcap_t* handle)
                                handle->fd = open(full_path, O_RDONLY, 0);
                        }
                        if (handle->fd < 0) {
-                               /*
-                                * Is the problem that we didn't have
-                                * sufficient permission to open it?
-                                */
-                               if (errno == EACCES) {
+                               if (errno == ENOENT)
+                               {
                                        /*
-                                        * Yes - return that error.
+                                        * The problem is that the file
+                                        * doesn't exist.  Report that as
+                                        * "no such device".  (That could
+                                        * mean "no such USB bus" or
+                                        * "monitoring not supported".)
                                         */
-                                       return PCAP_ERROR_PERM_DENIED;
+                                       ret = PCAP_ERROR_NO_SUCH_DEVICE;
                                }
-
-                               /*
-                                * No - was the problem something other
-                                * than "it doesn't exist"?
-                                */
-                               if (errno != ENOENT) {
+                               else if (errno == EACCES)
+                               {
                                        /*
-                                        * Yes - return *that* error.
+                                        * The problem is that we don't
+                                        * have sufficient permission to
+                                        * open the file.  Report that.
                                         */
-                                       pcap_fmt_errmsg_for_errno(handle->errbuf,
-                                           PCAP_ERRBUF_SIZE, errno,
-                                           "Can't open USB bus file %s",
-                                           full_path);
-                                       return PCAP_ERROR;
+                                       ret = PCAP_ERROR_PERM_DENIED;
                                }
-
-                               /*
-                                * No.  Report that as "no such device".
-                                * (That could mean "no such USB bus"
-                                * or "monitoring not supported".)
-                                */
-                               return PCAP_ERROR_NO_SUCH_DEVICE;
+                               else
+                               {
+                                       /*
+                                        * Some other error.
+                                        */
+                                       ret = PCAP_ERROR;
+                               }
+                               pcap_fmt_errmsg_for_errno(handle->errbuf,
+                                   PCAP_ERRBUF_SIZE, errno,
+                                   "Can't open USB bus file %s",
+                                   full_path);
+                               return ret;
                        }
                }