can: etas_es58x: Replace 0-element raw_msg array
PATCH 1/1 can: etas_es58x: Replace 0-element raw_msg array
While raw_msg isn't a fixed size, it does have a maximum size. Adjust the struct to represent this and avoid the following warning when building with -Wzero-length-bounds:
drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':drivers/net/can/usb/etas_es58x/es58x_fd.c:360:35: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]
360 | tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len];
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22,
from drivers/net/can/usb/etas_es58x/es58x_fd.c:17:
drivers/net/can/usb/etas_es58x/es58x_fd.h:231:6: note: while referencing 'raw_msg'
231 | u8 raw_msg[0];
| ^~~~~~~--- drivers/net/can/usb/etas_es58x/es581_4.h | 2 +- drivers/net/can/usb/etas_es58x/es58x_fd.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/can/usb/etas_es58x/es581_4.h +1 -1
@@ -192,7 +192,7 @@ struct es581_4_urb_cmd {struct es581_4_rx_cmd_ret rx_cmd_ret;__le64 timestamp;u8 rx_cmd_ret_u8; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;__le16 reserved_for_crc16_do_not_use;
drivers/net/can/usb/etas_es58x/es58x_fd.h +1 -1
@@ -228,7 +228,7 @@ struct es58x_fd_urb_cmd {struct es58x_fd_tx_ack_msg tx_ack_msg;__le64 timestamp;__le32 rx_cmd_ret_le32; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;__le16 reserved_for_crc16_do_not_use;
Signature · 1 line
2.30.2
Quoted 55 lines · Kees Cook
While
raw_msgisn't a fixed size, it does have a maximum size. Adjust the struct to represent this and avoid the following warning when building with -Wzero-length-bounds:
drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':drivers/net/can/usb/etas_es58x/es58x_fd.c:360:35: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]360 | tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22, from drivers/net/can/usb/etas_es58x/es58x_fd.c:17: drivers/net/can/usb/etas_es58x/es58x_fd.h:231:6: note: while referencing 'raw_msg' 231 | u8 raw_msg[0]; | ^~~~~~~Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Cc "David S. Miller" <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Arunachalam Santhanam <[email protected]>Cc Vincent Mailhol <[email protected]>Signed-off-by Kees Cook <[email protected]>--- drivers/net/can/usb/etas_es58x/es581_4.h | 2 +- drivers/net/can/usb/etas_es58x/es58x_fd.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/net/can/usb/etas_es58x/es581_4.h +1
-1@@ -192,7 +192,7 @@ struct es581_4_urb_cmd {struct es581_4_rx_cmd_ret rx_cmd_ret;__le64 timestamp;u8 rx_cmd_ret_u8; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;__le16 reserved_for_crc16_do_not_use;drivers/net/can/usb/etas_es58x/es58x_fd.h +1
-2@@ -228,7 +228,7 @@ struct es58x_fd_urb_cmd {struct es58x_fd_tx_ack_msg tx_ack_msg;__le64 timestamp;__le32 rx_cmd_ret_le32; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;__le16 reserved_for_crc16_do_not_use; --
Quoted 1 line
2.30.2
raw_msg is part of a union so its maximum size is implicitly the biggest size of the other member of that union:
| struct es58x_fd_urb_cmd {
| __le16 SOF;
| u8 cmd_type;
| u8 cmd_id;
| u8 channel_idx;
| __le16 msg_len;
|
| union {
| struct es58x_fd_tx_conf_msg tx_conf_msg;
| u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN];
| u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN];
| struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX];
| struct es58x_fd_rx_event_msg rx_event_msg;
| struct es58x_fd_tx_ack_msg tx_ack_msg;
| __le64 timestamp;
| __le32 rx_cmd_ret_le32;
| u8 raw_msg[0];
| } __packed;
|
| __le16 reserved_for_crc16_do_not_use;
| } __packed;ram_msg can then be used to manipulate the other fields at the byte level. I am sorry but I fail to understand why this is an issue.
Also, the proposed fix drastically increases the size of the structure.
Yours sincerely,
Vincent
Quoted 54 lines · Vincent MAILHOL
Quoted 53 lines · Kees Cook
While
raw_msgisn't a fixed size, it does have a maximum size. Adjust the struct to represent this and avoid the following warning when building with -Wzero-length-bounds:
drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':drivers/net/can/usb/etas_es58x/es58x_fd.c:360:35: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]360 | tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22, from drivers/net/can/usb/etas_es58x/es58x_fd.c:17: drivers/net/can/usb/etas_es58x/es58x_fd.h:231:6: note: while referencing 'raw_msg' 231 | u8 raw_msg[0]; | ^~~~~~~Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Cc "David S. Miller" <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Arunachalam Santhanam <[email protected]>Cc Vincent Mailhol <[email protected]>Signed-off-by Kees Cook <[email protected]>--- drivers/net/can/usb/etas_es58x/es581_4.h | 2 +- drivers/net/can/usb/etas_es58x/es58x_fd.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/net/can/usb/etas_es58x/es581_4.h +1
-1@@ -192,7 +192,7 @@ struct es581_4_urb_cmd {struct es581_4_rx_cmd_ret rx_cmd_ret;__le64 timestamp;u8 rx_cmd_ret_u8; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;__le16 reserved_for_crc16_do_not_use;drivers/net/can/usb/etas_es58x/es58x_fd.h +1
-1@@ -228,7 +228,7 @@ struct es58x_fd_urb_cmd {struct es58x_fd_tx_ack_msg tx_ack_msg;__le64 timestamp;__le32 rx_cmd_ret_le32; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;
Quoted 6 lines
Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_fd.h +0
-1__le16 reserved_for_crc16_do_not_use; --2.30.2
raw_msgis part of a union so its maximum size is implicitly the biggest size of the other member of that union:
Yup, understood. See below...
Quoted 23 lines
| struct es58x_fd_urb_cmd {
| __le16 SOF;
| u8 cmd_type;
| u8 cmd_id;
| u8 channel_idx;
| __le16 msg_len;
|
| union {
| struct es58x_fd_tx_conf_msg tx_conf_msg;
| u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN];
| u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN];
| struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX];
| struct es58x_fd_rx_event_msg rx_event_msg;
| struct es58x_fd_tx_ack_msg tx_ack_msg;
| __le64 timestamp;
| __le32 rx_cmd_ret_le32;
| u8 raw_msg[0];
| } __packed;
|
| __le16 reserved_for_crc16_do_not_use;
| } __packed;Quoted 2 lines
ram_msgcan then be used to manipulate the other fields at the byte level. I am sorry but I fail to understand why this is an issue.
The issue is with using a 0-element array (these are being removed from the kernel[1] so we can add -Warray-bounds). Normally in this situation I would replace the 0-element array with a flexible array, but this case is unusual in several ways:
- There is a trailing struct member (reserved_for_crc16_do_not_use), which is never accessed (good), and documented as "please never access this".
- struct es58x_fd_urb_cmd is statically allocated (it is written into from the URB handler).
- The message lengths coming from the USB device are stored in a u16, which looked like it was possible to overflow the buffer.
In taking a closer look, I see that the URB command length is checked, and the in-data length is checked as well, so the overflow concern appears to be addressed.
Quoted 1 line
Also, the proposed fix drastically increases the size of the structure.
Indeed. I will send a v2, now that I see that the overflow concern isn't an issue.
Thanks!
-Kees
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays
Signature · 1 line
Kees Cook
Quoted 90 lines · Kees Cook
Quoted 54 lines · Vincent MAILHOL
Quoted 53 lines · Kees Cook
While
raw_msgisn't a fixed size, it does have a maximum size. Adjust the struct to represent this and avoid the following warning when building with -Wzero-length-bounds:
drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':drivers/net/can/usb/etas_es58x/es58x_fd.c:360:35: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]360 | tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len]; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22, from drivers/net/can/usb/etas_es58x/es58x_fd.c:17: drivers/net/can/usb/etas_es58x/es58x_fd.h:231:6: note: while referencing 'raw_msg' 231 | u8 raw_msg[0]; | ^~~~~~~Cc Wolfgang Grandegger <[email protected]>Cc Marc Kleine-Budde <[email protected]>Cc "David S. Miller" <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Arunachalam Santhanam <[email protected]>Cc Vincent Mailhol <[email protected]>Signed-off-by Kees Cook <[email protected]>--- drivers/net/can/usb/etas_es58x/es581_4.h | 2 +- drivers/net/can/usb/etas_es58x/es58x_fd.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/net/can/usb/etas_es58x/es581_4.h +1
-1@@ -192,7 +192,7 @@ struct es581_4_urb_cmd {struct es581_4_rx_cmd_ret rx_cmd_ret;__le64 timestamp;u8 rx_cmd_ret_u8; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;__le16 reserved_for_crc16_do_not_use;drivers/net/can/usb/etas_es58x/es58x_fd.h +1
-1@@ -228,7 +228,7 @@ struct es58x_fd_urb_cmd {struct es58x_fd_tx_ack_msg tx_ack_msg;__le64 timestamp;__le32 rx_cmd_ret_le32; -u8 raw_msg[0 ]; +u8 raw_msg[USHRT_MAX ]; } __packed;Quoted 6 lines
Quoted 3 lines
drivers/net/can/usb/etas_es58x/es58x_fd.h +0
-1__le16 reserved_for_crc16_do_not_use; --2.30.2
raw_msgis part of a union so its maximum size is implicitly the biggest size of the other member of that union:Yup, understood. See below...
Quoted 23 lines
| struct es58x_fd_urb_cmd { | __le16 SOF; | u8 cmd_type; | u8 cmd_id; | u8 channel_idx; | __le16 msg_len; | | union { | struct es58x_fd_tx_conf_msg tx_conf_msg; | u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; | u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; | struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; | struct es58x_fd_rx_event_msg rx_event_msg; | struct es58x_fd_tx_ack_msg tx_ack_msg; | __le64 timestamp; | __le32 rx_cmd_ret_le32; | u8 raw_msg[0]; | } __packed; | | __le16 reserved_for_crc16_do_not_use; | } __packed;Quoted 2 lines
ram_msgcan then be used to manipulate the other fields at the byte level. I am sorry but I fail to understand why this is an issue.
Quoted 8 lines
The issue is with using a 0-element array (these are being removed from the kernel[1] so we can add -Warray-bounds). Normally in this situation I would replace the 0-element array with a flexible array, but this case is unusual in several ways:
- There is a trailing struct member (
reserved_for_crc16_do_not_use), which is never accessed (good), and documented as "please never access this".
Yes. And FYI, this field is here so that
| sizeof(struct es58x_fd_urb_cmd)
returns the correct maximum size.
And, of course, because this structure will be sent to the device, there is no possibility to reorder those fields.
Quoted 6 lines
- struct
es58x_fd_urb_cmdis statically allocated (it is written into from the URB handler).- The message lengths coming from the USB device are stored in a u16, which looked like it was possible to overflow the buffer.
Quoted 8 lines
In taking a closer look, I see that the URB command length is checked, and the in-data length is checked as well, so the overflow concern appears to be addressed.
Quoted 1 line
Also, the proposed fix drastically increases the size of the structure.
Indeed. I will send a v2, now that I see that the overflow concern isn't an issue.
Thanks for the explanation. That makes sense.
At the end, the only goal of raw_msg[] is to have a tag pointing to the beginning of the union. It would be virtually identical to something like:
| u8 raw_msg[];
| union {
| /* ... */
| } __packed ;I had a look at your work and especially at your struct_group() macro. Do you think it would make sense to introduce a union_group()?
Result would look like:
| union_group_attr(urb_msg, __packed, /* raw_msg renamed to urb_msg */ | struct es58x_fd_tx_conf_msg tx_conf_msg; | u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; | u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; | struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; | struct es58x_fd_rx_event_msg rx_event_msg; | struct es58x_fd_tx_ack_msg tx_ack_msg; | __le64 timestamp; | __le32 rx_cmd_ret_le32; | );
And I can then use urb_msg in place of the old raw_msg (might need a bit of rework here and there but I can take care of it).
This is the most pretty way I can think of to remove this zero length array. Keeping the raw_msg[] but with another size seems odd to me.
Or maybe I would be the only one using this feature in the full tree? In that case, maybe it would make sense to keep the union_group_attr() macro local to the etas_es58x driver?
Yours sincerely,
Vincent
Quoted 27 lines · Vincent MAILHOL
At the end, the only goal of raw_msg[] is to have a tag pointing to the beginning of the union. It would be virtually identical to something like:
| u8 raw_msg[]; | union { | /* ... */ | } __packed ;I had a look at your work and especially at your
struct_group()macro. Do you think it would make sense to introduce aunion_group()?Result would look like:
| union_group_attr(urb_msg, __packed, /* raw_msg renamed to urb_msg */ | struct es58x_fd_tx_conf_msg tx_conf_msg; | u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; | u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; | struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; | struct es58x_fd_rx_event_msg rx_event_msg; | struct es58x_fd_tx_ack_msg tx_ack_msg; | __le64 timestamp; | __le32 rx_cmd_ret_le32; | );And I can then use
urb_msgin place of the oldraw_msg(might need a bit of rework here and there but I can take care of it).
Quoted 6 lines
This is the most pretty way I can think of to remove this zero length array. Keeping the raw_msg[] but with another size seems odd to me.
Or maybe I would be the only one using this feature in the full tree? In that case, maybe it would make sense to keep the
union_group_attr()macro local to theetas_es58xdriver?
I actually ended up with something close to this idea, but more generalized for other cases in the kernel. There was a sane way to include a "real" flexible array in a union (or alone in a struct), so I've proposed this flex_array() helper:
https://lore.kernel.org/lkml/[email protected]/
and then it's just a drop-in replacement for all the places that need this fixed, including etas_es58x:
https://lore.kernel.org/lkml/[email protected]/#Z30drivers:net:can:usb:etas_es58x:es581_4.h
Hopefully this will work out; I think it's as clean as we can get for now. :)
Signature · 1 line
Kees Cook
Quoted 41 lines · Kees Cook
Quoted 27 lines · Vincent MAILHOL
At the end, the only goal of raw_msg[] is to have a tag pointing to the beginning of the union. It would be virtually identical to something like:
| u8 raw_msg[]; | union { | /* ... */ | } __packed ;I had a look at your work and especially at your
struct_group()macro. Do you think it would make sense to introduce aunion_group()?Result would look like:
| union_group_attr(urb_msg, __packed, /* raw_msg renamed to urb_msg */ | struct es58x_fd_tx_conf_msg tx_conf_msg; | u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; | u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; | struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; | struct es58x_fd_rx_event_msg rx_event_msg; | struct es58x_fd_tx_ack_msg tx_ack_msg; | __le64 timestamp; | __le32 rx_cmd_ret_le32; | );And I can then use
urb_msgin place of the oldraw_msg(might need a bit of rework here and there but I can take care of it).Quoted 6 lines
This is the most pretty way I can think of to remove this zero length array. Keeping the raw_msg[] but with another size seems odd to me.
Or maybe I would be the only one using this feature in the full tree? In that case, maybe it would make sense to keep the
union_group_attr()macro local to theetas_es58xdriver?I actually ended up with something close to this idea, but more generalized for other cases in the kernel. There was a sane way to include a "real" flexible array in a union (or alone in a struct), so I've proposed this
flex_array()helper:
https://lore.kernel.org/lkml/[email protected]/
Quoted 6 lines
and then it's just a drop-in replacement for all the places that need this fixed, including
etas_es58x:
https://lore.kernel.org/lkml/[email protected]/#Z30drivers:net:can:usb:etas_es58x:es581_4.hHopefully this will work out; I think it's as clean as we can get for now. :)
The __flex_array itself is a nasty hack :D but the rest is clean.
Is this compliant to the C standard? Well, I guess that as long as both GCC and LLVM supports it, it is safe to add it to the kernel.
I like the final result. I will do a bit more testing and give my acknowledgement if everything goes well.
Yours sincerely,
Vincent
Quoted 42 lines · Vincent MAILHOL
Quoted 41 lines · Kees Cook
Quoted 27 lines · Vincent MAILHOL
At the end, the only goal of raw_msg[] is to have a tag pointing to the beginning of the union. It would be virtually identical to something like:
| u8 raw_msg[]; | union { | /* ... */ | } __packed ;I had a look at your work and especially at your
struct_group()macro. Do you think it would make sense to introduce aunion_group()?Result would look like:
| union_group_attr(urb_msg, __packed, /* raw_msg renamed to urb_msg */ | struct es58x_fd_tx_conf_msg tx_conf_msg; | u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; | u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; | struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; | struct es58x_fd_rx_event_msg rx_event_msg; | struct es58x_fd_tx_ack_msg tx_ack_msg; | __le64 timestamp; | __le32 rx_cmd_ret_le32; | );And I can then use
urb_msgin place of the oldraw_msg(might need a bit of rework here and there but I can take care of it).Quoted 6 lines
This is the most pretty way I can think of to remove this zero length array. Keeping the raw_msg[] but with another size seems odd to me.
Or maybe I would be the only one using this feature in the full tree? In that case, maybe it would make sense to keep the
union_group_attr()macro local to theetas_es58xdriver?I actually ended up with something close to this idea, but more generalized for other cases in the kernel. There was a sane way to include a "real" flexible array in a union (or alone in a struct), so I've proposed this
flex_array()helper:
https://lore.kernel.org/lkml/[email protected]/
Quoted 8 lines
Quoted 6 lines
and then it's just a drop-in replacement for all the places that need this fixed, including
etas_es58x:
https://lore.kernel.org/lkml/[email protected]/#Z30drivers:net:can:usb:etas_es58x:es581_4.hHopefully this will work out; I think it's as clean as we can get for now. :)
The
__flex_arrayitself is a nasty hack :D
Indeed. ;)
Quoted 1 line
but the rest is clean.
Thanks!
Quoted 3 lines
Is this compliant to the C standard? Well, I guess that as long as both GCC and LLVM supports it, it is safe to add it to the kernel.
The kernel already uses a bunch of compiler extensions, none of which were legal under the C standard to begin with. :) So, really, this is about normalizing what we're already doing and finding a single hack that helps the code base for readability and robustness.
Quoted 2 lines
I like the final result. I will do a bit more testing and give my acknowledgement if everything goes well.
Great; thank you!
Signature · 1 line
Kees Cook