Commit db243b79 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by David S. Miller

net/ipv4/ipv6: Replace one-element arraya with flexible-array members

There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use “flexible array members”[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

Use an anonymous union with a couple of anonymous structs in order to
keep userspace unchanged and refactor the related code accordingly:

$ pahole -C group_filter net/ipv4/ip_sockglue.o
struct group_filter {
	union {
		struct {
			__u32      gf_interface_aux;     /*     0     4 */

			/* XXX 4 bytes hole, try to pack */

			struct __kernel_sockaddr_storage gf_group_aux; /*     8   128 */
			/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
			__u32      gf_fmode_aux;         /*   136     4 */
			__u32      gf_numsrc_aux;        /*   140     4 */
			struct __kernel_sockaddr_storage gf_slist[1]; /*   144   128 */
		};                                       /*     0   272 */
		struct {
			__u32      gf_interface;         /*     0     4 */

			/* XXX 4 bytes hole, try to pack */

			struct __kernel_sockaddr_storage gf_group; /*     8   128 */
			/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
			__u32      gf_fmode;             /*   136     4 */
			__u32      gf_numsrc;            /*   140     4 */
			struct __kernel_sockaddr_storage gf_slist_flex[0]; /*   144     0 */
		};                                       /*     0   144 */
	};                                               /*     0   272 */

	/* size: 272, cachelines: 5, members: 1 */
	/* last cacheline: 16 bytes */
};

$ pahole -C compat_group_filter net/ipv4/ip_sockglue.o
struct compat_group_filter {
	union {
		struct {
			__u32      gf_interface_aux;     /*     0     4 */
			struct __kernel_sockaddr_storage gf_group_aux __attribute__((__aligned__(4))); /*     4   128 */
			/* --- cacheline 2 boundary (128 bytes) was 4 bytes ago --- */
			__u32      gf_fmode_aux;         /*   132     4 */
			__u32      gf_numsrc_aux;        /*   136     4 */
			struct __kernel_sockaddr_storage gf_slist[1] __attribute__((__aligned__(4))); /*   140   128 */
		} __attribute__((__packed__)) __attribute__((__aligned__(4)));                     /*     0   268 */
		struct {
			__u32      gf_interface;         /*     0     4 */
			struct __kernel_sockaddr_storage gf_group __attribute__((__aligned__(4))); /*     4   128 */
			/* --- cacheline 2 boundary (128 bytes) was 4 bytes ago --- */
			__u32      gf_fmode;             /*   132     4 */
			__u32      gf_numsrc;            /*   136     4 */
			struct __kernel_sockaddr_storage gf_slist_flex[0] __attribute__((__aligned__(4))); /*   140     0 */
		} __attribute__((__packed__)) __attribute__((__aligned__(4)));                     /*     0   140 */
	} __attribute__((__aligned__(1)));               /*     0   268 */

	/* size: 268, cachelines: 5, members: 1 */
	/* forced alignments: 1 */
	/* last cacheline: 12 bytes */
} __attribute__((__packed__));

This helps with the ongoing efforts to globally enable -Warray-bounds
and get us closer to being able to tighten the FORTIFY_SOURCE routines
on memcpy().

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.10/process/deprecated.html#zero-length-and-one-element-arrays

Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/109Signed-off-by: default avatarGustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent d15040a3
...@@ -71,13 +71,26 @@ struct compat_group_source_req { ...@@ -71,13 +71,26 @@ struct compat_group_source_req {
} __packed; } __packed;
struct compat_group_filter { struct compat_group_filter {
__u32 gf_interface; union {
struct __kernel_sockaddr_storage gf_group struct {
__aligned(4); __u32 gf_interface_aux;
__u32 gf_fmode; struct __kernel_sockaddr_storage gf_group_aux
__u32 gf_numsrc; __aligned(4);
struct __kernel_sockaddr_storage gf_slist[1] __u32 gf_fmode_aux;
__aligned(4); __u32 gf_numsrc_aux;
struct __kernel_sockaddr_storage gf_slist[1]
__aligned(4);
} __packed;
struct {
__u32 gf_interface;
struct __kernel_sockaddr_storage gf_group
__aligned(4);
__u32 gf_fmode;
__u32 gf_numsrc;
struct __kernel_sockaddr_storage gf_slist_flex[]
__aligned(4);
} __packed;
};
} __packed; } __packed;
#endif /* NET_COMPAT_H */ #endif /* NET_COMPAT_H */
...@@ -222,11 +222,22 @@ struct group_source_req { ...@@ -222,11 +222,22 @@ struct group_source_req {
}; };
struct group_filter { struct group_filter {
__u32 gf_interface; /* interface index */ union {
struct __kernel_sockaddr_storage gf_group; /* multicast address */ struct {
__u32 gf_fmode; /* filter mode */ __u32 gf_interface_aux; /* interface index */
__u32 gf_numsrc; /* number of sources */ struct __kernel_sockaddr_storage gf_group_aux; /* multicast address */
struct __kernel_sockaddr_storage gf_slist[1]; /* interface index */ __u32 gf_fmode_aux; /* filter mode */
__u32 gf_numsrc_aux; /* number of sources */
struct __kernel_sockaddr_storage gf_slist[1]; /* interface index */
};
struct {
__u32 gf_interface; /* interface index */
struct __kernel_sockaddr_storage gf_group; /* multicast address */
__u32 gf_fmode; /* filter mode */
__u32 gf_numsrc; /* number of sources */
struct __kernel_sockaddr_storage gf_slist_flex[]; /* interface index */
};
};
}; };
#define GROUP_FILTER_SIZE(numsrc) \ #define GROUP_FILTER_SIZE(numsrc) \
......
...@@ -790,7 +790,8 @@ static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen) ...@@ -790,7 +790,8 @@ static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
goto out_free_gsf; goto out_free_gsf;
err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc, err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc,
gsf->gf_fmode, &gsf->gf_group, gsf->gf_slist); gsf->gf_fmode, &gsf->gf_group,
gsf->gf_slist_flex);
out_free_gsf: out_free_gsf:
kfree(gsf); kfree(gsf);
return err; return err;
...@@ -799,7 +800,7 @@ static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen) ...@@ -799,7 +800,7 @@ static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
int optlen) int optlen)
{ {
const int size0 = offsetof(struct compat_group_filter, gf_slist); const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
struct compat_group_filter *gf32; struct compat_group_filter *gf32;
unsigned int n; unsigned int n;
void *p; void *p;
...@@ -813,7 +814,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -813,7 +814,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
p = kmalloc(optlen + 4, GFP_KERNEL); p = kmalloc(optlen + 4, GFP_KERNEL);
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */ gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
err = -EFAULT; err = -EFAULT;
if (copy_from_sockptr(gf32, optval, optlen)) if (copy_from_sockptr(gf32, optval, optlen))
...@@ -826,7 +827,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -826,7 +827,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
goto out_free_gsf; goto out_free_gsf;
err = -EINVAL; err = -EINVAL;
if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen) if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
goto out_free_gsf; goto out_free_gsf;
/* numsrc >= (4G-140)/128 overflow in 32 bits */ /* numsrc >= (4G-140)/128 overflow in 32 bits */
...@@ -834,7 +835,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -834,7 +835,7 @@ static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf) if (n > sock_net(sk)->ipv4.sysctl_igmp_max_msf)
goto out_free_gsf; goto out_free_gsf;
err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode, err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
&gf32->gf_group, gf32->gf_slist); &gf32->gf_group, gf32->gf_slist_flex);
out_free_gsf: out_free_gsf:
kfree(p); kfree(p);
return err; return err;
...@@ -1455,7 +1456,7 @@ static bool getsockopt_needs_rtnl(int optname) ...@@ -1455,7 +1456,7 @@ static bool getsockopt_needs_rtnl(int optname)
static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval, static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
int __user *optlen, int len) int __user *optlen, int len)
{ {
const int size0 = offsetof(struct group_filter, gf_slist); const int size0 = offsetof(struct group_filter, gf_slist_flex);
struct group_filter __user *p = optval; struct group_filter __user *p = optval;
struct group_filter gsf; struct group_filter gsf;
int num; int num;
...@@ -1467,7 +1468,7 @@ static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval, ...@@ -1467,7 +1468,7 @@ static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
return -EFAULT; return -EFAULT;
num = gsf.gf_numsrc; num = gsf.gf_numsrc;
err = ip_mc_gsfget(sk, &gsf, p->gf_slist); err = ip_mc_gsfget(sk, &gsf, p->gf_slist_flex);
if (err) if (err)
return err; return err;
if (gsf.gf_numsrc < num) if (gsf.gf_numsrc < num)
...@@ -1481,7 +1482,7 @@ static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval, ...@@ -1481,7 +1482,7 @@ static int ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval, static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
int __user *optlen, int len) int __user *optlen, int len)
{ {
const int size0 = offsetof(struct compat_group_filter, gf_slist); const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
struct compat_group_filter __user *p = optval; struct compat_group_filter __user *p = optval;
struct compat_group_filter gf32; struct compat_group_filter gf32;
struct group_filter gf; struct group_filter gf;
...@@ -1498,7 +1499,7 @@ static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval, ...@@ -1498,7 +1499,7 @@ static int compat_ip_get_mcast_msfilter(struct sock *sk, void __user *optval,
num = gf.gf_numsrc = gf32.gf_numsrc; num = gf.gf_numsrc = gf32.gf_numsrc;
gf.gf_group = gf32.gf_group; gf.gf_group = gf32.gf_group;
err = ip_mc_gsfget(sk, &gf, p->gf_slist); err = ip_mc_gsfget(sk, &gf, p->gf_slist_flex);
if (err) if (err)
return err; return err;
if (gf.gf_numsrc < num) if (gf.gf_numsrc < num)
......
...@@ -225,7 +225,7 @@ static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -225,7 +225,7 @@ static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
goto out_free_gsf; goto out_free_gsf;
ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist); ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex);
out_free_gsf: out_free_gsf:
kfree(gsf); kfree(gsf);
return ret; return ret;
...@@ -234,7 +234,7 @@ static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -234,7 +234,7 @@ static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
int optlen) int optlen)
{ {
const int size0 = offsetof(struct compat_group_filter, gf_slist); const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
struct compat_group_filter *gf32; struct compat_group_filter *gf32;
void *p; void *p;
int ret; int ret;
...@@ -249,7 +249,7 @@ static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -249,7 +249,7 @@ static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
gf32 = p + 4; /* we want ->gf_group and ->gf_slist aligned */ gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
ret = -EFAULT; ret = -EFAULT;
if (copy_from_sockptr(gf32, optval, optlen)) if (copy_from_sockptr(gf32, optval, optlen))
goto out_free_p; goto out_free_p;
...@@ -261,14 +261,14 @@ static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, ...@@ -261,14 +261,14 @@ static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
goto out_free_p; goto out_free_p;
ret = -EINVAL; ret = -EINVAL;
if (offsetof(struct compat_group_filter, gf_slist[n]) > optlen) if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
goto out_free_p; goto out_free_p;
ret = ip6_mc_msfilter(sk, &(struct group_filter){ ret = ip6_mc_msfilter(sk, &(struct group_filter){
.gf_interface = gf32->gf_interface, .gf_interface = gf32->gf_interface,
.gf_group = gf32->gf_group, .gf_group = gf32->gf_group,
.gf_fmode = gf32->gf_fmode, .gf_fmode = gf32->gf_fmode,
.gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist); .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex);
out_free_p: out_free_p:
kfree(p); kfree(p);
...@@ -1048,7 +1048,7 @@ static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, ...@@ -1048,7 +1048,7 @@ static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
static int ipv6_get_msfilter(struct sock *sk, void __user *optval, static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
int __user *optlen, int len) int __user *optlen, int len)
{ {
const int size0 = offsetof(struct group_filter, gf_slist); const int size0 = offsetof(struct group_filter, gf_slist_flex);
struct group_filter __user *p = optval; struct group_filter __user *p = optval;
struct group_filter gsf; struct group_filter gsf;
int num; int num;
...@@ -1062,7 +1062,7 @@ static int ipv6_get_msfilter(struct sock *sk, void __user *optval, ...@@ -1062,7 +1062,7 @@ static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
num = gsf.gf_numsrc; num = gsf.gf_numsrc;
lock_sock(sk); lock_sock(sk);
err = ip6_mc_msfget(sk, &gsf, p->gf_slist); err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex);
if (!err) { if (!err) {
if (num > gsf.gf_numsrc) if (num > gsf.gf_numsrc)
num = gsf.gf_numsrc; num = gsf.gf_numsrc;
...@@ -1077,7 +1077,7 @@ static int ipv6_get_msfilter(struct sock *sk, void __user *optval, ...@@ -1077,7 +1077,7 @@ static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval, static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
int __user *optlen) int __user *optlen)
{ {
const int size0 = offsetof(struct compat_group_filter, gf_slist); const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
struct compat_group_filter __user *p = optval; struct compat_group_filter __user *p = optval;
struct compat_group_filter gf32; struct compat_group_filter gf32;
struct group_filter gf; struct group_filter gf;
...@@ -1100,7 +1100,7 @@ static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval, ...@@ -1100,7 +1100,7 @@ static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
lock_sock(sk); lock_sock(sk);
err = ip6_mc_msfget(sk, &gf, p->gf_slist); err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex);
release_sock(sk); release_sock(sk);
if (err) if (err)
return err; return err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment