media: venus: hfi_msgs.h: Replace one-element arrays with flexible-array members
authorGustavo A. R. Silva <gustavoars@kernel.org>
Fri, 4 Jun 2021 00:43:38 +0000 (02:43 +0200)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Thu, 17 Jun 2021 07:42:30 +0000 (09:42 +0200)
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>
drivers/media/platform/qcom/venus/hfi_msgs.c
drivers/media/platform/qcom/venus/hfi_msgs.h

index a2d436d..d9fde66 100644 (file)
@@ -251,11 +251,11 @@ sys_get_prop_image_version(struct device *dev,
 
        req_bytes = pkt->hdr.size - sizeof(*pkt);
 
-       if (req_bytes < VER_STR_SZ || !pkt->data[1] || pkt->num_properties > 1)
+       if (req_bytes < VER_STR_SZ || !pkt->data[0] || pkt->num_properties > 1)
                /* bad packet */
                return;
 
-       img_ver = (u8 *)&pkt->data[1];
+       img_ver = pkt->data;
 
        dev_dbg(dev, VDBGL "F/W version: %s\n", img_ver);
 
@@ -277,7 +277,7 @@ static void hfi_sys_property_info(struct venus_core *core,
                return;
        }
 
-       switch (pkt->data[0]) {
+       switch (pkt->property) {
        case HFI_PROPERTY_SYS_IMAGE_VERSION:
                sys_get_prop_image_version(dev, pkt);
                break;
@@ -338,7 +338,7 @@ session_get_prop_profile_level(struct hfi_msg_session_property_info_pkt *pkt,
                /* bad packet */
                return HFI_ERR_SESSION_INVALID_PARAMETER;
 
-       hfi = (struct hfi_profile_level *)&pkt->data[1];
+       hfi = (struct hfi_profile_level *)&pkt->data[0];
        profile_level->profile = hfi->profile;
        profile_level->level = hfi->level;
 
@@ -355,11 +355,11 @@ session_get_prop_buf_req(struct hfi_msg_session_property_info_pkt *pkt,
 
        req_bytes = pkt->shdr.hdr.size - sizeof(*pkt);
 
-       if (!req_bytes || req_bytes % sizeof(*buf_req) || !pkt->data[1])
+       if (!req_bytes || req_bytes % sizeof(*buf_req) || !pkt->data[0])
                /* bad packet */
                return HFI_ERR_SESSION_INVALID_PARAMETER;
 
-       buf_req = (struct hfi_buffer_requirements *)&pkt->data[1];
+       buf_req = (struct hfi_buffer_requirements *)&pkt->data[0];
        if (!buf_req)
                return HFI_ERR_SESSION_INVALID_PARAMETER;
 
@@ -391,7 +391,7 @@ static void hfi_session_prop_info(struct venus_core *core,
                goto done;
        }
 
-       switch (pkt->data[0]) {
+       switch (pkt->property) {
        case HFI_PROPERTY_CONFIG_BUFFER_REQUIREMENTS:
                memset(hprop->bufreq, 0, sizeof(hprop->bufreq));
                error = session_get_prop_buf_req(pkt, hprop->bufreq);
@@ -404,7 +404,7 @@ static void hfi_session_prop_info(struct venus_core *core,
        case HFI_PROPERTY_CONFIG_VDEC_ENTROPY:
                break;
        default:
-               dev_dbg(dev, VDBGM "unknown property id:%x\n", pkt->data[0]);
+               dev_dbg(dev, VDBGM "unknown property id:%x\n", pkt->property);
                return;
        }
 
index 526d9f5..5105136 100644 (file)
@@ -113,7 +113,8 @@ struct hfi_msg_sys_ping_ack_pkt {
 struct hfi_msg_sys_property_info_pkt {
        struct hfi_pkt_hdr hdr;
        u32 num_properties;
-       u32 data[1];
+       u32 property;
+       u8 data[];
 };
 
 struct hfi_msg_session_load_resources_done_pkt {
@@ -233,7 +234,8 @@ struct hfi_msg_session_parse_sequence_header_done_pkt {
 struct hfi_msg_session_property_info_pkt {
        struct hfi_session_hdr_pkt shdr;
        u32 num_properties;
-       u32 data[1];
+       u32 property;
+       u8 data[];
 };
 
 struct hfi_msg_session_release_resources_done_pkt {