2022-07-13 06:36:52

by Khalid Masum

[permalink] [raw]
Subject: [RFC PATCH 1/1] net: kcm: Use sk_psock size for kcm_psock_cache

`struct sock` has a member `sk_user_data`, which got its memory allocated
in `kcm_attach` by `kcm_psock_cache` with the size of `kcm_psock`. Which
is not enough when the member is used as `sk_psock` causing out of bound
read.

Use `sk_psock` size to allocate memory instead for `sk_user_data`.

Reported-by: [email protected]
Signed-off-by: Khalid Masum <[email protected]>
---
net/kcm/kcmsock.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
index 71899e5a5a11..688bee56f90c 100644
--- a/net/kcm/kcmsock.c
+++ b/net/kcm/kcmsock.c
@@ -23,6 +23,7 @@
#include <linux/workqueue.h>
#include <linux/syscalls.h>
#include <linux/sched/signal.h>
+#include <linux/skmsg.h>

#include <net/kcm.h>
#include <net/netns/generic.h>
@@ -2041,7 +2042,7 @@ static int __init kcm_init(void)
goto fail;

kcm_psockp = kmem_cache_create("kcm_psock_cache",
- sizeof(struct kcm_psock), 0,
+ sizeof(struct sk_psock), 0,
SLAB_HWCACHE_ALIGN, NULL);
if (!kcm_psockp)
goto fail;
--
2.36.1


2022-07-13 06:51:16

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [RFC PATCH 1/1] net: kcm: Use sk_psock size for kcm_psock_cache

On Wed, Jul 13, 2022 at 12:32:04PM +0600, Khalid Masum wrote:
> `struct sock` has a member `sk_user_data`, which got its memory allocated
> in `kcm_attach` by `kcm_psock_cache` with the size of `kcm_psock`. Which
> is not enough when the member is used as `sk_psock` causing out of bound
> read.
>
> Use `sk_psock` size to allocate memory instead for `sk_user_data`.
>
> Reported-by: [email protected]
> Signed-off-by: Khalid Masum <[email protected]>
> ---
> net/kcm/kcmsock.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)

What commit id does this fix?

thanks,

greg k-h

2022-07-13 18:20:53

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [RFC PATCH 1/1] net: kcm: Use sk_psock size for kcm_psock_cache

On Wed, 13 Jul 2022 12:32:04 +0600 Khalid Masum wrote:
> `struct sock` has a member `sk_user_data`, which got its memory allocated
> in `kcm_attach` by `kcm_psock_cache` with the size of `kcm_psock`. Which
> is not enough when the member is used as `sk_psock` causing out of bound
> read.
>
> Use `sk_psock` size to allocate memory instead for `sk_user_data`.
>
> Reported-by: [email protected]
> Signed-off-by: Khalid Masum <[email protected]>

Odd CC list, it looks like a netdev problem but netdev@ is not copied.

We had an other parson looking at this on netdev. Is this happening
when legit kernel socket gets mixed with BPF sockmap?

Does this fix work?

https://lore.kernel.org/netdev/[email protected]/

2022-07-13 18:31:55

by Khalid Masum

[permalink] [raw]
Subject: Re: [RFC PATCH 1/1] net: kcm: Use sk_psock size for kcm_psock_cache

On Thu, Jul 14, 2022 at 12:16 AM Jakub Kicinski <[email protected]> wrote:
>
> On Wed, 13 Jul 2022 12:32:04 +0600 Khalid Masum wrote:
> > `struct sock` has a member `sk_user_data`, which got its memory allocated
> > in `kcm_attach` by `kcm_psock_cache` with the size of `kcm_psock`. Which
> > is not enough when the member is used as `sk_psock` causing out of bound
> > read.
> >
> > Use `sk_psock` size to allocate memory instead for `sk_user_data`.
> >
> > Reported-by: [email protected]
> > Signed-off-by: Khalid Masum <[email protected]>
>
> Odd CC list, it looks like a netdev problem but netdev@ is not copied.

I am sorry for the inconvenience. I resent the patch with netdev in CC here:
https://lore.kernel.org/netdev/[email protected]/

> We had an other parson looking at this on netdev. Is this happening
> when legit kernel socket gets mixed with BPF sockmap?
>
> Does this fix work?
I will take a look at it.
> https://lore.kernel.org/netdev/[email protected]/

Thanks,
Khalid Masum