Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp926894pxb; Wed, 6 Apr 2022 04:31:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwmnVwX3MyDhcWT5+8kuYY1+L2JmisiYCYXcWCnf3Y/rzfnYOpSyHl6UVo9hGCS2rhrbK+ X-Received: by 2002:a17:902:e545:b0:154:4d5b:2006 with SMTP id n5-20020a170902e54500b001544d5b2006mr8165686plf.94.1649244661256; Wed, 06 Apr 2022 04:31:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649244661; cv=none; d=google.com; s=arc-20160816; b=G9iurgltomLJJeYK/j2HpwekaTtF1goh0n9Oe6PapOnX7kS7PPqYNBLOGGsSeiWfFO 2YE41IJJI+9O7hWzVB/uAAY+/kDok7fYZkMDwQgRHwM/ZhtWwufjy7fCLsYJ2UIQuQ/F Ly4Jwa0Y5GAy7rHM0ddGwJzLmgnyP9B2gV7qJ6u8yMlsSL231S/oZzKxK+6sYA2DMto7 p/1N2GHtotN8P1s6YtSDngBeljq0j4aJ2RZZs4xCiV5a1OsAeMqLbYbdsW6Ub0WX4iYN gglEOP2JkRAwlTm2YCMpMcXZf/E6304zKmWPmwQB1I8UIpdoBgIIexM64s/IVaKpGEte gJng== 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=TeDRNcLHsig4VTjL/I4luxXM1U5HsN1MD+bpqVfNbVM=; b=abomWE2D3CVs7cbyVQpvSqrDM3dc98V2YDklu0OyJ8mzT4rUgu2ckjkPAED2aRXytW wV1Q7S9gQiSLenO8JOAvpqXVj3mTzgUQ/A9r7QDdUduUAK2H7OyTTLRrkISDSZZT0Ij0 EB4MtefO7focpldPjmHIj8x/esKikDLfr/6U+IOn65jr1+co2coIe55nZIHM+kGnEx48 iXuq53YXvna2jx1RbWxYES3FGVUyVIkunmVkRnx/S9ZwWxiIC0xgJFZah0/j935xO2tR b0IXQvgBdpofUOwmIrDuTsrlde3RsEgfL5m/EIsv2KbpPSFUKk+ngOREvlMR1wAXtBcY hXXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1+vJ5Daj; 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 y35-20020a056a00182300b004fa3a8e0099si17905237pfa.336.2022.04.06.04.30.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 04:31:01 -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=1+vJ5Daj; 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 18ECF558316; Wed, 6 Apr 2022 02:52:57 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1453467AbiDEULR (ORCPT + 99 others); Tue, 5 Apr 2022 16:11:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358202AbiDEK2F (ORCPT ); Tue, 5 Apr 2022 06:28:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3554DE9F; Tue, 5 Apr 2022 03:16:56 -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 41CAD61562; Tue, 5 Apr 2022 10:16:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52CCCC385A1; Tue, 5 Apr 2022 10:16:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649153815; bh=lnod+bwqkuNFIbknHIzQi2XPw5to1kqtNKK3bniWJ/4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1+vJ5DajhgS+n7z6OD+3yhjMcMf1wSS+whORuDkVjaO6diWPFMjxANpiYWcEbAyf2 pY6jJyjVCVtnxfIVfU/s3VYKCGbsr8XVMscrZlQgzgBp44pnTSF/1MPocD5GF6BCoo NQu3KIsQum5QtDr3ABTCasSEqjpO3/OLWMrT8Hhw= 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.10 356/599] bpf, sockmap: Fix memleak in tcp_bpf_sendmsg while sk msg is full Date: Tue, 5 Apr 2022 09:30:50 +0200 Message-Id: <20220405070309.426596324@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@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 e4bb89599b44..545181a1ae04 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