Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3468602rwa; Tue, 23 Aug 2022 05:23:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR6j9+mCccQbKcHRJHF7EdpoIduIKW1I/RdnGc+hCrtrU0IbmBJu1IMK2HAbd0BmzfrUhuc/ X-Received: by 2002:a17:907:8317:b0:731:2189:7f4d with SMTP id mq23-20020a170907831700b0073121897f4dmr15928617ejc.468.1661257399359; Tue, 23 Aug 2022 05:23:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661257399; cv=none; d=google.com; s=arc-20160816; b=njcX1Yy1HzhswqTm+YUnXaUI2COSFKM0nmpLqoLtvlNRuO28LnEk272/Mk3BeGlK0v y8GJR2l/qnnedidfK5IeziinnQWz4VXHa69BOdsGcwPksZHSFPzfCXe4S0/fF5L8wRkh vmjGyiYGNfHmk/5rqzAG8FCvv8S8i01MWKb4pn5NN8p5z6t5v9dyPyUmT6uEmXxprscH kkSVZ667QARdVmXooiWXyjy0fOQ2U9S87bOBqhseQseJBTiEOGru+97bly9TWRRjHvPW ldEL9cYQiq9K9ysXX0i3K8gy8Ilsf4fAwCqtgFzEjPLcI+lYM7zcPkus4Uczy7OGElhd T8VQ== 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=9dj2+7ucmPH2itaTO5HRsMx6iq1MIjMzsF0m0VatmbU=; b=PgjMAhpFNQk3TSGPCBeuHqGkrzdokIwOCcWeSKswnDg7d48raYq6IiWd1md/1EyjcH jQP4tV9NgyiNuuVfTA9nT6OLyR61sC1kQZWI60I6xvt+0v4oOzMZ75oOX6JLxTcJRPv2 JJPcB839AYFil59LFH/ATEt0Te6QGEwMppAOSx0NVuJqD+oyUBvNJNt3yoilbj36kmX+ /1oaFqXIz8r8ZSKC0OlKnED15/8ivnhkAxDvvbp0buw2EBSmcUL6ir442BcsvNS6tuRk R+QWMdguC5WQzcafoCPsZIubl0OTPd4Dc5a2wgq2A1I+3BcGK9cGASaQe8OUDrcqDmGu OtAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=P2XM2iWH; 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 cs10-20020a170906dc8a00b00730672860bdsi12110751ejc.123.2022.08.23.05.22.53; Tue, 23 Aug 2022 05:23:19 -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=P2XM2iWH; 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 S1357007AbiHWK4O (ORCPT + 99 others); Tue, 23 Aug 2022 06:56:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356545AbiHWKvK (ORCPT ); Tue, 23 Aug 2022 06:51:10 -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 81410AB4CE; Tue, 23 Aug 2022 02:12:44 -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 C1FAA60DB4; Tue, 23 Aug 2022 09:12:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEBD5C433D7; Tue, 23 Aug 2022 09:12:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661245963; bh=QXVVy9ogRtOgoitRqY+eqdk8CN1f2+haTzgyHkGY9K0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P2XM2iWHJzScaDvkMeZmuyQC0E5xoI89lHLHNJ6KVrm7Zoldkib2nhLvcZ978lLUa gMI+/135XX+6vegSGoySd7iEJ0RhKtSkB7epF1rNqGn8vSiTfjxT/173o2PVgJoQ0Q DZ5U0xvsZj8Wsspo0H4QxLrKM252n4SYhujV4JfM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stefano Garzarella , Peilin Ye , "David S. Miller" , syzbot+b03f55bf128f9a38f064@syzkaller.appspotmail.com Subject: [PATCH 4.19 238/287] vsock: Fix memory leak in vsock_connect() Date: Tue, 23 Aug 2022 10:26:47 +0200 Message-Id: <20220823080109.079929857@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080100.268827165@linuxfoundation.org> References: <20220823080100.268827165@linuxfoundation.org> User-Agent: quilt/0.67 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: Peilin Ye commit 7e97cfed9929eaabc41829c395eb0d1350fccb9d upstream. 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 Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/vmw_vsock/af_vsock.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1215,7 +1215,14 @@ static int vsock_stream_connect(struct s * 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;