Skip to content

Commit 77541f7

Browse files
Harshvardhan Jhamartinkpetersen
authored andcommitted
scsi: megaraid_mm: Fix end of loop tests for list_for_each_entry()
The list_for_each_entry() iterator, "adapter" in this code, can never be NULL. If we exit the loop without finding the correct adapter then "adapter" points invalid memory that is an offset from the list head. This will eventually lead to memory corruption and presumably a kernel crash. Link: https://lore.kernel.org/r/[email protected] Acked-by: Sumit Saxena <[email protected]> Signed-off-by: Harshvardhan Jha <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent d712d3f commit 77541f7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

drivers/scsi/megaraid/megaraid_mm.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
238238
mimd_t mimd;
239239
uint32_t adapno;
240240
int iterator;
241-
241+
bool is_found;
242242

243243
if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
244244
*rval = -EFAULT;
@@ -254,12 +254,16 @@ mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
254254

255255
adapter = NULL;
256256
iterator = 0;
257+
is_found = false;
257258

258259
list_for_each_entry(adapter, &adapters_list_g, list) {
259-
if (iterator++ == adapno) break;
260+
if (iterator++ == adapno) {
261+
is_found = true;
262+
break;
263+
}
260264
}
261265

262-
if (!adapter) {
266+
if (!is_found) {
263267
*rval = -ENODEV;
264268
return NULL;
265269
}
@@ -725,6 +729,7 @@ ioctl_done(uioc_t *kioc)
725729
uint32_t adapno;
726730
int iterator;
727731
mraid_mmadp_t* adapter;
732+
bool is_found;
728733

729734
/*
730735
* When the kioc returns from driver, make sure it still doesn't
@@ -747,19 +752,23 @@ ioctl_done(uioc_t *kioc)
747752
iterator = 0;
748753
adapter = NULL;
749754
adapno = kioc->adapno;
755+
is_found = false;
750756

751757
con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
752758
"ioctl that was timedout before\n"));
753759

754760
list_for_each_entry(adapter, &adapters_list_g, list) {
755-
if (iterator++ == adapno) break;
761+
if (iterator++ == adapno) {
762+
is_found = true;
763+
break;
764+
}
756765
}
757766

758767
kioc->timedout = 0;
759768

760-
if (adapter) {
769+
if (is_found)
761770
mraid_mm_dealloc_kioc( adapter, kioc );
762-
}
771+
763772
}
764773
else {
765774
wake_up(&wait_q);

0 commit comments

Comments
 (0)