From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8DF1ECAAD8 for ; Fri, 16 Sep 2022 06:39:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230044AbiIPGjq (ORCPT ); Fri, 16 Sep 2022 02:39:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230059AbiIPGjj (ORCPT ); Fri, 16 Sep 2022 02:39:39 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 151DFF32 for ; Thu, 15 Sep 2022 23:39:34 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id dv25so47145951ejb.12 for ; Thu, 15 Sep 2022 23:39:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=vvA/mvvSLM9uLQG38TGu9Y+RlTnepBsr7g4wPM9aIIg=; b=DgDKo6ADvXbPoPcmmuk1X2YQw1DXiBMf8Wx6bOA2cU2eRNOQ0u0EaL1/DyZ8BV67Ne 7EyAkjE2z9ASNk/HI9NC+4QGPTA0uZGcqxRLR47Xi6D2gfNn+rDgKAX3JBhgUECuG6NC M2h4GfReBpb+z+8CpedpYrrb4RrkIMF/zokBZqBWxsaJOcQE9Wj1MW8Ep7uvLNkNrjRr LiMXceubitfvKGEcSqwNWzhg5ZY6x+4KJTflmP98wCKJhsJ4hSoKJXn/MwV8hLbgCtV2 TuuAo9TXwA15ivLXkcbeY3s7opDXWOLMxSQnZGsVPADIIFfq6AVkfYXScjabJcs18OV7 v7uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=vvA/mvvSLM9uLQG38TGu9Y+RlTnepBsr7g4wPM9aIIg=; b=fb5duQZ471gSme8s7oy4SjNBaxvsHzwer1XggjjlrRUlqd9flvVGyMASonzbbJIGfu 69CwZYdTw+noDMOxKOCx+n9n/nCP0x5HBFtfPvJYQPHQCJ+tFNT9dNvPMYelGycrA4Ll NDs2IwwiKWv7YvN0AoQ/R/PlnVt/+Zm377+wBVO/LbTZ2ASrjzgqE16610ddY8k964IO DQ3u6B4KxlLk8QzZGoxrJjI0b3bHo6Mh7nn5y5pyWMiyz+OmdWR4Xfxf18Oi/zWV3sbd o53DVFQtBQ+UV68pcg4QwlO+sp3778qX/QhEAGrtJfiT5/ubEKfTBb0Df9HQLHS+v1kl aZTQ== X-Gm-Message-State: ACrzQf3xl6oKqdUUJtjr8kOjjuIt6tGb0nyLQ82u+8L9BpxNVI0KcgKX B3I48zxv7NHWhASoQ8DiChlA9VgKHxVY3agA1ms= X-Google-Smtp-Source: AMsMyM5N+6CjojnRxi41Vb0thsccYyyy86S95n9bkg3sqD6CX1INX3Rhkhf3WeV2G4qTgk896k3SrhrL3y8zAMOc3JM= X-Received: by 2002:a17:907:970a:b0:77d:a10c:e089 with SMTP id jg10-20020a170907970a00b0077da10ce089mr2526092ejc.364.1663310373366; Thu, 15 Sep 2022 23:39:33 -0700 (PDT) MIME-Version: 1.0 References: <20220907030754.GU1089@zhen-hp.sh.intel.com> In-Reply-To: From: Zheng Hacker Date: Fri, 16 Sep 2022 14:39:21 +0800 Message-ID: Subject: Re: [PATCH] drm/i915/gvt: fix double-free bug in split_2MB_gtt_entry. To: Greg KH Cc: Zhenyu Wang , "alex000young@gmail.com" , "security@kernel.org" , "dri-devel@lists.freedesktop.org" , "tvrtko.ursulin@linux.intel.com" , "airlied@linux.ie" , "intel-gfx@lists.freedesktop.org" , "joonas.lahtinen@linux.intel.com" , "linux-kernel@vger.kernel.org" , xmzyshypnc <1002992920@qq.com>, "jani.nikula@linux.intel.com" , "daniel@ffwll.ch" , "rodrigo.vivi@intel.com" , "intel-gvt-dev@lists.freedesktop.org" , "zhi.a.wang@intel.com" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >From 8d95c1399e3ff345500a575e21254a73b0c89144 Mon Sep 17 00:00:00 2001 From: xmzyshypnc <1002992920@qq.com> Date: Fri, 16 Sep 2022 14:37:48 +0800 Subject: [PATCH] drm/i915/gvt: fix double-free bug in split_2MB_gtt_entry There is a double-free security bug in split_2MB_gtt_entry. Here is a calling chain : ppgtt_populate_spt->ppgtt_populate_shadow_entry->split_2MB_gtt_entry. If intel_gvt_dma_map_guest_page failed, it will call ppgtt_invalidate_spt, which will finally call ppgtt_free_spt and kfree(spt). But the caller does not notice that, and it will call ppgtt_free_spt again in error path. Fix this by only freeing spt in ppgtt_invalidate_spt in good case. Signed-off-by: xmzyshypnc <1002992920@qq.com> --- drivers/gpu/drm/i915/gvt/gtt.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.= c index 9f14fded8c0c..31d2a8d56384 100644 --- a/drivers/gpu/drm/i915/gvt/gtt.c +++ b/drivers/gpu/drm/i915/gvt/gtt.c @@ -959,7 +959,7 @@ static inline int ppgtt_put_spt(struct intel_vgpu_ppgtt_spt *spt) return atomic_dec_return(&spt->refcount); } -static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt); +static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *sptm, int is_error); static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *e) @@ -995,7 +995,7 @@ static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, ops->get_pfn(e)); return -ENXIO; } - return ppgtt_invalidate_spt(s); + return ppgtt_invalidate_spt(s, 0); } static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, @@ -1016,7 +1016,7 @@ static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, intel_gvt_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT); } -static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) +static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt, int is_e= rror) { struct intel_vgpu *vgpu =3D spt->vgpu; struct intel_gvt_gtt_entry e; @@ -1059,9 +1059,11 @@ static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) } } - trace_spt_change(spt->vgpu->id, "release", spt, + if (!is_error) { + trace_spt_change(spt->vgpu->id, "release", spt, spt->guest_page.gfn, spt->shadow_page.type); - ppgtt_free_spt(spt); + ppgtt_free_spt(spt); + } return 0; fail: gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n", @@ -1215,7 +1217,7 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgp= u, ret =3D intel_gvt_dma_map_guest_page(vgpu, start_gfn + sub_index, PAGE_SIZE, &dma_addr); if (ret) { - ret =3D ppgtt_invalidate_spt(spt); + ret =3D ppgtt_invalidate_spt(spt, 1); return ret; } sub_se.val64 =3D se->val64; @@ -1393,7 +1395,7 @@ static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt, ret =3D -ENXIO; goto fail; } - ret =3D ppgtt_invalidate_spt(s); + ret =3D ppgtt_invalidate_spt(s, 0); if (ret) goto fail; } else { --=20 2.25.1 Zheng Hacker =E4=BA=8E2022=E5=B9=B49=E6=9C=888= =E6=97=A5=E5=91=A8=E5=9B=9B 19:59=E5=86=99=E9=81=93=EF=BC=9A > > Hi Greg, > > I got it, Greg. > > Mid-Autumn Festival is coming and I will have a couple of days off. > I'll see what I can do after holiday :) > > Regards, > > Zheng Wang > > =E5=9C=A8 2022=E5=B9=B49=E6=9C=888=E6=97=A5=E6=98=9F=E6=9C=9F=E5=9B=9B=EF= =BC=8CGreg KH =E5=86=99=E9=81=93=EF=BC=9A >> >> On Thu, Sep 08, 2022 at 05:09:40PM +0800, Zheng Hacker wrote: >> > Hi Zhenyu, >> > >> > This issue has been open for a few days. Could you plz write a patch >> > for that :) I'm not familiar with the logical code here. >> >> As this is only able to be hit in a theoretical system, it isn't that >> high of a priority, if any priority at all. Why not try to write a >> patch for it yourself to help resolve the issue faster? >> >> thanks, >> >> greg k-h From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D93CEC54EE9 for ; Fri, 16 Sep 2022 06:39:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EF90B10ECC2; Fri, 16 Sep 2022 06:39:37 +0000 (UTC) Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by gabe.freedesktop.org (Postfix) with ESMTPS id 000BB10ECC2; Fri, 16 Sep 2022 06:39:34 +0000 (UTC) Received: by mail-ej1-x631.google.com with SMTP id r18so47205078eja.11; Thu, 15 Sep 2022 23:39:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=vvA/mvvSLM9uLQG38TGu9Y+RlTnepBsr7g4wPM9aIIg=; b=DgDKo6ADvXbPoPcmmuk1X2YQw1DXiBMf8Wx6bOA2cU2eRNOQ0u0EaL1/DyZ8BV67Ne 7EyAkjE2z9ASNk/HI9NC+4QGPTA0uZGcqxRLR47Xi6D2gfNn+rDgKAX3JBhgUECuG6NC M2h4GfReBpb+z+8CpedpYrrb4RrkIMF/zokBZqBWxsaJOcQE9Wj1MW8Ep7uvLNkNrjRr LiMXceubitfvKGEcSqwNWzhg5ZY6x+4KJTflmP98wCKJhsJ4hSoKJXn/MwV8hLbgCtV2 TuuAo9TXwA15ivLXkcbeY3s7opDXWOLMxSQnZGsVPADIIFfq6AVkfYXScjabJcs18OV7 v7uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=vvA/mvvSLM9uLQG38TGu9Y+RlTnepBsr7g4wPM9aIIg=; b=sH0iiANQ9JUvQ2YwT5cmWZzSoX3GESe+eFaNZ2AS7JmNJHiqXBvbW2k3AWxlKMhOwR Jnq/sO4fkboWQgv+aAviYlnPdz2P+pIympxwXiSNXEWYhBzBEHSR6lkxRu0UsLrjZgMr Vpw/CtJMBrYcDZQd1xy/3l63C00b55ajUC39nqMY8Mj1sToJ9zg56/dN5XoLQ9/xprya lbJ5AlQjv6wuPWsTbao2qyY8lZP+MgxKwMHXK+1hITkrAZU2P2j0i/kLw0yV1Bu8eLQX Hx78rM+j68Ajrd310cNZLBL2TXGIdcVCrZH/D40oEO0hmr4hGN8JyyYI8wTaSmTe3ntu vx1w== X-Gm-Message-State: ACrzQf0HmUFIlVsvr49nKnwPUPdDyRVe3qOMlzMejG9Jew6/y1k9twVk 6UUUnVL39nUgplYAg/EL3khjZsTcJcIW+tilmlw= X-Google-Smtp-Source: AMsMyM5N+6CjojnRxi41Vb0thsccYyyy86S95n9bkg3sqD6CX1INX3Rhkhf3WeV2G4qTgk896k3SrhrL3y8zAMOc3JM= X-Received: by 2002:a17:907:970a:b0:77d:a10c:e089 with SMTP id jg10-20020a170907970a00b0077da10ce089mr2526092ejc.364.1663310373366; Thu, 15 Sep 2022 23:39:33 -0700 (PDT) MIME-Version: 1.0 References: <20220907030754.GU1089@zhen-hp.sh.intel.com> In-Reply-To: From: Zheng Hacker Date: Fri, 16 Sep 2022 14:39:21 +0800 Message-ID: Subject: Re: [PATCH] drm/i915/gvt: fix double-free bug in split_2MB_gtt_entry. To: Greg KH Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "alex000young@gmail.com" , "security@kernel.org" , "tvrtko.ursulin@linux.intel.com" , "airlied@linux.ie" , "intel-gfx@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , xmzyshypnc <1002992920@qq.com>, "rodrigo.vivi@intel.com" , "intel-gvt-dev@lists.freedesktop.org" , "zhi.a.wang@intel.com" Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" >From 8d95c1399e3ff345500a575e21254a73b0c89144 Mon Sep 17 00:00:00 2001 From: xmzyshypnc <1002992920@qq.com> Date: Fri, 16 Sep 2022 14:37:48 +0800 Subject: [PATCH] drm/i915/gvt: fix double-free bug in split_2MB_gtt_entry There is a double-free security bug in split_2MB_gtt_entry. Here is a calling chain : ppgtt_populate_spt->ppgtt_populate_shadow_entry->split_2MB_gtt_entry. If intel_gvt_dma_map_guest_page failed, it will call ppgtt_invalidate_spt, which will finally call ppgtt_free_spt and kfree(spt). But the caller does not notice that, and it will call ppgtt_free_spt again in error path. Fix this by only freeing spt in ppgtt_invalidate_spt in good case. Signed-off-by: xmzyshypnc <1002992920@qq.com> --- drivers/gpu/drm/i915/gvt/gtt.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.= c index 9f14fded8c0c..31d2a8d56384 100644 --- a/drivers/gpu/drm/i915/gvt/gtt.c +++ b/drivers/gpu/drm/i915/gvt/gtt.c @@ -959,7 +959,7 @@ static inline int ppgtt_put_spt(struct intel_vgpu_ppgtt_spt *spt) return atomic_dec_return(&spt->refcount); } -static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt); +static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *sptm, int is_error); static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *e) @@ -995,7 +995,7 @@ static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, ops->get_pfn(e)); return -ENXIO; } - return ppgtt_invalidate_spt(s); + return ppgtt_invalidate_spt(s, 0); } static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, @@ -1016,7 +1016,7 @@ static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, intel_gvt_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT); } -static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) +static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt, int is_e= rror) { struct intel_vgpu *vgpu =3D spt->vgpu; struct intel_gvt_gtt_entry e; @@ -1059,9 +1059,11 @@ static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) } } - trace_spt_change(spt->vgpu->id, "release", spt, + if (!is_error) { + trace_spt_change(spt->vgpu->id, "release", spt, spt->guest_page.gfn, spt->shadow_page.type); - ppgtt_free_spt(spt); + ppgtt_free_spt(spt); + } return 0; fail: gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n", @@ -1215,7 +1217,7 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgp= u, ret =3D intel_gvt_dma_map_guest_page(vgpu, start_gfn + sub_index, PAGE_SIZE, &dma_addr); if (ret) { - ret =3D ppgtt_invalidate_spt(spt); + ret =3D ppgtt_invalidate_spt(spt, 1); return ret; } sub_se.val64 =3D se->val64; @@ -1393,7 +1395,7 @@ static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt, ret =3D -ENXIO; goto fail; } - ret =3D ppgtt_invalidate_spt(s); + ret =3D ppgtt_invalidate_spt(s, 0); if (ret) goto fail; } else { --=20 2.25.1 Zheng Hacker =E4=BA=8E2022=E5=B9=B49=E6=9C=888= =E6=97=A5=E5=91=A8=E5=9B=9B 19:59=E5=86=99=E9=81=93=EF=BC=9A > > Hi Greg, > > I got it, Greg. > > Mid-Autumn Festival is coming and I will have a couple of days off. > I'll see what I can do after holiday :) > > Regards, > > Zheng Wang > > =E5=9C=A8 2022=E5=B9=B49=E6=9C=888=E6=97=A5=E6=98=9F=E6=9C=9F=E5=9B=9B=EF= =BC=8CGreg KH =E5=86=99=E9=81=93=EF=BC=9A >> >> On Thu, Sep 08, 2022 at 05:09:40PM +0800, Zheng Hacker wrote: >> > Hi Zhenyu, >> > >> > This issue has been open for a few days. Could you plz write a patch >> > for that :) I'm not familiar with the logical code here. >> >> As this is only able to be hit in a theoretical system, it isn't that >> high of a priority, if any priority at all. Why not try to write a >> patch for it yourself to help resolve the issue faster? >> >> thanks, >> >> greg k-h From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 637DAC6FA8B for ; Mon, 19 Sep 2022 13:29:46 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 812E810E64C; Mon, 19 Sep 2022 13:29:24 +0000 (UTC) Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by gabe.freedesktop.org (Postfix) with ESMTPS id 000BB10ECC2; Fri, 16 Sep 2022 06:39:34 +0000 (UTC) Received: by mail-ej1-x631.google.com with SMTP id r18so47205078eja.11; Thu, 15 Sep 2022 23:39:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date; bh=vvA/mvvSLM9uLQG38TGu9Y+RlTnepBsr7g4wPM9aIIg=; b=DgDKo6ADvXbPoPcmmuk1X2YQw1DXiBMf8Wx6bOA2cU2eRNOQ0u0EaL1/DyZ8BV67Ne 7EyAkjE2z9ASNk/HI9NC+4QGPTA0uZGcqxRLR47Xi6D2gfNn+rDgKAX3JBhgUECuG6NC M2h4GfReBpb+z+8CpedpYrrb4RrkIMF/zokBZqBWxsaJOcQE9Wj1MW8Ep7uvLNkNrjRr LiMXceubitfvKGEcSqwNWzhg5ZY6x+4KJTflmP98wCKJhsJ4hSoKJXn/MwV8hLbgCtV2 TuuAo9TXwA15ivLXkcbeY3s7opDXWOLMxSQnZGsVPADIIFfq6AVkfYXScjabJcs18OV7 v7uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date; bh=vvA/mvvSLM9uLQG38TGu9Y+RlTnepBsr7g4wPM9aIIg=; b=sH0iiANQ9JUvQ2YwT5cmWZzSoX3GESe+eFaNZ2AS7JmNJHiqXBvbW2k3AWxlKMhOwR Jnq/sO4fkboWQgv+aAviYlnPdz2P+pIympxwXiSNXEWYhBzBEHSR6lkxRu0UsLrjZgMr Vpw/CtJMBrYcDZQd1xy/3l63C00b55ajUC39nqMY8Mj1sToJ9zg56/dN5XoLQ9/xprya lbJ5AlQjv6wuPWsTbao2qyY8lZP+MgxKwMHXK+1hITkrAZU2P2j0i/kLw0yV1Bu8eLQX Hx78rM+j68Ajrd310cNZLBL2TXGIdcVCrZH/D40oEO0hmr4hGN8JyyYI8wTaSmTe3ntu vx1w== X-Gm-Message-State: ACrzQf0HmUFIlVsvr49nKnwPUPdDyRVe3qOMlzMejG9Jew6/y1k9twVk 6UUUnVL39nUgplYAg/EL3khjZsTcJcIW+tilmlw= X-Google-Smtp-Source: AMsMyM5N+6CjojnRxi41Vb0thsccYyyy86S95n9bkg3sqD6CX1INX3Rhkhf3WeV2G4qTgk896k3SrhrL3y8zAMOc3JM= X-Received: by 2002:a17:907:970a:b0:77d:a10c:e089 with SMTP id jg10-20020a170907970a00b0077da10ce089mr2526092ejc.364.1663310373366; Thu, 15 Sep 2022 23:39:33 -0700 (PDT) MIME-Version: 1.0 References: <20220907030754.GU1089@zhen-hp.sh.intel.com> In-Reply-To: From: Zheng Hacker Date: Fri, 16 Sep 2022 14:39:21 +0800 Message-ID: To: Greg KH Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailman-Approved-At: Mon, 19 Sep 2022 13:29:10 +0000 Subject: Re: [Intel-gfx] [PATCH] drm/i915/gvt: fix double-free bug in split_2MB_gtt_entry. X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "alex000young@gmail.com" , "security@kernel.org" , "airlied@linux.ie" , "intel-gfx@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , xmzyshypnc <1002992920@qq.com>, "daniel@ffwll.ch" , "rodrigo.vivi@intel.com" , "intel-gvt-dev@lists.freedesktop.org" Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" >From 8d95c1399e3ff345500a575e21254a73b0c89144 Mon Sep 17 00:00:00 2001 From: xmzyshypnc <1002992920@qq.com> Date: Fri, 16 Sep 2022 14:37:48 +0800 Subject: [PATCH] drm/i915/gvt: fix double-free bug in split_2MB_gtt_entry There is a double-free security bug in split_2MB_gtt_entry. Here is a calling chain : ppgtt_populate_spt->ppgtt_populate_shadow_entry->split_2MB_gtt_entry. If intel_gvt_dma_map_guest_page failed, it will call ppgtt_invalidate_spt, which will finally call ppgtt_free_spt and kfree(spt). But the caller does not notice that, and it will call ppgtt_free_spt again in error path. Fix this by only freeing spt in ppgtt_invalidate_spt in good case. Signed-off-by: xmzyshypnc <1002992920@qq.com> --- drivers/gpu/drm/i915/gvt/gtt.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.= c index 9f14fded8c0c..31d2a8d56384 100644 --- a/drivers/gpu/drm/i915/gvt/gtt.c +++ b/drivers/gpu/drm/i915/gvt/gtt.c @@ -959,7 +959,7 @@ static inline int ppgtt_put_spt(struct intel_vgpu_ppgtt_spt *spt) return atomic_dec_return(&spt->refcount); } -static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt); +static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *sptm, int is_error); static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *e) @@ -995,7 +995,7 @@ static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, ops->get_pfn(e)); return -ENXIO; } - return ppgtt_invalidate_spt(s); + return ppgtt_invalidate_spt(s, 0); } static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, @@ -1016,7 +1016,7 @@ static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, intel_gvt_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT); } -static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) +static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt, int is_e= rror) { struct intel_vgpu *vgpu =3D spt->vgpu; struct intel_gvt_gtt_entry e; @@ -1059,9 +1059,11 @@ static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) } } - trace_spt_change(spt->vgpu->id, "release", spt, + if (!is_error) { + trace_spt_change(spt->vgpu->id, "release", spt, spt->guest_page.gfn, spt->shadow_page.type); - ppgtt_free_spt(spt); + ppgtt_free_spt(spt); + } return 0; fail: gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n", @@ -1215,7 +1217,7 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgp= u, ret =3D intel_gvt_dma_map_guest_page(vgpu, start_gfn + sub_index, PAGE_SIZE, &dma_addr); if (ret) { - ret =3D ppgtt_invalidate_spt(spt); + ret =3D ppgtt_invalidate_spt(spt, 1); return ret; } sub_se.val64 =3D se->val64; @@ -1393,7 +1395,7 @@ static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt, ret =3D -ENXIO; goto fail; } - ret =3D ppgtt_invalidate_spt(s); + ret =3D ppgtt_invalidate_spt(s, 0); if (ret) goto fail; } else { --=20 2.25.1 Zheng Hacker =E4=BA=8E2022=E5=B9=B49=E6=9C=888= =E6=97=A5=E5=91=A8=E5=9B=9B 19:59=E5=86=99=E9=81=93=EF=BC=9A > > Hi Greg, > > I got it, Greg. > > Mid-Autumn Festival is coming and I will have a couple of days off. > I'll see what I can do after holiday :) > > Regards, > > Zheng Wang > > =E5=9C=A8 2022=E5=B9=B49=E6=9C=888=E6=97=A5=E6=98=9F=E6=9C=9F=E5=9B=9B=EF= =BC=8CGreg KH =E5=86=99=E9=81=93=EF=BC=9A >> >> On Thu, Sep 08, 2022 at 05:09:40PM +0800, Zheng Hacker wrote: >> > Hi Zhenyu, >> > >> > This issue has been open for a few days. Could you plz write a patch >> > for that :) I'm not familiar with the logical code here. >> >> As this is only able to be hit in a theoretical system, it isn't that >> high of a priority, if any priority at all. Why not try to write a >> patch for it yourself to help resolve the issue faster? >> >> thanks, >> >> greg k-h