From patchwork Mon May 2 13:09:43 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: 12834177 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 A655BC433EF for ; Mon, 2 May 2022 13:10:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385167AbiEBNNd (ORCPT ); Mon, 2 May 2022 09:13:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385168AbiEBNNc (ORCPT ); Mon, 2 May 2022 09:13:32 -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 9E18D19290 for ; Mon, 2 May 2022 06:10:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651497002; 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=6WaotQP90v4LJiFV9U7D4wGlm3uQ+5ZZcBUavInrV48=; b=Z+proRlf13zm9Avmvo9+QkoMJaTo3lq0noMcw/KqrMgEWjSiqoIkaLs7xkWq+nui4NC8pg 0p1xtbLG7nKXnr+KZQZM7Fy895r8Sgup6Wawa1y1787qdTCsrAteoXbsnIu5Gh6xs6DbCP G93ig+5R45LZrvsQlD3n1zFrUhee8EI= 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-230-zUn0azRCOh2XWySqXacJdg-1; Mon, 02 May 2022 09:10:01 -0400 X-MC-Unique: zUn0azRCOh2XWySqXacJdg-1 Received: by mail-wr1-f70.google.com with SMTP id d28-20020adf9b9c000000b0020ad4a50e14so5299837wrc.3 for ; Mon, 02 May 2022 06:10:01 -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=6WaotQP90v4LJiFV9U7D4wGlm3uQ+5ZZcBUavInrV48=; b=GkS9Dk6T5hXsr71WO4ohEOtmcf4GQkkC5leKNm52YHVjDZvxLJYoEQ3ediYEvE6nGG zk9JgFuISr9b3E/V6H2IljNmyj0MylqSqLZccfHq5UZabBtXyixf5HG5+kcZX79pHprt kgQGqnS1mKzGTmpcjcElTf5q+V7WRdgzgHEX8On6IWa7NTZy6PZ3XsrwiJCiun4xE8lH r1ir3ba+aUYRNtX9KpGhkVUtNAY+7B8Sk01BoS1DbxDlt3vvVD9D06NfAa7xSraBPB/s XfGMwG4FN/YgKbj5hfjVKXVpaUYmRY9EvXvvm386wVrPrb5xQqdmnTOqG4CT2CyMw3e9 OW5A== X-Gm-Message-State: AOAM530q5ttlw4z4HPX6Kxmo8F2yhTJfqQV8kgg1lrNGEeCCRvH1OH9t WQpe4tcbYY6Zx7A4Kr7cNa1t+Ceu8YAR0bnpi5hNhSEOcKr8vXl/2+nuKoh6HYIEdJPrp5zIKM0 yZFGLAF7RFjRyoJznERZcd6M= X-Received: by 2002:a05:6000:1707:b0:20c:660c:7c78 with SMTP id n7-20020a056000170700b0020c660c7c78mr2875390wrc.150.1651497000465; Mon, 02 May 2022 06:10:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxjmpu5DF0NDgIenLWQrfwWpzc9ggipOyyqoLLJb25FhgbRN0nzSaLoP3BCeGiVSQj+kuDHOA== X-Received: by 2002:a05:6000:1707:b0:20c:660c:7c78 with SMTP id n7-20020a056000170700b0020c660c7c78mr2875372wrc.150.1651497000266; Mon, 02 May 2022 06:10:00 -0700 (PDT) Received: from minerva.home ([92.176.231.205]) by smtp.gmail.com with ESMTPSA id y16-20020adfc7d0000000b0020adc114136sm9302081wrg.0.2022.05.02.06.09.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 06:09:59 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: Maxime Ripard , Thomas Zimmermann , Javier Martinez Canillas , Alex Deucher , Changcheng Deng , Daniel Vetter , Helge Deller , Sam Ravnborg , Xiyu Yang , Zhen Lei , dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org Subject: [PATCH 1/2] fbdev: Check in file_fb_info() if the fb_info was already been freed Date: Mon, 2 May 2022 15:09:43 +0200 Message-Id: <20220502130944.363776-2-javierm@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220502130944.363776-1-javierm@redhat.com> References: <20220502130944.363776-1-javierm@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org If real driver probes, the fbdev core kicks out all drivers that are using a framebuffer that were provided by the system firmware. But it could be a user-space process still has a file descriptor for the fbdev device node. This can lead to a NULL pointer dereference, if the framebuffer device is unregistered and associated data freed, but later in the .release callback is attempted to access its struct fb_info. To prevent this, make file_fb_info() to also check the fb_info reference counter and just return NULL if this equals zero. Since that means it has already been freed. Signed-off-by: Javier Martinez Canillas Acked-by: Thomas Zimmermann --- drivers/video/fbdev/core/fbmem.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c index 84427470367b..20d8929df79f 100644 --- a/drivers/video/fbdev/core/fbmem.c +++ b/drivers/video/fbdev/core/fbmem.c @@ -751,8 +751,13 @@ static struct fb_info *file_fb_info(struct file *file) int fbidx = iminor(inode); struct fb_info *info = registered_fb[fbidx]; - if (info != file->private_data) - info = NULL; + if (!info) + return NULL; + + /* check that the fb_info has not changed or was already freed */ + if (info != file->private_data || refcount_read(&info->count) == 0) + return NULL; + return info; } From patchwork Mon May 2 13:09:44 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: 12834178 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 25F3EC433F5 for ; Mon, 2 May 2022 13:10:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234947AbiEBNNe (ORCPT ); Mon, 2 May 2022 09:13:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385171AbiEBNNd (ORCPT ); Mon, 2 May 2022 09:13:33 -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 0BF8F1929B for ; Mon, 2 May 2022 06:10:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651497004; 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=9jDUUd5wUXo34iLbUpw6jbiGIGpSLkimligm8emfXD8=; b=ZWl/aEnmro5ACWR9UlnTh6c+x5G79ScezfLP8Sg9f+3ysj4wEynuSdSexBiPcLYZWHd/oc 1VGVzm27h57S52KgASVBulwafzbixZbTe3qFSp5/w6MU0gDBifRpC8+QUOY2Sk8NYB0259 Kzhuzp7n/S7rUGRnLn+HWclX2TYQkyQ= 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-338-txYOYMtNM2S6nzvDyddF-w-1; Mon, 02 May 2022 09:10:03 -0400 X-MC-Unique: txYOYMtNM2S6nzvDyddF-w-1 Received: by mail-wm1-f71.google.com with SMTP id r186-20020a1c44c3000000b00393f52ed5ceso9202057wma.7 for ; Mon, 02 May 2022 06:10:02 -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=9jDUUd5wUXo34iLbUpw6jbiGIGpSLkimligm8emfXD8=; b=Ijjmj++LGJp1kk+jWJX3lkzb9Mz8mZMC35u7DEOE0SSbmEt7Qb3cpAl+fyuS+21xCP 1scpp1GEiZ2J7ELpobCw/x+ksJrAnm3GnURKim31I/oP4J21U5bwauXW868oMeSXE/dy u+lIcTc92ME2DJBZkkFah36f3Wnec+/V/zQ2oVema4uKdX3mOx4SN1aZF1q8R/gSKojd kw0dzoWBYP77VdBj1Pq3px9KtudRgH+qEHs4dRjlSDCaFNazmaASSvmS07zBRouZt2oH n0RBQaYmMUWHYFVohX64ipgcL4EqN9jJbDk932rppE2d1Pah30+BOJXEoVYmgqftTbdN jO4Q== X-Gm-Message-State: AOAM5325eetzxkgsLdYuilWTQeSbod2WkMlZDP7bTHbgn+HXlKNDBjVm pkmEyW54N8KkkAtmp2hD8PS2B9p/J0KRN3uq8kgvMgmxbg0V+RYVOUl6N873Lk3EoyiPZKw46B0 xrbB8xJzatZtCL2D6DPcMVZo= X-Received: by 2002:adf:d1c9:0:b0:203:c225:d042 with SMTP id b9-20020adfd1c9000000b00203c225d042mr9246075wrd.630.1651497001637; Mon, 02 May 2022 06:10:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOkTguHAtECK69/s/1UYa4VaS8/fQLKinHt0F/MRa24f9/shVWIWvvemP/4uJ44EqjTVqlaQ== X-Received: by 2002:adf:d1c9:0:b0:203:c225:d042 with SMTP id b9-20020adfd1c9000000b00203c225d042mr9246054wrd.630.1651497001394; Mon, 02 May 2022 06:10:01 -0700 (PDT) Received: from minerva.home ([92.176.231.205]) by smtp.gmail.com with ESMTPSA id y16-20020adfc7d0000000b0020adc114136sm9302081wrg.0.2022.05.02.06.10.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 06:10:01 -0700 (PDT) From: Javier Martinez Canillas To: linux-kernel@vger.kernel.org Cc: Maxime Ripard , Thomas Zimmermann , Javier Martinez Canillas , Alex Deucher , Changcheng Deng , Daniel Vetter , Guenter Roeck , Helge Deller , Sam Ravnborg , Zhen Lei , Zheyu Ma , dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org Subject: [PATCH 2/2] fbdev: Make fb_release() return -ENODEV if fbdev was unregistered Date: Mon, 2 May 2022 15:09:44 +0200 Message-Id: <20220502130944.363776-3-javierm@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220502130944.363776-1-javierm@redhat.com> References: <20220502130944.363776-1-javierm@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fbdev@vger.kernel.org A reference to the framebuffer device struct fb_info is stored in the file private data, but this reference could no longer be valid and must not be accessed directly. Instead, the file_fb_info() accessor function must be used since it does sanity checking to make sure that the fb_info is valid. This can happen for example if the fbdev driver was one that is using a framebuffer provided by the system firmware. In that case, the fbdev core could unregister the framebuffer device if a real video driver is probed. Reported-by: Maxime Ripard Signed-off-by: Javier Martinez Canillas Reviewed-by: Thomas Zimmermann Reported-by: Junxiao Chang --- drivers/video/fbdev/core/fbmem.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c index 20d8929df79f..d68097105f93 100644 --- a/drivers/video/fbdev/core/fbmem.c +++ b/drivers/video/fbdev/core/fbmem.c @@ -1439,7 +1439,10 @@ fb_release(struct inode *inode, struct file *file) __acquires(&info->lock) __releases(&info->lock) { - struct fb_info * const info = file->private_data; + struct fb_info * const info = file_fb_info(file); + + if (!info) + return -ENODEV; lock_fb_info(info); if (info->fbops->fb_release)