Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4300097pxf; Tue, 23 Mar 2021 07:35:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwEXZKE9gTI8oMJp+cB8KzIgiYoaZB2vMCEPJiw6EMCU2kjFAIgHM7BsocQ8Mh0jJRC5JCJ X-Received: by 2002:a17:906:5e55:: with SMTP id b21mr5206834eju.289.1616510152266; Tue, 23 Mar 2021 07:35:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616510152; cv=none; d=google.com; s=arc-20160816; b=OSSNyjC6GbSOdz/uM804BnrjERZ15fHx1pWiUjG4+2b8m/mWulbzy9nOUQTnAKdAjA hi11Ebh2r2ssrh/tnb4V+LQr4oga+5cBXUg9+oE7LJxwX1gV7JeYMeBYuZNOaA+3VAi/ ySeOFwS8hsTrP9AC6THe5azFnJ7/UagcqkrZ/c9zABfv2pBkwNMxZuhxhEuFtfrY22Vg af/58Fz/yI4Q2xrW0JZKCm9WFYQ0RJQLQd6XqHQ7+goRTW+RsmP9kVbxFWeD2JkEperz TuczOKE5mJyGyNTZvcTT39MYA1u0EvuP37TpCTRbnn8h99up+CEqJcdPOkamkhx7krZc WZ/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=0Jq/F710yph7giIFuOon0hHnQr6Etp9FxSOgX4AsDyE=; b=QwWro7WLBFPxGGMHrJ4hTcoABPQRRRgsVDHR9c/NRW8wLeFgTAkws9xWpmiXzKrs5k PxXREm0LK1FYTE16/y0hUcGUkpsWnEkopP2OOjVFExcU7pbO2G9ia6vNNcaYNxpaqztV 36sykBxvivM4fHti+bao7O6XsqDUdLz4utvHE/vAiQVP0z+u/g/MKji8dcUhiX6UBCcH KgAuVKpkZsuZXnoJlR2NgxoJOjf+ntflHcVe9ZGLTQJh6fcR7nIz0Yc9BSaw369JpIw5 kZBY9kxzocFgV7EDFtNzAm8kEuVFKnUiztBeI2WwUOLOM/U352dmcGc0s3Y6Qy0LAhBY 4/JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RKoYtfx+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mj17si13373824ejb.28.2021.03.23.07.35.28; Tue, 23 Mar 2021 07:35:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RKoYtfx+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S232332AbhCWOeb (ORCPT + 99 others); Tue, 23 Mar 2021 10:34:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:52736 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232000AbhCWOdy (ORCPT ); Tue, 23 Mar 2021 10:33:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616510034; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0Jq/F710yph7giIFuOon0hHnQr6Etp9FxSOgX4AsDyE=; b=RKoYtfx+3+DaLUBPw8xDzwKhGPY2qqm6ZEpwREjGmIf8I41SscW8dKkJYa6mFu9Hse22lp f9r/pNDc+1HQuj4G2b/NipoeB3A+q7OclCzGCXfy4Y9kvbVdAlc22Cy91zp50LPi/x13Dt 6E5Afzj3TSPDj50THJUBwNur+4sxdvY= Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-565-EBdiPE7SNb-JLl5xgFi_iA-1; Tue, 23 Mar 2021 10:33:52 -0400 X-MC-Unique: EBdiPE7SNb-JLl5xgFi_iA-1 Received: by mail-ot1-f69.google.com with SMTP id c21so1196604oto.18 for ; Tue, 23 Mar 2021 07:33:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=0Jq/F710yph7giIFuOon0hHnQr6Etp9FxSOgX4AsDyE=; b=fAx7+r/zWEiZRbDuT2fMQ+Xc4QTihxibKWuFm7inAqTJgCGc1WMMlWvY0UHnXG3o7e +UHBD804Or8cSMYqE2PYRIlsZw4r2DADdteZV+h7Y6jm9fA8fM2SjbQ85vHXYqLWVskE c4a7D7xNmQFt649Z3sOBqGO0o8G28C0WXebd+gzijy6kfJzk97we1LMgAdg+tdYhTvbd poF9A1mztxBbk/4xowBVoG6v9GqodNyM7h97Q/olynvupVV24X87CUk4L+/m2NHXI6w1 cc5hYoYWWYbJwNYPzDPmBWzEpoeoAN4qd6WsMl5hMK+uswRZAQ/ZlW+Q6lkSQCLJp90e NXqA== X-Gm-Message-State: AOAM533GirFsDk3TPCmdNYeM3xHZjj9TXGw990yHUfRLxMjka3XAjJ/u RA9Xk+ZQet5LHOk5UY/c8iXQhdgu9OVTPtKybq24C2XGfCnZmMxwc5svwtfuzepdqMf+2/ri249 tsJ5fym9N1UnFqRkpceQo5TvYKwrv6knGSBG3W88AkTtyJxhIcAOdKeimSJSJC2LJFsQbS0egzQ == X-Received: by 2002:aca:4a95:: with SMTP id x143mr3509569oia.59.1616510031286; Tue, 23 Mar 2021 07:33:51 -0700 (PDT) X-Received: by 2002:aca:4a95:: with SMTP id x143mr3509539oia.59.1616510030878; Tue, 23 Mar 2021 07:33:50 -0700 (PDT) Received: from [192.168.0.173] (ip68-103-222-6.ks.ok.cox.net. [68.103.222.6]) by smtp.gmail.com with ESMTPSA id a13sm3696881ooj.14.2021.03.23.07.33.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Mar 2021 07:33:50 -0700 (PDT) Subject: Re: [PATCH] fuse: Fix a potential double free in virtio_fs_get_tree To: Lv Yunlong , vgoyal@redhat.com, stefanha@redhat.com, miklos@szeredi.hu Cc: virtualization@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org References: <20210323051831.13575-1-lyl2019@mail.ustc.edu.cn> From: Connor Kuehl Message-ID: Date: Tue, 23 Mar 2021 09:33:49 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: <20210323051831.13575-1-lyl2019@mail.ustc.edu.cn> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/23/21 12:18 AM, Lv Yunlong wrote: > In virtio_fs_get_tree, fm is allocated by kzalloc() and > assigned to fsc->s_fs_info by fsc->s_fs_info=fm statement. > If the kzalloc() failed, it will goto err directly, so that Right, I follow this so far. > fsc->s_fs_info must be non-NULL and fm will be freed. But this I don't follow in the context of the stuff that happens in out_err. > But later fm is freed again when virtio_fs_fill_super() fialed. > I think the statement if (fsc->s_fs_info) {kfree(fm);} is > misplaced. I'm not sure this can double free, because: * If fm = kzalloc[..] fails, the function bails early. * If sget_fc() fails, the function cleans up fm and fc and bails early. * If sget_fc() succeeds and allocated a new superblock, fc->s_fs_info pointer is moved to sb->s_fs_info and fc->s_fs_info is set to NULL, so the first free hasn't happened yet. * If sget_fc() succeeds and somehow returns an existing superblock (which I think is tested by checking if fc->s_fs_info is not NULL, since otherwise it'd have been moved to the superblock and set to NULL in sget_fc), I think sb->s_root would not be NULL, therefore the flow of control wouldn't enter the if-block where virtio_fs_fill_super could fail which means the code won't reach the double free. That's just my reading of it though, and I'm wondering if that makes sense to others :-) One last comment inline: > My patch puts this statement in the correct palce to avoid > double free. > > Signed-off-by: Lv Yunlong > --- > fs/fuse/virtio_fs.c | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c > index 8868ac31a3c0..727cf436828f 100644 > --- a/fs/fuse/virtio_fs.c > +++ b/fs/fuse/virtio_fs.c > @@ -1437,10 +1437,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc) > > fsc->s_fs_info = fm; > sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc); > - if (fsc->s_fs_info) { > - fuse_conn_put(fc); > - kfree(fm); > - } > + > if (IS_ERR(sb)) > return PTR_ERR(sb); By removing the check from here, it now looks like if sget_fc() fails, then this early return will leak fm's memory and fc's reference. Connor > > @@ -1457,6 +1454,11 @@ static int virtio_fs_get_tree(struct fs_context *fsc) > sb->s_flags |= SB_ACTIVE; > } > > + if (fsc->s_fs_info) { > + fuse_conn_put(fc); > + kfree(fm); > + } > + > WARN_ON(fsc->root); > fsc->root = dget(sb->s_root); > return 0; >