mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Riyan Dhiman <riyandhiman14@gmail.com>
To: aacraid@microsemi.com, James.Bottomley@HansenPartnership.com,
	martin.petersen@oracle.com
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	Riyan Dhiman <riyandhiman14@gmail.com>
Subject: [PATCH] scsi: aacraid: Prevent premature freeing and improve memory allocation checks for fsa_dev
Date: Tue,  3 Sep 2024 22:49:11 +0530	[thread overview]
Message-ID: <20240903171911.9197-1-riyandhiman14@gmail.com> (raw)

Refactor the allocation and freeing sequence of `fsa_dev` to ensure
that memory is not prematurely freed, which could lead to use-after-free errors
or undefined behavior.

Changes:
- Modified the order of memory operations by allocating new memory for fsa_dev
  first and checking for success before freeing the old fsa_dev pointer.
- Updated the error handling to ensure -ENOMEM is returned if allocation fails,
  preserving the existing valid memory.

Signed-off-by: Riyan Dhiman <riyandhiman14@gmail.com>
---
 drivers/scsi/aacraid/aachba.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/drivers/scsi/aacraid/aachba.c b/drivers/scsi/aacraid/aachba.c
index b22857c6f3f4..f3fc9b622aee 100644
--- a/drivers/scsi/aacraid/aachba.c
+++ b/drivers/scsi/aacraid/aachba.c
@@ -490,18 +490,14 @@ int aac_get_containers(struct aac_dev *dev)
 	if (dev->fsa_dev == NULL ||
 		dev->maximum_num_containers != maximum_num_containers) {
 
-		fsa_dev_ptr = dev->fsa_dev;
-
-		dev->fsa_dev = kcalloc(maximum_num_containers,
+		fsa_dev_ptr = kcalloc(maximum_num_containers,
 					sizeof(*fsa_dev_ptr), GFP_KERNEL);
-
-		kfree(fsa_dev_ptr);
-		fsa_dev_ptr = NULL;
-
-
-		if (!dev->fsa_dev)
+		if(!fsa_dev_ptr)
 			return -ENOMEM;
 
+		kfree(dev->fsa_dev);
+		dev->fsa_dev = fsa_dev_ptr;
+
 		dev->maximum_num_containers = maximum_num_containers;
 	}
 	for (index = 0; index < dev->maximum_num_containers; index++) {
-- 
2.46.0


                 reply	other threads:[~2024-09-03 17:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240903171911.9197-1-riyandhiman14@gmail.com \
    --to=riyandhiman14@gmail.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=aacraid@microsemi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®