net: Use sockaddr_storage for getsockopt(SO_PEERNAME).
Subsystems (3)
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
@@ -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
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 fromsockaddr_storageand has a bigger size than fortified functions expect. (See Link)We must cast the protocol-specific address back to
sockaddr_storageto 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 assockaddr_storage.Let's use
sockaddr_storageimplicitly.
Thanks.
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 fromsockaddr_storageand has a bigger size than fortified functions expect. (See Link)
Quoted 8 lines
We must cast the protocol-specific address back to
sockaddr_storageto 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 assockaddr_storage.Let's use
sockaddr_storageimplicitly.
explicitly
Quoted 2 lines
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 fromsockaddr_storageand has a bigger size than fortified functions expect. (See Link)We must cast the protocol-specific address back to
sockaddr_storageto 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 assockaddr_storage.Let's use
sockaddr_storageimplicitly.explicitly
Will fix in v2, thanks!
Quoted 5 lines
Quoted 3 lines
Reviewed-by Willem de Bruijn <[email protected]>