Skip to contentSkip to conversation

media: venus: hfi_cmds: Replace one-element array with flex-array member and use __counted_by

PATCH In review Gustavo A. R. Silva 4 participants 6 messages Oct 9, 2023 – Jan 9, 2024

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

PATCH 1/1 media: venus: hfi_cmds: Replace one-element array with flex-array member and use __counted_by

Array data in struct hfi_sfr is being used as a fake flexible array at run-time:

drivers/media/platform/qcom/venus/hfi_venus.c:
1033 p = memchr(sfr->data, '\0', sfr->buf_size);
1034 /*

1035          * SFR isn't guaranteed to be NULL terminated since SYS_ERROR indicates
1036          * that Venus is in the process of crashing.
1037          */

1038 if (!p)
1039 sfr->data[sfr->buf_size - 1] = '\0';
1040
1041 dev_err_ratelimited(dev, "SFR message from FW: %s\n", sfr->data);

Fake flexible arrays are deprecated, and should be replaced by flexible-array members. So, replace one-element array with a flexible-array member in struct hfi_sfr.

While there, also annotate array data with __counted_by() to prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

This results in no differences in binary output.

This issue was found with the help of Coccinelle, and audited and fixed manually.

---
 drivers/media/platform/qcom/venus/hfi_cmds.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/venus/hfi_cmds.h +1 -1
index dd9c5066442d..20acd412ee7b 100644
@@ -242,7 +242,7 @@ struct hfi_session_parse_sequence_header_pkt {
 
 struct hfi_sfr {
 	u32 buf_size;
-	u8 data[1];
+	u8 data[] __counted_by(buf_size);
 };
 
 struct hfi_sys_test_ssr_pkt {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Gustavo A. R. Silva

Array data in struct hfi_sfr is being used as a fake flexible array at run-time:

drivers/media/platform/qcom/venus/hfi_venus.c:
1033 p = memchr(sfr->data, '\0', sfr->buf_size);
1034 /*

1035          * SFR isn't guaranteed to be NULL terminated since SYS_ERROR indicates
1036          * that Venus is in the process of crashing.
1037          */

1038 if (!p)
1039 sfr->data[sfr->buf_size - 1] = '\0';
1040
1041 dev_err_ratelimited(dev, "SFR message from FW: %s\n", sfr->data);

Fake flexible arrays are deprecated, and should be replaced by flexible-array members. So, replace one-element array with a flexible-array member in struct hfi_sfr.

Quoted 8 lines

While there, also annotate array data with __counted_by() to prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

This results in no differences in binary output.

Thanks for checking!

Quoted 5 lines

This issue was found with the help of Coccinelle, and audited and fixed manually.

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 26 lines · Kees Cook
Quoted 25 lines · Gustavo A. R. Silva

Array data in struct hfi_sfr is being used as a fake flexible array at run-time:

drivers/media/platform/qcom/venus/hfi_venus.c:
1033 p = memchr(sfr->data, '\0', sfr->buf_size);
1034 /*

1035          * SFR isn't guaranteed to be NULL terminated since SYS_ERROR indicates
1036          * that Venus is in the process of crashing.
1037          */

1038 if (!p)
1039 sfr->data[sfr->buf_size - 1] = '\0';
1040
1041 dev_err_ratelimited(dev, "SFR message from FW: %s\n", sfr->data);

Fake flexible arrays are deprecated, and should be replaced by flexible-array members. So, replace one-element array with a flexible-array member in struct hfi_sfr.

While there, also annotate array data with __counted_by() to prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions).

Quoted 3 lines
Quoted 1 line

This results in no differences in binary output.

Thanks for checking!

Sorry for shameless plug, a quick question: has any compiler implemented support for counted_by() at this point?

#3 lore raw
Quoted 4 lines

Sorry for shameless plug, a quick question: has any compiler implemented support for counted_by() at this point?

Not yet. And at least for GCC, it's expected to be released in v15.

Thanks
--
Gustavo

#4 lore raw
Quoted 6 lines · Gustavo A. R. Silva
Quoted 4 lines

Sorry for shameless plug, a quick question: has any compiler implemented support for counted_by() at this point?

Not yet. And at least for GCC, it's expected to be released in v15.

I see. Thank you.

I got confused by include/linux/compiler_attributes.h comment, as I'm on clang-18 currently, seems that we need to bump min compilers version. Oh, and clang link 404-s on me. I'll send a quick patch, I guess.

#5 lore raw
Quoted 6 lines · Sergey Senozhatsky
Quoted 5 lines · Gustavo A. R. Silva
Quoted 4 lines

Sorry for shameless plug, a quick question: has any compiler implemented support for counted_by() at this point?

Quoted 6 lines
Quoted 1 line

Not yet. And at least for GCC, it's expected to be released in v15.

I see. Thank you.

I got confused by include/linux/compiler_attributes.h comment, as I'm on clang-18 currently, seems that we need to bump min compilers version.

Ah yes, compiler devs have been running into some issues, and they had to postpone the release of the attribute.

Quoted 2 lines

Oh, and clang link 404-s on me. I'll send a quick patch, I guess.

You're right, ick!

--
Gustavo