Skip to content

Commit ef44a1e

Browse files
Li Zefantiwai
authored andcommitted
ALSA: sound/core: use memdup_user()
Remove open-coded memdup_user(). Signed-off-by: Li Zefan <lizf@cn.fujitsu.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 577c9c4 commit ef44a1e

5 files changed

Lines changed: 60 additions & 99 deletions

File tree

‎sound/core/control.c‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -723,14 +723,11 @@ static int snd_ctl_elem_read_user(struct snd_card *card,
723723
{
724724
struct snd_ctl_elem_value *control;
725725
int result;
726-
727-
control = kmalloc(sizeof(*control), GFP_KERNEL);
728-
if (control == NULL)
729-
return -ENOMEM;
730-
if (copy_from_user(control, _control, sizeof(*control))) {
731-
kfree(control);
732-
return -EFAULT;
733-
}
726+
727+
control = memdup_user(_control, sizeof(*control));
728+
if (IS_ERR(control))
729+
return PTR_ERR(control);
730+
734731
snd_power_lock(card);
735732
result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
736733
if (result >= 0)
@@ -784,13 +781,10 @@ static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
784781
struct snd_card *card;
785782
int result;
786783

787-
control = kmalloc(sizeof(*control), GFP_KERNEL);
788-
if (control == NULL)
789-
return -ENOMEM;
790-
if (copy_from_user(control, _control, sizeof(*control))) {
791-
kfree(control);
792-
return -EFAULT;
793-
}
784+
control = memdup_user(_control, sizeof(*control));
785+
if (IS_ERR(control))
786+
return PTR_ERR(control);
787+
794788
card = file->card;
795789
snd_power_lock(card);
796790
result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
@@ -916,13 +910,10 @@ static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
916910
if (op_flag > 0) {
917911
if (size > 1024 * 128) /* sane value */
918912
return -EINVAL;
919-
new_data = kmalloc(size, GFP_KERNEL);
920-
if (new_data == NULL)
921-
return -ENOMEM;
922-
if (copy_from_user(new_data, tlv, size)) {
923-
kfree(new_data);
924-
return -EFAULT;
925-
}
913+
914+
new_data = memdup_user(tlv, size);
915+
if (IS_ERR(new_data))
916+
return PTR_ERR(new_data);
926917
change = ue->tlv_data_size != size;
927918
if (!change)
928919
change = memcmp(ue->tlv_data, new_data, size);

‎sound/core/pcm_compat.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,11 @@ static int snd_pcm_ioctl_hw_params_compat(struct snd_pcm_substream *substream,
232232
if (! (runtime = substream->runtime))
233233
return -ENOTTY;
234234

235-
data = kmalloc(sizeof(*data), GFP_KERNEL);
236-
if (data == NULL)
237-
return -ENOMEM;
238235
/* only fifo_size is different, so just copy all */
239-
if (copy_from_user(data, data32, sizeof(*data32))) {
240-
err = -EFAULT;
241-
goto error;
242-
}
236+
data = memdup_user(data32, sizeof(*data32));
237+
if (IS_ERR(data))
238+
return PTR_ERR(data);
239+
243240
if (refine)
244241
err = snd_pcm_hw_refine(substream, data);
245242
else

‎sound/core/pcm_native.c‎

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,16 @@ static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
327327
struct snd_pcm_hw_params *params;
328328
int err;
329329

330-
params = kmalloc(sizeof(*params), GFP_KERNEL);
331-
if (!params) {
332-
err = -ENOMEM;
333-
goto out;
334-
}
335-
if (copy_from_user(params, _params, sizeof(*params))) {
336-
err = -EFAULT;
337-
goto out;
338-
}
330+
params = memdup_user(_params, sizeof(*params));
331+
if (IS_ERR(params))
332+
return PTR_ERR(params);
333+
339334
err = snd_pcm_hw_refine(substream, params);
340335
if (copy_to_user(_params, params, sizeof(*params))) {
341336
if (!err)
342337
err = -EFAULT;
343338
}
344-
out:
339+
345340
kfree(params);
346341
return err;
347342
}
@@ -465,21 +460,16 @@ static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
465460
struct snd_pcm_hw_params *params;
466461
int err;
467462

468-
params = kmalloc(sizeof(*params), GFP_KERNEL);
469-
if (!params) {
470-
err = -ENOMEM;
471-
goto out;
472-
}
473-
if (copy_from_user(params, _params, sizeof(*params))) {
474-
err = -EFAULT;
475-
goto out;
476-
}
463+
params = memdup_user(_params, sizeof(*params));
464+
if (IS_ERR(params))
465+
return PTR_ERR(params);
466+
477467
err = snd_pcm_hw_params(substream, params);
478468
if (copy_to_user(_params, params, sizeof(*params))) {
479469
if (!err)
480470
err = -EFAULT;
481471
}
482-
out:
472+
483473
kfree(params);
484474
return err;
485475
}
@@ -2593,13 +2583,11 @@ static int snd_pcm_playback_ioctl1(struct file *file,
25932583
return -EFAULT;
25942584
if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
25952585
return -EFAULT;
2596-
bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2597-
if (bufs == NULL)
2598-
return -ENOMEM;
2599-
if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2600-
kfree(bufs);
2601-
return -EFAULT;
2602-
}
2586+
2587+
bufs = memdup_user(xfern.bufs,
2588+
sizeof(void *) * runtime->channels);
2589+
if (IS_ERR(bufs))
2590+
return PTR_ERR(bufs);
26032591
result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
26042592
kfree(bufs);
26052593
__put_user(result, &_xfern->result);
@@ -2675,13 +2663,11 @@ static int snd_pcm_capture_ioctl1(struct file *file,
26752663
return -EFAULT;
26762664
if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
26772665
return -EFAULT;
2678-
bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2679-
if (bufs == NULL)
2680-
return -ENOMEM;
2681-
if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2682-
kfree(bufs);
2683-
return -EFAULT;
2684-
}
2666+
2667+
bufs = memdup_user(xfern.bufs,
2668+
sizeof(void *) * runtime->channels);
2669+
if (IS_ERR(bufs))
2670+
return PTR_ERR(bufs);
26852671
result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
26862672
kfree(bufs);
26872673
__put_user(result, &_xfern->result);
@@ -3312,18 +3298,12 @@ static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
33123298
int err;
33133299

33143300
params = kmalloc(sizeof(*params), GFP_KERNEL);
3315-
if (!params) {
3316-
err = -ENOMEM;
3317-
goto out;
3318-
}
3319-
oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3320-
if (!oparams) {
3321-
err = -ENOMEM;
3322-
goto out;
3323-
}
3301+
if (!params)
3302+
return -ENOMEM;
33243303

3325-
if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3326-
err = -EFAULT;
3304+
oparams = memdup_user(_oparams, sizeof(*oparams));
3305+
if (IS_ERR(oparams)) {
3306+
err = PTR_ERR(oparams);
33273307
goto out;
33283308
}
33293309
snd_pcm_hw_convert_from_old_params(params, oparams);
@@ -3333,9 +3313,10 @@ static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
33333313
if (!err)
33343314
err = -EFAULT;
33353315
}
3316+
3317+
kfree(oparams);
33363318
out:
33373319
kfree(params);
3338-
kfree(oparams);
33393320
return err;
33403321
}
33413322

