Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp707012pxb; Tue, 5 Apr 2022 19:39:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVuKLZQAdcA4vavgBnzMc2bj9deM0WXPLeQZXpY36jEsqtcPUrUxPn2C4xJHb3KmOp242B X-Received: by 2002:a17:90b:314b:b0:1c7:4a4f:6740 with SMTP id ip11-20020a17090b314b00b001c74a4f6740mr7481757pjb.145.1649212757269; Tue, 05 Apr 2022 19:39:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649212757; cv=none; d=google.com; s=arc-20160816; b=CULw3G2Tpf3JeDOUILkeUWjKT8GR9MYBlH31LYGksXDZMeKqd1250+/6h3oErKDXul WoNH2QmrGUu6Kw/w0jq5GHqjd5z/IyaqXzQsWwEzMGqtDTxh+phitNUeJJJtx+Zg77sJ 5Xn1UBGWA+wx4n/lfNKyOQhb5LW1aCObJIHYrwMFKUm234NX1pi8Ef64r8ao6ILoHaLi UQdy2dU/XwXRou0rw100XOdQG60ZR7Ji8ss4hY4KUM6nfCP8QJZxnF8k3JOiN3L72u03 Obf9WoVQVZs9Hw2g+xctVQXnY8ln1nHuy0Nn7Mjx1bekSnSIqTVmZD/Rl/AS11RIw+gf mjqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=lmHck5rqKqBX+cbBHZCFLbLBGfaN1b0H+6gTRZ8LgGc=; b=nvq8sLLABSFJY72uColm7ljLna/7WfGK5ywqUI9wPepIZVM/O6mw8BliFZKNMbtVZb h2+0EG1h2issu2MlKh/kempiQSINXHWUg1KFYM1gIFIX0QrUomF7eV4itZZiCvaTLhOj Js6JDRZVOAGNAfHqwZPRmN92UJTLi4UAkoMM45a2CJzrE9IHzRmRoayqYgzLhJ1bMsUl jc4suhs85vdtFt5N6FNq74HUyix4z5RsKPjgFN0awfQqaEd0lMvtZtpzWKft6zff8tDz cTqSTkpucC6jTcDQjezfoM9JgZTvW6iXhVI1wIKWqoZnUTpe2qThf0YrwWpZVCpu8FQZ ovHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DJmH4ocs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q21-20020a62e115000000b004fa93c1118esi13148232pfh.173.2022.04.05.19.39.02; Tue, 05 Apr 2022 19:39:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DJmH4ocs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347228AbiDEJqQ (ORCPT + 99 others); Tue, 5 Apr 2022 05:46:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238653AbiDEITQ (ORCPT ); Tue, 5 Apr 2022 04:19:16 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E15A6D195; Tue, 5 Apr 2022 01:09:17 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 11103B81A37; Tue, 5 Apr 2022 08:09:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52437C385A0; Tue, 5 Apr 2022 08:09:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649146154; bh=vjsrtqUyE3qmQ6zDoHeGzm8tnzALeS3OuodQrayDuPM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DJmH4ocsROX649+5ifZzoswys5rcJ+c4KPzSw6GHAq91ld/d3GpslGwlkXjz7twCI 2FbjXdTDOX0AVkMDEAPCOTUVSBlC/OvgZQNXW8bxbFeHAhBPyCoR5u+I/Va72YfJuS mtMU/k03wSSQeGY5xCoM08nJFfo/NuTf81OHJei8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wang Yufen , Daniel Borkmann , John Fastabend , Sasha Levin Subject: [PATCH 5.17 0661/1126] bpf, sockmap: Fix memleak in tcp_bpf_sendmsg while sk msg is full Date: Tue, 5 Apr 2022 09:23:28 +0200 Message-Id: <20220405070427.030507667@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wang Yufen [ Upstream commit 9c34e38c4a870eb30b13f42f5b44f42e9d19ccb8 ] If tcp_bpf_sendmsg() is running while sk msg is full. When sk_msg_alloc() returns -ENOMEM error, tcp_bpf_sendmsg() goes to wait_for_memory. If partial memory has been alloced by sk_msg_alloc(), that is, msg_tx->sg.size is greater than osize after sk_msg_alloc(), memleak occurs. To fix we use sk_msg_trim() to release the allocated memory, then goto wait for memory. Other call paths of sk_msg_alloc() have the similar issue, such as tls_sw_sendmsg(), so handle sk_msg_trim logic inside sk_msg_alloc(), as Cong Wang suggested. This issue can cause the following info: WARNING: CPU: 3 PID: 7950 at net/core/stream.c:208 sk_stream_kill_queues+0xd4/0x1a0 Call Trace: inet_csk_destroy_sock+0x55/0x110 __tcp_close+0x279/0x470 tcp_close+0x1f/0x60 inet_release+0x3f/0x80 __sock_release+0x3d/0xb0 sock_close+0x11/0x20 __fput+0x92/0x250 task_work_run+0x6a/0xa0 do_exit+0x33b/0xb60 do_group_exit+0x2f/0xa0 get_signal+0xb6/0x950 arch_do_signal_or_restart+0xac/0x2a0 exit_to_user_mode_prepare+0xa9/0x200 syscall_exit_to_user_mode+0x12/0x30 do_syscall_64+0x46/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xae WARNING: CPU: 3 PID: 2094 at net/ipv4/af_inet.c:155 inet_sock_destruct+0x13c/0x260 Call Trace: __sk_destruct+0x24/0x1f0 sk_psock_destroy+0x19b/0x1c0 process_one_work+0x1b3/0x3c0 kthread+0xe6/0x110 ret_from_fork+0x22/0x30 Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface") Signed-off-by: Wang Yufen Signed-off-by: Daniel Borkmann Acked-by: John Fastabend Link: https://lore.kernel.org/bpf/20220304081145.2037182-3-wangyufen@huawei.com Signed-off-by: Sasha Levin --- net/core/skmsg.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 929a2b096b04..cc381165ea08 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -27,6 +27,7 @@ int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len, int elem_first_coalesce) { struct page_frag *pfrag = sk_page_frag(sk); + u32 osize = msg->sg.size; int ret = 0; len -= msg->sg.size; @@ -35,13 +36,17 @@ int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len, u32 orig_offset; int use, i; - if (!sk_page_frag_refill(sk, pfrag)) - return -ENOMEM; + if (!sk_page_frag_refill(sk, pfrag)) { + ret = -ENOMEM; + goto msg_trim; + } orig_offset = pfrag->offset; use = min_t(int, len, pfrag->size - orig_offset); - if (!sk_wmem_schedule(sk, use)) - return -ENOMEM; + if (!sk_wmem_schedule(sk, use)) { + ret = -ENOMEM; + goto msg_trim; + } i = msg->sg.end; sk_msg_iter_var_prev(i); @@ -71,6 +76,10 @@ int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len, } return ret; + +msg_trim: + sk_msg_trim(sk, msg, osize); + return ret; } EXPORT_SYMBOL_GPL(sk_msg_alloc); -- 2.34.1