]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Replace all open()/close() with platform_open()/platform_open2()/platform_close()
authorGuido Vranken <guidovranken@gmail.com>
Tue, 27 Jun 2017 10:06:12 +0000 (12:06 +0200)
committerGuido Vranken <guidovranken@gmail.com>
Tue, 27 Jun 2017 10:06:12 +0000 (12:06 +0200)
18 files changed:
src/openvpn/console_builtin.c
src/openvpn/console_systemd.c
src/openvpn/crypto.c
src/openvpn/error.c
src/openvpn/event.c
src/openvpn/misc.c
src/openvpn/mstats.c
src/openvpn/mtcp.c
src/openvpn/mudp.c
src/openvpn/packet_id.c
src/openvpn/platform.c
src/openvpn/platform.h
src/openvpn/ps.c
src/openvpn/route.c
src/openvpn/socket.h
src/openvpn/ssl_verify.c
src/openvpn/status.c
src/openvpn/tun.c

index d3bfc665560cc719ff5d5f37ff528e1ca1b9cdf9..bccc0501e310f53229c62cf366fb1e3289183721 100644 (file)
@@ -209,7 +209,7 @@ get_console_input(const char *prompt, const bool echo, char *input, const int ca
      */
     if (!isatty(0) && !isatty(2) )
     {
-        int fd = open( "/dev/tty", O_RDWR );
+        int fd = platform_open2( "/dev/tty", O_RDWR );
         if (fd < 0)
         {
             msg(M_FATAL, "neither stdin nor stderr are a tty device and you have neither a "
@@ -217,7 +217,7 @@ get_console_input(const char *prompt, const bool echo, char *input, const int ca
                 "you need to use --askpass to make passphrase-protected keys work, and you "
                 "can not use --auth-nocache.", prompt );
         }
-        close(fd);
+        platform_close(fd);
     }
 
     if (echo)
index 1d18b1a7dfdcf2c1c02832042cb15c824c1dfcaa..bf81ff7d6d7afc03d4cb163bc9d6cf2f8d77f89c 100644 (file)
@@ -83,7 +83,7 @@ get_console_input_systemd(const char *prompt, const bool echo, char *input, cons
         chomp(input);
         ret = true;
     }
-    close(std_out);
+    platform_close(std_out);
 
     argv_reset(&argv);
 
index 81f15051acd00c6361a4147a15a3619bdc592236..ec85ae4e4d54ef11e651a00a158a1286f812abf9 100644 (file)
@@ -1272,7 +1272,7 @@ read_key_file(struct key2 *key2, const char *file, const unsigned int flags)
         {
             msg(M_FATAL, "Key file ('%s') can be a maximum of %d bytes", file, (int)in.capacity);
         }
-        close(fd);
+        platform_close(fd);
     }
 
     cp = (unsigned char *)in.data;
@@ -1483,7 +1483,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file, now formatted in out, to file */
     buf_write_string_file(&out, filename, fd);
 
-    if (close(fd))
+    if (platform_close(fd))
     {
         msg(M_ERR, "Close error on shared secret file %s", filename);
     }
index e439c0189972a0c2ffbe6107cb4a29f5df2a67f2..9743b078c0fdbaddd21ab26bc550cddddb491bff 100644 (file)
@@ -41,6 +41,7 @@
 #include "integer.h"
 #include "ps.h"
 #include "mstats.h"
+#include "platform.h"
 
 
 #if SYSLOG_CAPABILITY
@@ -603,7 +604,7 @@ redirect_stdout_stderr(const char *file, bool append)
 #elif defined(HAVE_DUP2)
     if (!std_redir)
     {
-        int out = open(file,
+        int out = platform_open(file,
                        O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC),
                        S_IRUSR | S_IWUSR);
 
@@ -624,7 +625,7 @@ redirect_stdout_stderr(const char *file, bool append)
 
         if (out > 2)
         {
-            close(out);
+            platform_close(out);
         }
 
         std_redir = true;
index 9dc079373d4c284431bf63f2ee92efb52f4ff4a8..0decc1488b5bee850a5811901ad891a374e61259 100644 (file)
@@ -34,6 +34,7 @@
 #include "integer.h"
 #include "event.h"
 #include "fdmisc.h"
+#include "platform.h"
 
 #include "memdbg.h"
 
@@ -533,7 +534,7 @@ static void
 ep_free(struct event_set *es)
 {
     struct ep_set *eps = (struct ep_set *) es;
-    close(eps->epfd);
+    platform_close(eps->epfd);
     free(eps->events);
     free(eps);
 }
index 092fba80268f3720c0f214dfa4b456d84bc3cc0d..81b7e6fa4c4a45e8bec192b74c69e6be36111214 100644 (file)
@@ -44,6 +44,7 @@
 #include "route.h"
 #include "console.h"
 #include "win32.h"
+#include "platform.h"
 
 #include "memdbg.h"
 
@@ -174,7 +175,7 @@ set_std_files_to_null(bool stdin_only)
 {
 #if defined(HAVE_DUP) && defined(HAVE_DUP2)
     int fd;
-    if ((fd = open("/dev/null", O_RDWR, 0)) != -1)
+    if ((fd = platform_open("/dev/null", O_RDWR, 0)) != -1)
     {
         dup2(fd, 0);
         if (!stdin_only)
@@ -184,7 +185,7 @@ set_std_files_to_null(bool stdin_only)
         }
         if (fd > 2)
         {
-            close(fd);
+            platform_close(fd);
         }
     }
 #endif
@@ -398,7 +399,7 @@ openvpn_popen(const struct argv *a,  const struct env_set *es)
                 pid = fork();
                 if (pid == (pid_t)0)       /* child side */
                 {
-                    close(pipe_stdout[0]);         /* Close read end */
+                    platform_close(pipe_stdout[0]);         /* Close read end */
                     dup2(pipe_stdout[1],1);
                     execve(cmd, argv, envp);
                     exit(127);
@@ -407,14 +408,14 @@ openvpn_popen(const struct argv *a,  const struct env_set *es)
                 {
                     int status = 0;
 
-                    close(pipe_stdout[1]);        /* Close write end */
+                    platform_close(pipe_stdout[1]);        /* Close write end */
                     waitpid(pid, &status, 0);
                     ret = pipe_stdout[0];
                 }
                 else       /* fork failed */
                 {
-                    close(pipe_stdout[0]);
-                    close(pipe_stdout[1]);
+                    platform_close(pipe_stdout[0]);
+                    platform_close(pipe_stdout[1]);
                     msg(M_ERR, "openvpn_popen: unable to fork %s", cmd);
                 }
             }
@@ -941,7 +942,7 @@ create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
         fd = platform_open(retfname, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
         if (fd != -1)
         {
-            close(fd);
+            platform_close(fd);
             return retfname;
         }
         else if (fd == -1 && errno != EEXIST)
index 4d02654f2e1c4ec94039672c60b6b8e9e678f35c..3184bf5b83bd38986a0c0e39d6144642c5524f61 100644 (file)
@@ -67,7 +67,7 @@ mstats_open(const char *fn)
     }
 
     /* create file that will be memory mapped */
-    fd = open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
+    fd = platform_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
     if (fd < 0)
     {
         msg(M_ERR, "mstats_open: cannot open: %s", fn);
@@ -82,7 +82,7 @@ mstats_open(const char *fn)
     if (stat != sizeof(ms))
     {
         msg(M_ERR, "mstats_open: write error: %s", fn);
-        close(fd);
+        platform_close(fd);
         return;
     }
 
@@ -91,12 +91,12 @@ mstats_open(const char *fn)
     if (data == MAP_FAILED)
     {
         msg(M_ERR, "mstats_open: write error: %s", fn);
-        close(fd);
+        platform_close(fd);
         return;
     }
 
     /* close the fd (mmap now controls the file) */
-    if (close(fd))
+    if (platform_close(fd))
     {
         msg(M_ERR, "mstats_open: close error: %s", fn);
     }
index cb940d8a1f9e13db0655d9c761921a88a3615c20..416de3e1c0d09a21e689e0eb5cd5dd34ee469ed0 100644 (file)
 
 #include "syshead.h"
 
+#include "platform.h"
 #if P2MP_SERVER
 
 #include "multi.h"
 #include "forward-inline.h"
-
 #include "memdbg.h"
 
 #ifdef HAVE_SYS_INOTIFY_H
@@ -830,7 +830,7 @@ tunnel_server_tcp(struct context *top)
     }
 
 #ifdef ENABLE_ASYNC_PUSH
-    close(top->c2.inotify_fd);
+    platform_close(top->c2.inotify_fd);
 #endif
 
     /* shut down management interface */
index 793678d8aff8f273dc9dbc17ff359e3145b5d09a..5fe3d1a2e4062eaa47101a849ebad885a57a75f2 100644 (file)
@@ -34,6 +34,7 @@
 #include "multi.h"
 #include <inttypes.h>
 #include "forward-inline.h"
+#include "platform.h"
 
 #include "memdbg.h"
 
@@ -358,7 +359,7 @@ tunnel_server_udp_single_threaded(struct context *top)
     }
 
 #ifdef ENABLE_ASYNC_PUSH
-    close(top->c2.inotify_fd);
+    platform_close(top->c2.inotify_fd);
 #endif
 
     /* shut down management interface */
index e544dfa642c8beb9fd6caa900640c351b98ca744..efa4f217e91be7af8b04edb37b898f8153be4f68 100644 (file)
@@ -423,7 +423,7 @@ packet_id_persist_close(struct packet_id_persist *p)
 {
     if (packet_id_persist_enabled(p))
     {
-        if (close(p->fd))
+        if (platform_close(p->fd))
         {
             msg(D_PID_PERSIST | M_ERRNO, "Close error on --replay-persist file %s", p->filename);
         }
index 0efa6e27069f956f55e32431bb512be6951a4693..8db51537664daf43a303a72125f38d30716e3e82 100644 (file)
@@ -369,6 +369,13 @@ platform_open(const char *path, int flags, int mode)
 #endif
 }
 
+int
+platform_open2(const char *path, int flags)
+{
+    FUZZING_BLOCK;
+    return open(path, flags);
+}
+
 int
 platform_stat(const char *path, platform_stat_t *buf)
 {
@@ -405,6 +412,11 @@ ssize_t platform_write(int fd, const void* buf, size_t len)
     return platform_send(fd, buf, len, 0);
 }
 
+int platform_close(int fd)
+{
+    return 0;
+}
+
 ssize_t platform_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 {
     return 1;
index c633959d13a11464efa9730b2fb8b50b325f0682..b6ffb2494dbc3a31e88551240c18834b7c11c411 100644 (file)
@@ -139,6 +139,7 @@ int platform_fclose(FILE *stream);
 FILE *platform_fopen(const char *path, const char *mode);
 
 int platform_open(const char *path, int flags, int mode);
+int platform_open2(const char *path, int flags);
 
 #ifdef _WIN32
 typedef struct _stat platform_stat_t;
@@ -151,6 +152,7 @@ ssize_t platform_recv(int sockfd, void* buf, size_t len, int flags);
 ssize_t platform_send(int sockfd, const void* buf, size_t len, int flags);
 ssize_t platform_read(int fd, void* buf, size_t len);
 ssize_t platform_write(int fd, const void* buf, size_t len);
+int platform_close(int fd);
 ssize_t platform_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
 char* platform_fgets(char *s, int size, FILE *stream);
 int platform_fgetc(FILE *stream);
index b2bc4da9aa82a5af64bd7ff86afe96a31315f3e3..755c6c5bac4734503ab4b4a16ef848bea3cef37c 100644 (file)
@@ -366,7 +366,7 @@ journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_c
             {
                 msg(M_WARN, "PORT SHARE: writing to journal file (%s) failed", jfn);
             }
-            close(fd);
+            platform_close(fd);
             cp->jfn = jfn;
         }
         else
index 96388e8b18d363ffd7c3724254bdb7625df977c8..43750c8ed3437f048e660b7e0d8558f657bb7ee5 100644 (file)
@@ -3345,7 +3345,7 @@ get_default_gateway(struct route_gateway_info *rgi)
 done:
     if (sd >= 0)
     {
-        close(sd);
+        platform_close(sd);
     }
     gc_free(&gc);
 }
@@ -3513,7 +3513,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
 done:
     if (nls >= 0)
     {
-        close(nls);
+        platform_close(nls);
     }
 }
 
