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: 12840278 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 91001C433F5 for ; Thu, 5 May 2022 22:04:31 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D05AA10E1A1; Thu, 5 May 2022 22:04:30 +0000 (UTC) Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2A1ED10E1A1 for ; Thu, 5 May 2022 22:04:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788268; 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=ncwUrPXa6D//xAE+gUKX4W9jlEJvsWNrctcaxVQSCb8=; b=PMi2RoYAU17Y80cUBB8EuRwJ+zKPK4V7RKazqZfOlTX/icHoaQ4832p0wWZYCW4D23LMe/ BF/K7TysPMXwqCeagF1EQvG5o6ia8G0tJ8uk5Qfs0z5NxzuxjErHWpi6xSbFrn1l/5afOh +9slWxl6KP8sQGBaSrKcD4UfgEb6m7c= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-492-kvh7cwAMOgWQi3VQpKNWGw-1; Thu, 05 May 2022 18:04:25 -0400 X-MC-Unique: kvh7cwAMOgWQi3VQpKNWGw-1 Received: by mail-wr1-f70.google.com with SMTP id ba21-20020a0560001c1500b0020ca6a45dfcso3827wrb.9 for ; Thu, 05 May 2022 15:04:24 -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=aGTyy2Vaucl5dtiBfHzdXGOAJeD1lVCZO+r5IscBMRcDfiv2O5Ua+tipnRUJAe6Kbb hVBTp06fd7gW4tkA9X21zE0Xbj/pzemr/6MmZkQpRieOl4Opg3aEwTmMgZ3rZoIcQqkT 8gDbQ3VF4AxbWe1CMPbW8bP3Znnms5T86wLq0OnhvNewM8P99YMJSMsGTEyQ3mWPtloO poTOS3Sz/YtVHVroMt5bcAzzWFsqdWA+9HIbN2pT/9ZVY6U1MLQIqlYuX0aDuZc3+CVe dfBav58RSr2SBS9viPhW0OShNHT185gZwFCnT0DjEYDYdHho6MXXDkPBjuofMatb3ul6 Hl1Q== X-Gm-Message-State: AOAM531HbyeRwAnqQJYOl7WoEx6I5rGasZL98/UvETO7d88SLqNmAjUe m3eGKxqvQWTYfh+37PiMRIUvEKHdXxZO8gxF3UuFy4JdPlh7Mnmyw5v/FAnQ8q1AuUg7eIhhIzt SRKCPQobUUvCTwogPJv1vFSqjupFS X-Received: by 2002:a05:600c:3d8c:b0:394:6097:9994 with SMTP id bi12-20020a05600c3d8c00b0039460979994mr6726652wmb.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 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=javierm@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Javier Martinez Canillas , dri-devel@lists.freedesktop.org, Thomas Zimmermann , Daniel Vetter Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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: 12840279 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 38D44C433F5 for ; Thu, 5 May 2022 22:05:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 83AED10E39E; Thu, 5 May 2022 22:05:10 +0000 (UTC) Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id B870E10E3E2 for ; Thu, 5 May 2022 22:05:09 +0000 (UTC) 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CpySyazXcP9jeQEo03DKJEoNJ7MOw25wdUGi6hnO0cY=; b=JABCmbM8vx7KfdP2EhaLyBJKCMzSXzvZYTSFgMOa1PhFv4Quc+ZGvnrndegRvFTgEbasOd sFtJyWQxN550BZWFCjlpyLyeLUZYXiGtT95wLCjozAGe+ypN4LhUvIBID+rxLCz8VlpSxy zdEN3D1TMcAwX8AwiRaALxxa7zKefOM= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-CGL20kY6O5uK4c1Z5v-rXg-1; Thu, 05 May 2022 18:05:08 -0400 X-MC-Unique: CGL20kY6O5uK4c1Z5v-rXg-1 Received: by mail-wm1-f71.google.com with SMTP id s14-20020a05600c29ce00b00394611a8059so2376456wmd.4 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=x66cQlsr2IsM3XGngzwHjK61g9mri2O54kc0trz3gOCayXoRo+qyY4TAFeKq6uY7LG 1Ng5eZopwHzeONdiSW6FsvBwhLEAdLEh4i9ODfRgCp17cFsqxOHGypGG+T+a97j16OU3 j9h40zESPgTImjFR/uTZ1mpc/bDNM/GWroIavaERbzAi0x/UDggFcsliAO+JB6am5/GC ETBugjSMTvSbxlL/3rZ01ECnv/C+RXDwn9FLyQ7QGIEreJDMQSS4kbkZftEOD5NcfjFV YbBGNpw6CzjOKYL8g+XdKyRhGb93+fkD65srfJTW8BhuEfN2AOu52Pv8r4c9prN+ViaB mK1Q== X-Gm-Message-State: AOAM530/xfPscnSkv3YS905ZcZewjZsoPIZigXH5S7hgUiqBpUSA+d3L gnb5qORLPDEB/L/glddwcyRGJvySnYS2T4QO3XcbDsKeKbLpwqtNt4U7J+LUyJ2ug+r/e/liD40 f2L923+uyDp7qV0DD1qpo0Emmv6Cz X-Received: by 2002:a5d:6c65:0:b0:20c:5230:f145 with SMTP id r5-20020a5d6c65000000b0020c5230f145mr142607wrz.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 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=javierm@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Javier Martinez Canillas , dri-devel@lists.freedesktop.org, Hans de Goede , Thomas Zimmermann Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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: 12840280 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 803E3C433F5 for ; Thu, 5 May 2022 22:06:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AD09110E3E2; Thu, 5 May 2022 22:06:00 +0000 (UTC) Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.133.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6280410E3E2 for ; Thu, 5 May 2022 22:05:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788358; 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=XOMIAnnTamSRotiOWkLRdWbDfwtm7Vtf7J5gIUBLvsY=; b=KVmT3BGatAwG+bW+HbDVFyphnyta6J0lZoMDbXVC/G7ni6bWbOindIwqG+2ioOxLy6TWgA 0xtMHgKDrbSmH+xXzNimPqwSSigqcGgt+8XZKt3s9KSXylb8hlRDO+oEST6An019LhHs4r wbb7SrRjpJDQ5S0tbeGKnxqvtcYPX+w= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-624-rsTBRLtENyqiU5rXhAsdaw-1; Thu, 05 May 2022 18:05:56 -0400 X-MC-Unique: rsTBRLtENyqiU5rXhAsdaw-1 Received: by mail-wr1-f70.google.com with SMTP id g7-20020adfbc87000000b0020ac76d254bso1876214wrh.6 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=R+1Ygawrba0Tj2sZfC6HrJrIArWLdV+GfVXmoETVZYF/GkUeGC8pWIljiCRAAOoeAL XlnysiDJcQ3y+koKW8E5esh+svFLCN+kZiaaZPDNrCr9G+6hFiCauSgrnIDj5PWfgSMq xxbmD8JIP8ZCgYbLH51Whwoepf6qZL8s0wZHkXgu0sUDrSaUtPj1m92AaUR9Tjo2aEL0 ckWKSN0x0Z6oE5bXR6Nn8p+CrjOKBCZpWNLfNLseTiv3WQbgH8M4V5Y+K54AANZkc3yy kwTEwdyukpiQKY/XC0TL4LE6aj0fV+Q1awWwkmTUAEEQNJ+x6QkApE8rwKkS9ZZymtb7 I3Zg== X-Gm-Message-State: AOAM531tOUWUwBXLyAkWqWGOtatYabib59s0txGUyBfBuketyZ7vXDj5 RRKXH+pUYWAO0SAq5iStLk1kCvP+TuOJ0bsa/0Y88FDkjWWc5bsZR4rpdRVrdVjahLYiXWyNVAB 7ePdVzloyoI970h6Z1idg/I2QPkLa X-Received: by 2002:a05:600c:214c:b0:394:2dfe:2754 with SMTP id v12-20020a05600c214c00b003942dfe2754mr287442wml.135.1651788354980; 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 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=javierm@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Javier Martinez Canillas , dri-devel@lists.freedesktop.org, Peter Jones , Thomas Zimmermann Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 Reported-by: kernel test robot --- (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: 12840284 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 5D0B5C433F5 for ; Thu, 5 May 2022 22:06:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 79B7A10E440; Thu, 5 May 2022 22:06:38 +0000 (UTC) Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [170.10.129.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id E34EB10E440 for ; Thu, 5 May 2022 22:06:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651788395; 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=2QUs/dFpZjXD63m2oUdzjI7+Wpnx04uqjBRSmxR+fJo=; b=Gr9AbVu5GAZCenfLo4KJ7ptYfezKMwv/Jgc5ISx16rPqAIrJdoqnI/P9MiJLTXBej0l/ir Yr3sjNKlL6JzyZNcYQZ+McqAv0nBXpMCWLgCUQiUqw2cbNJhE8cCbUo6/m6fc/WqUhdpQP tW3iPaA4/5I8Erv1Lm3h5zMaQfYX1U4= 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-465-PoM41cxiO3ySqcrG2mfJoQ-1; Thu, 05 May 2022 18:06:34 -0400 X-MC-Unique: PoM41cxiO3ySqcrG2mfJoQ-1 Received: by mail-wm1-f70.google.com with SMTP id v191-20020a1cacc8000000b0038ce818d2efso2221019wme.1 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=0nn605L0RQeILMyRWUanTwg1zUB75gqHBybnC4/kBuQQAxcOtZMHtqTxXfVkNGxsJs fQ1ax3ULrlinU86bSSxsvZoZD7i8jcVQCuxypo/IzhWsM/nyJDRwKtYajncNFUY4U1Eq PDXDYQwVEB32efI3orRHLz3Tnlrc4WCo8eRotVONBF6xnQYjxxEbDNzqXQGJRGWrCewZ nainpD0Uawhwyc1eKxlv47cf+1v3Haat1BKR4QS7PtpDNmHzhhJii1yvN7sxjUHBN93Q pB+YLCl/y8SfWfgCWMIlK8TnOMRV1m/z6bUd+ZCAmfSXVyJWSuu93+pG7MNFcxJyLUvB RK5A== X-Gm-Message-State: AOAM530ZA8CKLqw17uvMjyQSakJOUALEGRkQmHRr7YAgMlUominS5jYN 6gHN9M27OwiTptqE2LtiFLSh+JwPCUSDoeyQZJYtNGl4mRfTS2ZnJDG/h4u78D6ChEQ0A/hC7Ar 6PnomBH5Szu9N9OBvLFN0WR34fce5 X-Received: by 2002:a5d:6dd1:0:b0:207:92c4:eaef with SMTP id d17-20020a5d6dd1000000b0020792c4eaefmr165074wrz.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 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=javierm@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: linux-fbdev@vger.kernel.org, Daniel Vetter , Helge Deller , Javier Martinez Canillas , dri-devel@lists.freedesktop.org, Hans de Goede , Thomas Zimmermann Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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; }