Skip to contentSkip to conversation

libnfnetlink

PATCH In review Maik Hentsche 3 participants 4 messages Sep 29, 2006 – Oct 11, 2006 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libnfnetlink

Hello Pablo, hello readers of the list,
I found another bug in libnfnetlink. The comment of nfnl_recv states, in case of success 0 is returned. In fact at success the returnvalue of recvfrom is returned, which is the number of received bytes (libnfnetlink_recv_comment.patch). The second issue is a little more serious. The comment states, in case of an error, errno is set when in fact it is not. I appended a patch for two occurences, but I since I don't know, in which case addrlen might be != sizeof(peer) and what peer.nl_pid means (and therefore why it is a problem, if it's not 0) two error cases without appropriate errno value still exist.

so long
Maik

src/libnfnetlink.c +6 -2
diff -U 3 -b -B -d -i -r -N -x .svn -- libnfnetlink-0.0.16-pablo-r1/src/libnfnetlink.c libnfnetlink-0.0.16-pablo-r1.new/src/libnfnetlink.c
@@ -462,8 +462,10 @@
 	assert(len > 0);
 	
 	if (len < sizeof(struct nlmsgerr)
-	    || len < sizeof(struct nlmsghdr))
+	    || len < sizeof(struct nlmsghdr)){
+		errno = ENOBUFS;
 		return -1; 
+	}
 
 	addrlen = sizeof(h->peer);
 	status = recvfrom(h->fd, buf, len, 0, (struct sockaddr *)&peer,	
@@ -478,8 +480,10 @@
 		return -1;
 
 	nlh = (struct nlmsghdr *)buf;
-	if (nlh->nlmsg_flags & MSG_TRUNC || status > len)
+	if (nlh->nlmsg_flags & MSG_TRUNC || status > len){
+		errno = ENOBUFS;
 		return -1;
+	}
 
 	return status;
 }
src/libnfnetlink.c +2 -2
diff -U 3 -b -B -d -i -r -N -x .svn -- libnfnetlink-0.0.16-pablo-r1/src/libnfnetlink.c libnfnetlink-0.0.16-pablo-r1.new/src/libnfnetlink.c
@@ -443,8 +443,8 @@
  * that the data is well-formed. Such checkings are done by the parsing
  * functions.
  *
- * On success, 0 is returned. On error, -1 is returned and errno is set
- * appropiately.
+ * On success, the number of bytes received is returned. On error, -1 is
+ * returned and errno is set appropiately.
  *
  * Note that ENOBUFS is returned in case that nfnetlink is exhausted. In
  * that case is possible that the information requested is incomplete.
#1 lore raw
Quoted 10 lines · Maik Hentsche

Hello Pablo, hello readers of the list,
I found another bug in libnfnetlink. The comment of nfnl_recv states, in case of success 0 is returned. In fact at success the returnvalue of recvfrom is returned, which is the number of received bytes (libnfnetlink_recv_comment.patch). The second issue is a little more serious. The comment states, in case of an error, errno is set when in fact it is not. I appended a patch for two occurences, but I since I don't know, in which case addrlen might be != sizeof(peer) and what peer.nl_pid means (and therefore why it is a problem, if it's not 0) two error cases without appropriate errno value still exist.

addrlen != sizeof(peer) should never happen. I can't think of anything better than EINVAL. nl_pid != 0 means the message originated in userspace and some other program is trying to feed us messages. We could handle this by just calling recvmsg again. But this is mainly because I can't think of a proper errno code for this either :)

#2 lore raw
Quoted 12 lines · Patrick McHardy
Quoted 10 lines · Maik Hentsche

Hello Pablo, hello readers of the list,
I found another bug in libnfnetlink. The comment of nfnl_recv states, in case of success 0 is returned. In fact at success the returnvalue of recvfrom is returned, which is the number of received bytes (libnfnetlink_recv_comment.patch). The second issue is a little more serious. The comment states, in case of an error, errno is set when in fact it is not. I appended a patch for two occurences, but I since I don't know, in which case addrlen might be != sizeof(peer) and what peer.nl_pid means (and therefore why it is a problem, if it's not 0) two error cases without appropriate errno value still exist.

Quoted 5 lines

addrlen != sizeof(peer) should never happen. I can't think of anything better than EINVAL. nl_pid != 0 means the message originated in userspace and some other program is trying to feed us messages. We could handle this by just calling recvmsg again. But this is mainly because I can't think of a proper errno code for this either :)

what do you think about the following solution?

Quoted 2 lines
	if (len < sizeof(struct nlmsgerr)
	    || len < sizeof(struct nlmsghdr))

errno = EBADMSG;

Quoted 2 lines

[...]

	if (addrlen != sizeof(peer))

errno = EINVAL;

Quoted 3 lines
		return -1;

	if (peer.nl_pid != 0)

errno = ENOMSG;

Quoted 4 lines
		return -1;

	nlh = (struct nlmsghdr *)buf;
	if (nlh->nlmsg_flags & MSG_TRUNC || status > len)

errno = ENOSPC;

Quoted 1 line
		return -1;
Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#3 lore raw
Quoted 17 lines · Pablo Neira Ayuso

what do you think about the following solution?

Quoted 2 lines
	if (len < sizeof(struct nlmsgerr)
	    || len < sizeof(struct nlmsghdr))

errno = EBADMSG;

Quoted 2 lines

[...]

	if (addrlen != sizeof(peer))

errno = EINVAL;

Quoted 6 lines
Quoted 3 lines
		return -1;

	if (peer.nl_pid != 0)

errno = ENOMSG;

The above all seem fine.

Quoted 8 lines
Quoted 5 lines
		return -1;

	nlh = (struct nlmsghdr *)buf;
	if (nlh->nlmsg_flags & MSG_TRUNC || status > len)

errno = ENOSPC;

ENOSPC is fine for MSG_TRUNC, but it is a msghdr flag, not nlmsg. status > len implies serious recvmsg brokeness and it doesn't really make sense to check for kernel bugs, so I'd remove it.