Skip to content

Commit a74b2bf

Browse files
seanyoungmchehab
authored andcommitted
media: lirc: do not pass ERR_PTR to kfree
If memdup_user() fails, txbuf will be an error pointer and passed to kfree. Reported-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Sean Young <sean@mess.org> Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
1 parent b996157 commit a74b2bf

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

‎drivers/media/rc/lirc_dev.c‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
231231
{
232232
struct lirc_fh *fh = file->private_data;
233233
struct rc_dev *dev = fh->rc;
234-
unsigned int *txbuf = NULL;
234+
unsigned int *txbuf;
235235
struct ir_raw_event *raw = NULL;
236236
ssize_t ret;
237237
size_t count;
@@ -246,32 +246,32 @@ static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
246246

247247
if (!dev->registered) {
248248
ret = -ENODEV;
249-
goto out;
249+
goto out_unlock;
250250
}
251251

252252
start = ktime_get();
253253

254254
if (!dev->tx_ir) {
255255
ret = -EINVAL;
256-
goto out;
256+
goto out_unlock;
257257
}
258258

259259
if (fh->send_mode == LIRC_MODE_SCANCODE) {
260260
struct lirc_scancode scan;
261261

262262
if (n != sizeof(scan)) {
263263
ret = -EINVAL;
264-
goto out;
264+
goto out_unlock;
265265
}
266266

267267
if (copy_from_user(&scan, buf, sizeof(scan))) {
268268
ret = -EFAULT;
269-
goto out;
269+
goto out_unlock;
270270
}
271271

272272
if (scan.flags || scan.keycode || scan.timestamp) {
273273
ret = -EINVAL;
274-
goto out;
274+
goto out_unlock;
275275
}
276276

277277
/*
@@ -283,26 +283,26 @@ static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
283283
if (scan.scancode > U32_MAX ||
284284
!rc_validate_scancode(scan.rc_proto, scan.scancode)) {
285285
ret = -EINVAL;
286-
goto out;
286+
goto out_unlock;
287287
}
288288

289289
raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
290290
if (!raw) {
291291
ret = -ENOMEM;
292-
goto out;
292+
goto out_unlock;
293293
}
294294

295295
ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
296296
raw, LIRCBUF_SIZE);
297297
if (ret < 0)
298-
goto out;
298+
goto out_kfree;
299299

300300
count = ret;
301301

302302
txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
303303
if (!txbuf) {
304304
ret = -ENOMEM;
305-
goto out;
305+
goto out_kfree;
306306
}
307307

308308
for (i = 0; i < count; i++)
@@ -318,34 +318,34 @@ static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
318318
} else {
319319
if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
320320
ret = -EINVAL;
321-
goto out;
321+
goto out_unlock;
322322
}
323323

324324
count = n / sizeof(unsigned int);
325325
if (count > LIRCBUF_SIZE || count % 2 == 0) {
326326
ret = -EINVAL;
327-
goto out;
327+
goto out_unlock;
328328
}
329329

330330
txbuf = memdup_user(buf, n);
331331
if (IS_ERR(txbuf)) {
332332
ret = PTR_ERR(txbuf);
333-
goto out;
333+
goto out_unlock;
334334
}
335335
}
336336

337337
for (i = 0; i < count; i++) {
338338
if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
339339
ret = -EINVAL;
340-
goto out;
340+
goto out_kfree;
341341
}
342342

343343
duration += txbuf[i];
344344
}
345345

346346
ret = dev->tx_ir(dev, txbuf, count);
347347
if (ret < 0)
348-
goto out;
348+
goto out_kfree;
349349

350350
if (fh->send_mode == LIRC_MODE_SCANCODE) {
351351
ret = n;
@@ -368,10 +368,11 @@ static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
368368
schedule_timeout(usecs_to_jiffies(towait));
369369
}
370370

371-
out:
372-
mutex_unlock(&dev->lock);
371+
out_kfree:
373372
kfree(txbuf);
374373
kfree(raw);
374+
out_unlock:
375+
mutex_unlock(&dev->lock);
375376
return ret;
376377
}
377378

0 commit comments

Comments
 (0)