[PATCH net-next 2/2] net/packet: Use copy_safe_from_sockptr to dedupe code

Joe Damato <[email protected]>
Newsgroups org.kernel.vger.netdev,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
Reduce code duplication by using copy_safe_from_sockptr instead of
repeated length checks followed by a copy.

Signed-off-by: Joe Damato <[email protected]>
---
 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.