nl80211: fix wpa supplicant ctrl socket permissions
[project/iwinfo.git] / iwinfo_nl80211.c
index d4d7538263df0105651ce1ebdfff152ae68428c0..aceef18230ffd9e419dd1ddaf4ff758004eb9390 100644 (file)
@@ -938,6 +938,18 @@ static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
                sprintf(remote.sun_path, "/var/run/wpa_supplicant-%s/%s",
                        ifname, ifname);
 
+       /* Set client socket file permissions so that bind() creates the client
+       * socket with these permissions and there is no need to try to change
+       * them with chmod() after bind() which would have potential issues with
+       * race conditions. These permissions are needed to make sure the server
+       * side (wpa_supplicant or hostapd) can reply to the control interface
+       * messages.
+       *
+       * The lchown() calls below after bind() are also part of the needed
+       * operations to allow the response to go through. Those are using the
+       * no-deference-symlinks version to avoid races. */
+       fchmod(sock, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+
        if (fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0)
        {
                close(sock);
@@ -966,6 +978,10 @@ static int nl80211_wpactl_connect(const char *ifname, struct sockaddr_un *local)
                return -1;
        }
 
+       /* Set group even if we do not have privileges to change owner */
+       lchown(local->sun_path, -1, 101);
+       lchown(local->sun_path, 101, 101);
+
        return sock;
 }
 
@@ -1095,7 +1111,6 @@ static char * nl80211_ifadd(const char *ifname)
 static void nl80211_ifdel(const char *ifname)
 {
        struct nl80211_msg_conveyor *req;
-       int err;
 
        req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
        if (req)
@@ -1235,7 +1250,7 @@ static int nl80211_get_ssid(const char *ifname, char *buf)
        /* failed, try to obtain Mesh ID */
        if (sb.ssid[0] == 0)
                iwinfo_ubus_query(res ? res : ifname, "mesh_id",
-                                 sb.ssid, IWINFO_ESSID_MAX_SIZE + 1);
+                                 buf, IWINFO_ESSID_MAX_SIZE + 1);
 
        return (sb.ssid[0] == 0) ? -1 : 0;
 }
@@ -2371,7 +2386,6 @@ static int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
 {
        int err, ch_cur;
        int dbm_max = -1, dbm_cur, dbm_cnt;
-       struct nl80211_msg_conveyor *req;
        struct iwinfo_txpwrlist_entry entry;
 
        if (nl80211_get_channel(ifname, &ch_cur))
@@ -2901,6 +2915,8 @@ static int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
                                iwinfo_ifup(ifname);
                                nl80211_hostapd_hup(ifname);
                        }
+                       else
+                               rv = -1;
 
                        nl80211_ifdel(res);
                        return rv;
@@ -3052,7 +3068,7 @@ struct nl80211_modes
        uint16_t he_phy_cap[6];
 };
 
-static int nl80211_eval_modelist(struct nl80211_modes *m)
+static void nl80211_eval_modelist(struct nl80211_modes *m)
 {
        /* Treat any nonzero capability as 11n */
        if (m->nl_ht > 0)
@@ -3119,8 +3135,6 @@ static int nl80211_get_modelist_cb(struct nl_msg *msg, void *arg)
 {
        struct nl80211_modes *m = arg;
        int bands_remain, freqs_remain;
-       uint16_t caps = 0;
-       uint32_t vht_caps = 0;
        struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
        struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
@@ -3390,7 +3404,6 @@ static int nl80211_get_mbssid_support(const char *ifname, int *buf)
 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
 {
        char *phy, compat[64], path[PATH_MAX];
-       int i;
 
        /* Try to determine the phy name from the given interface */
        phy = nl80211_ifname2phy(ifname);