greybus: firmware: use strscpy, fix tag size
Subsystems (3)
PATCH 1/1 greybus: firmware: use strscpy, fix tag size
Increase the size of firmware_tag arrays in the following structs from GB_FIRMWARE_U_TAG_MAX_SIZE to GB_FIRMWARE_U_TAG_MAX_SIZE + 1 to accommodate null termination:
- fw_mgmt_ioc_intf_load_and_validate
- fw_mgmt_ioc_get_backend_version
- fw_mgmt_ioc_backend_fw_update
- fw_mgmt_ioc_get_intf_version
Replace strncpy() with strscpy() to ensure proper null termination as firmware_tag is interpreted as a null-terminated string and printed with %s.
--- .../greybus/Documentation/firmware/firmware.c | 12 ++++++------ drivers/staging/greybus/greybus_firmware.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)
drivers/staging/greybus/Documentation/firmware/firmware.c +6 -6
@@ -63,8 +63,8 @@ static int update_intf_firmware(int fd) intf_load.major =0 ; intf_load.minor =0 ; -strncpy ((char *)&intf_load.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&intf_load.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); ret =ioctl (fd,FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE , &intf_load);if (ret <0 ) {
@@ -101,8 +101,8 @@ static int update_backend_firmware(int fd)/* Get Backend Firmware Version */ printf ("Getting Backend Firmware Version\n" ); -strncpy ((char *)&backend_fw_info.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&backend_fw_info.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); retry_fw_version: ret =ioctl (fd,FW_MGMT_IOC_GET_BACKEND_FW , &backend_fw_info);
@@ -129,8 +129,8 @@ static int update_backend_firmware(int fd)/* Try Backend Firmware Update over Unipro */ printf ("Updating Backend Firmware\n" ); -strncpy ((char *)&backend_update.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&backend_update.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); retry_fw_update: backend_update.status =0 ;
drivers/staging/greybus/greybus_firmware.h +4 -4
@@ -38,20 +38,20 @@/* IOCTL support */ struct fw_mgmt_ioc_get_intf_version { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u16 major;__u16 minor; } __packed;struct fw_mgmt_ioc_get_backend_version { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u16 major;__u16 minor;__u8 status; } __packed;struct fw_mgmt_ioc_intf_load_and_validate { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u8 load_method;__u8 status;__u16 major;
@@ -59,7 +59,7 @@ struct fw_mgmt_ioc_intf_load_and_validate { } __packed;struct fw_mgmt_ioc_backend_fw_update { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u8 status; } __packed;
Signature · 1 line
2.49.0
Greg KH
maintainerQuoted 84 lines · Pranav Tyagi
Increase the size of
firmware_tagarrays in the following structs fromGB_FIRMWARE_U_TAG_MAX_SIZEtoGB_FIRMWARE_U_TAG_MAX_SIZE+ 1 to accommodate null termination:
-fw_mgmt_ioc_intf_load_and_validate
-fw_mgmt_ioc_get_backend_version
-fw_mgmt_ioc_backend_fw_update
-fw_mgmt_ioc_get_intf_versionReplace
strncpy()withstrscpy()to ensure proper null termination asfirmware_tagis interpreted as a null-terminated string and printed with %s.Signed-off-by Pranav Tyagi <[email protected]>--- .../greybus/Documentation/firmware/firmware.c | 12 ++++++------ drivers/staging/greybus/greybus_firmware.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)drivers/staging/greybus/Documentation/firmware/firmware.c +6
-6@@ -63,8 +63,8 @@ static int update_intf_firmware(int fd) intf_load.major =0 ; intf_load.minor =0 ; -strncpy ((char *)&intf_load.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&intf_load.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); ret =ioctl (fd,FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE , &intf_load);if (ret <0 ) {@@ -101,8 +101,8 @@ static int update_backend_firmware(int fd)/* Get Backend Firmware Version */ printf ("Getting Backend Firmware Version\n" ); -strncpy ((char *)&backend_fw_info.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&backend_fw_info.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); retry_fw_version: ret =ioctl (fd,FW_MGMT_IOC_GET_BACKEND_FW , &backend_fw_info);@@ -129,8 +129,8 @@ static int update_backend_firmware(int fd)/* Try Backend Firmware Update over Unipro */ printf ("Updating Backend Firmware\n" ); -strncpy ((char *)&backend_update.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&backend_update.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); retry_fw_update: backend_update.status =0 ;drivers/staging/greybus/greybus_firmware.h +3
-3@@ -38,20 +38,20 @@/* IOCTL support */ struct fw_mgmt_ioc_get_intf_version { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u16 major;__u16 minor; } __packed;struct fw_mgmt_ioc_get_backend_version { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u16 major;__u16 minor;__u8 status; } __packed;struct fw_mgmt_ioc_intf_load_and_validate { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u8 load_method;__u8 status;__u16 major;
drivers/staging/greybus/greybus_firmware.h struct fw_mgmt_ioc_intf_load_and_validate { · line 65
Quoted 8 lines
drivers/staging/greybus/greybus_firmware.h +1
-1@@ -59,7 +59,7 @@ struct fw_mgmt_ioc_intf_load_and_validate { } __packed;struct fw_mgmt_ioc_backend_fw_update { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u8 status; } __packed;
You are changing the size of a userspace structure here, are you SURE this is allowed?
How was this tested?
thanks,
greg k-h
Quoted 98 lines · Greg KH
Quoted 84 lines · Pranav Tyagi
Increase the size of
firmware_tagarrays in the following structs fromGB_FIRMWARE_U_TAG_MAX_SIZEtoGB_FIRMWARE_U_TAG_MAX_SIZE+ 1 to accommodate null termination:
-fw_mgmt_ioc_intf_load_and_validate
-fw_mgmt_ioc_get_backend_version
-fw_mgmt_ioc_backend_fw_update
-fw_mgmt_ioc_get_intf_versionReplace
strncpy()withstrscpy()to ensure proper null termination asfirmware_tagis interpreted as a null-terminated string and printed with %s.Signed-off-by Pranav Tyagi <[email protected]>--- .../greybus/Documentation/firmware/firmware.c | 12 ++++++------ drivers/staging/greybus/greybus_firmware.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)drivers/staging/greybus/Documentation/firmware/firmware.c +6
-6@@ -63,8 +63,8 @@ static int update_intf_firmware(int fd) intf_load.major =0 ; intf_load.minor =0 ; -strncpy ((char *)&intf_load.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&intf_load.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); ret =ioctl (fd,FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE , &intf_load);if (ret <0 ) {@@ -101,8 +101,8 @@ static int update_backend_firmware(int fd)/* Get Backend Firmware Version */ printf ("Getting Backend Firmware Version\n" ); -strncpy ((char *)&backend_fw_info.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&backend_fw_info.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); retry_fw_version: ret =ioctl (fd,FW_MGMT_IOC_GET_BACKEND_FW , &backend_fw_info);@@ -129,8 +129,8 @@ static int update_backend_firmware(int fd)/* Try Backend Firmware Update over Unipro */ printf ("Updating Backend Firmware\n" ); -strncpy ((char *)&backend_update.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&backend_update.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 ); retry_fw_update: backend_update.status =0 ;drivers/staging/greybus/greybus_firmware.h +3
-3@@ -38,20 +38,20 @@/* IOCTL support */ struct fw_mgmt_ioc_get_intf_version { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u16 major;__u16 minor; } __packed;struct fw_mgmt_ioc_get_backend_version { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u16 major;__u16 minor;__u8 status; } __packed;struct fw_mgmt_ioc_intf_load_and_validate { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u8 load_method;__u8 status;__u16 major;drivers/staging/greybus/greybus_firmware.h struct fw_mgmt_ioc_intf_load_and_validate { · line 65
Quoted 8 lines
drivers/staging/greybus/greybus_firmware.h +1
-1@@ -59,7 +59,7 @@ struct fw_mgmt_ioc_intf_load_and_validate { } __packed;struct fw_mgmt_ioc_backend_fw_update { -__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE ]; +__u8 firmware_tag[GB_FIRMWARE_U_TAG_MAX_SIZE +1 ];__u8 status; } __packed;You are changing the size of a userspace structure here, are you SURE this is allowed?
Quoted 5 lines
How was this tested?
thanks,
greg k-h
Hi,
You're absolutely right — changing the size of a userspace-visible structure like this is not allowed and I apologize for the oversight.
I did compile and boot the kernel with the patch applied, but I now realize that I should have also tested it against the actual userspace tooling to validate ABI compatibility — which I didn’t do. That was a miss on my part.
Thanks for the review and the guidance. I’ll fix the patch to preserve structure sizes and ensure safe null termination without breaking the ABI — likely by using strscpy() with size - 1 and setting the last byte explicitly if needed.
Apologies for the oversight.
Regards
Pranav Tyagi
Quoted 23 lines · Pranav Tyagi
Increase the size of
firmware_tagarrays in the following structs fromGB_FIRMWARE_U_TAG_MAX_SIZEtoGB_FIRMWARE_U_TAG_MAX_SIZE+ 1 to accommodate null termination:
-fw_mgmt_ioc_intf_load_and_validate
-fw_mgmt_ioc_get_backend_version
-fw_mgmt_ioc_backend_fw_update
-fw_mgmt_ioc_get_intf_versionReplace
strncpy()withstrscpy()to ensure proper null termination asfirmware_tagis interpreted as a null-terminated string and printed with %s.Signed-off-by Pranav Tyagi <[email protected]>--- .../greybus/Documentation/firmware/firmware.c | 12 ++++++------ drivers/staging/greybus/greybus_firmware.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)drivers/staging/greybus/Documentation/firmware/firmware.c +0
-0
drivers/staging/greybus/Documentation/firmware/firmware.c static int update_intf_firmware(int fd) · line 67
Quoted 7 lines
drivers/staging/greybus/Documentation/firmware/firmware.c +2
-2@@ -63,8 +63,8 @@ static int update_intf_firmware(int fd) intf_load.major =0 ; intf_load.minor =0 ; -strncpy ((char *)&intf_load.firmware_tag, firmware_tag, -GB_FIRMWARE_U_TAG_MAX_SIZE ); +strscpy ((char *)&intf_load.firmware_tag, firmware_tag, +GB_FIRMWARE_U_TAG_MAX_SIZE +1 );
This is in the Documentation/ directory. It's sample userspace code. Userspace code doesn't have strscpy() so this won't compile.
regards,
dan carpenter