Alsa-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy()
@ 2020-07-29 22:18 Gustavo A. R. Silva
  2020-07-30  8:41 ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-29 22:18 UTC (permalink / raw)
  To: Jaroslav Kysela, Takashi Iwai
  Cc: alsa-devel, linux-kernel, Gustavo A. R. Silva

Make use of the flex_array_size() helper to calculate the size of a
flexible array member within an enclosing structure.

This helper offers defense-in-depth against potential integer overflows
and makes it explicitly clear that we are dealing with a flexible array
member.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 sound/core/vmaster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sound/core/vmaster.c b/sound/core/vmaster.c
index ab36f9898711..21ce4082cb5f 100644
--- a/sound/core/vmaster.c
+++ b/sound/core/vmaster.c
@@ -262,7 +262,8 @@ int _snd_ctl_add_follower(struct snd_kcontrol *master,
 		return -ENOMEM;
 	srec->kctl = follower;
 	srec->follower = *follower;
-	memcpy(srec->follower.vd, follower->vd, follower->count * sizeof(*follower->vd));
+	memcpy(srec->follower.vd, follower->vd, flex_array_size(srec, follower.vd,
+								srec->follower.count));
 	srec->master = master_link;
 	srec->flags = flags;
 
-- 
2.27.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy()
  2020-07-29 22:18 [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy() Gustavo A. R. Silva
@ 2020-07-30  8:41 ` Takashi Iwai
  2020-07-30 13:12   ` Gustavo A. R. Silva
  0 siblings, 1 reply; 4+ messages in thread
From: Takashi Iwai @ 2020-07-30  8:41 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: linux-kernel, alsa-devel, Takashi Iwai

On Thu, 30 Jul 2020 00:18:29 +0200,
Gustavo A. R. Silva wrote:
> 
> Make use of the flex_array_size() helper to calculate the size of a
> flexible array member within an enclosing structure.
> 
> This helper offers defense-in-depth against potential integer overflows
> and makes it explicitly clear that we are dealing with a flexible array
> member.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  sound/core/vmaster.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/core/vmaster.c b/sound/core/vmaster.c
> index ab36f9898711..21ce4082cb5f 100644
> --- a/sound/core/vmaster.c
> +++ b/sound/core/vmaster.c
> @@ -262,7 +262,8 @@ int _snd_ctl_add_follower(struct snd_kcontrol *master,
>  		return -ENOMEM;
>  	srec->kctl = follower;
>  	srec->follower = *follower;
> -	memcpy(srec->follower.vd, follower->vd, follower->count * sizeof(*follower->vd));
> +	memcpy(srec->follower.vd, follower->vd, flex_array_size(srec, follower.vd,
> +								srec->follower.count));

Changing from follower->count to srec->follower.count isn't obvious,
so it should have been mentioned in the log; other than that, the code
change looks good.

But since the API isn't in Linus tree yet, I'll postpone the merge
until the API reaches to upstream.  Maybe I can merge this during RC1
merge window, as those are trivial.


BTW, looking at those patterns, I wonder whether it'd make sense to
make the whole memset() call as a macro like

	safe_copy_array(srec->follower.vd, follower->vd, follower->count);

?


thanks,

Takashi

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy()
  2020-07-30  8:41 ` Takashi Iwai
@ 2020-07-30 13:12   ` Gustavo A. R. Silva
  2020-07-30 14:57     ` Takashi Iwai
  0 siblings, 1 reply; 4+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-30 13:12 UTC (permalink / raw)
  To: Takashi Iwai, Gustavo A. R. Silva; +Cc: linux-kernel, alsa-devel, Takashi Iwai



On 7/30/20 03:41, Takashi Iwai wrote:
> On Thu, 30 Jul 2020 00:18:29 +0200,
> Gustavo A. R. Silva wrote:
>>
>> Make use of the flex_array_size() helper to calculate the size of a
>> flexible array member within an enclosing structure.
>>
>> This helper offers defense-in-depth against potential integer overflows
>> and makes it explicitly clear that we are dealing with a flexible array
>> member.
>>
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>> ---
>>  sound/core/vmaster.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/sound/core/vmaster.c b/sound/core/vmaster.c
>> index ab36f9898711..21ce4082cb5f 100644
>> --- a/sound/core/vmaster.c
>> +++ b/sound/core/vmaster.c
>> @@ -262,7 +262,8 @@ int _snd_ctl_add_follower(struct snd_kcontrol *master,
>>  		return -ENOMEM;
>>  	srec->kctl = follower;
>>  	srec->follower = *follower;
>> -	memcpy(srec->follower.vd, follower->vd, follower->count * sizeof(*follower->vd));
>> +	memcpy(srec->follower.vd, follower->vd, flex_array_size(srec, follower.vd,
>> +								srec->follower.count));
> 
> Changing from follower->count to srec->follower.count isn't obvious,
> so it should have been mentioned in the log; other than that, the code
> change looks good.
> 

Yeah; you're right. I'll include that in the changelog text.

> But since the API isn't in Linus tree yet, I'll postpone the merge
> until the API reaches to upstream.  Maybe I can merge this during RC1
> merge window, as those are trivial.
> 

The API is already in mainline, see:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/overflow.h#n320

> 
> BTW, looking at those patterns, I wonder whether it'd make sense to
> make the whole memset() call as a macro like
> 
> 	safe_copy_array(srec->follower.vd, follower->vd, follower->count);
> 
> ?
> 

Yep; I was actually thinking about that, yesterday. I might implement it
for the next development cycle. :)

Thanks for the feedback.
--
Gustavo



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy()
  2020-07-30 13:12   ` Gustavo A. R. Silva
@ 2020-07-30 14:57     ` Takashi Iwai
  0 siblings, 0 replies; 4+ messages in thread
From: Takashi Iwai @ 2020-07-30 14:57 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: linux-kernel, alsa-devel, Takashi Iwai, Gustavo A. R. Silva

On Thu, 30 Jul 2020 15:12:33 +0200,
Gustavo A. R. Silva wrote:
> 
> 
> 
> On 7/30/20 03:41, Takashi Iwai wrote:
> > On Thu, 30 Jul 2020 00:18:29 +0200,
> > Gustavo A. R. Silva wrote:
> >>
> >> Make use of the flex_array_size() helper to calculate the size of a
> >> flexible array member within an enclosing structure.
> >>
> >> This helper offers defense-in-depth against potential integer overflows
> >> and makes it explicitly clear that we are dealing with a flexible array
> >> member.
> >>
> >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> >> ---
> >>  sound/core/vmaster.c | 3 ++-
> >>  1 file changed, 2 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/sound/core/vmaster.c b/sound/core/vmaster.c
> >> index ab36f9898711..21ce4082cb5f 100644
> >> --- a/sound/core/vmaster.c
> >> +++ b/sound/core/vmaster.c
> >> @@ -262,7 +262,8 @@ int _snd_ctl_add_follower(struct snd_kcontrol *master,
> >>  		return -ENOMEM;
> >>  	srec->kctl = follower;
> >>  	srec->follower = *follower;
> >> -	memcpy(srec->follower.vd, follower->vd, follower->count * sizeof(*follower->vd));
> >> +	memcpy(srec->follower.vd, follower->vd, flex_array_size(srec, follower.vd,
> >> +								srec->follower.count));
> > 
> > Changing from follower->count to srec->follower.count isn't obvious,
> > so it should have been mentioned in the log; other than that, the code
> > change looks good.
> > 
> 
> Yeah; you're right. I'll include that in the changelog text.

OK, then I'll wait for the v2 for merging.

> > But since the API isn't in Linus tree yet, I'll postpone the merge
> > until the API reaches to upstream.  Maybe I can merge this during RC1
> > merge window, as those are trivial.
> > 
> 
> The API is already in mainline, see:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/overflow.h#n320

Ah, thanks, I didn't expect that the API change comes after rc1 into
mainline.  Then I can merge later the commits based on the late rc.


Takashi

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-29 22:18 [PATCH][next] ALSA: vmaster: Use flex_array_size() helper in memcpy() Gustavo A. R. Silva
2020-07-30  8:41 ` Takashi Iwai
2020-07-30 13:12   ` Gustavo A. R. Silva
2020-07-30 14:57     ` Takashi Iwai

Alsa-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/alsa-devel/0 alsa-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 alsa-devel alsa-devel/ https://lore.kernel.org/alsa-devel \
		alsa-devel@alsa-project.org
	public-inbox-index alsa-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.alsa-project.alsa-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git