Skip to contentSkip to conversation

net: Use sockaddr_storage for getsockopt(SO_PEERNAME).

PATCH Superseded Kuniyuki Iwashima 3 participants 4 messages Jul 20, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: Use sockaddr_storage for getsockopt(SO_PEERNAME).

Commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") started applying strict rules to standard string functions.

It does not work well with conventional socket code around each protocol- specific struct sockaddr_XXX, which is cast from sockaddr_storage and has a bigger size than fortified functions expect. (See Link)

We must cast the protocol-specific address back to sockaddr_storage to call such functions.

However, in the case of getsockaddr(SO_PEERNAME), the rationale is a bit unclear as the buffer is defined by char[128] which is the same size as sockaddr_storage.

Let's use sockaddr_storage implicitly.

---
 net/core/sock.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/sock.c +3 -3
index ab1e8d1bd5a1..4ad267ba0099 100644
@@ -1806,14 +1806,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
 
 	case SO_PEERNAME:
 	{
-		char address[128];
+		struct sockaddr_storage address;
 
-		lv = sock->ops->getname(sock, (struct sockaddr *)address, 2);
+		lv = sock->ops->getname(sock, (struct sockaddr *)&address, 2);
 		if (lv < 0)
 			return -ENOTCONN;
 		if (lv < len)
 			return -EINVAL;
-		if (copy_to_sockptr(optval, address, len))
+		if (copy_to_sockptr(optval, &address, len))
 			return -EFAULT;
 		goto lenout;
 	}
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Kuniyuki Iwashima

Commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") started applying strict rules to standard string functions.

It does not work well with conventional socket code around each protocol- specific struct sockaddr_XXX, which is cast from sockaddr_storage and has a bigger size than fortified functions expect. (See Link)

We must cast the protocol-specific address back to sockaddr_storage to call such functions.

Quoted 8 lines

However, in the case of getsockaddr(SO_PEERNAME), the rationale is a bit unclear as the buffer is defined by char[128] which is the same size as sockaddr_storage.

Let's use sockaddr_storage implicitly.

Thanks.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Kuniyuki Iwashima
Quoted 7 lines · Kuniyuki Iwashima

Commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") started applying strict rules to standard string functions.

It does not work well with conventional socket code around each protocol- specific struct sockaddr_XXX, which is cast from sockaddr_storage and has a bigger size than fortified functions expect. (See Link)

Quoted 8 lines

We must cast the protocol-specific address back to sockaddr_storage to call such functions.

However, in the case of getsockaddr(SO_PEERNAME), the rationale is a bit unclear as the buffer is defined by char[128] which is the same size as sockaddr_storage.

Let's use sockaddr_storage implicitly.

explicitly

Quoted 2 lines
#3 lore raw

From: Willem de Bruijn <[email protected]>
Date: Thu, 20 Jul 2023 09:50:46 -0400

Quoted 11 lines
Quoted 10 lines · Kuniyuki Iwashima

Commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") started applying strict rules to standard string functions.

It does not work well with conventional socket code around each protocol- specific struct sockaddr_XXX, which is cast from sockaddr_storage and has a bigger size than fortified functions expect. (See Link)

We must cast the protocol-specific address back to sockaddr_storage to call such functions.

Quoted 7 lines
Quoted 5 lines

However, in the case of getsockaddr(SO_PEERNAME), the rationale is a bit unclear as the buffer is defined by char[128] which is the same size as sockaddr_storage.

Let's use sockaddr_storage implicitly.

explicitly

Will fix in v2, thanks!

Quoted 5 lines
Quoted 3 lines