network: convert to strnprintf()

parent 2b38c3a1
...@@ -231,10 +231,10 @@ static int lxc_is_ip_forwarding_enabled(const char *ifname, int family) ...@@ -231,10 +231,10 @@ static int lxc_is_ip_forwarding_enabled(const char *ifname, int family)
if (family != AF_INET && family != AF_INET6) if (family != AF_INET && family != AF_INET6)
return ret_set_errno(-1, EINVAL); return ret_set_errno(-1, EINVAL);
ret = snprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s", ret = strnprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s",
family == AF_INET ? "ipv4" : "ipv6", ifname, family == AF_INET ? "ipv4" : "ipv6", ifname,
"forwarding"); "forwarding");
if (ret < 0 || (size_t)ret >= sizeof(path)) if (ret < 0)
return ret_set_errno(-1, E2BIG); return ret_set_errno(-1, E2BIG);
return lxc_read_file_expect(path, buf, 1, "1"); return lxc_read_file_expect(path, buf, 1, "1");
...@@ -252,8 +252,8 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -252,8 +252,8 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
if (handler->conf->reboot) if (handler->conf->reboot)
lxc_netdev_delete_by_name(veth1); lxc_netdev_delete_by_name(veth1);
} else { } else {
err = snprintf(veth1buf, sizeof(veth1buf), "vethXXXXXX"); err = strnprintf(veth1buf, sizeof(veth1buf), "vethXXXXXX");
if (err < 0 || (size_t)err >= sizeof(veth1buf)) if (err < 0)
return -1; return -1;
veth1 = lxc_ifname_alnum_case_sensitive(veth1buf); veth1 = lxc_ifname_alnum_case_sensitive(veth1buf);
...@@ -264,8 +264,8 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -264,8 +264,8 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
memcpy(netdev->priv.veth_attr.veth1, veth1, IFNAMSIZ); memcpy(netdev->priv.veth_attr.veth1, veth1, IFNAMSIZ);
} }
err = snprintf(veth2buf, sizeof(veth2buf), "vethXXXXXX"); err = strnprintf(veth2buf, sizeof(veth2buf), "vethXXXXXX");
if (err < 0 || (size_t)err >= sizeof(veth2buf)) if (err < 0)
return -1; return -1;
veth2 = lxc_ifname_alnum_case_sensitive(veth2buf); veth2 = lxc_ifname_alnum_case_sensitive(veth2buf);
...@@ -465,8 +465,8 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n ...@@ -465,8 +465,8 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
return -1; return -1;
} }
err = snprintf(peer, sizeof(peer), "mcXXXXXX"); err = strnprintf(peer, sizeof(peer), "mcXXXXXX");
if (err < 0 || (size_t)err >= sizeof(peer)) if (err < 0)
return -1; return -1;
if (!lxc_ifname_alnum_case_sensitive(peer)) if (!lxc_ifname_alnum_case_sensitive(peer))
...@@ -615,8 +615,8 @@ static int instantiate_ipvlan(struct lxc_handler *handler, struct lxc_netdev *ne ...@@ -615,8 +615,8 @@ static int instantiate_ipvlan(struct lxc_handler *handler, struct lxc_netdev *ne
return -1; return -1;
} }
err = snprintf(peer, sizeof(peer), "ipXXXXXX"); err = strnprintf(peer, sizeof(peer), "ipXXXXXX");
if (err < 0 || (size_t)err >= sizeof(peer)) if (err < 0)
return -1; return -1;
if (!lxc_ifname_alnum_case_sensitive(peer)) if (!lxc_ifname_alnum_case_sensitive(peer))
...@@ -690,9 +690,9 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -690,9 +690,9 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
return -1; return -1;
} }
err = snprintf(peer, sizeof(peer), "vlan%d-%d", err = strnprintf(peer, sizeof(peer), "vlan%d-%d",
netdev->priv.vlan_attr.vid, vlan_cntr++); netdev->priv.vlan_attr.vid, vlan_cntr++);
if (err < 0 || (size_t)err >= sizeof(peer)) if (err < 0)
return -1; return -1;
err = lxc_vlan_create(netdev->link, peer, netdev->priv.vlan_attr.vid); err = lxc_vlan_create(netdev->link, peer, netdev->priv.vlan_attr.vid);
...@@ -1173,8 +1173,8 @@ char *is_wlan(const char *ifname) ...@@ -1173,8 +1173,8 @@ char *is_wlan(const char *ifname)
len = strlen(ifname) + strlen(PHYSNAME) - 1; len = strlen(ifname) + strlen(PHYSNAME) - 1;
path = must_realloc(NULL, len + 1); path = must_realloc(NULL, len + 1);
ret = snprintf(path, len, PHYSNAME, ifname); ret = strnprintf(path, len, PHYSNAME, ifname);
if (ret < 0 || (size_t)ret >= len) if (ret < 0)
return NULL; return NULL;
f = fopen(path, "re"); f = fopen(path, "re");
...@@ -1901,9 +1901,10 @@ static int ip_forwarding_set(const char *ifname, int family, int flag) ...@@ -1901,9 +1901,10 @@ static int ip_forwarding_set(const char *ifname, int family, int flag)
if (family != AF_INET && family != AF_INET6) if (family != AF_INET && family != AF_INET6)
return -EINVAL; return -EINVAL;
ret = snprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s", ret = strnprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s",
family == AF_INET ? "ipv4" : "ipv6", ifname, "forwarding"); family == AF_INET ? "ipv4" : "ipv6", ifname,
if (ret < 0 || (size_t)ret >= sizeof(path)) "forwarding");
if (ret < 0)
return -E2BIG; return -E2BIG;
return proc_sys_net_write(path, flag ? "1" : "0"); return proc_sys_net_write(path, flag ? "1" : "0");
...@@ -1927,10 +1928,10 @@ static int neigh_proxy_set(const char *ifname, int family, int flag) ...@@ -1927,10 +1928,10 @@ static int neigh_proxy_set(const char *ifname, int family, int flag)
if (family != AF_INET && family != AF_INET6) if (family != AF_INET && family != AF_INET6)
return -EINVAL; return -EINVAL;
ret = snprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s", ret = strnprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s",
family == AF_INET ? "ipv4" : "ipv6", ifname, family == AF_INET ? "ipv4" : "ipv6", ifname,
family == AF_INET ? "proxy_arp" : "proxy_ndp"); family == AF_INET ? "proxy_arp" : "proxy_ndp");
if (ret < 0 || (size_t)ret >= sizeof(path)) if (ret < 0)
return -E2BIG; return -E2BIG;
return proc_sys_net_write(path, flag ? "1" : "0"); return proc_sys_net_write(path, flag ? "1" : "0");
...@@ -1945,10 +1946,10 @@ static int lxc_is_ip_neigh_proxy_enabled(const char *ifname, int family) ...@@ -1945,10 +1946,10 @@ static int lxc_is_ip_neigh_proxy_enabled(const char *ifname, int family)
if (family != AF_INET && family != AF_INET6) if (family != AF_INET && family != AF_INET6)
return ret_set_errno(-1, EINVAL); return ret_set_errno(-1, EINVAL);
ret = snprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s", ret = strnprintf(path, sizeof(path), "/proc/sys/net/%s/conf/%s/%s",
family == AF_INET ? "ipv4" : "ipv6", ifname, family == AF_INET ? "ipv4" : "ipv6", ifname,
family == AF_INET ? "proxy_arp" : "proxy_ndp"); family == AF_INET ? "proxy_arp" : "proxy_ndp");
if (ret < 0 || (size_t)ret >= sizeof(path)) if (ret < 0)
return ret_set_errno(-1, E2BIG); return ret_set_errno(-1, E2BIG);
return lxc_read_file_expect(path, buf, 1, "1"); return lxc_read_file_expect(path, buf, 1, "1");
...@@ -2313,9 +2314,9 @@ bool is_ovs_bridge(const char *bridge) ...@@ -2313,9 +2314,9 @@ bool is_ovs_bridge(const char *bridge)
struct stat sb; struct stat sb;
char brdirname[22 + IFNAMSIZ + 1] = {0}; char brdirname[22 + IFNAMSIZ + 1] = {0};
ret = snprintf(brdirname, 22 + IFNAMSIZ + 1, "/sys/class/net/%s/bridge", ret = strnprintf(brdirname, 22 + IFNAMSIZ + 1,
bridge); "/sys/class/net/%s/bridge", bridge);
if (ret < 0 || (size_t)ret >= 22 + IFNAMSIZ + 1) if (ret < 0)
return false; return false;
ret = stat(brdirname, &sb); ret = stat(brdirname, &sb);
...@@ -2479,28 +2480,24 @@ char *lxc_ifname_alnum_case_sensitive(char *template) ...@@ -2479,28 +2480,24 @@ char *lxc_ifname_alnum_case_sensitive(char *template)
int setup_private_host_hw_addr(char *veth1) int setup_private_host_hw_addr(char *veth1)
{ {
int err, sockfd; __do_close int sockfd = -EBADF;
int err;
struct ifreq ifr; struct ifreq ifr;
sockfd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); sockfd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sockfd < 0) if (sockfd < 0)
return -errno; return -errno;
err = snprintf((char *)ifr.ifr_name, IFNAMSIZ, "%s", veth1); err = strnprintf((char *)ifr.ifr_name, IFNAMSIZ, "%s", veth1);
if (err < 0 || (size_t)err >= IFNAMSIZ) { if (err < 0)
close(sockfd); return err;
return -E2BIG;
}
err = ioctl(sockfd, SIOCGIFHWADDR, &ifr); err = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
if (err < 0) { if (err < 0)
close(sockfd);
return -errno; return -errno;
}
ifr.ifr_hwaddr.sa_data[0] = 0xfe; ifr.ifr_hwaddr.sa_data[0] = 0xfe;
err = ioctl(sockfd, SIOCSIFHWADDR, &ifr); err = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
close(sockfd);
if (err < 0) if (err < 0)
return -errno; return -errno;
...@@ -2594,8 +2591,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna ...@@ -2594,8 +2591,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
ret = snprintf(pidstr, sizeof(pidstr), "%d", pid); ret = strnprintf(pidstr, sizeof(pidstr), "%d", pid);
if (ret < 0 || ret >= sizeof(pidstr)) if (ret < 0)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
pidstr[sizeof(pidstr) - 1] = '\0'; pidstr[sizeof(pidstr) - 1] = '\0';
...@@ -2792,9 +2789,9 @@ static bool lxc_delete_network_unpriv(struct lxc_handler *handler) ...@@ -2792,9 +2789,9 @@ static bool lxc_delete_network_unpriv(struct lxc_handler *handler)
if (handler->nsfd[LXC_NS_NET] < 0) if (handler->nsfd[LXC_NS_NET] < 0)
return log_debug(false, "Cannot not guarantee safe deletion of network devices. Manual cleanup maybe needed"); return log_debug(false, "Cannot not guarantee safe deletion of network devices. Manual cleanup maybe needed");
ret = snprintf(netns_path, sizeof(netns_path), "/proc/%d/fd/%d", ret = strnprintf(netns_path, sizeof(netns_path), "/proc/%d/fd/%d",
lxc_raw_getpid(), handler->nsfd[LXC_NS_NET]); lxc_raw_getpid(), handler->nsfd[LXC_NS_NET]);
if (ret < 0 || ret >= sizeof(netns_path)) if (ret < 0)
return false; return false;
lxc_list_for_each(iterator, network) { lxc_list_for_each(iterator, network) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment