Skip to contentSkip to conversation

PATCH] firewire: add padding to some struct

DISCUSSION JiSheng Zhang 3 participants 7 messages Jul 18, 2008 – Jul 21, 2008 4 messages not in the archive Firewire

lore
Share & tools
lore
#0 lore raw

Hi,

Quoted 8 lines

From: Stefan Richter <[email protected]> Reply-To:
To: JiSheng Zhang <[email protected]>
Subject: Re: PATCH] firewire: add padding to some struct Date:Fri, 18 Jul 2008 13:38:25 +0200

Quoted 1 line · JiSheng Zhang

If p is a pointer to struct fw_cdev_event_response), p->data will point to

the

Quoted 2 lines
Quoted 2 lines

padding data rather than the right place, it will cause problem under some platforms. For example, in the function handle_device_event of

libraw1394(ported

Quoted 6 lines
Quoted 6 lines

to juju stack):
.....
case FW_CDEV_EVENT_RESPONSE:

    rc = u64_to_ptr(u->response.closure);
    if (rc->data != NULL)
	memcpy(rc->data, u->response.data, rc->length);//here it will lost the last

four

Quoted 11 lines
Quoted 6 lines

bytes

    errcode = juju_to_raw1394_errcode(u->response.rcode);

.....

Although this problem can be solved by add the offset to the pointer, but the member:__u32 data[0] lost its original meaning.

I don't understand what the problem is. As long as both kernel and library use "response.data" or "&response + offsetof(typeof(response), data)", they will write and read at the correct location.

This patch can fix the problem while not changing the struct definition.

Thanks in advance,
JiSheng

drivers/firewire/fw-cdev.c +3 -3
@@ -382,9 +382,9 @@
 
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }
 
 static int ioctl_send_request(struct client *client, void *buffer)
#1 lore raw
Quoted 11 lines · JiSheng Zhang

Hi,

Quoted 8 lines

From: Stefan Richter <[email protected]> Reply-To:
To: JiSheng Zhang <[email protected]>
Subject: Re: PATCH] firewire: add padding to some struct Date:Fri, 18 Jul 2008 13:38:25 +0200

Quoted 1 line · JiSheng Zhang

If p is a pointer to struct fw_cdev_event_response), p->data will point to

the

Quoted 1 line
Quoted 1 line

padding data rather than the right place, it will cause problem under some

Define "the right place". If p->data[] isn't the place for the data, then something's seriously wrong with either the producer or the consumer of that data -- or the data type definition if either is HW.

Quoted 29 lines
Quoted 1 line
Quoted 1 line

platforms. For example, in the function handle_device_event of

libraw1394(ported

Quoted 6 lines
Quoted 6 lines

to juju stack):
.....
case FW_CDEV_EVENT_RESPONSE:

    rc = u64_to_ptr(u->response.closure);
    if (rc->data != NULL)
	memcpy(rc->data, u->response.data, rc->length);//here it will lost the last

four

Quoted 11 lines
Quoted 6 lines

bytes

    errcode = juju_to_raw1394_errcode(u->response.rcode);

.....

Although this problem can be solved by add the offset to the pointer, but the member:__u32 data[0] lost its original meaning.

I don't understand what the problem is. As long as both kernel and library use "response.data" or "&response + offsetof(typeof(response), data)", they will write and read at the correct location.

This patch can fix the problem while not changing the struct definition.

Thanks in advance,
JiSheng

drivers/firewire/fw-cdev.c line 388

Quoted 10 lines
drivers/firewire/fw-cdev.c +3 -3
@@ -382,9 +382,9 @@
 
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }

Neither of these look correct.
If sizeof(struct ...) != offsetof(struct ..., data) as you claim is possible, then the old code will copy too much to/from ->response but the correct amount to/from ->response.data, and the new code will copy too much to/from ->response.data.

That's why C has offsetof():

	queue_event(client, &response->event,
		   &response->response,
		   offsetof(typeof(*response->responce), data), // I don't know the struct name
		   response->response.data, response->response.length);

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 47 lines · On Fri, 18 Jul 2008 17:27:44 +0200 Mikael Pettersson <[email protected]> wrote:
Quoted 11 lines · JiSheng Zhang

Hi,

Quoted 8 lines

From: Stefan Richter <[email protected]> Reply-To:
To: JiSheng Zhang <[email protected]>
Subject: Re: PATCH] firewire: add padding to some struct Date:Fri, 18 Jul 2008 13:38:25 +0200

Quoted 1 line · JiSheng Zhang

If p is a pointer to struct fw_cdev_event_response), p->data will point to

the

Quoted 1 line
Quoted 1 line

padding data rather than the right place, it will cause problem under some

Define "the right place". If p->data[] isn't the place for the data, then something's seriously wrong with either the producer or the consumer of that data -- or the data type definition if either is HW.

Quoted 30 lines
Quoted 1 line
Quoted 1 line

platforms. For example, in the function handle_device_event of

libraw1394(ported

Quoted 6 lines
Quoted 6 lines

to juju stack):
.....
case FW_CDEV_EVENT_RESPONSE:

    rc = u64_to_ptr(u->response.closure);
    if (rc->data != NULL)
	memcpy(rc->data, u->response.data, rc->length);//here it will lost the last

four

Quoted 11 lines
Quoted 6 lines

bytes

    errcode = juju_to_raw1394_errcode(u->response.rcode);

.....

Although this problem can be solved by add the offset to the pointer, but the member:__u32 data[0] lost its original meaning.

I don't understand what the problem is. As long as both kernel and library use "response.data" or "&response + offsetof(typeof(response), data)", they will write and read at the correct location.

This patch can fix the problem while not changing the struct definition.

Thanks in advance,
JiSheng

drivers/firewire/fw-cdev.c +0 -0
@@ -382,9 +382,9 @@
 
Quoted 14 lines

drivers/firewire/fw-cdev.c line 388

Quoted 9 lines
drivers/firewire/fw-cdev.c +3 -3
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }

Neither of these look correct.
If sizeof(struct ...) != offsetof(struct ..., data) as you claim is possible, then the old code will copy too much to/from ->response but the correct amount to/from ->response.data, and the new code will copy too much to/from ->response.data.

The old code will copy 4 extra bytes totally on some platforms, the new code is correct. The old one queue like this:
struct ...(excluding the padding bytes)|4 padding bytes|4 padding bytes|data

Quoted 7 lines

That's why C has offsetof():

	queue_event(client, &response->event,
		   &response->response,
		   offsetof(typeof(*response->responce), data), // I don't know the struct name
		   response->response.data, response->response.length);

Bye,
JiSheng

#4 lore raw
Quoted 60 lines · JiSheng Zhang
Quoted 57 lines · On Fri, 18 Jul 2008 17:27:44 +0200 Mikael Pettersson <[email protected]> wrote:
Quoted 11 lines · JiSheng Zhang

Hi,

Quoted 8 lines

From: Stefan Richter <[email protected]> Reply-To:
To: JiSheng Zhang <[email protected]>
Subject: Re: PATCH] firewire: add padding to some struct Date:Fri, 18 Jul 2008 13:38:25 +0200

Quoted 1 line · JiSheng Zhang

If p is a pointer to struct fw_cdev_event_response), p->data will point to

the

Quoted 1 line
Quoted 1 line

padding data rather than the right place, it will cause problem under some

Define "the right place". If p->data[] isn't the place for the data, then something's seriously wrong with either the producer or the consumer of that data -- or the data type definition if either is HW.

Quoted 39 lines
Quoted 1 line
Quoted 1 line

platforms. For example, in the function handle_device_event of

libraw1394(ported

Quoted 6 lines
Quoted 6 lines

to juju stack):
.....
case FW_CDEV_EVENT_RESPONSE:

    rc = u64_to_ptr(u->response.closure);
    if (rc->data != NULL)
	memcpy(rc->data, u->response.data, rc->length);//here it will lost the last

four

Quoted 11 lines
Quoted 6 lines

bytes

    errcode = juju_to_raw1394_errcode(u->response.rcode);

.....

Although this problem can be solved by add the offset to the pointer, but the member:__u32 data[0] lost its original meaning.

I don't understand what the problem is. As long as both kernel and library use "response.data" or "&response + offsetof(typeof(response), data)", they will write and read at the correct location.

This patch can fix the problem while not changing the struct definition.

Thanks in advance,
JiSheng

drivers/firewire/fw-cdev.c +3 -3
@@ -382,9 +382,9 @@
 
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }
Quoted 6 lines
Quoted 4 lines

Neither of these look correct.
If sizeof(struct ...) != offsetof(struct ..., data) as you claim is possible, then the old code will copy too much to/from ->response but the correct amount to/from ->response.data, and the new code will copy too much to/from ->response.data.

The old code will copy 4 extra bytes totally on some platforms, the new code is correct.

The new code is only correct if the variable length payload starts after the struct, i.e. (void*)(&response->response + 1), and not at the data field, i.e. (void*)response->response.data. Which is it? That's why I asked:

Quoted 3 lines
Quoted 3 lines

Define "the right place". If p->data[] isn't the place for the data, then something's seriously wrong with either the producer or the consumer of that data -- or the data type definition if either is HW.

#5 lore raw
In reply to JiSheng Zhang
Quoted 17 lines · JiSheng Zhang
Quoted 15 lines · On Fri, 18 Jul 2008 17:27:44 +0200 Mikael Pettersson <[email protected]> wrote:
Quoted 13 lines · JiSheng Zhang
drivers/firewire/fw-cdev.c +3 -3
@@ -382,9 +382,9 @@
 
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }
Quoted 15 lines
Quoted 4 lines

Neither of these look correct.
If sizeof(struct ...) != offsetof(struct ..., data) as you claim is possible, then the old code will copy too much to/from ->response but the correct amount to/from ->response.data, and the new code will copy too much to/from ->response.data.

The old code will copy 4 extra bytes totally on some platforms, the new code is correct. The old one queue like this:
struct ...(excluding the padding bytes)|4 padding bytes|4 padding bytes|data

Quoted 6 lines

That's why C has offsetof():

	queue_event(client, &response->event,
		   &response->response,
		   offsetof(typeof(*response->responce), data), // I don't know the struct name
		   response->response.data, response->response.length);

sizeof(struct ...) != offsetof(struct ..., data) happens for example on x86-64.

This is what I get with the current firewire drivers in a block read response from firecontrol on i686:

	Command: r . 0 0xfffff0000400 20
	reading from node 0, bus 1023, offset 0XFFFFF0000400 20 bytes
	read succeeded. Data follows (hex):
	04 04 04 8F
	31 33 39 34
	F0 00 A2 22
	00 10 DC 56
	00 FE D2 D4
	Ack code: complete

And the same on x86-64:

	Command: r . 0 0xfffff0000400 20
	reading from node 0, bus 1023, offset 0XFFFFF0000400 20 bytes
	read succeeded. Data follows (hex):
	04 04 04 8F
	04 04 04 8F
	31 33 39 34
	F0 00 A2 22
	00 10 DC 56
	Ack code: complete

	Command: r . 0 0xfffff0000400 24
	reading from node 0, bus 1023, offset 0XFFFFF0000400 20 bytes
	read succeeded. Data follows (hex):
	04 04 04 8F
	04 04 04 8F
	31 33 39 34
	F0 00 A2 22
	00 10 DC 56
	00 FE D2 D4
	Ack code: complete

I used libraw1394 from Dan's git repo. Gscanbus shows exactly the same results. So, x86-64 and all other architectures where struct fw_cdev_event* are aligned on u64 boundaries are currently seriously broken... but nobody noticed it before. The only breakage which I saw (and is obviously the result of this bug) is that gscanbus shows a wrong bus topology on x86-64 but the correct one on i686. The damage from this bug is limited though because
- most applications send requests which get responses with 0 or 4
bytes payload,
- no application (which can run on both old and new stack without
change) uses address range mappings, i.e. get incoming requests.

I'll look further into your proposed fix.

Signature · 3 lines
Stefan Richter
-=====-==--- -=== =--==
http://arcgraph.de/sr/

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 50 lines · On Sat, 19 Jul 2008 12:32:35 +0200 Stefan Richter <[email protected]>…
Quoted 17 lines · JiSheng Zhang
Quoted 15 lines · On Fri, 18 Jul 2008 17:27:44 +0200 Mikael Pettersson <[email protected]> wrote:
Quoted 13 lines · JiSheng Zhang
drivers/firewire/fw-cdev.c +3 -3
@@ -382,9 +382,9 @@
 
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }
Quoted 15 lines
Quoted 4 lines

Neither of these look correct.
If sizeof(struct ...) != offsetof(struct ..., data) as you claim is possible, then the old code will copy too much to/from ->response but the correct amount to/from ->response.data, and the new code will copy too much to/from ->response.data.

The old code will copy 4 extra bytes totally on some platforms, the new code is correct. The old one queue like this:
struct ...(excluding the padding bytes)|4 padding bytes|4 padding bytes|data

Quoted 6 lines

That's why C has offsetof():

	queue_event(client, &response->event,
		   &response->response,
		   offsetof(typeof(*response->responce), data), // I don't know the struct name
		   response->response.data, response->response.length);

sizeof(struct ...) != offsetof(struct ..., data) happens for example on x86-64.

This is what I get with the current firewire drivers in a block read response from firecontrol on i686:

	Command: r . 0 0xfffff0000400 20
	reading from node 0, bus 1023, offset 0XFFFFF0000400 20 bytes
	read succeeded. Data follows (hex):
	04 04 04 8F
	31 33 39 34
	F0 00 A2 22
	00 10 DC 56
	00 FE D2 D4
	Ack code: complete
Quoted 6 lines

And the same on x86-64:

	Command: r . 0 0xfffff0000400 20
	reading from node 0, bus 1023, offset 0XFFFFF0000400 20 bytes
	read succeeded. Data follows (hex):
	04 04 04 8F

this is the 4 extra padding bytes

Quoted 4 lines
	04 04 04 8F
	31 33 39 34
	F0 00 A2 22
	00 10 DC 56

here lost the last 4 bytes of data

Quoted 13 lines
	Ack code: complete

	Command: r . 0 0xfffff0000400 24
	reading from node 0, bus 1023, offset 0XFFFFF0000400 20 bytes
	read succeeded. Data follows (hex):
	04 04 04 8F
	04 04 04 8F
	31 33 39 34
	F0 00 A2 22
	00 10 DC 56
	00 FE D2 D4
	Ack code: complete
Quoted 4 lines

I used libraw1394 from Dan's git repo. Gscanbus shows exactly the same results. So, x86-64 and all other architectures where struct fw_cdev_event* are aligned on u64 boundaries are currently seriously broken... but nobody noticed it before. The only breakage which I saw

the read_topology_map in the testlibraw of libraw1394(with support to juju) will show similar breakage.

Quoted 5 lines

(and is obviously the result of this bug) is that gscanbus shows a wrong bus topology on x86-64 but the correct one on i686. The damage from this bug is limited though because
- most applications send requests which get responses with 0 or 4 bytes payload,

I think so.

Quoted 4 lines

- no application (which can run on both old and new stack without change) uses address range mappings, i.e. get incoming requests.

I'll look further into your proposed fix.

Thanks

Regards,
JiSheng

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw
Quoted 65 lines · On Sat, 19 Jul 2008 12:09:18 +0200 Mikael Pettersson <[email protected]> wrote:
Quoted 64 lines · JiSheng Zhang
Quoted 61 lines · On Fri, 18 Jul 2008 17:27:44 +0200 Mikael Pettersson <[email protected]> wrote:
Quoted 11 lines · JiSheng Zhang

Hi,

Quoted 8 lines

From: Stefan Richter <[email protected]> Reply-To:
To: JiSheng Zhang <[email protected]>
Subject: Re: PATCH] firewire: add padding to some struct Date:Fri, 18 Jul 2008 13:38:25 +0200

Quoted 1 line · JiSheng Zhang

If p is a pointer to struct fw_cdev_event_response), p->data will point to

the

Quoted 1 line
Quoted 1 line

padding data rather than the right place, it will cause problem under some

Define "the right place". If p->data[] isn't the place for the data, then something's seriously wrong with either the producer or the consumer of that data -- or the data type definition if either is HW.

Quoted 29 lines
Quoted 1 line
Quoted 1 line

platforms. For example, in the function handle_device_event of

libraw1394(ported

Quoted 6 lines
Quoted 6 lines

to juju stack):
.....
case FW_CDEV_EVENT_RESPONSE:

    rc = u64_to_ptr(u->response.closure);
    if (rc->data != NULL)
	memcpy(rc->data, u->response.data, rc->length);//here it will lost the last

four

Quoted 11 lines
Quoted 6 lines

bytes

    errcode = juju_to_raw1394_errcode(u->response.rcode);

.....

Although this problem can be solved by add the offset to the pointer, but the member:__u32 data[0] lost its original meaning.

I don't understand what the problem is. As long as both kernel and library use "response.data" or "&response + offsetof(typeof(response), data)", they will write and read at the correct location.

This patch can fix the problem while not changing the struct definition.

Thanks in advance,
JiSheng

drivers/firewire/fw-cdev.c line 388

Quoted 10 lines
drivers/firewire/fw-cdev.c +3 -3
@@ -382,9 +382,9 @@
 
 	response->response.type   = FW_CDEV_EVENT_RESPONSE;
 	response->response.rcode  = rcode;
-	queue_event(client, &response->event,
-		    &response->response, sizeof(response->response),
-		    response->response.data, response->response.length);
+	queue_event(client, &response->event, &response->response, 
+		    sizeof(response->response) + response->response.length,
+		    NULL, 0);
 }

Neither of these look correct.
If sizeof(struct ...) != offsetof(struct ..., data) as you claim is possible, then the old code will copy too much to/from ->response but the correct amount to/from ->response.data, and the new code will copy too much to/from ->response.data.

Quoted 6 lines
Quoted 2 lines

The old code will copy 4 extra bytes totally on some platforms, the new code is correct.

The new code is only correct if the variable length payload starts after the struct, i.e. (void*)(&response->response + 1), and not at the data field, i.e. (void*)response->response.data. Which is it? That's why I asked:

Yes, the payload starts at the data field(including the padding bytes). But there is no problem, "As long as both kernel and library use response.data" as Stefan said.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Define "the right place". If p->data[] isn't the place for the data, then something's seriously wrong with either the producer or the consumer of that data -- or the data type definition if either is HW.