@@ -3347,17 +3328,12 @@ static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
33473328
int err;
33483329

33493330
params = kmalloc(sizeof(*params), GFP_KERNEL);
3350-
if (!params) {
3351-
err = -ENOMEM;
3352-
goto out;
3353-
}
3354-
oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3355-
if (!oparams) {
3356-
err = -ENOMEM;
3357-
goto out;
3358-
}
3359-
if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3360-
err = -EFAULT;
3331+
if (!params)
3332+
return -ENOMEM;
3333+
3334+
oparams = memdup_user(_oparams, sizeof(*oparams));
3335+
if (IS_ERR(oparams)) {
3336+
err = PTR_ERR(oparams);
33613337
goto out;
33623338
}
33633339
snd_pcm_hw_convert_from_old_params(params, oparams);
@@ -3367,9 +3343,10 @@ static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
33673343
if (!err)
33683344
err = -EFAULT;
33693345
}
3346+
3347+
kfree(oparams);
33703348
out:
33713349
kfree(params);
3372-
kfree(oparams);
33733350
return err;
33743351
}
33753352
#endif /* CONFIG_SND_SUPPORT_OLD_API */

‎sound/core/seq/seq_compat.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ static int snd_seq_call_port_info_ioctl(struct snd_seq_client *client, unsigned
4848
struct snd_seq_port_info *data;
4949
mm_segment_t fs;
5050

51-
data = kmalloc(sizeof(*data), GFP_KERNEL);
52-
if (! data)
53-
return -ENOMEM;
51+
data = memdup_user(data32, sizeof(*data32));
52+
if (IS_ERR(data))
53+
return PTR_ERR(data);
5454

55-
if (copy_from_user(data, data32, sizeof(*data32)) ||
56-
get_user(data->flags, &data32->flags) ||
55+
if (get_user(data->flags, &data32->flags) ||
5756
get_user(data->time_queue, &data32->time_queue))
5857
goto error;
5958
data->kernel = NULL;

‎sound/core/timer.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,13 +1395,10 @@ static int snd_timer_user_ginfo(struct file *file,
13951395
struct list_head *p;
13961396
int err = 0;
13971397

1398-
ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1399-
if (! ginfo)
1400-
return -ENOMEM;
1401-
if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1402-
kfree(ginfo);
1403-
return -EFAULT;
1404-
}
1398+
ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1399+
if (IS_ERR(ginfo))
1400+
return PTR_ERR(ginfo);
1401+
14051402
tid = ginfo->tid;
14061403
memset(ginfo, 0, sizeof(*ginfo));
14071404
ginfo->tid = tid;

0 commit comments

Comments
 (0)