2021-10-13 09:20:44

by Jiasheng Jiang

[permalink] [raw]
Subject: [PATCH] ASoC: soc-compress: prevent the potentially use of null pointer

The codec_dai and the cpu_dai are from rtd. When the rtd->dais[0]
or rtd->dais[rtd->num_cpus] is None, it could lead to the use of
null pointer in the snd_soc_dai_stream_valid(). So it might be better
to add the check before the use of snd_soc_dai_stream_valid().

Fixes: 467fece ("ASoC: soc-dai: move snd_soc_dai_stream_valid() to soc-dai.c")
Signed-off-by: Jiasheng Jiang <[email protected]>
---
sound/soc/soc-compress.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/sound/soc/soc-compress.c b/sound/soc/soc-compress.c
index b4f5935..67c3df1 100644
--- a/sound/soc/soc-compress.c
+++ b/sound/soc/soc-compress.c
@@ -535,12 +535,14 @@ int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
}

/* check client and interface hw capabilities */
- if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
- snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
- playback = 1;
- if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
- snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
- capture = 1;
+ if (codec_dai && cpu_dai) {
+ if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
+ snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
+ playback = 1;
+ if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
+ snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
+ capture = 1;
+ }

/*
* Compress devices are unidirectional so only one of the directions
--
2.7.4


2021-10-14 07:34:29

by Dan Carpenter

[permalink] [raw]
Subject: [kbuild] Re: [PATCH] ASoC: soc-compress: prevent the potentially use of null pointer

Hi Jiasheng,

url: https://github.com/0day-ci/linux/commits/Jiasheng-Jiang/ASoC-soc-compress-prevent-the-potentially-use-of-null-pointer/20211013-172002
base: https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: i386-randconfig-m021-20211013 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>
Reported-by: Dan Carpenter <[email protected]>

smatch warnings:
sound/soc/soc-compress.c:595 snd_soc_new_compress() error: we previously assumed 'codec_dai' could be null (see line 538)

vim +/codec_dai +595 sound/soc/soc-compress.c

6f0c42269f000b Jie Yang 2015-10-13 511 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
1245b7005de02d Namarta Kohli 2012-08-16 512 {
9e7e3738ab0e90 Kuninori Morimoto 2017-10-11 513 struct snd_soc_component *component;
c2233a266178f8 Kuninori Morimoto 2020-03-30 514 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
c2233a266178f8 Kuninori Morimoto 2020-03-30 515 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
1245b7005de02d Namarta Kohli 2012-08-16 516 struct snd_compr *compr;
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 517 struct snd_pcm *be_pcm;
1245b7005de02d Namarta Kohli 2012-08-16 518 char new_name[64];
1245b7005de02d Namarta Kohli 2012-08-16 519 int ret = 0, direction = 0;
a1068045883ed4 Vinod Koul 2016-01-07 520 int playback = 0, capture = 0;
613fb50059cf19 Kuninori Morimoto 2020-01-10 521 int i;
1245b7005de02d Namarta Kohli 2012-08-16 522
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 523 /*
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 524 * make sure these are same value,
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 525 * and then use these as equally
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 526 */
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 527 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 528 BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
7428d8c8bd7936 Kuninori Morimoto 2020-10-30 529
6e1276a5e613d2 Bard Liao 2020-02-25 530 if (rtd->num_cpus > 1 ||
6e1276a5e613d2 Bard Liao 2020-02-25 531 rtd->num_codecs > 1) {
141dfc9e3751f5 Charles Keepax 2018-01-26 532 dev_err(rtd->card->dev,
6e1276a5e613d2 Bard Liao 2020-02-25 533 "Compress ASoC: Multi CPU/Codec not supported\n");
8151d5e60232d3 Benoit Cousson 2014-07-08 534 return -EINVAL;
8151d5e60232d3 Benoit Cousson 2014-07-08 535 }
8151d5e60232d3 Benoit Cousson 2014-07-08 536
1245b7005de02d Namarta Kohli 2012-08-16 537 /* check client and interface hw capabilities */
87e5451ef9ec85 Jiasheng Jiang 2021-10-13 @538 if (codec_dai && cpu_dai) {
^^^^^^^^^
Can this really be NULL?

467fece8fbc677 Kuninori Morimoto 2019-07-22 539 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
467fece8fbc677 Kuninori Morimoto 2019-07-22 540 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
a1068045883ed4 Vinod Koul 2016-01-07 541 playback = 1;
467fece8fbc677 Kuninori Morimoto 2019-07-22 542 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
467fece8fbc677 Kuninori Morimoto 2019-07-22 543 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
a1068045883ed4 Vinod Koul 2016-01-07 544 capture = 1;
87e5451ef9ec85 Jiasheng Jiang 2021-10-13 545 }
a1068045883ed4 Vinod Koul 2016-01-07 546
a1068045883ed4 Vinod Koul 2016-01-07 547 /*
a1068045883ed4 Vinod Koul 2016-01-07 548 * Compress devices are unidirectional so only one of the directions
a1068045883ed4 Vinod Koul 2016-01-07 549 * should be set, check for that (xor)
a1068045883ed4 Vinod Koul 2016-01-07 550 */
a1068045883ed4 Vinod Koul 2016-01-07 551 if (playback + capture != 1) {
141dfc9e3751f5 Charles Keepax 2018-01-26 552 dev_err(rtd->card->dev,
141dfc9e3751f5 Charles Keepax 2018-01-26 553 "Compress ASoC: Invalid direction for P %d, C %d\n",
a1068045883ed4 Vinod Koul 2016-01-07 554 playback, capture);
a1068045883ed4 Vinod Koul 2016-01-07 555 return -EINVAL;
a1068045883ed4 Vinod Koul 2016-01-07 556 }
a1068045883ed4 Vinod Koul 2016-01-07 557
a1068045883ed4 Vinod Koul 2016-01-07 558 if (playback)
1245b7005de02d Namarta Kohli 2012-08-16 559 direction = SND_COMPRESS_PLAYBACK;
daa2db59ce7e36 Charles Keepax 2013-04-18 560 else
a1068045883ed4 Vinod Koul 2016-01-07 561 direction = SND_COMPRESS_CAPTURE;
daa2db59ce7e36 Charles Keepax 2013-04-18 562
09f448a415ece4 Amadeusz Sławiński 2019-06-17 563 compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
7a0cf42edd9cc3 Markus Elfring 2017-08-10 564 if (!compr)
1245b7005de02d Namarta Kohli 2012-08-16 565 return -ENOMEM;
1245b7005de02d Namarta Kohli 2012-08-16 566
1f88eb0f0660f8 Charles Keepax 2013-02-05 567 compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
1f88eb0f0660f8 Charles Keepax 2013-02-05 568 GFP_KERNEL);
09f448a415ece4 Amadeusz Sławiński 2019-06-17 569 if (!compr->ops)
09f448a415ece4 Amadeusz Sławiński 2019-06-17 570 return -ENOMEM;
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 571
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 572 if (rtd->dai_link->dynamic) {
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 573 snprintf(new_name, sizeof(new_name), "(%s)",
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 574 rtd->dai_link->stream_name);
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 575
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 576 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
d3268a40d4b19f Qais Yousef 2015-01-14 577 rtd->dai_link->dpcm_playback,
d3268a40d4b19f Qais Yousef 2015-01-14 578 rtd->dai_link->dpcm_capture, &be_pcm);
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 579 if (ret < 0) {
141dfc9e3751f5 Charles Keepax 2018-01-26 580 dev_err(rtd->card->dev,
141dfc9e3751f5 Charles Keepax 2018-01-26 581 "Compress ASoC: can't create compressed for %s: %d\n",
141dfc9e3751f5 Charles Keepax 2018-01-26 582 rtd->dai_link->name, ret);
09f448a415ece4 Amadeusz Sławiński 2019-06-17 583 return ret;
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 584 }
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 585
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 586 rtd->pcm = be_pcm;
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 587 rtd->fe_compr = 1;
d3268a40d4b19f Qais Yousef 2015-01-14 588 if (rtd->dai_link->dpcm_playback)
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 589 be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
d3268a40d4b19f Qais Yousef 2015-01-14 590 else if (rtd->dai_link->dpcm_capture)
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 591 be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2a99ef0fdb35a0 Liam Girdwood 2014-01-17 592 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
aeb6fa0f15c71a Peng Donglin 2017-08-16 593 } else {
aeb6fa0f15c71a Peng Donglin 2017-08-16 594 snprintf(new_name, sizeof(new_name), "%s %s-%d",
aeb6fa0f15c71a Peng Donglin 2017-08-16 @595 rtd->dai_link->stream_name, codec_dai->name, num);
^^^^^^^^^^^^^^^
Other code assumes that it is non-NULL and doesn't check.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

_______________________________________________
kbuild mailing list -- [email protected]
To unsubscribe send an email to [email protected]