Skip to contentSkip to conversation

net: sched: cls_u32: Avoid memcpy() false-positive warning in u32_init_knode()

PATCH Superseded Jiayuan Chen 3 participants 5 messages Mar 5 – Mar 9

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: sched: cls_u32: Avoid memcpy() false-positive warning in u32_init_knode()

From: Jiayuan Chen <[email protected]>

Syzbot reported a warning in u32_init_knode() [1].

Similar to commit 7cba18332e36 ("net: sched: cls_u32: Avoid memcpy() false-positive warning") which addressed the same issue in u32_change(), use unsafe_memcpy() in u32_init_knode() to work around the compiler's inability to see into composite flexible array structs.

This silences the false-positive reported by syzbot:

memcpy: detected field-spanning write (size 32) of single field "&new->sel" at net/sched/cls_u32.c:855 (size 16)

Since the memory is correctly allocated with kzalloc_flex() using s->nkeys, this is purely a false positive and does not need a Fixes tag.

[1] https://syzkaller.appspot.com/bug?extid=d5ace703ed883df56e42

Made-with: Cursor

---
 net/sched/cls_u32.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/sched/cls_u32.c +4 -1
index 9241c025aa74..8f30cc82181d 100644
@@ -852,7 +852,10 @@ static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
 	/* Similarly success statistics must be moved as pointers */
 	new->pcpu_success = n->pcpu_success;
 #endif
-	memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
+	unsafe_memcpy(&new->sel, s, struct_size(s, keys, s->nkeys),
+		      /* A composite flex-array structure destination,
+		       * which was correctly sized with kzalloc_flex(),
+		       * above. */);
 
 	if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
 		kfree(new);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 33 lines · Jiayuan Chen

From: Jiayuan Chen <[email protected]>

Syzbot reported a warning in u32_init_knode() [1].

Similar to commit 7cba18332e36 ("net: sched: cls_u32: Avoid memcpy() false-positive warning") which addressed the same issue in u32_change(), use unsafe_memcpy() in u32_init_knode() to work around the compiler's inability to see into composite flexible array structs.

This silences the false-positive reported by syzbot:

memcpy: detected field-spanning write (size 32) of single field "&new->sel" at net/sched/cls_u32.c:855 (size 16)

Since the memory is correctly allocated with kzalloc_flex() using s->nkeys, this is purely a false positive and does not need a Fixes tag.

[1] https://syzkaller.appspot.com/bug?extid=d5ace703ed883df56e42

Made-with: Cursor

---
 net/sched/cls_u32.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/sched/cls_u32.c +0 -0
index 9241c025aa74..8f30cc82181d 100644

net/sched/cls_u32.c static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp, · line 858

Quoted 8 lines
net/sched/cls_u32.c +4 -1
@@ -852,7 +852,10 @@ static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
 /* Similarly success statistics must be moved as pointers */
 new->pcpu_success = n->pcpu_success;
 #endif
- memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
+ unsafe_memcpy(&new->sel, s, struct_size(s, keys, s->nkeys),
+ /* A composite flex-array structure destination,
+ * which was correctly sized with kzalloc_flex(),
+ * above. */);

I'm thinking whether it's more appropriate:

new->sel.hdr = s->hdr;
memcpy(new->sel.keys, s->keys, flex_array_size(s, keys, s->nkeys));

Quoted 5 lines
net/sched/cls_u32.c +0 -0
 if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
 kfree(new);
Signature · 1 line
2.43.0
#2 lore raw
Quoted 49 lines · Jiayuan Chen
Quoted 33 lines · Jiayuan Chen

From: Jiayuan Chen <[email protected]>

Syzbot reported a warning in u32_init_knode() [1].

Similar to commit 7cba18332e36 ("net: sched: cls_u32: Avoid memcpy() false-positive warning") which addressed the same issue in u32_change(), use unsafe_memcpy() in u32_init_knode() to work around the compiler's inability to see into composite flexible array structs.

This silences the false-positive reported by syzbot:

memcpy: detected field-spanning write (size 32) of single field "&new->sel" at net/sched/cls_u32.c:855 (size 16)

