Skip to contentSkip to conversation

drm/ttm: add a WARN_ON in ttm_set_driver_manager when array bounds (v2)

PATCH In review Guchun Chen 3 participants 4 messages Sep 10, 2021 – Sep 13, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ttm: add a WARN_ON in ttm_set_driver_manager when array bounds (v2)

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

v2: lower check level to WARN_ON

---
 include/drm/ttm/ttm_device.h | 1 +
 1 file changed, 1 insertion(+)
include/drm/ttm/ttm_device.h +1 -0
index 07d722950d5b..aa79953c807c 100644
@@ -291,6 +291,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
 					  struct ttm_resource_manager *manager)
 {
+	WARN_ON(type >= TTM_NUM_MEM_TYPES);
 	bdev->man_drv[type] = manager;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 18 lines · Guchun Chen

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

v2: lower check level to WARN_ON

---
  include/drm/ttm/ttm_device.h | 1 +
  1 file changed, 1 insertion(+)
include/drm/ttm/ttm_device.h +0 -0
index 07d722950d5b..aa79953c807c 100644

include/drm/ttm/ttm_device.h ttm_manager_type(struct ttm_device *bdev, int mem_type) · line 294

Quoted 4 lines
include/drm/ttm/ttm_device.h +1 -0
@@ -291,6 +291,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
  static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
  					  struct ttm_resource_manager *manager)
  {
+	WARN_ON(type >= TTM_NUM_MEM_TYPES);

Nit: I know nothing about this code, but from the context alone it would seem sensible to do

	if (WARN_ON(type >= TTM_NUM_MEM_TYPES))
		return;

to avoid making the subsequent assignment when we *know* it's invalid and likely to corrupt memory.

Robin.

Quoted 4 lines
include/drm/ttm/ttm_device.h +0 -0
  	bdev->man_drv[type] = manager;
  }
 
#2 lore raw

[Public]

Thanks for your suggestion, Robin. Do you agree with this as well, Christian and Xinhui?

Regards,
Guchun

-----Original Message-----
From: Robin Murphy <[email protected]>
Sent: Saturday, September 11, 2021 2:25 AM
To: Chen, Guchun <[email protected]>; [email protected]; [email protected]; Koenig, Christian <[email protected]>; Pan, Xinhui <[email protected]>; Deucher, Alexander <[email protected]>
Cc: Shi, Leslie <[email protected]>
Subject: Re: [PATCH] drm/ttm: add a WARN_ON in ttm_set_driver_manager when array bounds (v2)

Quoted 18 lines · Guchun Chen

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

v2: lower check level to WARN_ON

---
  include/drm/ttm/ttm_device.h | 1 +
  1 file changed, 1 insertion(+)
(quoted diff) +0 -0

b/include/drm/ttm/ttm_device.h index 07d722950d5b..aa79953c807c 100644

include/drm/ttm/ttm_device.h ttm_manager_type(struct ttm_device *bdev, int mem_type) · line 294

Quoted 4 lines
include/drm/ttm/ttm_device.h +1 -0
@@ -291,6 +291,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
  static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
  					  struct ttm_resource_manager *manager)
  {
+	WARN_ON(type >= TTM_NUM_MEM_TYPES);

Nit: I know nothing about this code, but from the context alone it would seem sensible to do

	if (WARN_ON(type >= TTM_NUM_MEM_TYPES))
		return;

to avoid making the subsequent assignment when we *know* it's invalid and likely to corrupt memory.

Robin.

Quoted 4 lines
include/drm/ttm/ttm_device.h +0 -0
  	bdev->man_drv[type] = manager;
  }
 
#3 lore raw

Well it will crash later on when accessing the invalid offset, so not much gained.

But either way works for me.

Christian.

Am 13.09.21 um 04:35 schrieb Chen, Guchun:

Quoted 50 lines

[Public]

Thanks for your suggestion, Robin. Do you agree with this as well, Christian and Xinhui?

Regards,
Guchun

-----Original Message-----
From: Robin Murphy <[email protected]>
Sent: Saturday, September 11, 2021 2:25 AM
To: Chen, Guchun <[email protected]>; [email protected]; [email protected]; Koenig, Christian <[email protected]>; Pan, Xinhui <[email protected]>; Deucher, Alexander <[email protected]>
Cc: Shi, Leslie <[email protected]>
Subject: Re: [PATCH] drm/ttm: add a WARN_ON in ttm_set_driver_manager when array bounds (v2)

Quoted 18 lines · Guchun Chen

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

v2: lower check level to WARN_ON

---
   include/drm/ttm/ttm_device.h | 1 +
   1 file changed, 1 insertion(+)
(quoted diff) +0 -0

b/include/drm/ttm/ttm_device.h index 07d722950d5b..aa79953c807c 100644

include/drm/ttm/ttm_device.h ttm_manager_type(struct ttm_device *bdev, int mem_type) · line 294

Quoted 4 lines
include/drm/ttm/ttm_device.h +1 -0
@@ -291,6 +291,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
   static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
   					  struct ttm_resource_manager *manager)
   {
+	WARN_ON(type >= TTM_NUM_MEM_TYPES);

Nit: I know nothing about this code, but from the context alone it would seem sensible to do

	if (WARN_ON(type >= TTM_NUM_MEM_TYPES))
		return;

to avoid making the subsequent assignment when we *know* it's invalid and likely to corrupt memory.

Robin.

Quoted 4 lines
include/drm/ttm/ttm_device.h +0 -0
   	bdev->man_drv[type] = manager;
   }