Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1011682pxb; Wed, 6 Apr 2022 06:41:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweCwonmyhh0GCtps/S344ZCAtmyW7OD+c6sJAQMkO30yHLtVftjHMgbvsURFwvgqb76uQg X-Received: by 2002:a63:af02:0:b0:375:57f0:8af1 with SMTP id w2-20020a63af02000000b0037557f08af1mr7163872pge.188.1649252474239; Wed, 06 Apr 2022 06:41:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649252474; cv=none; d=google.com; s=arc-20160816; b=wvhpgtxb100QS/+C75FK6Mk7DFQszBXzqZ0mWhCsR0IBThnxyg/gYcYFjXttycmco1 L+8+t0Jg0iiHhqKI2DzHRJxo3mD9aWpBRSgCgpd1a/d3SDiKifJszYhse6E6bNfu4gFo WKhOFYZttDbe0h9FNOo+OPGcVH3sqctQPLJNS077TI1rBtI2WJBXgqAnLqdvSFnw9PBu dLtRXHonzc98xOBR2/quHpXpaYXYF/uSNkmWmA+DwQQOQl1u8aBoUj/WewA7QkViz5UU 5N53KdUiiG4YFX1uRb9WADPHrWaPftJ4S19DH1ikGH5nFPsQj+nnGkRzCxZSsUDm+sZ/ gImw== 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=KbNpb3b/9ajLWp/MXtWitvXSBZaTNcQcQ4bSaWFvu92+wRu/zZL25qafsRoQ6oI0zu BptLMQb4rvLB1vl9oUqlS8gW+eVAp/ybb4tzQbDzxR8wjUIw74z08z6ZVDhoSMy9UuWx Rt63FEEY9AsXoKiuORDmA5TpZxN9IdcLy9FXK76xM0xNGLrVGq0zWj3WSwWrCkQam1L6 wjxJijMaSfBN4uhk71WJBzxIHvoDQov9azsKGYpWsAfCNk0mjT3HKDqd5DvM1F67oHkw noWRa+zxGHSF1PybKIUNVhLsoxss9rnxqm91E7RgcFXpAghsAYTJOUPd9P35AZBBhUtR 8fNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="0mV/OJUF"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id k6-20020aa79726000000b004fa3a8e0028si15054451pfg.223.2022.04.06.06.41.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 06:41:14 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="0mV/OJUF"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BCFED4E4BB3; Wed, 6 Apr 2022 04:27:51 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1845293AbiDFByM (ORCPT + 99 others); Tue, 5 Apr 2022 21:54:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349393AbiDEJtr (ORCPT ); Tue, 5 Apr 2022 05:49:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5638C15FFD; Tue, 5 Apr 2022 02:44:53 -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 dfw.source.kernel.org (Postfix) with ESMTPS id E739761675; Tue, 5 Apr 2022 09:44:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05FAEC385A1; Tue, 5 Apr 2022 09:44:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649151892; bh=vjsrtqUyE3qmQ6zDoHeGzm8tnzALeS3OuodQrayDuPM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0mV/OJUF344rRvnJLmR5K4+YFhU2M4zbTPjx9s6jn4VW62NpA6PoOh21ybmvV1W+4 qxIry2+CD+Oh5K3jWrjc1bqfDNMNNf4lPlB9oFHlLWBQKA2rnUVQIvYFIUHCDQbMn/ Lj5USYsK7TMLTdhjxevFLp47JpkD/uGdnwv7321o= 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.15 541/913] bpf, sockmap: Fix memleak in tcp_bpf_sendmsg while sk msg is full Date: Tue, 5 Apr 2022 09:26:43 +0200 Message-Id: <20220405070356.064605865@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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