xsk_getsockopt() is copying uninitialized stack memory to userspace when
`extra_stats` is `false`. Fix it by initializing `stats` with memset().
Cc: [email protected]
Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
Suggested-by: Dan Carpenter <[email protected]>
Signed-off-by: Peilin Ye <[email protected]>
---
net/xdp/xsk.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
index 26e3bba8c204..acf001908a0d 100644
--- a/net/xdp/xsk.c
+++ b/net/xdp/xsk.c
@@ -844,6 +844,8 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
bool extra_stats = true;
size_t stats_size;
+ memset(&stats, 0, sizeof(stats));
+
if (len < sizeof(struct xdp_statistics_v1)) {
return -EINVAL;
} else if (len < sizeof(stats)) {
--
2.25.1
On Mon, Jul 27, 2020 at 7:30 PM Peilin Ye <[email protected]> wrote:
>
> xsk_getsockopt() is copying uninitialized stack memory to userspace when
> `extra_stats` is `false`. Fix it by initializing `stats` with memset().
>
> Cc: [email protected]
8aa5a33578e9 is not in stable branches yet, so we don't need to Cc stable.
> Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
> Suggested-by: Dan Carpenter <[email protected]>
> Signed-off-by: Peilin Ye <[email protected]>
> ---
> net/xdp/xsk.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
> index 26e3bba8c204..acf001908a0d 100644
> --- a/net/xdp/xsk.c
> +++ b/net/xdp/xsk.c
> @@ -844,6 +844,8 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
> bool extra_stats = true;
> size_t stats_size;
>
> + memset(&stats, 0, sizeof(stats));
> +
xsk.c doesn't include linux/string.h directly, so using memset may break
build for some config combinations. We can probably just use
struct xdp_statistics stats = {};
Thanks,
Song
> if (len < sizeof(struct xdp_statistics_v1)) {
> return -EINVAL;
> } else if (len < sizeof(stats)) {
> --
> 2.25.1
>
On Mon, Jul 27, 2020 at 10:07:20PM -0700, Song Liu wrote:
> On Mon, Jul 27, 2020 at 7:30 PM Peilin Ye <[email protected]> wrote:
> >
> > xsk_getsockopt() is copying uninitialized stack memory to userspace when
> > `extra_stats` is `false`. Fix it by initializing `stats` with memset().
> >
> > Cc: [email protected]
>
> 8aa5a33578e9 is not in stable branches yet, so we don't need to Cc stable.
>
> > Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
> > Suggested-by: Dan Carpenter <[email protected]>
> > Signed-off-by: Peilin Ye <[email protected]>
> > ---
> > net/xdp/xsk.c | 2 ++
> > 1 file changed, 2 insertions(+)
> >
> > diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
> > index 26e3bba8c204..acf001908a0d 100644
> > --- a/net/xdp/xsk.c
> > +++ b/net/xdp/xsk.c
> > @@ -844,6 +844,8 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
> > bool extra_stats = true;
> > size_t stats_size;
> >
> > + memset(&stats, 0, sizeof(stats));
> > +
>
> xsk.c doesn't include linux/string.h directly, so using memset may break
> build for some config combinations. We can probably just use
>
> struct xdp_statistics stats = {};
I see. I will send v2 soon. Thank you for reviewing the patch!
Peilin Ye
xsk_getsockopt() is copying uninitialized stack memory to userspace when
`extra_stats` is `false`. Fix it.
Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
Suggested-by: Dan Carpenter <[email protected]>
Signed-off-by: Peilin Ye <[email protected]>
---
Doing `= {};` is sufficient since currently `struct xdp_statistics` is
defined as follows:
struct xdp_statistics {
__u64 rx_dropped;
__u64 rx_invalid_descs;
__u64 tx_invalid_descs;
__u64 rx_ring_full;
__u64 rx_fill_ring_empty_descs;
__u64 tx_ring_empty_descs;
};
When being copied to the userspace, `stats` will not contain any
uninitialized "holes" between struct fields.
Changes in v2:
- Remove the "Cc: [email protected]" tag. (Suggested by Song Liu
<[email protected]>)
- Initialize `stats` by assignment instead of using memset().
(Suggested by Song Liu <[email protected]>)
net/xdp/xsk.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
index 26e3bba8c204..b2b533eddebf 100644
--- a/net/xdp/xsk.c
+++ b/net/xdp/xsk.c
@@ -840,7 +840,7 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
switch (optname) {
case XDP_STATISTICS:
{
- struct xdp_statistics stats;
+ struct xdp_statistics stats = {};
bool extra_stats = true;
size_t stats_size;
--
2.25.1
On Tue, 28 Jul 2020 at 07:37, Peilin Ye <[email protected]> wrote:
>
> xsk_getsockopt() is copying uninitialized stack memory to userspace when
> `extra_stats` is `false`. Fix it.
>
> Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
> Suggested-by: Dan Carpenter <[email protected]>
> Signed-off-by: Peilin Ye <[email protected]>
> ---
Acked-by: Björn Töpel <[email protected]>
> Doing `= {};` is sufficient since currently `struct xdp_statistics` is
> defined as follows:
>
> struct xdp_statistics {
> __u64 rx_dropped;
> __u64 rx_invalid_descs;
> __u64 tx_invalid_descs;
> __u64 rx_ring_full;
> __u64 rx_fill_ring_empty_descs;
> __u64 tx_ring_empty_descs;
> };
>
> When being copied to the userspace, `stats` will not contain any
> uninitialized "holes" between struct fields.
>
> Changes in v2:
> - Remove the "Cc: [email protected]" tag. (Suggested by Song Liu
> <[email protected]>)
> - Initialize `stats` by assignment instead of using memset().
> (Suggested by Song Liu <[email protected]>)
>
> net/xdp/xsk.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
> index 26e3bba8c204..b2b533eddebf 100644
> --- a/net/xdp/xsk.c
> +++ b/net/xdp/xsk.c
> @@ -840,7 +840,7 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
> switch (optname) {
> case XDP_STATISTICS:
> {
> - struct xdp_statistics stats;
> + struct xdp_statistics stats = {};
> bool extra_stats = true;
> size_t stats_size;
>
> --
> 2.25.1
>
> On Jul 27, 2020, at 11:13 PM, Björn Töpel <[email protected]> wrote:
>
> On Tue, 28 Jul 2020 at 07:37, Peilin Ye <[email protected]> wrote:
>>
>> xsk_getsockopt() is copying uninitialized stack memory to userspace when
>> `extra_stats` is `false`. Fix it.
>>
>> Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
>> Suggested-by: Dan Carpenter <[email protected]>
>> Signed-off-by: Peilin Ye <[email protected]>
>> ---
>
> Acked-by: Björn Töpel <[email protected]>
Acked-by: Song Liu <[email protected]>
[...]
On Tue, Jul 28, 2020 at 7:37 AM Peilin Ye <[email protected]> wrote:
>
> xsk_getsockopt() is copying uninitialized stack memory to userspace when
> `extra_stats` is `false`. Fix it.
>
> Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
> Suggested-by: Dan Carpenter <[email protected]>
> Signed-off-by: Peilin Ye <[email protected]>
Acked-by: Arnd Bergmann <[email protected]>
On 7/28/20 7:36 AM, Peilin Ye wrote:
> xsk_getsockopt() is copying uninitialized stack memory to userspace when
> `extra_stats` is `false`. Fix it.
>
> Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
> Suggested-by: Dan Carpenter <[email protected]>
> Signed-off-by: Peilin Ye <[email protected]>
> ---
> Doing `= {};` is sufficient since currently `struct xdp_statistics` is
> defined as follows:
>
> struct xdp_statistics {
> __u64 rx_dropped;
> __u64 rx_invalid_descs;
> __u64 tx_invalid_descs;
> __u64 rx_ring_full;
> __u64 rx_fill_ring_empty_descs;
> __u64 tx_ring_empty_descs;
> };
>
> When being copied to the userspace, `stats` will not contain any
> uninitialized "holes" between struct fields.
I've added above explanation to the commit log since it's useful reasoning for later
on 'why' something has been done a certain way. Applied, thanks Peilin!
On Tue, Jul 28, 2020 at 12:53:59PM +0200, Daniel Borkmann wrote:
> On 7/28/20 7:36 AM, Peilin Ye wrote:
> > xsk_getsockopt() is copying uninitialized stack memory to userspace when
> > `extra_stats` is `false`. Fix it.
> >
> > Fixes: 8aa5a33578e9 ("xsk: Add new statistics")
> > Suggested-by: Dan Carpenter <[email protected]>
> > Signed-off-by: Peilin Ye <[email protected]>
> > ---
> > Doing `= {};` is sufficient since currently `struct xdp_statistics` is
> > defined as follows:
> >
> > struct xdp_statistics {
> > __u64 rx_dropped;
> > __u64 rx_invalid_descs;
> > __u64 tx_invalid_descs;
> > __u64 rx_ring_full;
> > __u64 rx_fill_ring_empty_descs;
> > __u64 tx_ring_empty_descs;
> > };
> >
> > When being copied to the userspace, `stats` will not contain any
> > uninitialized "holes" between struct fields.
>
> I've added above explanation to the commit log since it's useful reasoning for later
> on 'why' something has been done a certain way. Applied, thanks Peilin!
Ah, I see. Thank you for reviewing the patch!
Peilin Ye