@@ -3635,7 +3635,7 @@ get_default_gateway(struct route_gateway_info *rgi)
     {
         l = platform_read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
     } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
-    close(sockfd);
+    platform_close(sockfd);
     sockfd = -1;
 
     /* extract return data from routing socket */
@@ -3709,7 +3709,7 @@ get_default_gateway(struct route_gateway_info *rgi)
             msg(M_WARN, "GDG: ioctl #1 failed");
             goto done;
         }
-        close(sockfd);
+        platform_close(sockfd);
         sockfd = -1;
 
         rgi->gateway.netmask = ntohl(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
@@ -3740,7 +3740,7 @@ get_default_gateway(struct route_gateway_info *rgi)
             msg(M_WARN, "GDG: ioctl #2 failed");
             goto done;
         }
-        close(sockfd);
+        platform_close(sockfd);
         sockfd = -1;
 
         for (cp = buffer; cp <= buffer + ifc.ifc_len - sizeof(struct ifreq); )
@@ -3772,7 +3772,7 @@ get_default_gateway(struct route_gateway_info *rgi)
 done:
     if (sockfd >= 0)
     {
-        close(sockfd);
+        platform_close(sockfd);
     }
     gc_free(&gc);
 }
@@ -3865,7 +3865,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
     }
     while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
 
