netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] netdev: avoid CFI problems with sock priv helpers
@ 2025-01-15 16:14 Jakub Kicinski
  2025-01-15 17:01 ` Mina Almasry
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jakub Kicinski @ 2025-01-15 16:14 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, andrew+netdev, horms, Jakub Kicinski,
	Li Li, donald.hunter, sdf, almasrymina

Li Li reports that casting away callback type may cause issues
for CFI. Let's generate a small wrapper for each callback,
to make sure compiler sees the anticipated types.

Reported-by: Li Li <dualli@chromium.org>
Link: https://lore.kernel.org/CANBPYPjQVqmzZ4J=rVQX87a9iuwmaetULwbK_5_3YWk2eGzkaA@mail.gmail.com
Fixes: 170aafe35cb9 ("netdev: support binding dma-buf to netdevice")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: donald.hunter@gmail.com
CC: sdf@fomichev.me
CC: almasrymina@google.com
---
 net/core/netdev-genl-gen.c | 14 ++++++++++++--
 tools/net/ynl/ynl-gen-c.py | 16 +++++++++++++---
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/net/core/netdev-genl-gen.c b/net/core/netdev-genl-gen.c
index a89cbd8d87c3..996ac6a449eb 100644
--- a/net/core/netdev-genl-gen.c
+++ b/net/core/netdev-genl-gen.c
@@ -197,6 +197,16 @@ static const struct genl_multicast_group netdev_nl_mcgrps[] = {
 	[NETDEV_NLGRP_PAGE_POOL] = { "page-pool", },
 };
 
+static void __netdev_nl_sock_priv_init(void *priv)
+{
+	netdev_nl_sock_priv_init(priv);
+}
+
+static void __netdev_nl_sock_priv_destroy(void *priv)
+{
+	netdev_nl_sock_priv_destroy(priv);
+}
+
 struct genl_family netdev_nl_family __ro_after_init = {
 	.name		= NETDEV_FAMILY_NAME,
 	.version	= NETDEV_FAMILY_VERSION,
@@ -208,6 +218,6 @@ struct genl_family netdev_nl_family __ro_after_init = {
 	.mcgrps		= netdev_nl_mcgrps,
 	.n_mcgrps	= ARRAY_SIZE(netdev_nl_mcgrps),
 	.sock_priv_size	= sizeof(struct list_head),
-	.sock_priv_init	= (void *)netdev_nl_sock_priv_init,
-	.sock_priv_destroy = (void *)netdev_nl_sock_priv_destroy,
+	.sock_priv_init	= __netdev_nl_sock_priv_init,
+	.sock_priv_destroy = __netdev_nl_sock_priv_destroy,
 };
diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index d8201c4b1520..6750fdb42564 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -2384,6 +2384,17 @@ _C_KW = {
     if not kernel_can_gen_family_struct(family):
         return
 
+    if 'sock-priv' in family.kernel_family:
+        # Generate "trampolines" to make CFI happy
+        cw.write_func("static void", f"__{family.c_name}_nl_sock_priv_init",
+                      [f"{family.c_name}_nl_sock_priv_init(priv);"],
+                      ["void *priv"])
+        cw.nl()
+        cw.write_func("static void", f"__{family.c_name}_nl_sock_priv_destroy",
+                      [f"{family.c_name}_nl_sock_priv_destroy(priv);"],
+                      ["void *priv"])
+        cw.nl()
+
     cw.block_start(f"struct genl_family {family.ident_name}_nl_family __ro_after_init =")
     cw.p('.name\t\t= ' + family.fam_key + ',')
     cw.p('.version\t= ' + family.ver_key + ',')
@@ -2401,9 +2412,8 @@ _C_KW = {
         cw.p(f'.n_mcgrps\t= ARRAY_SIZE({family.c_name}_nl_mcgrps),')
     if 'sock-priv' in family.kernel_family:
         cw.p(f'.sock_priv_size\t= sizeof({family.kernel_family["sock-priv"]}),')
-        # Force cast here, actual helpers take pointer to the real type.
-        cw.p(f'.sock_priv_init\t= (void *){family.c_name}_nl_sock_priv_init,')
-        cw.p(f'.sock_priv_destroy = (void *){family.c_name}_nl_sock_priv_destroy,')
+        cw.p(f'.sock_priv_init\t= __{family.c_name}_nl_sock_priv_init,')
+        cw.p(f'.sock_priv_destroy = __{family.c_name}_nl_sock_priv_destroy,')
     cw.block_end(';')
 
 
-- 
2.48.0


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

* Re: [PATCH net] netdev: avoid CFI problems with sock priv helpers
  2025-01-15 16:14 [PATCH net] netdev: avoid CFI problems with sock priv helpers Jakub Kicinski
@ 2025-01-15 17:01 ` Mina Almasry
  2025-01-16 12:20 ` Paolo Abeni
  2025-01-16 12:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Mina Almasry @ 2025-01-15 17:01 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, andrew+netdev, horms, Li Li,
	donald.hunter, sdf

On Wed, Jan 15, 2025 at 8:14 AM Jakub Kicinski <kuba@kernel.org> wrote:
>
> Li Li reports that casting away callback type may cause issues
> for CFI. Let's generate a small wrapper for each callback,
> to make sure compiler sees the anticipated types.
>
> Reported-by: Li Li <dualli@chromium.org>
> Link: https://lore.kernel.org/CANBPYPjQVqmzZ4J=rVQX87a9iuwmaetULwbK_5_3YWk2eGzkaA@mail.gmail.com
> Fixes: 170aafe35cb9 ("netdev: support binding dma-buf to netdevice")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Thanks Jakub,

Reviewed-by: Mina Almasry <almasrymina@google.com>

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

* Re: [PATCH net] netdev: avoid CFI problems with sock priv helpers
  2025-01-15 16:14 [PATCH net] netdev: avoid CFI problems with sock priv helpers Jakub Kicinski
  2025-01-15 17:01 ` Mina Almasry
