Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4467442rwb; Mon, 8 Aug 2022 01:07:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR5QafRUnuScsO4nMFDnGyd/AmKJ96lrFJH1cEVSCnFtYxeJIiS7g364e4ev3WTbcoS75/Vo X-Received: by 2002:a05:6402:278c:b0:43d:cd35:db25 with SMTP id b12-20020a056402278c00b0043dcd35db25mr16683693ede.44.1659946055049; Mon, 08 Aug 2022 01:07:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659946055; cv=none; d=google.com; s=arc-20160816; b=Arlux6IoHdxLMgXcOXonoJAW5Q06szxuNFkJH2n+5W37dnuHJi036aZB8sSPUPwzC1 xZ4iZoBXFcOLxb4jZgAo9ytTu39ry7K5u3nha1nlDhtAcXA362kD3iEOKyR6fUwuziq7 1A4knVVVnpi9C6PwE1yXgt0APus57A7IkiPniWmJmVCbnD+d5gRxSp01byfkJgtvg2M9 BHS9QkcLtRNr6h4seEmEfb9pKtwv6evKASe5W9L5qEC6xXuZZnhazMi/bQHN6HPe0jwj hmChyseWq7YmXs8+C9ydFh9Ycmr29hD9xaG3K3efnJ+m370q1LhvqQP3bTkZiTO0+naG skXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=oC/JLDjGvWP0iiaK2n12rCrZ705odT+KLXUuwmVFqrk=; b=MeaSmaSDVlWVI6syD8R5SKTARLAGLnIx6AHP1IGqQ9Oj10P7IDt/T3smI+enenCDwn CUQ419aULrnSGQLJrFm2cYYQrL5R9lmyZRHI5K+mzh1zQp1xh7z1ePSiQ5n8rz6JnX0x 8Q4SxHCyBU0QKfuCyjg/0r9ST7qf9Y+8kahjCTHrFHNcHY2U4U23RmS7oEoEH/++qhaN T0FUOOmZ3HPeovMsWPccjH6CQy/Q+5nLI9BAESyIX5VO31Sm+GtnqLQPsPHezSNxVaZu Ft+o52w+N7UWuVvF281+eqp9gS8MOfwrvzIs3/m5vIZp+8M9Ukzpu9Yk5AcyhvSs9B5B Zzzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XkPX2y3D; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nb8-20020a1709071c8800b007308bb40f8bsi11520991ejc.198.2022.08.08.01.07.08; Mon, 08 Aug 2022 01:07:35 -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=@redhat.com header.s=mimecast20190719 header.b=XkPX2y3D; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241967AbiHHHzs (ORCPT + 99 others); Mon, 8 Aug 2022 03:55:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241993AbiHHHzq (ORCPT ); Mon, 8 Aug 2022 03:55:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 65EBF5FB5 for ; Mon, 8 Aug 2022 00:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659945344; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=oC/JLDjGvWP0iiaK2n12rCrZ705odT+KLXUuwmVFqrk=; b=XkPX2y3Di7PAF3CR4a1kFonl7N7m0o+1Pxif16pMeZ+I6NxqsuLf680g9EF+pZ1Op72i8W arYLHLD0o3UvdjHkWbtAgXrNA4YuJ4HJtC6LFSP8lk0Y/Jou7MKQk2BOX4Ye1FdRxjateN GYQ5wRbPht4AII1jBMM7/u93wgRBvMM= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-b9sHc5AcP-iTIB-lpSkyww-1; Mon, 08 Aug 2022 03:55:43 -0400 X-MC-Unique: b9sHc5AcP-iTIB-lpSkyww-1 Received: by mail-qv1-f69.google.com with SMTP id cv14-20020ad44d8e000000b004760bec67a8so4071939qvb.14 for ; Mon, 08 Aug 2022 00:55:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=oC/JLDjGvWP0iiaK2n12rCrZ705odT+KLXUuwmVFqrk=; b=u+BqovN3Vj8NhCSBptmircRzewfbKqErrJyGjsO+jKNvy1eW0t/xaQVX7p/VYmxUKQ jMPCD890s9/cyVDKMe/v+dEFv1GcKOj42pdxIW4GG4PKhnhPHL/+AEVwW2ndfYRRTNdP NrfZ85g8TjF7u6EkX188D1T/6SMgdH4izHXyrmxQSge6z9S/AQazalayRCnL76HBQFyx ahQ7ywS2mHHEhvbv7EN+cIVhYOwcli1fuqC9eZtQH5bNr8Z6b0lTz3UGuW57Rzqwlid0 Ty9T7yPWiTEoh/0032BTZqeTJ2cHA7y3htJ2woMdzmrnLeg6tuOQBRJGUZaDCQru6otg Gsqw== X-Gm-Message-State: ACgBeo0b6eS3eOIi+WQgztydmn//tawyEkLQfk1bi7rA5zpFlAJo1gvh ZAjhGC7v3L0jUHyhLRRX0mP8S4prZ/ky2H4A72Xpt2kXKNuCqTP23E1minIGo2c0njsN+f2Uqte WyuXUvpolwUk8yF7QHjQmGMC7 X-Received: by 2002:a05:620a:1706:b0:6b9:234:f735 with SMTP id az6-20020a05620a170600b006b90234f735mr13305788qkb.623.1659945342748; Mon, 08 Aug 2022 00:55:42 -0700 (PDT) X-Received: by 2002:a05:620a:1706:b0:6b9:234:f735 with SMTP id az6-20020a05620a170600b006b90234f735mr13305776qkb.623.1659945342506; Mon, 08 Aug 2022 00:55:42 -0700 (PDT) Received: from sgarzare-redhat (host-79-46-200-178.retail.telecomitalia.it. [79.46.200.178]) by smtp.gmail.com with ESMTPSA id w20-20020a05620a0e9400b006b8f8e9bd00sm8365097qkm.5.2022.08.08.00.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 00:55:41 -0700 (PDT) Date: Mon, 8 Aug 2022 09:55:33 +0200 From: Stefano Garzarella To: Peilin Ye Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Peilin Ye , George Zhang , Dmitry Torokhov , Andy King , virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH net v2 1/2] vsock: Fix memory leak in vsock_connect() Message-ID: <20220808075533.p7pczlnixb2phrun@sgarzare-redhat> References: <20220804020925.32167-1-yepeilin.cs@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,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 On Sun, Aug 07, 2022 at 02:00:11AM -0700, Peilin Ye wrote: >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 >Signed-off-by: Peilin Ye >--- >change since v1: > - merged with Stefano's patch [1] > >[1] https://gitlab.com/sgarzarella/linux/-/commit/2d0f0b9cbbb30d58fdcbca7c1a857fd8f3110d61 > >Hi Stefano, > >About the Fixes: tag, [2] introduced @connect_work, but all it did was >breaking @dwork into two and moving some INIT_DELAYED_WORK()'s, so I don't >think [2] introduced this memory leak? > >Since [2] has already been backported to 4.9 and 4.14, I think we can >Fixes: commit d021c344051a ("VSOCK: Introduce VM Sockets"), too, to make >backporting easier? Yep, I think it should be fine! > >[2] commit 455f05ecd2b2 ("vsock: split dwork to avoid reinitializations") > >Thanks, >Peilin Ye > > net/vmw_vsock/af_vsock.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > >diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c >index f04abf662ec6..fe14f6cbca22 100644 >--- a/net/vmw_vsock/af_vsock.c >+++ b/net/vmw_vsock/af_vsock.c >@@ -1391,7 +1391,13 @@ 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)) ^ Checkpatch warns here about line lenght. If you have to re-send, please split it. Anyway, the patch LGTM: Reviewed-by: Stefano Garzarella Thanks, Stefano