Skip to contentSkip to conversation

ALSA: vmaster: Use flex_array_size() helper in memcpy()

PATCH In review Gustavo A. R. Silva 3 participants 4 messages Jul 29, 2020 – Jul 30, 2020 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: vmaster: Use flex_array_size() helper in memcpy()

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.

---
 sound/core/vmaster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/core/vmaster.c +2 -1
index ab36f9898711..21ce4082cb5f 100644
@@ -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;
 
Signature · 1 line
2.27.0
#1 lore raw
Quoted 18 lines · 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.

---
 sound/core/vmaster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/core/vmaster.c +0 -0
index ab36f9898711..21ce4082cb5f 100644

sound/core/vmaster.c int _snd_ctl_add_follower(struct snd_kcontrol *master, · line 266

Quoted 6 lines
sound/core/vmaster.c +2 -1
@@ -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

#2 lore raw
Quoted 15 lines · Takashi Iwai
Quoted 13 lines · 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.

---
 sound/core/vmaster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines

sound/core/vmaster.c int _snd_ctl_add_follower(struct snd_kcontrol *master, · line 266

Quoted 11 lines
sound/core/vmaster.c +2 -1
index ab36f9898711..21ce4082cb5f 100644
@@ -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.

Quoted 4 lines

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

Quoted 8 lines

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

#3 lore raw
Quoted 36 lines · Gustavo A. R. Silva
Quoted 15 lines · Takashi Iwai
Quoted 13 lines · 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.

---
 sound/core/vmaster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines

sound/core/vmaster.c int _snd_ctl_add_follower(struct snd_kcontrol *master, · line 266

Quoted 11 lines
sound/core/vmaster.c +2 -1
index ab36f9898711..21ce4082cb5f 100644
@@ -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.

Quoted 1 line

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

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

Quoted 7 lines
Quoted 4 lines

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