summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2016-03-07 11:11:59 +0100
committerThomas Haller <thaller@redhat.com>2016-03-07 11:36:57 +0100
commit0e90f1ba83c4204cad1da6d96e00e99f8c168534 (patch)
tree0a410bea061ac6627257be463f6850ae8e82b0bc
parent1dbe1d70df6d7f34d5ae636f30859d438d917a18 (diff)
downloadNetworkManager-0e90f1ba83c4204cad1da6d96e00e99f8c168534.tar.gz
platform: add and use nm_utils_ifname_cpy() helper
Coverity complains rightly about "strncpy (dst, ifname, IFNAMSIZ)" because it might leave @dst non-NULL-terminated, in case @ifname is too long (which already would be a bug in the first place). Replace the strcpy() uses by a new helper nm_utils_ifname_cpy() that asserts against valid arguments.
-rw-r--r--src/devices/tests/test-lldp.c5
-rw-r--r--src/nm-core-utils.c11
-rw-r--r--src/nm-core-utils.h2
-rw-r--r--src/platform/nm-linux-platform.c2
-rw-r--r--src/platform/nm-platform-utils.c9
-rw-r--r--src/platform/tests/test-general.c3
-rw-r--r--src/platform/wifi/wifi-utils-wext.c24
7 files changed, 36 insertions, 20 deletions
diff --git a/src/devices/tests/test-lldp.c b/src/devices/tests/test-lldp.c
index dfae58b114..978821a686 100644
--- a/src/devices/tests/test-lldp.c
+++ b/src/devices/tests/test-lldp.c
@@ -27,8 +27,11 @@
#include <sys/types.h>
#include "nm-lldp-listener.h"
+
#include "test-common.h"
+#include "nm-test-utils.h"
+
typedef struct {
int ifindex;
int fd;
@@ -48,7 +51,7 @@ fixture_setup (test_fixture *fixture, gconstpointer user_data)
g_assert (fd >= 0);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
- strncpy (ifr.ifr_name, TEST_IFNAME, IFNAMSIZ);
+ nm_utils_ifname_cpy (ifr.ifr_name, TEST_IFNAME);
g_assert (ioctl (fd, TUNSETIFF, &ifr) >= 0);
/* Bring the interface up */
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c
index 256be10888..8d0131a780 100644
--- a/src/nm-core-utils.c
+++ b/src/nm-core-utils.c
@@ -2942,4 +2942,15 @@ nm_utils_parse_debug_string (const char *string,
return result;
}
+void
+nm_utils_ifname_cpy (char *dst, const char *name)
+{
+ g_return_if_fail (dst);
+ g_return_if_fail (name && name[0]);
+
+ nm_assert (nm_utils_iface_valid_name (name));
+
+ if (g_strlcpy (dst, name, IFNAMSIZ) >= IFNAMSIZ)
+ g_return_if_reached ();
+}
diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h
index 7a69fb1928..91a0899690 100644
--- a/src/nm-core-utils.h
+++ b/src/nm-core-utils.h
@@ -412,4 +412,6 @@ guint nm_utils_parse_debug_string (const char *string,
const GDebugKey *keys,
guint nkeys);
+void nm_utils_ifname_cpy (char *dst, const char *name);
+
#endif /* __NM_CORE_UTILS_H__ */
diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index 5a34827622..eb90211fe4 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -4835,7 +4835,7 @@ tun_add (NMPlatform *platform, const char *name, gboolean tap,
if (fd < 0)
return FALSE;
- strncpy (ifr.ifr_name, name, IFNAMSIZ);
+ nm_utils_ifname_cpy (ifr.ifr_name, name);
ifr.ifr_flags = tap ? IFF_TAP : IFF_TUN;
if (!pi)
diff --git a/src/platform/nm-platform-utils.c b/src/platform/nm-platform-utils.c
index 0f2656f238..6020a8f3be 100644
--- a/src/platform/nm-platform-utils.c
+++ b/src/platform/nm-platform-utils.c
@@ -56,7 +56,7 @@ ethtool_get (const char *name, gpointer edata)
nm_assert (strlen (name) < IFNAMSIZ);
memset (&ifr, 0, sizeof (ifr));
- strcpy (ifr.ifr_name, name);
+ nm_utils_ifname_cpy (ifr.ifr_name, name);
ifr.ifr_data = edata;
fd = socket (PF_INET, SOCK_DGRAM, 0);
@@ -344,7 +344,7 @@ nmp_utils_mii_supports_carrier_detect (const char *ifname)
}
memset (&ifr, 0, sizeof (struct ifreq));
- strncpy (ifr.ifr_name, ifname, IFNAMSIZ);
+ nm_utils_ifname_cpy (ifr.ifr_name, ifname);
errno = 0;
if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0) {
@@ -513,13 +513,14 @@ gboolean
nmp_utils_device_exists (const char *name)
{
#define SYS_CLASS_NET "/sys/class/net/"
- char sysdir[NM_STRLEN (SYS_CLASS_NET) + IFNAMSIZ] = SYS_CLASS_NET;
+ char sysdir[NM_STRLEN (SYS_CLASS_NET) + IFNAMSIZ];
if ( !name
|| strlen (name) >= IFNAMSIZ
|| !nm_utils_is_valid_path_component (name))
g_return_val_if_reached (FALSE);
- strcpy (&sysdir[NM_STRLEN (SYS_CLASS_NET)], name);
+ memcpy (sysdir, SYS_CLASS_NET, NM_STRLEN (SYS_CLASS_NET));
+ nm_utils_ifname_cpy (&sysdir[NM_STRLEN (SYS_CLASS_NET)], name);
return g_file_test (sysdir, G_FILE_TEST_EXISTS);
}
diff --git a/src/platform/tests/test-general.c b/src/platform/tests/test-general.c
index f342e5653e..eb5adb9541 100644
--- a/src/platform/tests/test-general.c
+++ b/src/platform/tests/test-general.c
@@ -18,12 +18,11 @@
* Copyright (C) 2015 Red Hat, Inc.
*/
-#include "nm-platform-utils.h"
+#include "nm-default.h"
#include <linux/rtnetlink.h>
#include "nm-linux-platform.h"
-#include "nm-default.h"
#include "nm-test-utils.h"
diff --git a/src/platform/wifi/wifi-utils-wext.c b/src/platform/wifi/wifi-utils-wext.c
index 613c03b7b2..26f4ce41a8 100644
--- a/src/platform/wifi/wifi-utils-wext.c
+++ b/src/platform/wifi/wifi-utils-wext.c
@@ -101,7 +101,7 @@ wifi_wext_get_mode (WifiData *data)
struct iwreq wrq;
memset (&wrq, 0, sizeof (struct iwreq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCGIWMODE, &wrq) < 0) {
if (errno != ENODEV) {
@@ -150,7 +150,7 @@ wifi_wext_set_mode (WifiData *data, const NM80211Mode mode)
return FALSE;
}
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCSIWMODE, &wrq) < 0) {
if (errno != ENODEV) {
nm_log_err (LOGD_HW | LOGD_WIFI, "(%s): error setting mode %d",
@@ -174,7 +174,7 @@ wifi_wext_set_powersave (WifiData *data, guint32 powersave)
} else
wrq.u.power.disabled = 1;
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCSIWPOWER, &wrq) < 0) {
if (errno != ENODEV) {
nm_log_err (LOGD_HW | LOGD_WIFI, "(%s): error setting powersave %" G_GUINT32_FORMAT,
@@ -193,7 +193,7 @@ wifi_wext_get_freq (WifiData *data)
struct iwreq wrq;
memset (&wrq, 0, sizeof (struct iwreq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCGIWFREQ, &wrq) < 0) {
nm_log_warn (LOGD_HW | LOGD_WIFI,
"(%s): error getting frequency: %s",
@@ -227,7 +227,7 @@ wifi_wext_get_bssid (WifiData *data, guint8 *out_bssid)
struct iwreq wrq;
memset (&wrq, 0, sizeof (wrq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCGIWAP, &wrq) < 0) {
nm_log_warn (LOGD_HW | LOGD_WIFI,
"(%s): error getting associated BSSID: %s",
@@ -246,7 +246,7 @@ wifi_wext_get_rate (WifiData *data)
int err;
memset (&wrq, 0, sizeof (wrq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
err = ioctl (wext->fd, SIOCGIWRATE, &wrq);
return ((err == 0) ? wrq.u.bitrate.value / 1000 : 0);
}
@@ -356,7 +356,7 @@ wifi_wext_get_qual (WifiData *data)
wrq.u.data.pointer = &stats;
wrq.u.data.length = sizeof (stats);
wrq.u.data.flags = 1; /* Clear updated flag */
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCGIWSTATS, &wrq) < 0) {
nm_log_warn (LOGD_HW | LOGD_WIFI,
@@ -393,7 +393,7 @@ wifi_wext_set_mesh_channel (WifiData *data, guint32 channel)
struct iwreq wrq;
memset (&wrq, 0, sizeof (struct iwreq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (channel > 0) {
wrq.u.freq.flags = IW_FREQ_FIXED;
@@ -425,7 +425,7 @@ wifi_wext_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len)
wrq.u.essid.length = len;
wrq.u.essid.flags = (len > 0) ? 1 : 0; /* 1=enable SSID, 0=disable/any */
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCSIWESSID, &wrq) == 0)
return TRUE;
@@ -448,7 +448,7 @@ wext_can_scan (WifiDataWext *wext)
struct iwreq wrq;
memset (&wrq, 0, sizeof (struct iwreq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
if (ioctl (wext->fd, SIOCSIWSCAN, &wrq) < 0) {
if (errno == EOPNOTSUPP)
return FALSE;
@@ -466,7 +466,7 @@ wext_get_range (WifiDataWext *wext,
struct iwreq wrq;
memset (&wrq, 0, sizeof (struct iwreq));
- g_strlcpy (wrq.ifr_name, wext->parent.iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (wrq.ifr_name, wext->parent.iface);
wrq.u.data.pointer = (caddr_t) range;
wrq.u.data.length = sizeof (struct iw_range);
@@ -666,7 +666,7 @@ wifi_wext_is_wifi (const char *iface)
fd = socket (PF_INET, SOCK_DGRAM, 0);
if (fd >= 0) {
- g_strlcpy (iwr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
+ nm_utils_ifname_cpy (iwr.ifr_ifrn.ifrn_name, iface);
if (ioctl (fd, SIOCGIWNAME, &iwr) == 0)
is_wifi = TRUE;
close (fd);