From bac98f86c98421f6153c749b6c77995ef778e2fb Mon Sep 17 00:00:00 2001 From: WHR Date: Tue, 3 Sep 2024 13:12:20 +0800 Subject: [PATCH] mfiutil: Handle potential ioctl(2) failures in mfi_flash.c The return value of function 'mfi_dcmd_command' should always be checked for the potential ioctl(2) failure. PR: 281158 MFC after: 1 week Pull Request: https://github.com/freebsd/freebsd-src/pull/1403 --- usr.sbin/mfiutil/mfi_flash.c | 39 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/usr.sbin/mfiutil/mfi_flash.c b/usr.sbin/mfiutil/mfi_flash.c index 2fbfc978edac..4d1930af4941 100644 --- a/usr.sbin/mfiutil/mfi_flash.c +++ b/usr.sbin/mfiutil/mfi_flash.c @@ -129,9 +129,15 @@ flash_adapter(int ac, char **av) /* First, ask the firmware to allocate space for the flash file. */ mbox_store_word(mbox, sb.st_size); - mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status); + if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, + &status) < 0) { + error = errno; + warn("Failed to allocate flash memory"); + goto error; + } if (status != MFI_STAT_OK) { - warnx("Failed to alloc flash memory: %s", mfi_status(status)); + warnx("Failed to allocate flash memory: %s", + mfi_status(status)); error = EIO; goto error; } @@ -148,19 +154,26 @@ flash_adapter(int ac, char **av) nread = read(flash, buf, FLASH_BUF_SIZE); if (nread <= 0 || nread % 1024 != 0) { warnx("Bad read from flash file"); - mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0, - NULL, 0, NULL); + if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, + NULL, 0, NULL, 0, NULL) < 0) { + warn("Failed to discard flash memory"); + } error = ENXIO; goto error; } mbox_store_word(mbox, offset); - mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread, - mbox, 4, &status); + if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread, + mbox, 4, &status) < 0) { + error = errno; + warn("Failed to download firmware"); + goto error; + } if (status != MFI_STAT_OK) { - warnx("Flash download failed: %s", mfi_status(status)); - mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0, - NULL, 0, NULL); + warnx("Failed to download firmware: %s", + mfi_status(status)); + mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, + 0, NULL, 0, NULL); error = ENXIO; goto error; } @@ -171,8 +184,12 @@ flash_adapter(int ac, char **av) /* Kick off the flash. */ printf("WARNING: Firmware flash in progress, do not reboot machine... "); fflush(stdout); - mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy), - NULL, 0, &status); + if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy), + NULL, 0, &status) < 0) { + error = errno; + printf("failed:\n\t%s\n", strerror(error)); + goto error; + } if (status != MFI_STAT_OK) { printf("failed:\n\t%s\n", mfi_status(status)); error = ENXIO;