-    close(sockfd);
+    platform_close(sockfd);
     sockfd = -1;
 
     /* extract return data from routing socket */
@@ -3938,7 +3938,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
 done:
     if (sockfd >= 0)
     {
-        close(sockfd);
+        platform_close(sockfd);
     }
 }
 
index 1384b3aa839e3eeed7221e19c9891c05e9df63a4..828e463ca963bfdbc816f6385a474256081880ce 100644 (file)
@@ -34,6 +34,7 @@
 #include "proxy.h"
 #include "socks.h"
 #include "misc.h"
+#include "platform.h"
 
 /*
  * OpenVPN's default port number as assigned by IANA.
@@ -274,7 +275,7 @@ int socket_finalize(
 
 #else  /* ifdef _WIN32 */
 
-#define openvpn_close_socket(s) close(s)
+#define openvpn_close_socket(s) platform_close(s)
 
 #endif
 
index 84ebe8c17c96e70be711706e8d55fa9388f09580..e07a9a4540589db12eac079f9d10385f307abfdc 100644 (file)
@@ -42,6 +42,7 @@
 #include "base64.h"
 #include "ssl_verify.h"
 #include "ssl_verify_backend.h"
+#include "platform.h"
 
 #ifdef ENABLE_CRYPTO_OPENSSL
 #include "ssl_verify_openssl.h"
