From patchwork Thu May 5 22:04:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 12840275 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 F36D6C433EF for ; Thu, 5 May 2022 22:04:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352708AbiEEWII (ORCPT ); Thu, 5 May 2022 18:08:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237116AbiEEWII (ORCPT ); Thu, 5 May 2022 18:08:08 -0400 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3B7F71EEF0 for ; Thu, 5 May 2022 15:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788266; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ncwUrPXa6D//xAE+gUKX4W9jlEJvsWNrctcaxVQSCb8=; b=Isl8L8SfNHWV+t69isu/BtN20Sgshf/20p+Iu+td7e6wq1+qXH2knLE4E/Uhwye1+93X0Y IHG2r22GyP17PK67t3geDT+zboBJqO/ytwvdrjtbwsE5lolvvk2LpZIsMYKCkwo6F7cMxG /n3gjTxzKGS0hfURfM3Q8/z6dBSem2g= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-586-B5bSwudJNImKQaS0N_vm6g-1; Thu, 05 May 2022 18:04:25 -0400 X-MC-Unique: B5bSwudJNImKQaS0N_vm6g-1 Received: by mail-wr1-f72.google.com with SMTP id p18-20020adf9592000000b00207bc12decbso1870534wrp.21 for ; Thu, 05 May 2022 15:04:25 -0700 (PDT) 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=ncwUrPXa6D//xAE+gUKX4W9jlEJvsWNrctcaxVQSCb8=; b=Jd2nA0/2oBAZ8KcHOL0tKHQpGZqKSLK1BmWvrWDghM2cUtfWohFrR8TEFg9j9qN1vM UcRrF/Pjm8eoLMXfAu4FBHpTr/+Bn8t0r5SpXJOLBqMHQ2v6xZdYXqYOyQtxBDxRClvr LNkWYvR8TEc5NUcy/NvyJ/JSzQH7Ulgf9XyCGdUs44RQ/vx3ntWcmFGrqw/eQGn2vdkp /lGb/Iebj7ZGoGRQmfyg2JHjEoO9PxfUzBJvW78POlZ+OowmZFwryMEh6eo2UN3PEtjq wuqhsicADAWmNieHTC7QC0WmMTO0jh8hT0lxiVbjULSdyFOK9690OKvAfSsZ0XVeKxw6 jzIA== X-Gm-Message-State: AOAM5303pt01BGDpn2Xrqt5nBP70t0XadhV1YiMzZwmL2PQBz7RyYB9q vbLfS/bDKTt2aGY5jZC//TbMgKTjrxNR2x22SiBk6Fr3U/ZZjgIdlhxGxL2COwdRFMusOaeEomT q4C0FuFisy3HI9N+mbp/jeY8= X-Received: by 2002:a05:600c:3d8c:b0:394:6097:9994 with SMTP id bi12-20020a05600c3d8c00b0039460979994mr6726651wmb.29.1651788263959; Thu, 05 May 2022 15:04:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfwhAOIQeVF2O3srNOg8rIqNBWMADJBM0ucjW4JDKss+KaRrP/Oe0Hd/iuu0biw9+SXEcE9g== X-Received: by 2002:a05:600c:3d8c:b0:394:6097:9994 with SMTP id bi12-20020a05600c3d8c00b0039460979994mr6726643wmb.29.1651788263758; Thu, 05 May 2022 15:04:23 -0700 (PDT) Received: from minerva.home (205.pool92-176-231.dynamic.orange.es. [92.176.231.205]) by smtp.gmail.com with ESMTPSA id m21-20020a05600c3b1500b003942a244f4bsm7980875wms.36.2022.05.05.15.04.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 15:04:23 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Daniel Vetter , Javier Martinez Canillas , Thomas Zimmermann Subject: [PATCH v3 1/4] fbdev: Prevent possible use-after-free in fb_release() Date: Fri, 6 May 2022 00:04:13 +0200 Message-Id: <20220505220413.365977-1-javierm@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505215947.364694-1-javierm@redhat.com> References: <20220505215947.364694-1-javierm@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org From: Daniel Vetter Most fbdev drivers have issues with the fb_info lifetime, because call to framebuffer_release() from their driver's .remove callback, rather than doing from fbops.fb_destroy callback. Doing that will destroy the fb_info too early, while references to it may still exist, leading to a use-after-free error. To prevent this, check the fb_info reference counter when attempting to kfree the data structure in framebuffer_release(). That will leak it but at least will prevent the mentioned error. Signed-off-by: Daniel Vetter Signed-off-by: Javier Martinez Canillas Reviewed-by: Thomas Zimmermann Reported-by: Andrzej Hajda Signed-off-by: Javier Martinez Canillas --- (no changes since v1) drivers/video/fbdev/core/fbsysfs.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/video/fbdev/core/fbsysfs.c b/drivers/video/fbdev/core/fbsysfs.c index 8c1ee9ecec3d..c2a60b187467 100644 --- a/drivers/video/fbdev/core/fbsysfs.c +++ b/drivers/video/fbdev/core/fbsysfs.c @@ -80,6 +80,10 @@ void framebuffer_release(struct fb_info *info) { if (!info) return; + + if (WARN_ON(refcount_read(&info->count))) + return; + kfree(info->apertures); kfree(info); } From patchwork Thu May 5 22:04:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 12840276 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 5D323C433F5 for ; Thu, 5 May 2022 22:05:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230419AbiEEWJF (ORCPT ); Thu, 5 May 2022 18:09:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245662AbiEEWIt (ORCPT ); Thu, 5 May 2022 18:08:49 -0400 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 19FAE5C64F for ; Thu, 5 May 2022 15:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788308; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CpySyazXcP9jeQEo03DKJEoNJ7MOw25wdUGi6hnO0cY=; b=Tv5Cn2NQWTmW/dIbpECPwyLPLHqHjROL43HRs6dwHygl2YsVs4kCbOo3jrzFrr4C/v7LWn VYi6wxz4ZGygO0FQIiv+pqH6G5rZuEso829J7Pp8CSFCXO81eMEVPPa8mSUz9Zf8WfoOX1 1TvURTOn1MvV+U+PuF6i/AIGoW8QDYU= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-z0VxhYQ2M4CtEAVgycIWFA-1; Thu, 05 May 2022 18:05:07 -0400 X-MC-Unique: z0VxhYQ2M4CtEAVgycIWFA-1 Received: by mail-wm1-f69.google.com with SMTP id c62-20020a1c3541000000b0038ec265155fso5104116wma.6 for ; Thu, 05 May 2022 15:05:06 -0700 (PDT) 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=CpySyazXcP9jeQEo03DKJEoNJ7MOw25wdUGi6hnO0cY=; b=FLJSYXBK5P8FUVEt11UEB1+yURLfpjmG8oASigrfyjX4QgL0+OrDy/TUlt2aB4p2SU WmaiiPeF1k5YIsyqDEGiIbdHnKCdhiKhgKlv1iMWW77SzkSJFdNhow13tg6eII6TCCVY Ya55rC1bSncfcxbgPhoTPoOV0VM58a4gm0xrQOwTW4jnCdFTCHe8OSAKdW7tQ7tuXyXD yjJ0k5KEfTlePzt92SRZaRmjYybkeRk6dj21XmaIyzlfDOMjV/JrcssYa34U1iToHAIU ZGUfH5TP6/du6AcNO8YrKkzTUfJ0iNd+lQgbK4GOE11SXW4q7W3tnhDoQ2zvGFHPAP2J fNPA== X-Gm-Message-State: AOAM532LcPHJySb0IfN2cTD1b9mO819tKyx8R/bq/pMOocUPtAaub5Bk JoCnADUDpErOnJwBeWaNNOtdukauJ7+npnbclGL1k+ER1nUujXkCzEx31KItj3yH46OXgxnFHXb nH0VQIazve56kdIxOZOA5ExY= X-Received: by 2002:a5d:6c65:0:b0:20c:5230:f145 with SMTP id r5-20020a5d6c65000000b0020c5230f145mr142606wrz.337.1651788305743; Thu, 05 May 2022 15:05:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzx4hSKJf5+8gqVXy4SQIIH/+d18yIIzPchwfl0/SeHroDdEocZasvgfA/q6M9W6ITqVZA+Yg== X-Received: by 2002:a5d:6c65:0:b0:20c:5230:f145 with SMTP id r5-20020a5d6c65000000b0020c5230f145mr142590wrz.337.1651788305549; Thu, 05 May 2022 15:05:05 -0700 (PDT) Received: from minerva.home (205.pool92-176-231.dynamic.orange.es. [92.176.231.205]) by smtp.gmail.com with ESMTPSA id n21-20020a7bc5d5000000b003942a244f47sm7942360wmk.32.2022.05.05.15.05.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 15:05:05 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Hans de Goede , Javier Martinez Canillas , Thomas Zimmermann Subject: [PATCH v3 2/4] fbdev: simplefb: Cleanup fb_info in .fb_destroy rather than .remove Date: Fri, 6 May 2022 00:04:56 +0200 Message-Id: <20220505220456.366090-1-javierm@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505215947.364694-1-javierm@redhat.com> References: <20220505215947.364694-1-javierm@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org The driver is calling framebuffer_release() in its .remove callback, but this will cause the struct fb_info to be freed too early. Since it could be that a reference is still hold to it if user-space opened the fbdev. This would lead to a use-after-free error if the framebuffer device was unregistered but later a user-space process tries to close the fbdev fd. To prevent this, move the framebuffer_release() call to fb_ops.fb_destroy instead of doing it in the driver's .remove callback. Strictly speaking, the code flow in the driver is still wrong because all the hardware cleanupd (i.e: iounmap) should be done in .remove while the software cleanup (i.e: releasing the framebuffer) should be done in the .fb_destroy handler. But this at least makes to match the behavior before commit 27599aacbaef ("fbdev: Hot-unplug firmware fb devices on forced removal"). Fixes: 27599aacbaef ("fbdev: Hot-unplug firmware fb devices on forced removal") Suggested-by: Daniel Vetter Signed-off-by: Javier Martinez Canillas Reviewed-by: Thomas Zimmermann Reviewed-by: Daniel Vetter --- (no changes since v1) drivers/video/fbdev/simplefb.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/video/fbdev/simplefb.c b/drivers/video/fbdev/simplefb.c index 94fc9c6d0411..2c198561c338 100644 --- a/drivers/video/fbdev/simplefb.c +++ b/drivers/video/fbdev/simplefb.c @@ -84,6 +84,10 @@ struct simplefb_par { static void simplefb_clocks_destroy(struct simplefb_par *par); static void simplefb_regulators_destroy(struct simplefb_par *par); +/* + * fb_ops.fb_destroy is called by the last put_fb_info() call at the end + * of unregister_framebuffer() or fb_release(). Do any cleanup here. + */ static void simplefb_destroy(struct fb_info *info) { struct simplefb_par *par = info->par; @@ -94,6 +98,8 @@ static void simplefb_destroy(struct fb_info *info) if (info->screen_base) iounmap(info->screen_base); + framebuffer_release(info); + if (mem) release_mem_region(mem->start, resource_size(mem)); } @@ -545,8 +551,8 @@ static int simplefb_remove(struct platform_device *pdev) { struct fb_info *info = platform_get_drvdata(pdev); + /* simplefb_destroy takes care of info cleanup */ unregister_framebuffer(info); - framebuffer_release(info); return 0; } From patchwork Thu May 5 22:05:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 12840277 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 B2C19C433F5 for ; Thu, 5 May 2022 22:06:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245599AbiEEWJl (ORCPT ); Thu, 5 May 2022 18:09:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237116AbiEEWJj (ORCPT ); Thu, 5 May 2022 18:09:39 -0400 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.129.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1BBC95DBF5 for ; Thu, 5 May 2022 15:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788357; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XOMIAnnTamSRotiOWkLRdWbDfwtm7Vtf7J5gIUBLvsY=; b=Ba1xRvzB9SAXzBEcc7v3Ut+zYDe2RBV33jCalF3XsLrAW3XtcdZVtzzLVzO7fa7eCxeiPu GI2paM6ysQt+cMJODb8LtFpA7WFU5P8Vt+6q1Cri7LPfax4uoXseBdTO2eo6EUnhd27qxl od8dfrpQ2gvCHswWY5g2/W6t/ZaPtoU= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-401-1PNBX738OZS9aY7Xo9CV5A-1; Thu, 05 May 2022 18:05:56 -0400 X-MC-Unique: 1PNBX738OZS9aY7Xo9CV5A-1 Received: by mail-wm1-f72.google.com with SMTP id s14-20020a05600c29ce00b00394611a8059so2377367wmd.4 for ; Thu, 05 May 2022 15:05:56 -0700 (PDT) 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=XOMIAnnTamSRotiOWkLRdWbDfwtm7Vtf7J5gIUBLvsY=; b=4lpCJ2rz2gwqNpIRZjz+a5iljVYSpDn7EWNZRuRf7rxwQijp9ReR2FeQHvzGk4j/C8 ZZU0tuvopEsqi2qPtoi+l4zJG45QhKPLs/xRxuzgizt8ymhbfSgef3eP6ugflpeo3Ute bH/ACs8xn6FAGnKXYFpnhG3kkmIf+VcZZuUvFU0BCOtOKjzrPV6IietLdZ4m5SVHfdQ1 xaiDr8lUFb6/r5QK9aaSNnsJLeVNQlpIKyOldHiDsNBSUTo56gLh3UQD6p9yksY+Dqf6 hGuc1qvtjngEWkt3kb8p6nV7WznAb2cYB+OXh+R9lrxHYOKEGBQMD/HHJV94Qt0/NRJL HRCw== X-Gm-Message-State: AOAM532w0Bc1rPe8k6mTHC/b6qrGjhQG2Nqh29eCtoAHl/BdsQB+QnoK BJ0NXssRUJFL3cgTy0ZfuzGAt5H2b8UKEl2msDLgAOuxiwq2oCG/6hjK9PEPM4gMpd1nKhpmPL7 2f/XFIahl79CdYvuTwh1tDNA= X-Received: by 2002:a05:600c:214c:b0:394:2dfe:2754 with SMTP id v12-20020a05600c214c00b003942dfe2754mr287441wml.135.1651788354979; Thu, 05 May 2022 15:05:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxeda1HHZwr4FX+lwN/TaIHgdOKHms0BlDVvSG/oTgRzg2lXGiKrE9u+GvlU9S+3hDsIgrQvw== X-Received: by 2002:a05:600c:214c:b0:394:2dfe:2754 with SMTP id v12-20020a05600c214c00b003942dfe2754mr287430wml.135.1651788354747; Thu, 05 May 2022 15:05:54 -0700 (PDT) Received: from minerva.home (205.pool92-176-231.dynamic.orange.es. [92.176.231.205]) by smtp.gmail.com with ESMTPSA id j28-20020a05600c1c1c00b003942a244f39sm7291679wms.18.2022.05.05.15.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 15:05:54 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Peter Jones , Javier Martinez Canillas , Thomas Zimmermann Subject: [PATCH v3 3/4] fbdev: efifb: Cleanup fb_info in .fb_destroy rather than .remove Date: Fri, 6 May 2022 00:05:40 +0200 Message-Id: <20220505220540.366218-1-javierm@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505215947.364694-1-javierm@redhat.com> References: <20220505215947.364694-1-javierm@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org The driver is calling framebuffer_release() in its .remove callback, but this will cause the struct fb_info to be freed too early. Since it could be that a reference is still hold to it if user-space opened the fbdev. This would lead to a use-after-free error if the framebuffer device was unregistered but later a user-space process tries to close the fbdev fd. To prevent this, move the framebuffer_release() call to fb_ops.fb_destroy instead of doing it in the driver's .remove callback. Strictly speaking, the code flow in the driver is still wrong because all the hardware cleanupd (i.e: iounmap) should be done in .remove while the software cleanup (i.e: releasing the framebuffer) should be done in the .fb_destroy handler. But this at least makes to match the behavior before commit 27599aacbaef ("fbdev: Hot-unplug firmware fb devices on forced removal"). Fixes: 27599aacbaef ("fbdev: Hot-unplug firmware fb devices on forced removal") Suggested-by: Daniel Vetter Signed-off-by: Javier Martinez Canillas Reviewed-by: Thomas Zimmermann Reviewed-by: Daniel Vetter --- (no changes since v1) drivers/video/fbdev/efifb.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/video/fbdev/efifb.c b/drivers/video/fbdev/efifb.c index ea42ba6445b2..cfa3dc0b4eee 100644 --- a/drivers/video/fbdev/efifb.c +++ b/drivers/video/fbdev/efifb.c @@ -243,6 +243,10 @@ static void efifb_show_boot_graphics(struct fb_info *info) static inline void efifb_show_boot_graphics(struct fb_info *info) {} #endif +/* + * fb_ops.fb_destroy is called by the last put_fb_info() call at the end + * of unregister_framebuffer() or fb_release(). Do any cleanup here. + */ static void efifb_destroy(struct fb_info *info) { if (efifb_pci_dev) @@ -254,6 +258,9 @@ static void efifb_destroy(struct fb_info *info) else memunmap(info->screen_base); } + + framebuffer_release(info); + if (request_mem_succeeded) release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size); @@ -620,9 +627,9 @@ static int efifb_remove(struct platform_device *pdev) { struct fb_info *info = platform_get_drvdata(pdev); + /* efifb_destroy takes care of info cleanup */ unregister_framebuffer(info); sysfs_remove_groups(&pdev->dev.kobj, efifb_groups); - framebuffer_release(info); return 0; } From patchwork Thu May 5 22:06:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Martinez Canillas X-Patchwork-Id: 12840283 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 674D7C433F5 for ; Thu, 5 May 2022 22:06:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386196AbiEEWKa (ORCPT ); Thu, 5 May 2022 18:10:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1386186AbiEEWK0 (ORCPT ); Thu, 5 May 2022 18:10:26 -0400 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7ACFA5E17F for ; Thu, 5 May 2022 15:06:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788402; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2QUs/dFpZjXD63m2oUdzjI7+Wpnx04uqjBRSmxR+fJo=; b=WcESISWiMzZAVXZlC6Auoti3kaoDmdKBICe0Pz2eeq57Z2nenRQ+QMDjYAxb7WHB8gzBN1 2nGtV2y0xieLFClKbUi/R1nGUA980/flHtICAI02uxOQqn0npEPZvF1j8+Njc9I5UVLL3t Nq8yEwWk044gwhpf9GjwnJEeW2j5vl4= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-660-PavVowaPOgOuqeQ3TPByUw-1; Thu, 05 May 2022 18:06:34 -0400 X-MC-Unique: PavVowaPOgOuqeQ3TPByUw-1 Received: by mail-wm1-f70.google.com with SMTP id t184-20020a1c46c1000000b00394209f54f1so2214560wma.4 for ; Thu, 05 May 2022 15:06:34 -0700 (PDT) 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=2QUs/dFpZjXD63m2oUdzjI7+Wpnx04uqjBRSmxR+fJo=; b=w6N3MbrU9o44NNQJ4A/oqkWtCP7QYm39ppmzIsY5ByTeZeFAyMqaAy7/o892s1yJKv s9SAPAmr+O3qoLkUfpRaJulWjmYOxX+Yv/YDOX4gUA5FpmdrnmNe6r1fH7mWxxELpCCU vtILzAprDRPRAPN3VSuEmcY/sHUALAD/c438Dvvx05qRRONCNcgmJb9qbSgF7nK/Fnvf cIWCzC90+qQ47Md0ay2fTdJ4HbNSuv2OzCxejI9zqzWJampYQQP7Wj4h378xB3bMRg8O htAgg/7sJxeliC5hpka9H4tA0/RmZTyYQLa1zn+VGZ5NYakVSAJr3y2HSZ2+xSh6CzG+ Wfqw== X-Gm-Message-State: AOAM531J/GPmnSO8M8bHNj8I4tB3c9gvIy+0F320gWUVCe8O+33GS3QH UAkcp0cHqNcMBzoSJqIX0IpJUnnwXXz4QMGi1a23ohsUPvm3kdJivwl2N6g1jVFSqdn3IbrcyRI bGCLfK6m+2C7KcJjIhyoJ40o= X-Received: by 2002:a5d:6dd1:0:b0:207:92c4:eaef with SMTP id d17-20020a5d6dd1000000b0020792c4eaefmr165075wrz.498.1651788393674; Thu, 05 May 2022 15:06:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJuZtwlqObinYznGZMuIaaeVki7i2UMoMVMO9mJCvQ2FsqRQYwPdVCdFx20Il32JBmnuIcGA== X-Received: by 2002:a5d:6dd1:0:b0:207:92c4:eaef with SMTP id d17-20020a5d6dd1000000b0020792c4eaefmr165062wrz.498.1651788393466; Thu, 05 May 2022 15:06:33 -0700 (PDT) Received: from minerva.home (205.pool92-176-231.dynamic.orange.es. [92.176.231.205]) by smtp.gmail.com with ESMTPSA id n11-20020a056000170b00b0020c5253d8c7sm2040236wrc.19.2022.05.05.15.06.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 15:06:33 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Hans de Goede , Javier Martinez Canillas , Thomas Zimmermann Subject: [PATCH v3 4/4] fbdev: vesafb: Cleanup fb_info in .fb_destroy rather than .remove Date: Fri, 6 May 2022 00:06:31 +0200 Message-Id: <20220505220631.366371-1-javierm@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220505215947.364694-1-javierm@redhat.com> References: <20220505215947.364694-1-javierm@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org The driver is calling framebuffer_release() in its .remove callback, but this will cause the struct fb_info to be freed too early. Since it could be that a reference is still hold to it if user-space opened the fbdev. This would lead to a use-after-free error if the framebuffer device was unregistered but later a user-space process tries to close the fbdev fd. To prevent this, move the framebuffer_release() call to fb_ops.fb_destroy instead of doing it in the driver's .remove callback. Strictly speaking, the code flow in the driver is still wrong because all the hardware cleanupd (i.e: iounmap) should be done in .remove while the software cleanup (i.e: releasing the framebuffer) should be done in the .fb_destroy handler. But this at least makes to match the behavior before commit 27599aacbaef ("fbdev: Hot-unplug firmware fb devices on forced removal"). Fixes: 27599aacbaef ("fbdev: Hot-unplug firmware fb devices on forced removal") Suggested-by: Daniel Vetter Signed-off-by: Javier Martinez Canillas Reviewed-by: Thomas Zimmermann Reviewed-by: Daniel Vetter --- Changes in v3: - Only move framebuffer_release() and don't do any other change (Daniel Vetter). Changes in v2: - Also do the change for vesafb (Thomas Zimmermann). drivers/video/fbdev/vesafb.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/video/fbdev/vesafb.c b/drivers/video/fbdev/vesafb.c index df6de5a9dd4c..e25e8de5ff67 100644 --- a/drivers/video/fbdev/vesafb.c +++ b/drivers/video/fbdev/vesafb.c @@ -179,6 +179,10 @@ static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green, return err; } +/* + * fb_ops.fb_destroy is called by the last put_fb_info() call at the end + * of unregister_framebuffer() or fb_release(). Do any cleanup here. + */ static void vesafb_destroy(struct fb_info *info) { struct vesafb_par *par = info->par; @@ -188,6 +192,8 @@ static void vesafb_destroy(struct fb_info *info) if (info->screen_base) iounmap(info->screen_base); release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size); + + framebuffer_release(info); } static struct fb_ops vesafb_ops = { @@ -484,10 +490,10 @@ static int vesafb_remove(struct platform_device *pdev) { struct fb_info *info = platform_get_drvdata(pdev); + /* vesafb_destroy takes care of info cleanup */ unregister_framebuffer(info); if (((struct vesafb_par *)(info->par))->region) release_region(0x3c0, 32); - framebuffer_release(info); return 0; }