Skip to contentSkip to conversation

: potential unaligned memory access in drivers/usb/gadget/rndis.c

PROBLEM [email protected] 5 participants 5 messages Mar 11, 2009 – Mar 16, 2009

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

Hello,

while working on a 2.6.18.2 kernel on ARM9 arch., I noticed RNDIS module might trigger lots of unaligned access exceptions. Looks like it comes from rndis_add_hdr() function, which uses pointer from skb_pull as if it is aligned on a 4bytes boundary.

void rndis_add_hdr (struct sk_buff *skb)

{
    struct rndis_packet_msg_type    *header;

    if (!skb)
        return;
    header = (void *) skb_push (skb, sizeof *header);
    memset (header, 0, sizeof *header);
    header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
    header->MessageLength = cpu_to_le32(skb->len);
    header->DataOffset = __constant_cpu_to_le32 (36);
    header->DataLength = cpu_to_le32(skb->len - sizeof *header);
}

It happened to me that skb_pull() returns a pointer to a location not aligned on a 4 bytes boundary.

As a quick workaround, I modified the code to:

void rndis_add_hdr (struct sk_buff *skb)
{
struct rndis_packet_msg_type *header;
static struct rndis_packet_msg_type new = {
__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG), /* MessageType */
0, /* MessageLength */
__constant_cpu_to_le32 (36), /* DataOffset */
0, /* DataLength */
0, /* OOBDataOffset */
0, /* OOBDataLength */
0, /* NumOOBDataElements */
0, /* PerPacketInfoOffset */
0, /* PerPacketInfoLength */
0, /* VcHandle */
0, /* Reserved */
};

if (!skb)
return;
header = (void *) skb_push (skb, sizeof *header); memset (header, 0, sizeof *header); /* probably not necessary */ new.MessageLength = cpu_to_le32(skb->len); new.DataLength = cpu_to_le32(skb->len - sizeof *header); memcpy((u8*)header,(u8*)&new,sizeof(new));
}

and did not have the unaligned exceptions anymore.

I had a look at rndis.c in latest kernel version (2.6.28.7), the rndis_add_hdr() function is the same as for 2.6.18.2.

Hope this helps.

Regards,

Yann Poupet.

Acked-by David Brownell
#1 lore raw

(cc linux-usb and netdev)

(it's drivers/usb/gadget/rndis.c)

Quoted 51 lines · On Wed, 11 Mar 2009 13:51:08 +0100 (CET)…

Hello,

while working on a 2.6.18.2 kernel on ARM9 arch., I noticed RNDIS module might trigger lots of unaligned access exceptions. Looks like it comes from rndis_add_hdr() function, which uses pointer from skb_pull as if it is aligned on a 4bytes boundary.

void rndis_add_hdr (struct sk_buff *skb)

{
    struct rndis_packet_msg_type    *header;

    if (!skb)
        return;
    header = (void *) skb_push (skb, sizeof *header);
    memset (header, 0, sizeof *header);
    header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
    header->MessageLength = cpu_to_le32(skb->len);
    header->DataOffset = __constant_cpu_to_le32 (36);
    header->DataLength = cpu_to_le32(skb->len - sizeof *header);
}

It happened to me that skb_pull() returns a pointer to a location not aligned on a 4 bytes boundary.

As a quick workaround, I modified the code to:

void rndis_add_hdr (struct sk_buff *skb)
{
struct rndis_packet_msg_type *header;
static struct rndis_packet_msg_type new = {
__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG), /* MessageType */
0, /* MessageLength */
__constant_cpu_to_le32 (36), /* DataOffset */
0, /* DataLength */
0, /* OOBDataOffset */
0, /* OOBDataLength */
0, /* NumOOBDataElements */
0, /* PerPacketInfoOffset */
0, /* PerPacketInfoLength */
0, /* VcHandle */
0, /* Reserved */
};

if (!skb)
return;
header = (void *) skb_push (skb, sizeof *header); memset (header, 0, sizeof *header); /* probably not necessary */ new.MessageLength = cpu_to_le32(skb->len); new.DataLength = cpu_to_le32(skb->len - sizeof *header); memcpy((u8*)header,(u8*)&new,sizeof(new));
}

Quoted 5 lines

and did not have the unaligned exceptions anymore.

I had a look at rndis.c in latest kernel version (2.6.28.7), the rndis_add_hdr() function is the same as for 2.6.18.2.

Maybe the below fixed this? afacit it is queued for 2.6.30 in the net-next tree:

commit 35c26c2cf6a6a2d1c48add732d8ba002bd90784c

Author:     Harvey Harrison <[email protected]>
AuthorDate: Sat Feb 14 22:56:56 2009 -0800
Commit:     David S. Miller <davem-fT/PcQaiUtIeIZ0/[email protected]>
CommitDate: Sat Feb 14 22:56:56 2009 -0800

rndis: remove private wrapper of __constant_cpu_to_le32

Use cpu_to_le32 directly as it handles constant folding now, replace direct uses of __constant_cpu_to_{endian} as well.

drivers/net/usb/rndis_host.c +11 -14
index bcd858c..b7f763e 100644
@@ -169,7 +169,7 @@ int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf, int buflen)
 				struct rndis_keepalive_c *msg = (void *)buf;
 
 				msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
