Blob Blame History Raw
From: James Smart <jsmart2021@gmail.com>
Date: Wed, 18 Dec 2019 15:58:03 -0800
Subject: scsi: lpfc: Fix ras_log via debugfs
Patch-mainline: v5.6-rc1
Git-commit: 9a20cc10fa0504693d2dadb90b2ae185755abc09
References: bsc#1164777 bsc#1164780 bsc#1165211

/sys/kernel/debug/lpfc/fn0/ras_log always shows the same ras_log even if
there are link bounce events triggered via issue_lip

Dynamic FW logging had logic that prematurely breaks from the buffer
filling loop.

Fix the check for buffer overrun by looking before copying and restricting
copy length to the remaining buffer.  When copying, ensure space for NULL
character is left in the buffer.  While in the routine - ensure the buffer
is cleared before adding elements.

Link: https://lore.kernel.org/r/20191218235808.31922-6-jsmart2021@gmail.com
Signed-off-by: Dick Kennedy <dick.kennedy@broadcom.com>
Signed-off-by: James Smart <jsmart2021@gmail.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Acked-by: Daniel Wagner <dwagner@suse.de>
---
 drivers/scsi/lpfc/lpfc_debugfs.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

--- a/drivers/scsi/lpfc/lpfc_debugfs.c
+++ b/drivers/scsi/lpfc/lpfc_debugfs.c
@@ -2085,6 +2085,8 @@ static int lpfc_debugfs_ras_log_data(str
 	int copied = 0;
 	struct lpfc_dmabuf *dmabuf, *next;
 
+	memset(buffer, 0, size);
+
 	spin_lock_irq(&phba->hbalock);
 	if (phba->ras_fwlog.state != ACTIVE) {
 		spin_unlock_irq(&phba->hbalock);
@@ -2094,10 +2096,15 @@ static int lpfc_debugfs_ras_log_data(str
 
 	list_for_each_entry_safe(dmabuf, next,
 				 &phba->ras_fwlog.fwlog_buff_list, list) {
+		/* Check if copying will go over size and a '\0' char */
+		if ((copied + LPFC_RAS_MAX_ENTRY_SIZE) >= (size - 1)) {
+			memcpy(buffer + copied, dmabuf->virt,
+			       size - copied - 1);
+			copied += size - copied - 1;
+			break;
+		}
 		memcpy(buffer + copied, dmabuf->virt, LPFC_RAS_MAX_ENTRY_SIZE);
 		copied += LPFC_RAS_MAX_ENTRY_SIZE;
-		if (size > copied)
-			break;
 	}
 	return copied;
 }