aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMilan P. Gandhi <mgandhi@redhat.com>2016-12-24 11:32:46 -0500
committerMartin K. Petersen <martin.petersen@oracle.com>2017-01-09 23:15:02 -0500
commitc7702b8c22712a06080e10f1d2dee1a133ec8809 (patch)
tree151e292f5407fd7fe7fb95d1da5724a821042b9b
parent2d1148f0f45079d25a0fa0d67e4fdb2a656d12fb (diff)
scsi: qla2xxx: Get mutex lock before checking optrom_state
There is a race condition with qla2xxx optrom functions where one thread might modify optrom buffer, optrom_state while other thread is still reading from it. In couple of crashes, it was found that we had successfully passed the following 'if' check where we confirm optrom_state to be QLA_SREADING. But by the time we acquired mutex lock to proceed with memory_read_from_buffer function, some other thread/process had already modified that option rom buffer and optrom_state from QLA_SREADING to QLA_SWAITING. Then we got ha->optrom_buffer 0x0 and crashed the system: if (ha->optrom_state != QLA_SREADING) return 0; mutex_lock(&ha->optrom_mutex); rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer, ha->optrom_region_size); mutex_unlock(&ha->optrom_mutex); With current optrom function we get following crash due to a race condition: [ 1479.466679] BUG: unable to handle kernel NULL pointer dereference at (null) [ 1479.466707] IP: [<ffffffff81326756>] memcpy+0x6/0x110 [...] [ 1479.473673] Call Trace: [ 1479.474296] [<ffffffff81225cbc>] ? memory_read_from_buffer+0x3c/0x60 [ 1479.474941] [<ffffffffa01574dc>] qla2x00_sysfs_read_optrom+0x9c/0xc0 [qla2xxx] [ 1479.475571] [<ffffffff8127e76b>] read+0xdb/0x1f0 [ 1479.476206] [<ffffffff811fdf9e>] vfs_read+0x9e/0x170 [ 1479.476839] [<ffffffff811feb6f>] SyS_read+0x7f/0xe0 [ 1479.477466] [<ffffffff816964c9>] system_call_fastpath+0x16/0x1b Below patch modifies qla2x00_sysfs_read_optrom, qla2x00_sysfs_write_optrom functions to get the mutex_lock before checking ha->optrom_state to avoid similar crashes. The patch was applied and tested and same crashes were no longer observed again. Tested-by: Milan P. Gandhi <mgandhi@redhat.com> Signed-off-by: Milan P. Gandhi <mgandhi@redhat.com> Reviewed-by: Laurence Oberman <loberman@redhat.com> Acked-by: Himanshu Madhani <himanshu.madhani@cavium.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r--drivers/scsi/qla2xxx/qla_attr.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/drivers/scsi/qla2xxx/qla_attr.c b/drivers/scsi/qla2xxx/qla_attr.c
index 47eb4d545d13..83f8527c0363 100644
--- a/drivers/scsi/qla2xxx/qla_attr.c
+++ b/drivers/scsi/qla2xxx/qla_attr.c
@@ -243,12 +243,15 @@ qla2x00_sysfs_read_optrom(struct file *filp, struct kobject *kobj,
243 struct qla_hw_data *ha = vha->hw; 243 struct qla_hw_data *ha = vha->hw;
244 ssize_t rval = 0; 244 ssize_t rval = 0;
245 245
246 mutex_lock(&ha->optrom_mutex);
247
246 if (ha->optrom_state != QLA_SREADING) 248 if (ha->optrom_state != QLA_SREADING)
247 return 0; 249 goto out;
248 250
249 mutex_lock(&ha->optrom_mutex);
250 rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer, 251 rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
251 ha->optrom_region_size); 252 ha->optrom_region_size);
253
254out:
252 mutex_unlock(&ha->optrom_mutex); 255 mutex_unlock(&ha->optrom_mutex);
253 256
254 return rval; 257 return rval;
@@ -263,14 +266,19 @@ qla2x00_sysfs_write_optrom(struct file *filp, struct kobject *kobj,
263 struct device, kobj))); 266 struct device, kobj)));
264 struct qla_hw_data *ha = vha->hw; 267 struct qla_hw_data *ha = vha->hw;
265 268
266 if (ha->optrom_state != QLA_SWRITING) 269 mutex_lock(&ha->optrom_mutex);
270
271 if (ha->optrom_state != QLA_SWRITING) {
272 mutex_unlock(&ha->optrom_mutex);
267 return -EINVAL; 273 return -EINVAL;
268 if (off > ha->optrom_region_size) 274 }
275 if (off > ha->optrom_region_size) {
276 mutex_unlock(&ha->optrom_mutex);
269 return -ERANGE; 277 return -ERANGE;
278 }
270 if (off + count > ha->optrom_region_size) 279 if (off + count > ha->optrom_region_size)
271 count = ha->optrom_region_size - off; 280 count = ha->optrom_region_size - off;
272 281
273 mutex_lock(&ha->optrom_mutex);
274 memcpy(&ha->optrom_buffer[off], buf, count); 282 memcpy(&ha->optrom_buffer[off], buf, count);
275 mutex_unlock(&ha->optrom_mutex); 283 mutex_unlock(&ha->optrom_mutex);
276 284