-				msg->msg_len = ccpu2(sizeof *msg);
+				msg->msg_len = cpu_to_le32(sizeof *msg);
 				msg->status = RNDIS_STATUS_SUCCESS;
 				retval = usb_control_msg(dev->udev,
 					usb_sndctrlpipe(dev->udev, 0),
@@ -237,7 +237,7 @@ static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
 	u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
 	u.get->oid = oid;
 	u.get->len = cpu_to_le32(in_len);
-	u.get->offset = ccpu2(20);
+	u.get->offset = cpu_to_le32(20);
 
 	retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
 	if (unlikely(retval < 0)) {
@@ -297,9 +297,9 @@ generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
 		goto fail;
 
 	u.init->msg_type = RNDIS_MSG_INIT;
-	u.init->msg_len = ccpu2(sizeof *u.init);
-	u.init->major_version = ccpu2(1);
-	u.init->minor_version = ccpu2(0);
+	u.init->msg_len = cpu_to_le32(sizeof *u.init);
+	u.init->major_version = cpu_to_le32(1);
+	u.init->minor_version = cpu_to_le32(0);
 
 	/* max transfer (in spec) is 0x4000 at full speed, but for
 	 * TX we'll stick to one Ethernet packet plus RNDIS framing.
@@ -403,10 +403,10 @@ generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
 	/* set a nonzero filter to enable data transfers */
 	memset(u.set, 0, sizeof *u.set);
 	u.set->msg_type = RNDIS_MSG_SET;
-	u.set->msg_len = ccpu2(4 + sizeof *u.set);
+	u.set->msg_len = cpu_to_le32(4 + sizeof *u.set);
 	u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
-	u.set->len = ccpu2(4);
-	u.set->offset = ccpu2((sizeof *u.set) - 8);
+	u.set->len = cpu_to_le32(4);
+	u.set->offset = cpu_to_le32((sizeof *u.set) - 8);
 	*(__le32 *)(u.buf + sizeof *u.set) = RNDIS_DEFAULT_FILTER;
 
 	retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
@@ -423,7 +423,7 @@ generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
 halt_fail_and_release:
 	memset(u.halt, 0, sizeof *u.halt);
 	u.halt->msg_type = RNDIS_MSG_HALT;
-	u.halt->msg_len = ccpu2(sizeof *u.halt);
+	u.halt->msg_len = cpu_to_le32(sizeof *u.halt);
 	(void) rndis_command(dev, (void *)u.halt, CONTROL_BUFFER_SIZE);
 fail_and_release:
 	usb_set_intfdata(info->data, NULL);
@@ -448,7 +448,7 @@ void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
 	halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
 	if (halt) {
 		halt->msg_type = RNDIS_MSG_HALT;
-		halt->msg_len = ccpu2(sizeof *halt);
+		halt->msg_len = cpu_to_le32(sizeof *halt);
 		(void) rndis_command(dev, (void *)halt, CONTROL_BUFFER_SIZE);
 		kfree(halt);
 	}
@@ -543,7 +543,7 @@ fill:
 	memset(hdr, 0, sizeof *hdr);
 	hdr->msg_type = RNDIS_MSG_PACKET;
 	hdr->msg_len = cpu_to_le32(skb->len);
-	hdr->data_offset = ccpu2(sizeof(*hdr) - 8);
+	hdr->data_offset = cpu_to_le32(sizeof(*hdr) - 8);
 	hdr->data_len = cpu_to_le32(len);
 
 	/* FIXME make the last packet always be short ... */
@@ -562,9 +562,6 @@ static const struct driver_info	rndis_info = {
 	.tx_fixup =	rndis_tx_fixup,
 };
 
-#undef ccpu2
-
-
 /*-------------------------------------------------------------------------*/
 
 static const struct usb_device_id	products [] = {
drivers/net/wireless/rndis_wlan.c +45 -45

126 lines not on this page · raw message

drivers/usb/gadget/rndis.c +46 -46

231 lines not on this page · raw message

include/linux/usb/rndis_host.h +41 -44

113 lines not on this page · raw message

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 9 lines · Andrew Morton

Maybe the below fixed this? afacit it is queued for 2.6.30 in the net-next tree:

commit 35c26c2cf6a6a2d1c48add732d8ba002bd90784c

Author:     Harvey Harrison <[email protected]>
AuthorDate: Sat Feb 14 22:56:56 2009 -0800
Commit:     David S. Miller <[email protected]>
CommitDate: Sat Feb 14 22:56:56 2009 -0800
Quoted 9 lines

rndis: remove private wrapper of __constant_cpu_to_le32

Use cpu_to_le32 directly as it handles constant folding now, replace direct uses of __constant_cpu_to_{endian} as well.

Doubtful, this patch should be a no-op, any alignment problems that existed before will still be there.

Harvey

maintainer
#3 lore raw

From: Harvey Harrison <[email protected]>
Date: Thu, 12 Mar 2009 16:51:37 -0700

Quoted 16 lines · Andrew Morton
Quoted 15 lines

Maybe the below fixed this? afacit it is queued for 2.6.30 in the net-next tree:

commit 35c26c2cf6a6a2d1c48add732d8ba002bd90784c

Author:     Harvey Harrison <[email protected]>
AuthorDate: Sat Feb 14 22:56:56 2009 -0800
Commit:     David S. Miller <davem-fT/PcQaiUtIeIZ0/[email protected]>
CommitDate: Sat Feb 14 22:56:56 2009 -0800

rndis: remove private wrapper of __constant_cpu_to_le32

Use cpu_to_le32 directly as it handles constant folding now, replace direct uses of __constant_cpu_to_{endian} as well.

Quoted 7 lines
Quoted 4 lines

Doubtful, this patch should be a no-op, any alignment problems that existed before will still be there.

Agreed, that patch won't do anything wrt. the alignment issue being reported here.
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
In reply to Andrew Morton
Quoted 3 lines · Andrew Morton
Quoted 3 lines

It happened to me that skb_pull() returns a pointer to a location not aligned on a 4 bytes boundary.

Does this arch set NET_IP_ALIGN appropriately?

If worst comes to worst, use put_unaligned() instead of a static (non-const) template for that header.

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html