Browse code

dns: Fix bug in error handling when talking to script

Comparing the result of read/write to a size_t value
is dangerous C. Since ssize_t and size_t have the same
size ssize_t is promoted to size_t, so -1 becomes
size_t max value and is not smaller than the expected
length.

Make sure to compare ssize_t to ssize_t to avoid any
suprises.

Change-Id: Ic395b6d1dce510bb4b499c5beba61f033a2a860b
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Heiko Hund <heiko@openvpn.net>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1208
Message-Id: <20250924121901.13532-1-gert@greenie.muc.de>
URL: https://sourceforge.net/p/openvpn/mailman/message/59238099/
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Frank Lichtenheld authored on 2025/09/24 21:18:55
Showing 1 changed files
... ...
@@ -642,11 +642,10 @@ run_updown_runner(bool up, struct options *o, const struct tuntap *tt,
642 642
 
643 643
         while (1)
644 644
         {
645
-            ssize_t rlen, wlen;
646 645
             char path[PATH_MAX];
647 646
 
648 647
             /* Block here until parent sends a path */
649
-            rlen = read(dns_pipe_fd[0], &path, sizeof(path));
648
+            ssize_t rlen = read(dns_pipe_fd[0], &path, sizeof(path));
650 649
             if (rlen < 1)
651 650
             {
652 651
                 if (rlen == -1 && errno == EINTR)
... ...
@@ -665,8 +664,8 @@ run_updown_runner(bool up, struct options *o, const struct tuntap *tt,
665 665
             /* Unblock parent process */
666 666
             while (1)
667 667
             {
668
-                wlen = write(ack_pipe_fd[1], &res, sizeof(res));
669
-                if ((wlen == -1 && errno != EINTR) || wlen < sizeof(res))
668
+                ssize_t wlen = write(ack_pipe_fd[1], &res, sizeof(res));
669
+                if ((wlen == -1 && errno != EINTR) || wlen < (ssize_t)sizeof(res))
670 670
                 {
671 671
                     /* Not much we can do about errors but exit */
672 672
                     close(dns_pipe_fd[0]);
... ...
@@ -727,7 +726,7 @@ run_up_down_command(bool up, struct options *o, const struct tuntap *tt,
727 727
         env_set_write_file(dvf, es);
728 728
 
729 729
         int wfd = updown_runner->fds[1];
730
-        size_t dvf_size = strlen(dvf) + 1;
730
+        ssize_t dvf_size = strlen(dvf) + 1;
731 731
         while (1)
732 732
         {
733 733
             ssize_t len = write(wfd, dvf, dvf_size);
... ...
@@ -746,7 +745,7 @@ run_up_down_command(bool up, struct options *o, const struct tuntap *tt,
746 746
         while (1)
747 747
         {
748 748
             ssize_t len = read(rfd, &status, sizeof(status));
749
-            if (len < sizeof(status))
749
+            if (len < (ssize_t)sizeof(status))
750 750
             {
751 751
                 if (len == -1 && errno == EINTR)
752 752
                 {