git: b10e93220e52 - stable/14 - mfiutil: Handle potential ioctl(2) failures in mfi_flash.c

From: Xin LI <delphij_at_FreeBSD.org>
Date: Tue, 10 Sep 2024 04:31:04 UTC
The branch stable/14 has been updated by delphij:

URL: https://cgit.FreeBSD.org/src/commit/?id=b10e93220e5221147483ef17f015863138e31583

commit b10e93220e5221147483ef17f015863138e31583
Author:     WHR <whr@rivoreo.one>
AuthorDate: 2024-09-03 05:12:20 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2024-09-10 04:30:51 +0000

    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
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1403
    
    (cherry picked from commit bac98f86c98421f6153c749b6c77995ef778e2fb)
---
 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;