Skip to content

Commit 2d3e5ca

Browse files
GustavoARSilvadavem330
authored andcommitted
net/ipv4: Replace one-element array with flexible-array member
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: $ pahole -C ip_msfilter net/ipv4/ip_sockglue.o struct ip_msfilter { union { struct { __be32 imsf_multiaddr_aux; /* 0 4 */ __be32 imsf_interface_aux; /* 4 4 */ __u32 imsf_fmode_aux; /* 8 4 */ __u32 imsf_numsrc_aux; /* 12 4 */ __be32 imsf_slist[1]; /* 16 4 */ }; /* 0 20 */ struct { __be32 imsf_multiaddr; /* 0 4 */ __be32 imsf_interface; /* 4 4 */ __u32 imsf_fmode; /* 8 4 */ __u32 imsf_numsrc; /* 12 4 */ __be32 imsf_slist_flex[0]; /* 16 0 */ }; /* 0 16 */ }; /* 0 20 */ /* size: 20, cachelines: 1, members: 1 */ /* last cacheline: 20 bytes */ }; Also, refactor the code accordingly and make use of the struct_size() and flex_array_size() helpers. 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: KSPP/linux#79 Link: KSPP/linux#109 Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 29a097b commit 2d3e5ca

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

include/uapi/linux/in.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,22 @@ struct ip_mreq_source {
188188
};
189189

190190
struct ip_msfilter {
191-
__be32 imsf_multiaddr;
192-
__be32 imsf_interface;
193-
__u32 imsf_fmode;
194-
__u32 imsf_numsrc;
195-
__be32 imsf_slist[1];
191+
union {
192+
struct {
193+
__be32 imsf_multiaddr_aux;
194+
__be32 imsf_interface_aux;
195+
__u32 imsf_fmode_aux;
196+
__u32 imsf_numsrc_aux;
197+
__be32 imsf_slist[1];
198+
};
199+
struct {
200+
__be32 imsf_multiaddr;
201+
__be32 imsf_interface;
202+
__u32 imsf_fmode;
203+
__u32 imsf_numsrc;
204+
__be32 imsf_slist_flex[];
205+
};
206+
};
196207
};
197208

198209
#define IP_MSFILTER_SIZE(numsrc) \

net/ipv4/igmp.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,8 +2475,8 @@ int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
24752475
goto done;
24762476
}
24772477
newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2478-
memcpy(newpsl->sl_addr, msf->imsf_slist,
2479-
msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2478+
memcpy(newpsl->sl_addr, msf->imsf_slist_flex,
2479+
flex_array_size(msf, imsf_slist_flex, msf->imsf_numsrc));
24802480
err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
24812481
msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
24822482
if (err) {
@@ -2551,14 +2551,14 @@ int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
25512551
count = psl->sl_count;
25522552
}
25532553
copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2554-
len = copycount * sizeof(psl->sl_addr[0]);
2554+
len = flex_array_size(psl, sl_addr, copycount);
25552555
msf->imsf_numsrc = count;
2556-
if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2557-
copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2556+
if (put_user(struct_size(optval, imsf_slist_flex, copycount), optlen) ||
2557+
copy_to_user(optval, msf, struct_size(optval, imsf_slist_flex, 0))) {
25582558
return -EFAULT;
25592559
}
25602560
if (len &&
2561-
copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2561+
copy_to_user(&optval->imsf_slist_flex[0], psl->sl_addr, len))
25622562
return -EFAULT;
25632563
return 0;
25642564
done:

net/ipv4/ip_sockglue.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,11 @@ static int set_mcast_msfilter(struct sock *sk, int ifindex,
663663
struct sockaddr_storage *group,
664664
struct sockaddr_storage *list)
665665
{
666-
int msize = IP_MSFILTER_SIZE(numsrc);
667666
struct ip_msfilter *msf;
668667
struct sockaddr_in *psin;
669668
int err, i;
670669

671-
msf = kmalloc(msize, GFP_KERNEL);
670+
msf = kmalloc(struct_size(msf, imsf_slist_flex, numsrc), GFP_KERNEL);
672671
if (!msf)
673672
return -ENOBUFS;
674673

@@ -684,7 +683,7 @@ static int set_mcast_msfilter(struct sock *sk, int ifindex,
684683

685684
if (psin->sin_family != AF_INET)
686685
goto Eaddrnotavail;
687-
msf->imsf_slist[i] = psin->sin_addr.s_addr;
686+
msf->imsf_slist_flex[i] = psin->sin_addr.s_addr;
688687
}
689688
err = ip_mc_msfilter(sk, msf, ifindex);
690689
kfree(msf);
@@ -1229,7 +1228,7 @@ static int do_ip_setsockopt(struct sock *sk, int level, int optname,
12291228
{
12301229
struct ip_msfilter *msf;
12311230

1232-
if (optlen < IP_MSFILTER_SIZE(0))
1231+
if (optlen < struct_size(msf, imsf_slist_flex, 0))
12331232
goto e_inval;
12341233
if (optlen > sysctl_optmem_max) {
12351234
err = -ENOBUFS;
@@ -1247,7 +1246,8 @@ static int do_ip_setsockopt(struct sock *sk, int level, int optname,
12471246
err = -ENOBUFS;
12481247
break;
12491248
}
1250-
if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1249+
if (struct_size(msf, imsf_slist_flex, msf->imsf_numsrc) >
1250+
optlen) {
12511251
kfree(msf);
12521252
err = -EINVAL;
12531253
break;
@@ -1660,11 +1660,12 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
16601660
{
16611661
struct ip_msfilter msf;
16621662

1663-
if (len < IP_MSFILTER_SIZE(0)) {
1663+
if (len < struct_size(&msf, imsf_slist_flex, 0)) {
16641664
err = -EINVAL;
16651665
goto out;
16661666
}
1667-
if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1667+
if (copy_from_user(&msf, optval,
1668+
struct_size(&msf, imsf_slist_flex, 0))) {
16681669
err = -EFAULT;
16691670
goto out;
16701671
}

0 commit comments

Comments
 (0)