2022-03-28 11:45:19

by Xiaomeng Tong

[permalink] [raw]
Subject: [PATCH] dpu1: dpu_encoder: fix a missing check on list iterator

The bug is here:
cstate = to_dpu_crtc_state(drm_crtc->state);

For the drm_for_each_crtc(), just like list_for_each_entry(),
the list iterator 'drm_crtc' will point to a bogus position
containing HEAD if the list is empty or no element is found.
This case must be checked before any use of the iterator,
otherwise it will lead to a invalid memory access.

To fix this bug, use a new variable 'iter' as the list iterator,
while use the origin variable 'drm_crtc' as a dedicated pointer
to point to the found element.

Cc: [email protected]
Fixes: b107603b4ad0f ("drm/msm/dpu: map mixer/ctl hw blocks in encoder modeset")
Signed-off-by: Xiaomeng Tong <[email protected]>
---
drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c b/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
index 1e648db439f9..d3fdb18e96f9 100644
--- a/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
+++ b/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
@@ -965,7 +965,7 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
struct dpu_kms *dpu_kms;
struct list_head *connector_list;
struct drm_connector *conn = NULL, *conn_iter;
- struct drm_crtc *drm_crtc;
+ struct drm_crtc *drm_crtc = NULL, *iter;
struct dpu_crtc_state *cstate;
struct dpu_global_state *global_state;
struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
@@ -1007,9 +1007,14 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
return;
}

- drm_for_each_crtc(drm_crtc, drm_enc->dev)
- if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
+ drm_for_each_crtc(iter, drm_enc->dev)
+ if (iter->state->encoder_mask & drm_encoder_mask(drm_enc)) {
+ drm_crtc = iter;
break;
+ }
+
+ if (!drm_crtc)
+ return;

/* Query resource that have been reserved in atomic check step. */
num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
--
2.17.1


2022-04-12 07:05:49

by Dmitry Baryshkov

[permalink] [raw]
Subject: Re: [PATCH] dpu1: dpu_encoder: fix a missing check on list iterator

On 27/03/2022 10:32, Xiaomeng Tong wrote:
> The bug is here:
> cstate = to_dpu_crtc_state(drm_crtc->state);
>
> For the drm_for_each_crtc(), just like list_for_each_entry(),
> the list iterator 'drm_crtc' will point to a bogus position
> containing HEAD if the list is empty or no element is found.
> This case must be checked before any use of the iterator,
> otherwise it will lead to a invalid memory access.
>
> To fix this bug, use a new variable 'iter' as the list iterator,
> while use the origin variable 'drm_crtc' as a dedicated pointer
> to point to the found element.
>
> Cc: [email protected]
> Fixes: b107603b4ad0f ("drm/msm/dpu: map mixer/ctl hw blocks in encoder modeset")
> Signed-off-by: Xiaomeng Tong <[email protected]>

Reviewed-by: Dmitry Baryshkov <[email protected]>

> ---
> drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c b/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
> index 1e648db439f9..d3fdb18e96f9 100644
> --- a/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
> +++ b/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
> @@ -965,7 +965,7 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
> struct dpu_kms *dpu_kms;
> struct list_head *connector_list;
> struct drm_connector *conn = NULL, *conn_iter;
> - struct drm_crtc *drm_crtc;
> + struct drm_crtc *drm_crtc = NULL, *iter;
> struct dpu_crtc_state *cstate;
> struct dpu_global_state *global_state;
> struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
> @@ -1007,9 +1007,14 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
> return;
> }
>
> - drm_for_each_crtc(drm_crtc, drm_enc->dev)
> - if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
> + drm_for_each_crtc(iter, drm_enc->dev)
> + if (iter->state->encoder_mask & drm_encoder_mask(drm_enc)) {
> + drm_crtc = iter;
> break;
> + }
> +
> + if (!drm_crtc)
> + return;
>
> /* Query resource that have been reserved in atomic check step. */
> num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,


--
With best wishes
Dmitry

2022-04-12 07:59:49

by Dmitry Baryshkov

[permalink] [raw]
Subject: Re: [PATCH] dpu1: dpu_encoder: fix a missing check on list iterator

On 11/04/2022 03:56, Dmitry Baryshkov wrote:
> On 27/03/2022 10:32, Xiaomeng Tong wrote:
>> The bug is here:
>>      cstate = to_dpu_crtc_state(drm_crtc->state);
>>
>> For the drm_for_each_crtc(), just like list_for_each_entry(),
>> the list iterator 'drm_crtc' will point to a bogus position
>> containing HEAD if the list is empty or no element is found.
>> This case must be checked before any use of the iterator,
>> otherwise it will lead to a invalid memory access.
>>
>> To fix this bug, use a new variable 'iter' as the list iterator,
>> while use the origin variable 'drm_crtc' as a dedicated pointer
>> to point to the found element.
>>
>> Cc: [email protected]
>> Fixes: b107603b4ad0f ("drm/msm/dpu: map mixer/ctl hw blocks in encoder
>> modeset")
>> Signed-off-by: Xiaomeng Tong <[email protected]>
>
> Reviewed-by: Dmitry Baryshkov <[email protected]>

On the other hand, this code has been removed in 5.18-rc1 in the commit
764332bf96244cbc8baf08aa35844b29106da312.

>
>> ---
>>   drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c | 11 ++++++++---
>>   1 file changed, 8 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
>> b/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
>> index 1e648db439f9..d3fdb18e96f9 100644
>> --- a/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
>> +++ b/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c
>> @@ -965,7 +965,7 @@ static void dpu_encoder_virt_mode_set(struct
>> drm_encoder *drm_enc,
>>       struct dpu_kms *dpu_kms;
>>       struct list_head *connector_list;
>>       struct drm_connector *conn = NULL, *conn_iter;
>> -    struct drm_crtc *drm_crtc;
>> +    struct drm_crtc *drm_crtc = NULL, *iter;
>>       struct dpu_crtc_state *cstate;
>>       struct dpu_global_state *global_state;
>>       struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
>> @@ -1007,9 +1007,14 @@ static void dpu_encoder_virt_mode_set(struct
>> drm_encoder *drm_enc,
>>           return;
>>       }
>> -    drm_for_each_crtc(drm_crtc, drm_enc->dev)
>> -        if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
>> +    drm_for_each_crtc(iter, drm_enc->dev)
>> +        if (iter->state->encoder_mask & drm_encoder_mask(drm_enc)) {
>> +            drm_crtc = iter;
>>               break;
>> +        }
>> +
>> +    if (!drm_crtc)
>> +        return;
>>       /* Query resource that have been reserved in atomic check step. */
>>       num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
>
>


--
With best wishes
Dmitry