Skip to content

Commit

Permalink
lib: return error if an incomplete message was read
Browse files Browse the repository at this point in the history
If recvmsg indicates that the message read was truncated libnl retries
to read the complete message after increasing the message buffer. This
only works if the message flags MSG_PEEK | MSG_TRUNC are set. If
NL_MSG_PEEK is not enabled on the nl_sock structure, flags are left
empty and the rest of the truncated message is discarded, hence a
subsequent recvmsg returns the next message (in case of a multipart
message, the NLMSG_DONE) is read and returned.
This patch aborts message processing if the message was truncated and
the NL_MSG_PEEK flags was not activated for the nl_sock structure.

http://lists.infradead.org/pipermail/libnl/2015-June/001888.html

[thaller@redhat.com: add NL_CAPABILITY_NL_RECV_FAIL_TRUNK_NO_PEEK]

Signed-off-by: Thomas Egerer <hakke_007@gmx.de>
Signed-off-by: Thomas Haller <thaller@redhat.com>
  • Loading branch information
spellingmistake authored and thom311 committed Jun 5, 2015
1 parent e206c25 commit bbdcaea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/netlink/utils.h
Expand Up @@ -135,6 +135,14 @@ enum {
NL_CAPABILITY_VERSION_3_2_26 = 7,
#define NL_CAPABILITY_VERSION_3_2_26 NL_CAPABILITY_VERSION_3_2_26

/**
* nl_recv() fails with NLE_MSG_TRUNC if a message got truncated
* with NL_MSG_PEEK disabled. Previously, the failed message was wrongly
* discarded and the next message received.
*/
NL_CAPABILITY_NL_RECV_FAIL_TRUNK_NO_PEEK = 8,
#define NL_CAPABILITY_NL_RECV_FAIL_TRUNK_NO_PEEK NL_CAPABILITY_NL_RECV_FAIL_TRUNK_NO_PEEK

__NL_CAPABILITY_MAX,
NL_CAPABILITY_MAX = (__NL_CAPABILITY_MAX - 1),
#define NL_CAPABILITY_MAX NL_CAPABILITY_MAX
Expand Down
7 changes: 7 additions & 0 deletions lib/nl.c
Expand Up @@ -725,6 +725,13 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,

if (iov.iov_len < n || (msg.msg_flags & MSG_TRUNC)) {
void *tmp;

/* respond with error to an incomplete message */
if (!(sk->s_flags & NL_MSG_PEEK)) {
retval = -NLE_MSG_TRUNC;
goto abort;
}

/* Provided buffer is not long enough, enlarge it
* to size of n (which should be total length of the message)
* and try again. */
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.c
Expand Up @@ -1150,7 +1150,7 @@ int nl_has_capability (int capability)
NL_CAPABILITY_ROUTE_LINK_GET_KERNEL_FAIL_OPNOTSUPP,
NL_CAPABILITY_ROUTE_ADDR_COMPARE_CACHEINFO,
NL_CAPABILITY_VERSION_3_2_26,
0),
NL_CAPABILITY_NL_RECV_FAIL_TRUNK_NO_PEEK),
/* IMPORTANT: these capability numbers are intended to be universal and stable
* for libnl3. Don't allocate new numbers on your own that differ from upstream
* libnl3.
Expand Down

0 comments on commit bbdcaea

Please sign in to comment.