Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp5053586rwb; Mon, 8 Aug 2022 11:16:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR75BYXQmaSZyXFBIb3sRq5yVV7wNJxA9woIdCN4vQtFVBCTQkXK7AuPqKTscPM2ojnlXRFf X-Received: by 2002:a05:6402:4cc:b0:43c:cd5c:dcfb with SMTP id n12-20020a05640204cc00b0043ccd5cdcfbmr18118802edw.277.1659982616568; Mon, 08 Aug 2022 11:16:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659982616; cv=none; d=google.com; s=arc-20160816; b=Oiq6hZro6sf0mU6LgzA/TvEwk3U5/HNvN2o7sJTyaKwRLsMh8wuQxGDJAt/44oscij O5+mxOj6J9EyjUgmg2xaGz5l7whY5gYzx0mMknQr4h9u46zDnP9ETDO8e++l0QKWE/pH pkD+Wkf6yNWVNHOqBZsSfDYQPYnTw2IozZIJTAhBtSa5GKI2Oq6K0kn74b0fXNEjf0RG B4mwKuiVa+igy2NBfpAsjM9yvdgf185ZZgzqn+ig/e3gbUm/zKOpnkLlFoVL1Bx4Abdx GNOvGCwCv+5h074MaztqaZLJ/Rhboe1dCF6QGE8PnHohPkD77i7RvBABW0Bf9zuk2U2Q rkoA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZbLrnLtwu2sUASVZtr1avkLgUbGCKkScbBilSB2wV4E=; b=cqQV2/eE+xc0iMRQmP9pc7U1RjDQosrff18jjzh1B7CTFakaV+3UxEtA8VU5uomW1S Iyg5CDA07ZWOq60qILl3+qz5RRTb6LhGrLhbyK9bzMJw9mzhCK+O7eDdlUSZmESGvnr+ r2CabjhdpjkSW+54rj4w76tTqYW8OmZudQP8EatN58dN5Lf6wBtgj6K107ukY4Kf9NcI TL/I28WIHRfSwzZDcudzEXFZrg8ANlrJYGOBh4Z+PG1IPZfU5GqKOj4Q5bH2r3uU4mhJ VjgVf13CaIHrQlIHC9/lUhY7dlDklsB+Jqm/cfXA7WqrGYxZx27wcfzCLAs71j36NBjM SWZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=g2qWbHHd; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c7-20020a05640227c700b0043ce06640e3si7672151ede.351.2022.08.08.11.16.29; Mon, 08 Aug 2022 11:16:56 -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=@gmail.com header.s=20210112 header.b=g2qWbHHd; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243599AbiHHSFW (ORCPT + 99 others); Mon, 8 Aug 2022 14:05:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243854AbiHHSFT (ORCPT ); Mon, 8 Aug 2022 14:05:19 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 089E810C1; Mon, 8 Aug 2022 11:05:18 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id o3-20020a17090a0a0300b001f7649cd317so2213903pjo.0; Mon, 08 Aug 2022 11:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZbLrnLtwu2sUASVZtr1avkLgUbGCKkScbBilSB2wV4E=; b=g2qWbHHd1FXZIKUtYFs2WlBkZHff24nK4BN4Aj8C5hk1GbGp0HdKFP5HPQKDA781lP naJOwJVf2UJrbIyid9reQzwM6Xfai0MqsBjc3mERpgrw4oy1CyJ+KCka68DwBbrxejyG 26uO8Y+BUOmnp2K+/aXKpGhDlXL58xiJk0HkUaXtuvCJur9txplAwGFOLwaV+YsVSUUd y3sIJsp+dc8SQ8dChsdw9vy15iksMEPm91m4Vzq8o+obvJ5+JOeT/OWM/NrNjV7LAK70 J2kSKkAQSrRnsHhcbdsoGyuPY29monns2UAdG2NmIp/18pDrDRiDS6vHs/swOHauVt4R E1rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZbLrnLtwu2sUASVZtr1avkLgUbGCKkScbBilSB2wV4E=; b=uc7VdJnVhFuLiS9EsNY6uCT1rbP7W9GlCcUu0nKZllxAeFbuIgbinvJD2ZFITdcf+Q /h0+RqU6Vr32LU71uZRsaXXPJlXvBKosl7q6LmStW8EqT5ABggxfq0n1fCD+dMiQhamH pkwl8xn+66EyKvuCM+6qdVdmPkickP78jOCL2W73gXQrhnIwdSzUqnhm4RQsL1PDnZgI j+6qWEVw5iEvTn5oKJB8JsSHSANqO4C7VWs/7bZ0UrlcsN7gwMrvUPy4o1KtciYjIePJ ej4rdBT4rFEO4S/w3WRZIRYe8lfUGGGXykEWruE7vAOXaMd1PtLAqZ2hq9UwTwzLeiqp USVA== X-Gm-Message-State: ACgBeo3MseMp9yhmL/gqokHumMCI5Mz9CA7OeLCi7FhmKS4lJVC7zpgX Qp0dmekT97N50PbI9hr6GA== X-Received: by 2002:a17:902:ecc7:b0:16e:ff60:4286 with SMTP id a7-20020a170902ecc700b0016eff604286mr18867545plh.28.1659981917503; Mon, 08 Aug 2022 11:05:17 -0700 (PDT) Received: from bytedance.bytedance.net ([74.199.177.246]) by smtp.gmail.com with ESMTPSA id a8-20020a170902ecc800b0016c5306917fsm9251785plh.53.2022.08.08.11.05.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 11:05:17 -0700 (PDT) From: Peilin Ye To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Peilin Ye , George Zhang , Dmitry Torokhov , Andy King , virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Peilin Ye Subject: [PATCH net v3 1/2] vsock: Fix memory leak in vsock_connect() Date: Mon, 8 Aug 2022 11:04:47 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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: Peilin Ye An O_NONBLOCK vsock_connect() request may try to reschedule @connect_work. Imagine the following sequence of vsock_connect() requests: 1. The 1st, non-blocking request schedules @connect_work, which will expire after 200 jiffies. Socket state is now SS_CONNECTING; 2. Later, the 2nd, blocking request gets interrupted by a signal after a few jiffies while waiting for the connection to be established. Socket state is back to SS_UNCONNECTED, but @connect_work is still pending, and will expire after 100 jiffies. 3. Now, the 3rd, non-blocking request tries to schedule @connect_work again. Since @connect_work is already scheduled, schedule_delayed_work() silently returns. sock_hold() is called twice, but sock_put() will only be called once in vsock_connect_timeout(), causing a memory leak reported by syzbot: BUG: memory leak unreferenced object 0xffff88810ea56a40 (size 1232): comm "syz-executor756", pid 3604, jiffies 4294947681 (age 12.350s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 28 00 07 40 00 00 00 00 00 00 00 00 00 00 00 00 (..@............ backtrace: [] sk_prot_alloc+0x3e/0x1b0 net/core/sock.c:1930 [] sk_alloc+0x32/0x2e0 net/core/sock.c:1989 [] __vsock_create.constprop.0+0x38/0x320 net/vmw_vsock/af_vsock.c:734 [] vsock_create+0xc1/0x2d0 net/vmw_vsock/af_vsock.c:2203 [] __sock_create+0x1ab/0x2b0 net/socket.c:1468 [] sock_create net/socket.c:1519 [inline] [] __sys_socket+0x6f/0x140 net/socket.c:1561 [] __do_sys_socket net/socket.c:1570 [inline] [] __se_sys_socket net/socket.c:1568 [inline] [] __x64_sys_socket+0x1a/0x20 net/socket.c:1568 [] do_syscall_x64 arch/x86/entry/common.c:50 [inline] [] do_syscall_64+0x35/0x80 arch/x86/entry/common.c:80 [] entry_SYSCALL_64_after_hwframe+0x44/0xae <...> Use mod_delayed_work() instead: if @connect_work is already scheduled, reschedule it, and undo sock_hold() to keep the reference count balanced. Reported-and-tested-by: syzbot+b03f55bf128f9a38f064@syzkaller.appspotmail.com Fixes: d021c344051a ("VSOCK: Introduce VM Sockets") Co-developed-by: Stefano Garzarella Signed-off-by: Stefano Garzarella Reviewed-by: Stefano Garzarella Signed-off-by: Peilin Ye --- change since v2: - wrapped long line (Stefano) change since v1: - merged with Stefano's patch [1] [1] https://gitlab.com/sgarzarella/linux/-/commit/2d0f0b9cbbb30d58fdcbca7c1a857fd8f3110d61 net/vmw_vsock/af_vsock.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index f04abf662ec6..4d68681f5abe 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1391,7 +1391,14 @@ static int vsock_connect(struct socket *sock, struct sockaddr *addr, * timeout fires. */ sock_hold(sk); - schedule_delayed_work(&vsk->connect_work, timeout); + + /* If the timeout function is already scheduled, + * reschedule it, then ungrab the socket refcount to + * keep it balanced. + */ + if (mod_delayed_work(system_wq, &vsk->connect_work, + timeout)) + sock_put(sk); /* Skip ahead to preserve error code set above. */ goto out_wait; -- 2.20.1