The firmware_tag string comes from userspace and may not be NUL
terminated. strlcpy() performs strlen() on the source buffer, which can
read past the end and potentially cause an Oops. strscpy() avoids this
and guarantees NUL termination without overflowing the destination.
Signed-off-by: Dharanitharan R <dharanitharan725(a)gmail.com>
---
Changes in v4:
- Replace strlcpy() with strscpy() based on maintainer feedback
- Added explanation about user-controlled buffers
Changes in v3:
- Fix Signed-off-by spacing
- Move changelog below '---'
Changes in v2:
- Fixed indentation issues reported by Greg KH
---
.../greybus/Documentation/firmware/firmware.c | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/drivers/staging/greybus/Documentation/firmware/firmware.c b/drivers/staging/greybus/Documentation/firmware/firmware.c
index 6f6410878ee6..84b9cf45fc8e 100644
--- a/drivers/staging/greybus/Documentation/firmware/firmware.c
+++ b/drivers/staging/greybus/Documentation/firmware/firmware.c
@@ -63,10 +63,11 @@ static int update_intf_firmware(int fd)
intf_load.major = 0;
intf_load.minor = 0;
- strlcpy(intf_load.firmware_tag, firmware_tag,
- GB_FIRMWARE_U_TAG_MAX_SIZE);
+ /* firmware_tag comes from userspace and may not be NUL terminated.
+ * strscpy() avoids strlen() on src and guarantees NUL termination.
+ */
+ strscpy(intf_load.firmware_tag, firmware_tag,
+ GB_FIRMWARE_U_TAG_MAX_SIZE);
ret = ioctl(fd, FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE, &intf_load);
if (ret < 0) {
@@ -103,8 +104,8 @@ static int update_backend_firmware(int fd)
/* Get Backend Firmware Version */
printf("Getting Backend Firmware Version\n");
- strlcpy(backend_fw_info.firmware_tag, firmware_tag,
- GB_FIRMWARE_U_TAG_MAX_SIZE);
+ strscpy(backend_fw_info.firmware_tag, firmware_tag,
+ GB_FIRMWARE_U_TAG_MAX_SIZE);
retry_fw_version:
@@ -132,8 +133,8 @@ static int update_backend_firmware(int fd)
/* Try Backend Firmware Update over Unipro */
printf("Updating Backend Firmware\n");
- strlcpy(backend_update.firmware_tag, firmware_tag,
- GB_FIRMWARE_U_TAG_MAX_SIZE);
+ strscpy(backend_update.firmware_tag, firmware_tag,
+ GB_FIRMWARE_U_TAG_MAX_SIZE);
retry_fw_update:
backend_update.status = 0;
--
2.43.0
Check the return values of send_control() and send_line_coding() during
device initialization in gb_uart_probe(). If these operations fail, the
device will be left in an inconsistent state, so propagate the error to
properly fail the probe.
Both functions call gb_operation_sync() which can fail with errors such
as -ENOMEM, -ENODEV, or -ETIMEDOUT. Ignoring these errors means the TTY
device would be registered despite incomplete initialization.
Signed-off-by: Nirbhay Sharma <nirbhay.lkd(a)gmail.com>
---
drivers/staging/greybus/uart.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/staging/greybus/uart.c b/drivers/staging/greybus/uart.c
index 10df5c37c83e..5cece0a6606f 100644
--- a/drivers/staging/greybus/uart.c
+++ b/drivers/staging/greybus/uart.c
@@ -879,14 +879,18 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
if (retval)
goto exit_put_port;
- send_control(gb_tty, gb_tty->ctrlout);
+ retval = send_control(gb_tty, gb_tty->ctrlout);
+ if (retval)
+ goto exit_connection_disable;
/* initialize the uart to be 9600n81 */
gb_tty->line_coding.rate = cpu_to_le32(9600);
gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
gb_tty->line_coding.data_bits = 8;
- send_line_coding(gb_tty);
+ retval = send_line_coding(gb_tty);
+ if (retval)
+ goto exit_connection_disable;
retval = gb_connection_enable(connection);
if (retval)
--
2.48.1