summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-02-16 17:43:26 +0100
committerThomas Haller <thaller@redhat.com>2018-02-21 12:08:46 +0100
commitff7f8b3a79243b62830137853c985900277adb4a (patch)
treeec9fed530047731c99f5ea2585507dc5a27ce770
parenta7bda2ed12b958fd605e78a2512aa7246046983a (diff)
downloadNetworkManager-ff7f8b3a79243b62830137853c985900277adb4a.tar.gz
netlink: use glib allocator functions for nlmsg_alloc*()
Glib is not out of memory safe, meaning it always aborts the program when an allocation fails. It is not possible to meaningfully handle out of memory when using glib. Replace all allocation functions for netlink message with their glib counter part and remove the NULL checks.
-rw-r--r--src/platform/nm-linux-platform.c21
-rw-r--r--src/platform/nm-netlink.c87
-rw-r--r--src/platform/nm-netlink.h4
-rw-r--r--src/platform/wifi/wifi-utils-nl80211.c19
4 files changed, 33 insertions, 98 deletions
diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index 3c9fa05ef5..0be16c4c20 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -2534,8 +2534,7 @@ _nl_msg_new_link (int nlmsg_type,
nm_assert (NM_IN_SET (nlmsg_type, RTM_DELLINK, RTM_NEWLINK, RTM_GETLINK));
- if (!(msg = nlmsg_alloc_simple (nlmsg_type, nlmsg_flags)))
- g_return_val_if_reached (NULL);
+ msg = nlmsg_alloc_simple (nlmsg_type, nlmsg_flags);
if (nlmsg_append (msg, &ifi, sizeof (ifi), NLMSG_ALIGNTO) < 0)
goto nla_put_failure;
@@ -2577,8 +2576,6 @@ _nl_msg_new_address (int nlmsg_type,
nm_assert (NM_IN_SET (nlmsg_type, RTM_NEWADDR, RTM_DELADDR));
msg = nlmsg_alloc_simple (nlmsg_type, nlmsg_flags);
- if (!msg)
- g_return_val_if_reached (NULL);
if (scope == -1) {
/* Allow having scope unset, and detect the scope (including IPv4 compatibility hack). */
@@ -2691,8 +2688,6 @@ _nl_msg_new_route (int nlmsg_type,
nm_assert (NM_IN_SET (nlmsg_type, RTM_NEWROUTE, RTM_DELROUTE));
msg = nlmsg_alloc_simple (nlmsg_type, (int) nlmsgflags);
- if (!msg)
- g_return_val_if_reached (NULL);
if (nlmsg_append (msg, &rtmsg, sizeof (rtmsg), NLMSG_ALIGNTO) < 0)
goto nla_put_failure;
@@ -2790,8 +2785,6 @@ _nl_msg_new_qdisc (int nlmsg_type,
};
msg = nlmsg_alloc_simple (nlmsg_type, nlmsg_flags);
- if (!msg)
- return NULL;
if (nlmsg_append (msg, &tcm, sizeof (tcm), NLMSG_ALIGNTO) < 0)
goto nla_put_failure;
@@ -2866,8 +2859,6 @@ _nl_msg_new_tfilter (int nlmsg_type,
};
msg = nlmsg_alloc_simple (nlmsg_type, nlmsg_flags);
- if (!msg)
- return NULL;
if (nlmsg_append (msg, &tcm, sizeof (tcm), NLMSG_ALIGNTO) < 0)
goto nla_put_failure;
@@ -4132,8 +4123,6 @@ do_request_all_no_delayed_actions (NMPlatform *platform, DelayedActionType actio
* because we need the sequence number.
*/
nlmsg = nlmsg_alloc_simple (klass->rtm_gettype, NLM_F_DUMP);
- if (!nlmsg)
- continue;
if ( klass->obj_type == NMP_OBJECT_TYPE_QDISC
|| klass->obj_type == NMP_OBJECT_TYPE_TFILTER) {
@@ -6520,11 +6509,7 @@ continue_reading:
guint32 seq_number;
char buf_nlmsghdr[400];
- msg = nlmsg_convert (hdr);
- if (!msg) {
- err = -ENOMEM;
- goto out;
- }
+ msg = nlmsg_alloc_convert (hdr);
nlmsg_set_proto (msg, NETLINK_ROUTE);
nlmsg_set_src (msg, &nla);
@@ -6647,7 +6632,7 @@ stop:
* Repeat reading. */
goto continue_reading;
}
-out:
+
if (interrupted)
err = -NLE_DUMP_INTR;
return err;
diff --git a/src/platform/nm-netlink.c b/src/platform/nm-netlink.c
index cf2e9eaff9..7baf4af1e2 100644
--- a/src/platform/nm-netlink.c
+++ b/src/platform/nm-netlink.c
@@ -296,32 +296,22 @@ nla_reserve (struct nl_msg *msg, int attrtype, int attrlen)
return nla;
}
-static struct nl_msg *
-_nlmsg_alloc(size_t len)
+struct nl_msg *
+nlmsg_alloc_size (size_t len)
{
struct nl_msg *nm;
- if (len < sizeof(struct nlmsghdr))
- len = sizeof(struct nlmsghdr);
+ if (len < sizeof (struct nlmsghdr))
+ len = sizeof (struct nlmsghdr);
- nm = calloc(1, sizeof(*nm));
- if (!nm)
- goto errout;
+ nm = g_new0 (struct nl_msg, 1);
nm->nm_refcnt = 1;
-
- nm->nm_nlh = calloc(1, len);
- if (!nm->nm_nlh)
- goto errout;
-
nm->nm_protocol = -1;
nm->nm_size = len;
- nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
-
+ nm->nm_nlh = g_malloc0 (len);
+ nm->nm_nlh->nlmsg_len = nlmsg_total_size (0);
return nm;
-errout:
- free(nm);
- return NULL;
}
/**
@@ -336,25 +326,19 @@ errout:
struct nl_msg *
nlmsg_alloc (void)
{
- return _nlmsg_alloc (get_default_page_size ());
+ return nlmsg_alloc_size (get_default_page_size ());
}
/**
* Allocate a new netlink message with maximum payload size specified.
*/
struct nl_msg *
-nlmsg_alloc_size (size_t max)
-{
- return _nlmsg_alloc (max);
-}
-
-struct nl_msg *
-nlmsg_inherit (struct nlmsghdr *hdr)
+nlmsg_alloc_inherit (struct nlmsghdr *hdr)
{
struct nl_msg *nm;
- nm = nlmsg_alloc();
- if (nm && hdr) {
+ nm = nlmsg_alloc ();
+ if (hdr) {
struct nlmsghdr *new = nm->nm_nlh;
new->nlmsg_type = hdr->nlmsg_type;
@@ -367,16 +351,12 @@ nlmsg_inherit (struct nlmsghdr *hdr)
}
struct nl_msg *
-nlmsg_convert (struct nlmsghdr *hdr)
+nlmsg_alloc_convert (struct nlmsghdr *hdr)
{
struct nl_msg *nm;
- nm = _nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len));
- if (!nm)
- return NULL;
-
+ nm = nlmsg_alloc_size (NLMSG_ALIGN (hdr->nlmsg_len));
memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
-
return nm;
}
@@ -388,7 +368,7 @@ nlmsg_alloc_simple (int nlmsgtype, int flags)
.nlmsg_flags = flags,
};
- return nlmsg_inherit (&nlh);
+ return nlmsg_alloc_inherit (&nlh);
}
int
@@ -396,7 +376,7 @@ nlmsg_append (struct nl_msg *n, void *data, size_t len, int pad)
{
void *tmp;
- tmp = nlmsg_reserve(n, len, pad);
+ tmp = nlmsg_reserve (n, len, pad);
if (tmp == NULL)
return -ENOMEM;
@@ -1094,11 +1074,7 @@ continue_reading:
hdr = (struct nlmsghdr *) buf;
while (nlmsg_ok(hdr, n)) {
nlmsg_free(msg);
- msg = nlmsg_convert(hdr);
- if (!msg) {
- err = -ENOMEM;
- goto out;
- }
+ msg = nlmsg_alloc_convert(hdr);
nlmsg_set_proto(msg, sk->s_proto);
nlmsg_set_src(msg, &nla);
@@ -1351,20 +1327,11 @@ nl_recv (struct nl_sock *sk, struct sockaddr_nl *nla,
page_size = getpagesize() * 4;
iov.iov_len = sk->s_bufsize ? : page_size;
- iov.iov_base = malloc(iov.iov_len);
-
- if (!iov.iov_base) {
- retval = -ENOMEM;
- goto abort;
- }
+ iov.iov_base = g_malloc (iov.iov_len);
if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
- msg.msg_control = malloc(msg.msg_controllen);
- if (!msg.msg_control) {
- retval = -ENOMEM;
- goto abort;
- }
+ msg.msg_control = g_malloc (msg.msg_controllen);
}
retry:
@@ -1390,11 +1357,7 @@ retry:
}
msg.msg_controllen *= 2;
- tmp = realloc(msg.msg_control, msg.msg_controllen);
- if (!tmp) {
- retval = -ENOMEM;
- goto abort;
- }
+ tmp = g_realloc (msg.msg_control, msg.msg_controllen);
msg.msg_control = tmp;
goto retry;
}
@@ -1412,11 +1375,7 @@ retry:
* to size of n (which should be total length of the message)
* and try again. */
iov.iov_len = n;
- tmp = realloc(iov.iov_base, iov.iov_len);
- if (!tmp) {
- retval = -ENOMEM;
- goto abort;
- }
+ tmp = g_realloc (iov.iov_base, iov.iov_len);
iov.iov_base = tmp;
flags = 0;
goto retry;
@@ -1441,11 +1400,7 @@ retry:
continue;
if (cmsg->cmsg_type != SCM_CREDENTIALS)
continue;
- tmpcreds = malloc(sizeof(*tmpcreds));
- if (!tmpcreds) {
- retval = -ENOMEM;
- goto abort;
- }
+ tmpcreds = g_malloc (sizeof(*tmpcreds));
memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds));
break;
}
diff --git a/src/platform/nm-netlink.h b/src/platform/nm-netlink.h
index 9c89748009..43bd238663 100644
--- a/src/platform/nm-netlink.h
+++ b/src/platform/nm-netlink.h
@@ -280,9 +280,9 @@ struct nl_msg *nlmsg_alloc (void);
struct nl_msg *nlmsg_alloc_size (size_t max);
-struct nl_msg *nlmsg_inherit (struct nlmsghdr *hdr);
+struct nl_msg *nlmsg_alloc_inherit (struct nlmsghdr *hdr);
-struct nl_msg *nlmsg_convert (struct nlmsghdr *hdr);
+struct nl_msg *nlmsg_alloc_convert (struct nlmsghdr *hdr);
struct nl_msg *nlmsg_alloc_simple (int nlmsgtype, int flags);
diff --git a/src/platform/wifi/wifi-utils-nl80211.c b/src/platform/wifi/wifi-utils-nl80211.c
index ed843bcea1..e0a9633591 100644
--- a/src/platform/wifi/wifi-utils-nl80211.c
+++ b/src/platform/wifi/wifi-utils-nl80211.c
@@ -88,8 +88,6 @@ genl_ctrl_resolve (struct nl_sock *sk, const char *name)
};
msg = nlmsg_alloc ();
- if (!msg)
- goto out;
if (!genlmsg_put (msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL,
0, 0, CTRL_CMD_GETFAMILY, 1))
@@ -165,19 +163,16 @@ error_handler (struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
static struct nl_msg *
_nl80211_alloc_msg (int id, int ifindex, int phy, guint32 cmd, guint32 flags)
{
- struct nl_msg *msg;
+ nm_auto_nlmsg struct nl_msg *msg = NULL;
msg = nlmsg_alloc ();
- if (msg) {
- genlmsg_put (msg, 0, 0, id, 0, flags, cmd, 0);
- NLA_PUT_U32 (msg, NL80211_ATTR_IFINDEX, ifindex);
- if (phy != -1)
- NLA_PUT_U32 (msg, NL80211_ATTR_WIPHY, phy);
- }
- return msg;
+ genlmsg_put (msg, 0, 0, id, 0, flags, cmd, 0);
+ NLA_PUT_U32 (msg, NL80211_ATTR_IFINDEX, ifindex);
+ if (phy != -1)
+ NLA_PUT_U32 (msg, NL80211_ATTR_WIPHY, phy);
+ return g_steal_pointer (&msg);
- nla_put_failure:
- nlmsg_free (msg);
+nla_put_failure:
return NULL;
}