* [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches
2026-03-30 19:57 [PATCH v4 1/5] drm/amdgpu: Add bounds checking to ib_{get,set}_value Benjamin Cheng
@ 2026-03-30 19:57 ` Benjamin Cheng
2026-03-30 20:31 ` Alex Deucher
2026-07-23 10:55 ` Dan Carpenter
2026-03-30 19:57 ` [PATCH v4 3/5] drm/amdgpu/vcn3: Prevent OOB reads when parsing dec msg Benjamin Cheng
` (2 subsequent siblings)
3 siblings, 2 replies; 8+ messages in thread
From: Benjamin Cheng @ 2026-03-30 19:57 UTC (permalink / raw)
To: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx
Cc: Leo Liu, Ruijing Dong, Benjamin Cheng
In the case that only one of lo/hi is valid, the patching could result
in a bad address written to in FW.
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
index eb4a15db2ef2..efdebd9c0a1f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
@@ -680,6 +680,9 @@ static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, struct amdgpu_ib *ib,
uint64_t addr;
int r;
+ if (lo >= ib->length_dw || hi >= ib->length_dw)
+ return -EINVAL;
+
if (index == 0xffffffff)
index = 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches
2026-03-30 19:57 ` [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches Benjamin Cheng
@ 2026-03-30 20:31 ` Alex Deucher
2026-07-23 10:55 ` Dan Carpenter
1 sibling, 0 replies; 8+ messages in thread
From: Alex Deucher @ 2026-03-30 20:31 UTC (permalink / raw)
To: Benjamin Cheng
Cc: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx, Leo Liu, Ruijing Dong
On Mon, Mar 30, 2026 at 4:09 PM Benjamin Cheng <benjamin.cheng@amd.com> wrote:
>
> In the case that only one of lo/hi is valid, the patching could result
> in a bad address written to in FW.
Missing your S-o-b. WIth that fixed:
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
> ---
> drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
> index eb4a15db2ef2..efdebd9c0a1f 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
> @@ -680,6 +680,9 @@ static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, struct amdgpu_ib *ib,
> uint64_t addr;
> int r;
>
> + if (lo >= ib->length_dw || hi >= ib->length_dw)
> + return -EINVAL;
> +
> if (index == 0xffffffff)
> index = 0;
>
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches
2026-03-30 19:57 ` [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches Benjamin Cheng
2026-03-30 20:31 ` Alex Deucher
@ 2026-07-23 10:55 ` Dan Carpenter
2026-07-23 12:09 ` Benjamin Cheng
1 sibling, 1 reply; 8+ messages in thread
From: Dan Carpenter @ 2026-07-23 10:55 UTC (permalink / raw)
To: Benjamin Cheng
Cc: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx, Leo Liu, Ruijing Dong, Harshit Mogalapalli
On Mon, Mar 30, 2026 at 03:57:54PM -0400, Benjamin Cheng wrote:
> In the case that only one of lo/hi is valid, the patching could result
> in a bad address written to in FW.
> ---
> drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
> index eb4a15db2ef2..efdebd9c0a1f 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
> @@ -680,6 +680,9 @@ static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, struct amdgpu_ib *ib,
> uint64_t addr;
> int r;
>
> + if (lo >= ib->length_dw || hi >= ib->length_dw)
> + return -EINVAL;
> +
I feel like the better fix would be to change the amdgpu_ib_get_value()
function to return negative for invalid indexes instead of returning 0.
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
562 static inline u32 amdgpu_ib_get_value(struct amdgpu_ib *ib, uint32_t idx)
563 {
564 if (idx < ib->length_dw)
565 return ib->ptr[idx];
566 return 0;
s/return 0/return -EINVAL/
567 }
amdgpu_vce_validate_bo() has the same issue where invalid lo hi are
treated as success.
regards,
dan carpenter
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches
2026-07-23 10:55 ` Dan Carpenter
@ 2026-07-23 12:09 ` Benjamin Cheng
0 siblings, 0 replies; 8+ messages in thread
From: Benjamin Cheng @ 2026-07-23 12:09 UTC (permalink / raw)
To: Dan Carpenter
Cc: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx, Leo Liu, Ruijing Dong, Harshit Mogalapalli
On 2026-07-23 06:55, Dan Carpenter wrote:
> [You don't often get email from error27@gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> On Mon, Mar 30, 2026 at 03:57:54PM -0400, Benjamin Cheng wrote:
>> In the case that only one of lo/hi is valid, the patching could result
>> in a bad address written to in FW.
>> ---
>> drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 3 +++
>> 1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
>> index eb4a15db2ef2..efdebd9c0a1f 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c
>> @@ -680,6 +680,9 @@ static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, struct amdgpu_ib *ib,
>> uint64_t addr;
>> int r;
>>
>> + if (lo >= ib->length_dw || hi >= ib->length_dw)
>> + return -EINVAL;
>> +
> I feel like the better fix would be to change the amdgpu_ib_get_value()
> function to return negative for invalid indexes instead of returning 0.
>
> drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
> 562 static inline u32 amdgpu_ib_get_value(struct amdgpu_ib *ib, uint32_t idx)
> 563 {
> 564 if (idx < ib->length_dw)
> 565 return ib->ptr[idx];
> 566 return 0;
>
> s/return 0/return -EINVAL/
>
> 567 }
>
> amdgpu_vce_validate_bo() has the same issue where invalid lo hi are
> treated as success.
That catches the issue earlier which can save some CPU cycles but amdgpu_vce_validate_bo() is supposed to be resilient to bad addresses since the user could just put bogus addresses into the IB. In this case we just need to prevent a partial address patch, i.e. the hi part of the address got patched with the physical address but the lo part did not (because it's outside the IB).
>
> regards,
> dan carpenter
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 3/5] drm/amdgpu/vcn3: Prevent OOB reads when parsing dec msg
2026-03-30 19:57 [PATCH v4 1/5] drm/amdgpu: Add bounds checking to ib_{get,set}_value Benjamin Cheng
2026-03-30 19:57 ` [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches Benjamin Cheng
@ 2026-03-30 19:57 ` Benjamin Cheng
2026-03-30 19:57 ` [PATCH v4 4/5] drm/amdgpu/vcn4: " Benjamin Cheng
2026-03-30 19:57 ` [PATCH v4 5/5] drm/amdgpu/vcn4: Prevent OOB reads when parsing IB Benjamin Cheng
3 siblings, 0 replies; 8+ messages in thread
From: Benjamin Cheng @ 2026-03-30 19:57 UTC (permalink / raw)
To: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx
Cc: Leo Liu, Ruijing Dong, Benjamin Cheng
Check bounds against the end of the BO whenever we access the msg.
Signed-off-by: Benjamin Cheng <benjamin.cheng@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Ruijing Dong <ruijing.dong@amd.com>
---
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c b/drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c
index 02d5c5af65f2..6fb4fcdbba4f 100644
--- a/drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c
@@ -1909,7 +1909,7 @@ static int vcn_v3_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
struct ttm_operation_ctx ctx = { false, false };
struct amdgpu_device *adev = p->adev;
struct amdgpu_bo_va_mapping *map;
- uint32_t *msg, num_buffers;
+ uint32_t *msg, num_buffers, len_dw;
struct amdgpu_bo *bo;
uint64_t start, end;
unsigned int i;
@@ -1930,6 +1930,11 @@ static int vcn_v3_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
return -EINVAL;
}
+ if (end - addr < 16) {
+ DRM_ERROR("VCN messages must be at least 4 DWORDs!\n");
+ return -EINVAL;
+ }
+
bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
@@ -1946,8 +1951,8 @@ static int vcn_v3_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
msg = ptr + addr - start;
- /* Check length */
if (msg[1] > end - addr) {
+ DRM_ERROR("VCN message header does not fit in BO!\n");
r = -EINVAL;
goto out;
}
@@ -1955,7 +1960,16 @@ static int vcn_v3_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
if (msg[3] != RDECODE_MSG_CREATE)
goto out;
+ len_dw = msg[1] / 4;
num_buffers = msg[2];
+
+ /* Verify that all indices fit within the claimed length. Each index is 4 DWORDs */
+ if (num_buffers > len_dw || 6 + num_buffers * 4 > len_dw) {
+ DRM_ERROR("VCN message has too many buffers!\n");
+ r = -EINVAL;
+ goto out;
+ }
+
for (i = 0, msg = &msg[6]; i < num_buffers; ++i, msg += 4) {
uint32_t offset, size, *create;
@@ -1965,14 +1979,15 @@ static int vcn_v3_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
offset = msg[1];
size = msg[2];
- if (offset + size > end) {
+ if (size < 4 || offset + size > end - addr) {
+ DRM_ERROR("VCN message buffer exceeds BO bounds!\n");
r = -EINVAL;
goto out;
}
create = ptr + addr + offset - start;
- /* H246, HEVC and VP9 can run on any instance */
+ /* H264, HEVC and VP9 can run on any instance */
if (create[0] == 0x7 || create[0] == 0x10 || create[0] == 0x11)
continue;
--
2.53.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v4 4/5] drm/amdgpu/vcn4: Prevent OOB reads when parsing dec msg
2026-03-30 19:57 [PATCH v4 1/5] drm/amdgpu: Add bounds checking to ib_{get,set}_value Benjamin Cheng
2026-03-30 19:57 ` [PATCH v4 2/5] drm/amdgpu/vce: Prevent partial address patches Benjamin Cheng
2026-03-30 19:57 ` [PATCH v4 3/5] drm/amdgpu/vcn3: Prevent OOB reads when parsing dec msg Benjamin Cheng
@ 2026-03-30 19:57 ` Benjamin Cheng
2026-03-30 19:57 ` [PATCH v4 5/5] drm/amdgpu/vcn4: Prevent OOB reads when parsing IB Benjamin Cheng
3 siblings, 0 replies; 8+ messages in thread
From: Benjamin Cheng @ 2026-03-30 19:57 UTC (permalink / raw)
To: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx
Cc: Leo Liu, Ruijing Dong, Benjamin Cheng
Check bounds against the end of the BO whenever we access the msg.
Signed-off-by: Benjamin Cheng <benjamin.cheng@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Ruijing Dong <ruijing.dong@amd.com>
---
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c b/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
index d17219be50f3..1a1cdc14841a 100644
--- a/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
@@ -1826,7 +1826,7 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
struct ttm_operation_ctx ctx = { false, false };
struct amdgpu_device *adev = p->adev;
struct amdgpu_bo_va_mapping *map;
- uint32_t *msg, num_buffers;
+ uint32_t *msg, num_buffers, len_dw;
struct amdgpu_bo *bo;
uint64_t start, end;
unsigned int i;
@@ -1847,6 +1847,11 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
return -EINVAL;
}
+ if (end - addr < 16) {
+ DRM_ERROR("VCN messages must be at least 4 DWORDs!\n");
+ return -EINVAL;
+ }
+
bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
@@ -1863,8 +1868,8 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
msg = ptr + addr - start;
- /* Check length */
if (msg[1] > end - addr) {
+ DRM_ERROR("VCN message header does not fit in BO!\n");
r = -EINVAL;
goto out;
}
@@ -1872,7 +1877,16 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
if (msg[3] != RDECODE_MSG_CREATE)
goto out;
+ len_dw = msg[1] / 4;
num_buffers = msg[2];
+
+ /* Verify that all indices fit within the claimed length. Each index is 4 DWORDs */
+ if (num_buffers > len_dw || 6 + num_buffers * 4 > len_dw) {
+ DRM_ERROR("VCN message has too many buffers!\n");
+ r = -EINVAL;
+ goto out;
+ }
+
for (i = 0, msg = &msg[6]; i < num_buffers; ++i, msg += 4) {
uint32_t offset, size, *create;
@@ -1882,7 +1896,8 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
offset = msg[1];
size = msg[2];
- if (offset + size > end) {
+ if (size < 4 || offset + size > end - addr) {
+ DRM_ERROR("VCN message buffer exceeds BO bounds!\n");
r = -EINVAL;
goto out;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v4 5/5] drm/amdgpu/vcn4: Prevent OOB reads when parsing IB
2026-03-30 19:57 [PATCH v4 1/5] drm/amdgpu: Add bounds checking to ib_{get,set}_value Benjamin Cheng
` (2 preceding siblings ...)
2026-03-30 19:57 ` [PATCH v4 4/5] drm/amdgpu/vcn4: " Benjamin Cheng
@ 2026-03-30 19:57 ` Benjamin Cheng
3 siblings, 0 replies; 8+ messages in thread
From: Benjamin Cheng @ 2026-03-30 19:57 UTC (permalink / raw)
To: Alex Deucher, Christian König, David (Ming Qiang) Wu,
amd-gfx
Cc: Leo Liu, Ruijing Dong, Benjamin Cheng
Rewrite the IB parsing to use amdgpu_ib_get_value() which handles the
bounds checks.
Signed-off-by: Benjamin Cheng <benjamin.cheng@amd.com>
Acked-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Ruijing Dong <ruijing.dong@amd.com>
---
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c b/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
index 1a1cdc14841a..5dec92691f73 100644
--- a/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
@@ -1928,9 +1928,10 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
static int vcn_v4_0_enc_find_ib_param(struct amdgpu_ib *ib, uint32_t id, int start)
{
int i;
+ uint32_t len;
- for (i = start; i < ib->length_dw && ib->ptr[i] >= 8; i += ib->ptr[i] / 4) {
- if (ib->ptr[i + 1] == id)
+ for (i = start; (len = amdgpu_ib_get_value(ib, i)) >= 8; i += len / 4) {
+ if (amdgpu_ib_get_value(ib, i + 1) == id)
return i;
}
return -1;
@@ -1941,8 +1942,6 @@ static int vcn_v4_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
struct amdgpu_ib *ib)
{
struct amdgpu_ring *ring = amdgpu_job_ring(job);
- struct amdgpu_vcn_decode_buffer *decode_buffer;
- uint64_t addr;
uint32_t val;
int idx = 0, sidx;
@@ -1953,20 +1952,22 @@ static int vcn_v4_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
while ((idx = vcn_v4_0_enc_find_ib_param(ib, RADEON_VCN_ENGINE_INFO, idx)) >= 0) {
val = amdgpu_ib_get_value(ib, idx + 2); /* RADEON_VCN_ENGINE_TYPE */
if (val == RADEON_VCN_ENGINE_TYPE_DECODE) {
- decode_buffer = (struct amdgpu_vcn_decode_buffer *)&ib->ptr[idx + 6];
+ uint32_t valid_buf_flag = amdgpu_ib_get_value(ib, idx + 6);
+ uint64_t msg_buffer_addr;
- if (!(decode_buffer->valid_buf_flag & 0x1))
+ if (!(valid_buf_flag & 0x1))
return 0;
- addr = ((u64)decode_buffer->msg_buffer_address_hi) << 32 |
- decode_buffer->msg_buffer_address_lo;
- return vcn_v4_0_dec_msg(p, job, addr);
+ msg_buffer_addr = ((u64)amdgpu_ib_get_value(ib, idx + 7)) << 32 |
+ amdgpu_ib_get_value(ib, idx + 8);
+ return vcn_v4_0_dec_msg(p, job, msg_buffer_addr);
} else if (val == RADEON_VCN_ENGINE_TYPE_ENCODE) {
sidx = vcn_v4_0_enc_find_ib_param(ib, RENCODE_IB_PARAM_SESSION_INIT, idx);
- if (sidx >= 0 && ib->ptr[sidx + 2] == RENCODE_ENCODE_STANDARD_AV1)
+ if (sidx >= 0 &&
+ amdgpu_ib_get_value(ib, sidx + 2) == RENCODE_ENCODE_STANDARD_AV1)
return vcn_v4_0_limit_sched(p, job);
}
- idx += ib->ptr[idx] / 4;
+ idx += amdgpu_ib_get_value(ib, idx) / 4;
}
return 0;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 8+ messages in thread