Use umask() to set file and UNIX socket permissions without race conditions.
[tinc] / src / net_setup.c
index ee8296c..0fedafa 100644 (file)
@@ -711,7 +711,12 @@ static bool setup_myself(void) {
        get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
        strictsubnets |= tunnelserver;
 
-
+       if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
+               if(max_connection_burst <= 0) {
+                       logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
+                       return false;
+               }
+       }
 
        if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
                if(udp_rcvbuf <= 0) {
@@ -828,12 +833,11 @@ static bool setup_myself(void) {
                io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
 
        /* Run tinc-up script to further initialize the tap interface */
-       char *envp[5];
+       char *envp[5] = {NULL};
        xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
        xasprintf(&envp[1], "DEVICE=%s", device ? : "");
        xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
        xasprintf(&envp[3], "NAME=%s", myself->name);
-       envp[4] = NULL;
 
        execute_script("tinc-up", envp);
 
@@ -864,7 +868,12 @@ static bool setup_myself(void) {
 
        unlink(unixsocketname);
 
-       if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
+       mode_t mask = umask(0);
+       umask(mask | 077);
+       int result = bind(unix_fd, (struct sockaddr *)&sa, sizeof sa);
+       umask(mask);
+
+       if(result < 0) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
                return false;
        }
@@ -1065,12 +1074,11 @@ void close_network_connections(void) {
        close(unix_socket.fd);
 #endif
 
-       char *envp[5];
+       char *envp[5] = {NULL};
        xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
        xasprintf(&envp[1], "DEVICE=%s", device ? : "");
        xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
        xasprintf(&envp[3], "NAME=%s", myself->name);
-       envp[4] = NULL;
 
        exit_requests();
        exit_edges();