Since the memory is correctly allocated with kzalloc_flex() using s->nkeys, this is purely a false positive and does not need a Fixes tag.

[1] https://syzkaller.appspot.com/bug?extid=d5ace703ed883df56e42

Made-with: Cursor

---
  net/sched/cls_u32.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
net/sched/cls_u32.c +0 -0
index 9241c025aa74..8f30cc82181d 100644

net/sched/cls_u32.c static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp, · line 858

Quoted 8 lines
net/sched/cls_u32.c +4 -1
@@ -852,7 +852,10 @@ static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
  /* Similarly success statistics must be moved as pointers */
  new->pcpu_success = n->pcpu_success;
  #endif
- memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
+ unsafe_memcpy(&new->sel, s, struct_size(s, keys, s->nkeys),
+ /* A composite flex-array structure destination,
+ * which was correctly sized with kzalloc_flex(),
+ * above. */);

I'm thinking whether it's more appropriate:

Quoted 2 lines

new->sel.hdr = s->hdr;
memcpy(new->sel.keys, s->keys, flex_array_size(s, keys, s->nkeys));

I think unsafe_memcpy() is "better" in this case, as it will mark code that could be updated/reviewed once the compiler can correctly see into composite flexible structures in the future.

-Gustavo

Quoted 7 lines
Quoted 6 lines
net/sched/cls_u32.c +0 -0
  if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
  kfree(new);
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#3 lore raw
In reply to Jiayuan Chen
Quoted 14 lines · Jiayuan Chen

From: Jiayuan Chen <[email protected]>

Syzbot reported a warning in u32_init_knode() [1].

Similar to commit 7cba18332e36 ("net: sched: cls_u32: Avoid memcpy() false-positive warning") which addressed the same issue in u32_change(), use unsafe_memcpy() in u32_init_knode() to work around the compiler's inability to see into composite flexible array structs.

This silences the false-positive reported by syzbot:

memcpy: detected field-spanning write (size 32) of single field "&new->sel" at net/sched/cls_u32.c:855 (size 16)

Quoted 8 lines

Since the memory is correctly allocated with kzalloc_flex() using s->nkeys, this is purely a false positive and does not need a Fixes tag.

[1] https://syzkaller.appspot.com/bug?extid=d5ace703ed883df56e42

Thanks
-Gustavo

Quoted 21 lines

Made-with: Cursor

---
  net/sched/cls_u32.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
net/sched/cls_u32.c +4 -1
index 9241c025aa74..8f30cc82181d 100644
@@ -852,7 +852,10 @@ static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
  	/* Similarly success statistics must be moved as pointers */
  	new->pcpu_success = n->pcpu_success;
  #endif
-	memcpy(&new->sel, s, struct_size(s, keys, s->nkeys));
+	unsafe_memcpy(&new->sel, s, struct_size(s, keys, s->nkeys),
+		      /* A composite flex-array structure destination,
+		       * which was correctly sized with kzalloc_flex(),
+		       * above. */);
 
  	if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
  		kfree(new);
reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Jiayuan Chen
Quoted 17 lines · Jiayuan Chen

From: Jiayuan Chen <[email protected]>

Syzbot reported a warning in u32_init_knode() [1].

Similar to commit 7cba18332e36 ("net: sched: cls_u32: Avoid memcpy() false-positive warning") which addressed the same issue in u32_change(), use unsafe_memcpy() in u32_init_knode() to work around the compiler's inability to see into composite flexible array structs.

This silences the false-positive reported by syzbot:

memcpy: detected field-spanning write (size 32) of single field "&new->sel" at net/sched/cls_u32.c:855 (size 16)

Since the memory is correctly allocated with kzalloc_flex() using s->nkeys, this is purely a false positive and does not need a Fixes tag.

Quoted 6 lines

[1] https://syzkaller.appspot.com/bug?extid=d5ace703ed883df56e42

Made-with: Cursor

FWIIW, Made-with is a new tag to me, I would have expected Assisted-by.

That notwithstanding, this looks good to me.