@@ -655,7 +656,7 @@ cleanup:
 
     if (fd != -1)
     {
-        close(fd);
+        platform_close(fd);
     }
     gc_free(&gc);
     return ret;
index 0b6ac8795ba37f6761a319cf671bae46a98335ce..61d00378a6f76570e7e25e3d39d21fcadd60c243 100644 (file)
@@ -212,7 +212,7 @@ status_close(struct status_output *so)
         }
         if (so->fd >= 0)
         {
-            if (close(so->fd) < 0)
+            if (platform_close(so->fd) < 0)
             {
                 ret = false;
             }
index 7ec3025c93f3419497810422e5f396c667c7c86a..f001de4ba63c722f9c58aabc71e0e59f50131bc8 100644 (file)
@@ -1740,7 +1740,7 @@ open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
             if (dynamic && strcmp( dev, "tap" ) == 0)
             {
                 struct ifreq ifr;
-                if ((tt->fd = open( "/dev/tap", O_RDWR)) < 0)
+                if ((tt->fd = platform_open( "/dev/tap", O_RDWR)) < 0)
                 {
                     msg(M_FATAL, "Cannot allocate NetBSD TAP dev dynamically");
                 }
@@ -1765,7 +1765,7 @@ open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
                                      "/dev/%s%d", dev, i);
                     openvpn_snprintf(dynamic_name, sizeof(dynamic_name),
                                      "%s%d", dev, i);
-                    if ((tt->fd = open(tunname, O_RDWR)) > 0)
+                    if ((tt->fd = platform_open(tunname, O_RDWR)) > 0)
                     {
                         dynamic_opened = true;
                         break;
@@ -1795,7 +1795,7 @@ open_tun_generic(const char *dev, const char *dev_type, const char *dev_node,
                 tt->persistent_if = true;
             }
 
-            if ((tt->fd = open(tunname, O_RDWR)) < 0)
+            if ((tt->fd = platform_open(tunname, O_RDWR)) < 0)
             {
                 msg(M_ERR, "Cannot open TUN/TAP dev %s", tunname);
             }
@@ -1817,7 +1817,7 @@ close_tun_generic(struct tuntap *tt)
 {
     if (tt->fd >= 0)
     {
-        close(tt->fd);
+        platform_close(tt->fd);
     }
     if (tt->actual_name)
     {
@@ -1862,7 +1862,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
     /* Android 4.4 workaround */
     if (oldtunfd >=0 && android_method == ANDROID_OPEN_AFTER_CLOSE)
     {
-        close(oldtunfd);
+        platform_close(oldtunfd);
         openvpn_sleep(2);
     }
 
@@ -1882,7 +1882,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
 
     if (oldtunfd>=0 && android_method == ANDROID_OPEN_BEFORE_CLOSE)
     {
-        close(oldtunfd);
+        platform_close(oldtunfd);
     }
 
     /* Set the actual name to a dummy name */
@@ -1952,7 +1952,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
         /*
          * Open the interface
          */
-        if ((tt->fd = open(node, O_RDWR)) < 0)
+        if ((tt->fd = platform_open2(node, O_RDWR)) < 0)
         {
             msg(M_ERR, "ERROR: Cannot open TUN/TAP dev %s", node);
         }
@@ -2025,7 +2025,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
                 {
                     msg(M_WARN | M_ERRNO, "Note: Cannot set tx queue length on %s", ifr.ifr_name);
                 }
-                close(ctl_fd);
+                platform_close(ctl_fd);
             }
             else
             {
@@ -2246,12 +2246,12 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
             dev);
     }
 
-    if ((tt->ip_fd = open(ip_node, O_RDWR, 0)) < 0)
+    if ((tt->ip_fd = platform_open(ip_node, O_RDWR, 0)) < 0)
     {
         msg(M_ERR, "Can't open %s", ip_node);
     }
 
-    if ((tt->fd = open(dev_node, O_RDWR, 0)) < 0)
+    if ((tt->fd = platform_open(dev_node, O_RDWR, 0)) < 0)
     {
         msg(M_ERR, "Can't open %s", dev_node);
     }
@@ -2305,7 +2305,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
         }
     }
 
