5.15-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Zack Rusin <zack.ru...@broadcom.com>

[ Upstream commit 517621b7060096e48e42f545fa6646fc00252eac ]

vmw_context_cotable can return either an error or a null pointer and its
usage sometimes went unchecked. Subsequent code would then try to access
either a null pointer or an error value.

The invalid dereferences were only possible with malformed userspace
apps which never properly initialized the rendering contexts.

Check the results of vmw_context_cotable to fix the invalid derefs.

Thanks:
ziming zhang(@ezrak1e) from Ant Group Light-Year Security Lab
who was the first person to discover it.
Niels De Graef who reported it and helped to track down the poc.

Fixes: 9c079b8ce8bf ("drm/vmwgfx: Adapt execbuf to the new validation api")
Cc: <sta...@vger.kernel.org> # v4.20+
Reported-by: Niels De Graef  <ndegr...@redhat.com>
Signed-off-by: Zack Rusin <zack.ru...@broadcom.com>
Cc: Martin Krastev <martin.kras...@broadcom.com>
Cc: Maaz Mombasawala <maaz.mombasaw...@broadcom.com>
Cc: Ian Forbes <ian.for...@broadcom.com>
Cc: Broadcom internal kernel review list <bcm-kernel-feedback-l...@broadcom.com>
Cc: dri-devel@lists.freedesktop.org
Reviewed-by: Maaz Mombasawala <maaz.mombasaw...@broadcom.com>
Reviewed-by: Martin Krastev <martin.kras...@broadcom.com>
Link: 
https://patchwork.freedesktop.org/patch/msgid/20240110200305.94086-1-zack.ru...@broadcom.com
Signed-off-by: Sasha Levin <sas...@kernel.org>
---
 drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c 
b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
index b91f8d17404d6..21134c7f18382 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c
@@ -472,7 +472,7 @@ static int vmw_resource_context_res_add(struct vmw_private 
*dev_priv,
            vmw_res_type(ctx) == vmw_res_dx_context) {
                for (i = 0; i < cotable_max; ++i) {
                        res = vmw_context_cotable(ctx, i);
-                       if (IS_ERR(res))
+                       if (IS_ERR_OR_NULL(res))
                                continue;
 
                        ret = vmw_execbuf_res_noctx_val_add(sw_context, res,
@@ -1277,6 +1277,8 @@ static int vmw_cmd_dx_define_query(struct vmw_private 
*dev_priv,
                return -EINVAL;
 
        cotable_res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXQUERY);
+       if (IS_ERR_OR_NULL(cotable_res))
+               return cotable_res ? PTR_ERR(cotable_res) : -EINVAL;
        ret = vmw_cotable_notify(cotable_res, cmd->body.queryId);
 
        return ret;
@@ -2455,6 +2457,8 @@ static int vmw_cmd_dx_view_define(struct vmw_private 
*dev_priv,
                return ret;
 
        res = vmw_context_cotable(ctx_node->ctx, vmw_view_cotables[view_type]);
+       if (IS_ERR_OR_NULL(res))
+               return res ? PTR_ERR(res) : -EINVAL;
        ret = vmw_cotable_notify(res, cmd->defined_id);
        if (unlikely(ret != 0))
                return ret;
@@ -2540,8 +2544,8 @@ static int vmw_cmd_dx_so_define(struct vmw_private 
*dev_priv,
 
        so_type = vmw_so_cmd_to_type(header->id);
        res = vmw_context_cotable(ctx_node->ctx, vmw_so_cotables[so_type]);
-       if (IS_ERR(res))
-               return PTR_ERR(res);
+       if (IS_ERR_OR_NULL(res))
+               return res ? PTR_ERR(res) : -EINVAL;
        cmd = container_of(header, typeof(*cmd), header);
        ret = vmw_cotable_notify(res, cmd->defined_id);
 
@@ -2660,6 +2664,8 @@ static int vmw_cmd_dx_define_shader(struct vmw_private 
*dev_priv,
                return -EINVAL;
 
        res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_DXSHADER);
+       if (IS_ERR_OR_NULL(res))
+               return res ? PTR_ERR(res) : -EINVAL;
        ret = vmw_cotable_notify(res, cmd->body.shaderId);
        if (ret)
                return ret;
@@ -2981,6 +2987,8 @@ static int vmw_cmd_dx_define_streamoutput(struct 
vmw_private *dev_priv,
        }
 
        res = vmw_context_cotable(ctx_node->ctx, SVGA_COTABLE_STREAMOUTPUT);
+       if (IS_ERR_OR_NULL(res))
+               return res ? PTR_ERR(res) : -EINVAL;
        ret = vmw_cotable_notify(res, cmd->body.soid);
        if (ret)
                return ret;
-- 
2.43.0



Reply via email to