[bpf] bpf: cpumap: Fix memory leak in cpu_map_update_elem

Message ID 20230711115848.2701559-1-pulehui@huaweicloud.com
State New
Headers
Series [bpf] bpf: cpumap: Fix memory leak in cpu_map_update_elem |

Commit Message

Pu Lehui July 11, 2023, 11:58 a.m. UTC
  From: Pu Lehui <pulehui@huawei.com>

Syzkaller reported a memory leak as follows:

BUG: memory leak
unreferenced object 0xff110001198ef748 (size 192):
  comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
  hex dump (first 32 bytes):
    00 00 00 00 4a 19 00 00 80 ad e3 e4 fe ff c0 00  ....J...........
    00 b2 d3 0c 01 00 11 ff 28 f5 8e 19 01 00 11 ff  ........(.......
  backtrace:
    [<ffffffffadd28087>] __cpu_map_entry_alloc+0xf7/0xb00
    [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
    [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
    [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
    [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
    [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
    [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6

BUG: memory leak
unreferenced object 0xff110001198ef528 (size 192):
  comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
  hex dump (first 32 bytes):
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<ffffffffadd281f0>] __cpu_map_entry_alloc+0x260/0xb00
    [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
    [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
    [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
    [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
    [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
    [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6

BUG: memory leak
unreferenced object 0xff1100010fd93d68 (size 8):
  comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
  hex dump (first 8 bytes):
    00 00 00 00 00 00 00 00                          ........
  backtrace:
    [<ffffffffade5db3e>] kvmalloc_node+0x11e/0x170
    [<ffffffffadd28280>] __cpu_map_entry_alloc+0x2f0/0xb00
    [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
    [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
    [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
    [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
    [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
    [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6

In the cpu_map_update_elem flow, when kthread_stop is called before
calling the threadfn of rcpu->kthread, since the KTHREAD_SHOULD_STOP bit
of kthread has been set by kthread_stop, the threadfn of rcpu->kthread
will never be executed, and rcpu->refcnt will never be 0, which will
lead to the allocated rcpu, rcpu->queue and rcpu->queue->queue cannot be
released.

Calling kthread_stop before executing kthread's threadfn will return
-EINTR. We can complete the release of memory resources in this state.

Fixes: 6710e1126934 ("bpf: introduce new bpf cpu map type BPF_MAP_TYPE_CPUMAP")
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
 kernel/bpf/cpumap.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
  

Comments

Jesper Dangaard Brouer July 11, 2023, 4:06 p.m. UTC | #1
On 11/07/2023 13.58, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
> 
> Syzkaller reported a memory leak as follows:
> 
[...]>
> In the cpu_map_update_elem flow, when kthread_stop is called before
> calling the threadfn of rcpu->kthread, since the KTHREAD_SHOULD_STOP bit
> of kthread has been set by kthread_stop, the threadfn of rcpu->kthread
> will never be executed, and rcpu->refcnt will never be 0, which will
> lead to the allocated rcpu, rcpu->queue and rcpu->queue->queue cannot be
> released.
> 
> Calling kthread_stop before executing kthread's threadfn will return
> -EINTR. We can complete the release of memory resources in this state.
> 
> Fixes: 6710e1126934 ("bpf: introduce new bpf cpu map type BPF_MAP_TYPE_CPUMAP")
> Signed-off-by: Pu Lehui <pulehui@huawei.com>

LGTM, thanks for fixing this.

Acked-by: Jesper Dangaard Brouer <hawk@kernel.org>
  
Hou Tao July 12, 2023, 2:12 a.m. UTC | #2
On 7/11/2023 7:58 PM, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
>
> Syzkaller reported a memory leak as follows:
>
> BUG: memory leak
> unreferenced object 0xff110001198ef748 (size 192):
>   comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
>   hex dump (first 32 bytes):
>     00 00 00 00 4a 19 00 00 80 ad e3 e4 fe ff c0 00  ....J...........
>     00 b2 d3 0c 01 00 11 ff 28 f5 8e 19 01 00 11 ff  ........(.......
>   backtrace:
>     [<ffffffffadd28087>] __cpu_map_entry_alloc+0xf7/0xb00
>     [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
>     [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
>     [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
>     [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
>     [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
>     [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
>
> BUG: memory leak
> unreferenced object 0xff110001198ef528 (size 192):
>   comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
>   hex dump (first 32 bytes):
>     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>   backtrace:
>     [<ffffffffadd281f0>] __cpu_map_entry_alloc+0x260/0xb00
>     [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
>     [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
>     [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
>     [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
>     [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
>     [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
>
> BUG: memory leak
> unreferenced object 0xff1100010fd93d68 (size 8):
>   comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
>   hex dump (first 8 bytes):
>     00 00 00 00 00 00 00 00                          ........
>   backtrace:
>     [<ffffffffade5db3e>] kvmalloc_node+0x11e/0x170
>     [<ffffffffadd28280>] __cpu_map_entry_alloc+0x2f0/0xb00
>     [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
>     [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
>     [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
>     [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
>     [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
>     [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
>
> In the cpu_map_update_elem flow, when kthread_stop is called before
> calling the threadfn of rcpu->kthread, since the KTHREAD_SHOULD_STOP bit
> of kthread has been set by kthread_stop, the threadfn of rcpu->kthread
> will never be executed, and rcpu->refcnt will never be 0, which will
> lead to the allocated rcpu, rcpu->queue and rcpu->queue->queue cannot be
> released.
>
> Calling kthread_stop before executing kthread's threadfn will return
> -EINTR. We can complete the release of memory resources in this state.
>
> Fixes: 6710e1126934 ("bpf: introduce new bpf cpu map type BPF_MAP_TYPE_CPUMAP")
> Signed-off-by: Pu Lehui <pulehui@huawei.com>

Acked-by: Hou Tao <houtao1@huawei.com>
  
Alexei Starovoitov July 12, 2023, 3:04 a.m. UTC | #3
On Tue, Jul 11, 2023 at 7:12 PM Hou Tao <houtao@huaweicloud.com> wrote:
>
>
>
> On 7/11/2023 7:58 PM, Pu Lehui wrote:
> > From: Pu Lehui <pulehui@huawei.com>
> >
> > Syzkaller reported a memory leak as follows:
> >
> > BUG: memory leak
> > unreferenced object 0xff110001198ef748 (size 192):
> >   comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
> >   hex dump (first 32 bytes):
> >     00 00 00 00 4a 19 00 00 80 ad e3 e4 fe ff c0 00  ....J...........
> >     00 b2 d3 0c 01 00 11 ff 28 f5 8e 19 01 00 11 ff  ........(.......
> >   backtrace:
> >     [<ffffffffadd28087>] __cpu_map_entry_alloc+0xf7/0xb00
> >     [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
> >     [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
> >     [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
> >     [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
> >     [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
> >     [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
> >
> > BUG: memory leak
> > unreferenced object 0xff110001198ef528 (size 192):
> >   comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
> >   hex dump (first 32 bytes):
> >     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
> >     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
> >   backtrace:
> >     [<ffffffffadd281f0>] __cpu_map_entry_alloc+0x260/0xb00
> >     [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
> >     [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
> >     [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
> >     [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
> >     [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
> >     [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
> >
> > BUG: memory leak
> > unreferenced object 0xff1100010fd93d68 (size 8):
> >   comm "syz-executor.3", pid 17672, jiffies 4298118891 (age 9.906s)
> >   hex dump (first 8 bytes):
> >     00 00 00 00 00 00 00 00                          ........
> >   backtrace:
> >     [<ffffffffade5db3e>] kvmalloc_node+0x11e/0x170
> >     [<ffffffffadd28280>] __cpu_map_entry_alloc+0x2f0/0xb00
> >     [<ffffffffadd28d8e>] cpu_map_update_elem+0x2fe/0x3d0
> >     [<ffffffffadc6d0fd>] bpf_map_update_value.isra.0+0x2bd/0x520
> >     [<ffffffffadc7349b>] map_update_elem+0x4cb/0x720
> >     [<ffffffffadc7d983>] __se_sys_bpf+0x8c3/0xb90
> >     [<ffffffffb029cc80>] do_syscall_64+0x30/0x40
> >     [<ffffffffb0400099>] entry_SYSCALL_64_after_hwframe+0x61/0xc6
> >
> > In the cpu_map_update_elem flow, when kthread_stop is called before
> > calling the threadfn of rcpu->kthread, since the KTHREAD_SHOULD_STOP bit
> > of kthread has been set by kthread_stop, the threadfn of rcpu->kthread
> > will never be executed, and rcpu->refcnt will never be 0, which will
> > lead to the allocated rcpu, rcpu->queue and rcpu->queue->queue cannot be
> > released.
> >
> > Calling kthread_stop before executing kthread's threadfn will return
> > -EINTR. We can complete the release of memory resources in this state.
> >
> > Fixes: 6710e1126934 ("bpf: introduce new bpf cpu map type BPF_MAP_TYPE_CPUMAP")
> > Signed-off-by: Pu Lehui <pulehui@huawei.com>
>
> Acked-by: Hou Tao <houtao1@huawei.com>
>

Applied. Thanks
  

Patch

diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c
index 8a33e8747a0e..6ae02be7a48e 100644
--- a/kernel/bpf/cpumap.c
+++ b/kernel/bpf/cpumap.c
@@ -122,22 +122,6 @@  static void get_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
 	atomic_inc(&rcpu->refcnt);
 }
 
-/* called from workqueue, to workaround syscall using preempt_disable */
-static void cpu_map_kthread_stop(struct work_struct *work)
-{
-	struct bpf_cpu_map_entry *rcpu;
-
-	rcpu = container_of(work, struct bpf_cpu_map_entry, kthread_stop_wq);
-
-	/* Wait for flush in __cpu_map_entry_free(), via full RCU barrier,
-	 * as it waits until all in-flight call_rcu() callbacks complete.
-	 */
-	rcu_barrier();
-
-	/* kthread_stop will wake_up_process and wait for it to complete */
-	kthread_stop(rcpu->kthread);
-}
-
 static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
 {
 	/* The tear-down procedure should have made sure that queue is
@@ -165,6 +149,30 @@  static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
 	}
 }
 
+/* called from workqueue, to workaround syscall using preempt_disable */
+static void cpu_map_kthread_stop(struct work_struct *work)
+{
+	struct bpf_cpu_map_entry *rcpu;
+	int err;
+
+	rcpu = container_of(work, struct bpf_cpu_map_entry, kthread_stop_wq);
+
+	/* Wait for flush in __cpu_map_entry_free(), via full RCU barrier,
+	 * as it waits until all in-flight call_rcu() callbacks complete.
+	 */
+	rcu_barrier();
+
+	/* kthread_stop will wake_up_process and wait for it to complete */
+	err = kthread_stop(rcpu->kthread);
+	if (err) {
+		/* kthread_stop may be called before cpu_map_kthread_run
+		 * is executed, so we need to release the memory related
+		 * to rcpu.
+		 */
+		put_cpu_map_entry(rcpu);
+	}
+}
+
 static void cpu_map_bpf_prog_run_skb(struct bpf_cpu_map_entry *rcpu,
 				     struct list_head *listp,
 				     struct xdp_cpumap_stats *stats)