Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: venus: hfi_msgs.h: Replace one-element arrays with flexible-array members

There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use “flexible array members”[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

Use flexible-array members in struct hfi_msg_sys_property_info_pkt and
hfi_msg_session_property_info_pkt instead of one-element arrays, and
refactor the code accordingly.

Also, this helps with the ongoing efforts to enable -Warray-bounds by
fixing the following warnings:

CC [M] drivers/media/platform/qcom/venus/hfi_msgs.o
drivers/media/platform/qcom/venus/hfi_msgs.c: In function ‘hfi_sys_property_info’:
drivers/media/platform/qcom/venus/hfi_msgs.c:246:35: warning: array subscript 1 is above array bounds of ‘u32[1]’ {aka ‘unsigned int[1]’} [-Warray-bounds]
246 | if (req_bytes < 128 || !pkt->data[1] || pkt->num_properties > 1)
| ~~~~~~~~~^~~
drivers/media/platform/qcom/venus/hfi_msgs.c: In function ‘hfi_session_prop_info’:
drivers/media/platform/qcom/venus/hfi_msgs.c:342:62: warning: array subscript 1 is above array bounds of ‘u32[1]’ {aka ‘unsigned int[1]’} [-Warray-bounds]
342 | if (!req_bytes || req_bytes % sizeof(*buf_req) || !pkt->data[1])
| ~~~~~~~~~^~~

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.9/process/deprecated.html#zero-length-and-one-element-arrays

Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/109

Co-developed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Stanimir Varbanov <stanimir.varbanov@linaro.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

authored by

Gustavo A. R. Silva and committed by
Mauro Carvalho Chehab
6f2f49ae c73c23f3

+12 -10
+8 -8
drivers/media/platform/qcom/venus/hfi_msgs.c
··· 251 251 252 252 req_bytes = pkt->hdr.size - sizeof(*pkt); 253 253 254 - if (req_bytes < VER_STR_SZ || !pkt->data[1] || pkt->num_properties > 1) 254 + if (req_bytes < VER_STR_SZ || !pkt->data[0] || pkt->num_properties > 1) 255 255 /* bad packet */ 256 256 return; 257 257 258 - img_ver = (u8 *)&pkt->data[1]; 258 + img_ver = pkt->data; 259 259 260 260 dev_dbg(dev, VDBGL "F/W version: %s\n", img_ver); 261 261 ··· 277 277 return; 278 278 } 279 279 280 - switch (pkt->data[0]) { 280 + switch (pkt->property) { 281 281 case HFI_PROPERTY_SYS_IMAGE_VERSION: 282 282 sys_get_prop_image_version(dev, pkt); 283 283 break; ··· 338 338 /* bad packet */ 339 339 return HFI_ERR_SESSION_INVALID_PARAMETER; 340 340 341 - hfi = (struct hfi_profile_level *)&pkt->data[1]; 341 + hfi = (struct hfi_profile_level *)&pkt->data[0]; 342 342 profile_level->profile = hfi->profile; 343 343 profile_level->level = hfi->level; 344 344 ··· 355 355 356 356 req_bytes = pkt->shdr.hdr.size - sizeof(*pkt); 357 357 358 - if (!req_bytes || req_bytes % sizeof(*buf_req) || !pkt->data[1]) 358 + if (!req_bytes || req_bytes % sizeof(*buf_req) || !pkt->data[0]) 359 359 /* bad packet */ 360 360 return HFI_ERR_SESSION_INVALID_PARAMETER; 361 361 362 - buf_req = (struct hfi_buffer_requirements *)&pkt->data[1]; 362 + buf_req = (struct hfi_buffer_requirements *)&pkt->data[0]; 363 363 if (!buf_req) 364 364 return HFI_ERR_SESSION_INVALID_PARAMETER; 365 365 ··· 391 391 goto done; 392 392 } 393 393 394 - switch (pkt->data[0]) { 394 + switch (pkt->property) { 395 395 case HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS: 396 396 memset(hprop->bufreq, 0, sizeof(hprop->bufreq)); 397 397 error = session_get_prop_buf_req(pkt, hprop->bufreq); ··· 404 404 case HFI_PROPERTY_CONFIG_VDEC_ENTROPY: 405 405 break; 406 406 default: 407 - dev_dbg(dev, VDBGM "unknown property id:%x\n", pkt->data[0]); 407 + dev_dbg(dev, VDBGM "unknown property id:%x\n", pkt->property); 408 408 return; 409 409 } 410 410
+4 -2
drivers/media/platform/qcom/venus/hfi_msgs.h
··· 113 113 struct hfi_msg_sys_property_info_pkt { 114 114 struct hfi_pkt_hdr hdr; 115 115 u32 num_properties; 116 - u32 data[1]; 116 + u32 property; 117 + u8 data[]; 117 118 }; 118 119 119 120 struct hfi_msg_session_load_resources_done_pkt { ··· 234 233 struct hfi_msg_session_property_info_pkt { 235 234 struct hfi_session_hdr_pkt shdr; 236 235 u32 num_properties; 237 - u32 data[1]; 236 + u32 property; 237 + u8 data[]; 238 238 }; 239 239 240 240 struct hfi_msg_session_release_resources_done_pkt {