@ 2025-01-16 12:20 ` Paolo Abeni
  2025-01-16 12:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Abeni @ 2025-01-16 12:20 UTC (permalink / raw)
  To: Jakub Kicinski, davem
  Cc: netdev, edumazet, andrew+netdev, horms, Li Li, donald.hunter, sdf,
	almasrymina



On 1/15/25 5:14 PM, Jakub Kicinski wrote:
> Li Li reports that casting away callback type may cause issues
> for CFI. Let's generate a small wrapper for each callback,
> to make sure compiler sees the anticipated types.
> 
> Reported-by: Li Li <dualli@chromium.org>
> Link: https://lore.kernel.org/CANBPYPjQVqmzZ4J=rVQX87a9iuwmaetULwbK_5_3YWk2eGzkaA@mail.gmail.com
> Fixes: 170aafe35cb9 ("netdev: support binding dma-buf to netdevice")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> CC: donald.hunter@gmail.com
> CC: sdf@fomichev.me
> CC: almasrymina@google.com

FTR, I took the liberty of applying this patch before the 24H grace
period to fit todays net PR and avoid shipping a 6.13 final with a known
serious issue.

/P


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

* Re: [PATCH net] netdev: avoid CFI problems with sock priv helpers
  2025-01-15 16:14 [PATCH net] netdev: avoid CFI problems with sock priv helpers Jakub Kicinski
  2025-01-15 17:01 ` Mina Almasry
  2025-01-16 12:20 ` Paolo Abeni
@ 2025-01-16 12:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-01-16 12:20 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, andrew+netdev, horms, dualli,
	donald.hunter, sdf, almasrymina

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:

On Wed, 15 Jan 2025 08:14:36 -0800 you wrote:
> Li Li reports that casting away callback type may cause issues
> for CFI. Let's generate a small wrapper for each callback,
> to make sure compiler sees the anticipated types.
> 
> Reported-by: Li Li <dualli@chromium.org>
> Link: https://lore.kernel.org/CANBPYPjQVqmzZ4J=rVQX87a9iuwmaetULwbK_5_3YWk2eGzkaA@mail.gmail.com
> Fixes: 170aafe35cb9 ("netdev: support binding dma-buf to netdevice")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> 
> [...]

Here is the summary with links:
  - [net] netdev: avoid CFI problems with sock priv helpers
    https://git.kernel.org/netdev/net/c/a50da36562cd

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2025-01-16 12:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-15 16:14 [PATCH net] netdev: avoid CFI problems with sock priv helpers Jakub Kicinski
2025-01-15 17:01 ` Mina Almasry
2025-01-16 12:20 ` Paolo Abeni
2025-01-16 12:20 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).