-    if ((if_fd = open(dev_node, O_RDWR, 0)) < 0)
+    if ((if_fd = platform_open(dev_node, O_RDWR, 0)) < 0)
     {
         msg(M_ERR, "Can't open %s (2)", dev_node);
     }
@@ -2367,7 +2367,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
         }
 
         /* Open arp_fd */
-        if ((arp_fd = open(arp_node, O_RDWR, 0)) < 0)
+        if ((arp_fd = platform_open(arp_node, O_RDWR, 0)) < 0)
         {
             msg(M_ERR, "Can't open %s\n", arp_node);
         }
@@ -2399,7 +2399,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
         {
             msg(M_ERR, "Can't link %s device to ARP", dev_tuntap_type);
         }
-        close(arp_fd);
+        platform_close(arp_fd);
     }
 
     CLEAR(ifr);
@@ -2472,13 +2472,13 @@ solaris_close_tun(struct tuntap *tt)
                 msg(M_WARN | M_ERRNO, "Can't unlink interface(ip)");
             }
 
-            close(tt->ip_fd);
+            platform_close(tt->ip_fd);
             tt->ip_fd = -1;
         }
 
         if (tt->fd >= 0)
         {
-            close(tt->fd);
+            platform_close(tt->fd);
             tt->fd = -1;
         }
     }
@@ -3030,7 +3030,7 @@ utun_open_helper(struct ctl_info ctlInfo, int utunnum)
 
     if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
     {
-        close(fd);
+        platform_close(fd);
         msg(M_INFO, "Opening utun (%s): %s", "ioctl(CTLIOCGINFO)",
             strerror(errno));
         return -2;
@@ -3052,7 +3052,7 @@ utun_open_helper(struct ctl_info ctlInfo, int utunnum)
     {
         msg(M_INFO, "Opening utun (%s): %s", "connect(AF_SYS_CONTROL)",
             strerror(errno));
-        close(fd);
+        platform_close(fd);
         return -1;
     }
 
@@ -3319,7 +3319,7 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
         tt->persistent_if = TRUE;
     }
 
-    if ((tt->fd = open(tunname, O_RDWR)) < 0)
+    if ((tt->fd = platform_open(tunname, O_RDWR)) < 0)
     {
         msg(M_ERR, "Cannot open TAP device '%s'", tunname);
     }