From: Joe Damato <joe@dama.to>
To: netdev@vger.kernel.org,
Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>
Cc: andrew+netdev@lunn.ch, willemb@google.com,
Joe Damato <joe@dama.to>,
linux-kernel@vger.kernel.org
Subject: [PATCH net-next 2/2] net/packet: Use copy_safe_from_sockptr to dedupe code
Date: Tue, 11 Aug 2026 11:47:20 -0700 [thread overview]
Message-ID: <20260811184722.2612345-3-joe@dama.to> (raw)
In-Reply-To: <20260811184722.2612345-1-joe@dama.to>
Reduce code duplication by using copy_safe_from_sockptr instead of
repeated length checks followed by a copy.
Signed-off-by: Joe Damato <joe@dama.to>
---
net/packet/af_packet.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index ee60dcc639ad..9d326c2b04b3 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -3961,10 +3961,9 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
{
int val;
- if (optlen < sizeof(val))
- return -EINVAL;
- if (copy_from_sockptr(&val, optval, sizeof(val)))
- return -EFAULT;
+ ret = copy_safe_from_sockptr(&val, sizeof(val), optval, optlen);
+ if (ret)
+ return ret;
packet_sock_flag_set(po, PACKET_SOCK_AUXDATA, val);
return 0;
@@ -3973,10 +3972,9 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
{
int val;
- if (optlen < sizeof(val))
- return -EINVAL;
- if (copy_from_sockptr(&val, optval, sizeof(val)))
- return -EFAULT;
+ ret = copy_safe_from_sockptr(&val, sizeof(val), optval, optlen);
+ if (ret)
+ return ret;
packet_sock_flag_set(po, PACKET_SOCK_ORIGDEV, val);
return 0;
@@ -3988,10 +3986,9 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
if (sock->type != SOCK_RAW)
return -EINVAL;
- if (optlen < sizeof(val))
- return -EINVAL;
- if (copy_from_sockptr(&val, optval, sizeof(val)))
- return -EFAULT;
+ ret = copy_safe_from_sockptr(&val, sizeof(val), optval, optlen);
+ if (ret)
+ return ret;
if (optname == PACKET_VNET_HDR_SZ) {
if (val && val != sizeof(struct virtio_net_hdr) &&
--
2.53.0-Meta
next prev parent reply other threads:[~2026-08-11 18:47 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 18:47 [PATCH net-next 0/2] packet socket cleanup Joe Damato
2026-08-11 18:47 ` [PATCH net-next 1/2] net/packet: Reduce VLAN tag code duplication Joe Damato
2026-08-12 12:28 ` Willem de Bruijn
2026-08-11 18:47 ` Joe Damato [this message]
2026-08-12 12:19 ` [PATCH net-next 2/2] net/packet: Use copy_safe_from_sockptr to dedupe code Willem de Bruijn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260811184722.2612345-3-joe@dama.to \
--to=joe@dama.to \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemb@google.com \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox