Skip to contentSkip to conversation

old NLMSG_OK fix

PATCH Christoph Hellwig 3 participants 5 messages May 31, 2004 – Jun 28, 2004

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I just stumbled over NLMSG_OK and RTA_OK changes in the debian kernel package. It seems they are from
http://oss.sgi.com/projects/netdev/archive/2000-09/msg00001.html

Any comments?

include/linux/netlink.h +3 -1
diff -urN kernel-source-2.6.6/include/linux/netlink.h kernel-source-2.6.6-1/include/linux/netlink.h
@@ -73,7 +73,8 @@
 #define NLMSG_DATA(nlh)  ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))
 #define NLMSG_NEXT(nlh,len)	 ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
 				  (struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
-#define NLMSG_OK(nlh,len) ((len) > 0 && (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
+#define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
+			   (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
 			   (nlh)->nlmsg_len <= (len))
 #define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))
 
@@ -93,6 +94,7 @@
 #ifdef __KERNEL__
 
 #include <linux/capability.h>
+#include <linux/skbuff.h>
 
 struct netlink_skb_parms
 {
include/linux/rtnetlink.h +2 -1
diff -urN kernel-source-2.6.6/include/linux/rtnetlink.h kernel-source-2.6.6-1/include/linux/rtnetlink.h
@@ -69,7 +69,8 @@
 
 #define RTA_ALIGNTO	4
 #define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) )
-#define RTA_OK(rta,len) ((len) > 0 && (rta)->rta_len >= sizeof(struct rtattr) && \
+#define RTA_OK(rta,len) ((len) >= (int)sizeof(struct rtattr) && \
+			 (rta)->rta_len >= sizeof(struct rtattr) && \
 			 (rta)->rta_len <= (len))
 #define RTA_NEXT(rta,attrlen)	((attrlen) -= RTA_ALIGN((rta)->rta_len), \
 				 (struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
#1 lore raw
Quoted 5 lines · Christoph Hellwig

I just stumbled over NLMSG_OK and RTA_OK changes in the debian kernel package. It seems they are from
http://oss.sgi.com/projects/netdev/archive/2000-09/msg00001.html

Any comments?

ping

include/linux/netlink.h +2 -1
@@ -73,7 +73,8 @@
 #define NLMSG_DATA(nlh)  ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))
 #define NLMSG_NEXT(nlh,len)	 ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
 				  (struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
-#define NLMSG_OK(nlh,len) ((len) > 0 && (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
+#define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
+			   (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
 			   (nlh)->nlmsg_len <= (len))
 #define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))
 
include/linux/rtnetlink.h +2 -1
@@ -69,7 +69,8 @@
 
 #define RTA_ALIGNTO	4
 #define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) )
-#define RTA_OK(rta,len) ((len) > 0 && (rta)->rta_len >= sizeof(struct rtattr) && \
+#define RTA_OK(rta,len) ((len) >= (int)sizeof(struct rtattr) && \
+			 (rta)->rta_len >= sizeof(struct rtattr) && \
 			 (rta)->rta_len <= (len))
 #define RTA_NEXT(rta,attrlen)	((attrlen) -= RTA_ALIGN((rta)->rta_len), \
 				 (struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
#2 lore raw
Quoted 1 line · On Sun, 27 Jun 2004 19:15:52 +0200 Christoph Hellwig <[email protected]> wrote:

http://oss.sgi.com/projects/netdev/archive/2000-09/msg00001.html

It works because there is always 16 bytes of scratch at the end of an SKB more than was allocated for the actual data. So blindly deref'ing the nlmsg_len value is fine here.

There is no danger for OOPS's or kernel corruption.

I believe I responded exactly like this the last time this patch was presented.

#3 lore raw
Quoted 8 lines · David S. Miller
Quoted 1 line · On Sun, 27 Jun 2004 19:15:52 +0200 Christoph Hellwig <[email protected]> wrote:

http://oss.sgi.com/projects/netdev/archive/2000-09/msg00001.html

It works because there is always 16 bytes of scratch at the end of an SKB more than was allocated for the actual data. So blindly deref'ing the nlmsg_len value is fine here.

Yes but this is also used by user-space appliations where this scratch space may not exist. NETLINK messages can travel from one application to another so exploits are possible.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email:  Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 6 lines · On Mon, 28 Jun 2004 19:43:37 +1000 Herbert Xu <[email protected]>…
Quoted 5 lines · David S. Miller
Quoted 1 line · On Sun, 27 Jun 2004 19:15:52 +0200 Christoph Hellwig <[email protected]> wrote:

http://oss.sgi.com/projects/netdev/archive/2000-09/msg00001.html

Quoted 7 lines
Quoted 3 lines

It works because there is always 16 bytes of scratch at the end of an SKB more than was allocated for the actual data. So blindly deref'ing the nlmsg_len value is fine here.

Yes but this is also used by user-space appliations where this scratch space may not exist. NETLINK messages can travel from one application to another so exploits are possible.

You're right, thanks for pointing this out. I'll add it to my tree.