Skip to contentSkip to conversation

drm/amdkfd: Check bounds in allocate_event_notification_slot

PATCH Superseded David Francis 2 participants 4 messages May 21 – May 25

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: Check bounds in allocate_event_notification_slot

The valid event ids go from 0 to signal_mapped_size / 8 (usually 256).

allocate_event_notification_slot has an option to specify an event id to allocate at, used by CRIU. We weren't checking the bounds on that value.

Check them.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +3 -0
index e9be798c0a2b..5a4fe68a7986 100644
@@ -107,6 +107,9 @@ static int allocate_event_notification_slot(struct kfd_process *p,
 	}
 
 	if (restore_id) {
+		if (*restore_id < 0 || *restore_id >= p->signal_mapped_size / 8)
+			return -EINVAL;
+
 		id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
 				GFP_KERNEL);
 	} else {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 19 lines · David Francis

The valid event ids go from 0 to signal_mapped_size / 8 (usually 256).

allocate_event_notification_slot has an option to specify an event id to allocate at, used by CRIU. We weren't checking the bounds on that value.

Check them.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
index e9be798c0a2b..5a4fe68a7986 100644

drivers/gpu/drm/amd/amdkfd/kfd_events.c static int allocate_event_notification_slot(struct kfd_process *p, · line 115

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +3 -0
@@ -107,6 +107,9 @@ static int allocate_event_notification_slot(struct kfd_process *p,
 	}
 
 	if (restore_id) {
+		if (*restore_id < 0 || *restore_id >= p->signal_mapped_size / 8)
+			return -EINVAL;
+
 		id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
 				GFP_KERNEL);
 	} else {

This may be a dumb question, but the use of the * means "restore_id" is dereferenced. If the conditions are evaluated, either will result in restore_id being dereferenced, even if both conditions are false. Then can *restore_id still be safely dereferenced in the idr_alloc call?

#2 lore raw
Quoted 4 lines

This may be a dumb question, but the use of the * means "restore_id" is dereferenced. If the conditions are evaluated, either will result in restore_id being dereferenced, even if both conditions are false. Then can *restore_id still be safely dereferenced in the idr_alloc call?

restore_id is a normal pointer to an int. Dereferencing it should have no side effects. So this is perfectly safe

David Francis

________________________________________
From: Joshua Peisach <[email protected]>
Sent: Thursday, May 21, 2026 9:35 AM
To: Francis, David; [email protected]
Cc: amd-gfx
Subject: Re: [PATCH] drm/amdkfd: Check bounds in allocate_event_notification_slot

Quoted 19 lines · David Francis

The valid event ids go from 0 to signal_mapped_size / 8 (usually 256).

allocate_event_notification_slot has an option to specify an event id to allocate at, used by CRIU. We weren't checking the bounds on that value.

Check them.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
index e9be798c0a2b..5a4fe68a7986 100644

drivers/gpu/drm/amd/amdkfd/kfd_events.c static int allocate_event_notification_slot(struct kfd_process *p, · line 115

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +3 -0
@@ -107,6 +107,9 @@ static int allocate_event_notification_slot(struct kfd_process *p,
      }
 
      if (restore_id) {
+             if (*restore_id < 0 || *restore_id >= p->signal_mapped_size / 8)
+                     return -EINVAL;
+
              id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
                              GFP_KERNEL);
      } else {

This may be a dumb question, but the use of the * means "restore_id" is dereferenced. If the conditions are evaluated, either will result in restore_id being dereferenced, even if both conditions are false. Then can *restore_id still be safely dereferenced in the idr_alloc call?

Reviewed-by
#3 lore raw
Quoted 10 lines · David Francis
Quoted 4 lines

This may be a dumb question, but the use of the * means "restore_id" is dereferenced. If the conditions are evaluated, either will result in restore_id being dereferenced, even if both conditions are false. Then can *restore_id still be safely dereferenced in the idr_alloc call?

restore_id is a normal pointer to an int. Dereferencing it should have no side effects. So this is perfectly safe

David Francis

Ok. (I tested this myself too with a simple C program).

And I see above the condition is a check for p->signal_page, so:

Quoted 41 lines

________________________________________
From: Joshua Peisach <[email protected]>
Sent: Thursday, May 21, 2026 9:35 AM
To: Francis, David; [email protected]
Cc: amd-gfx
Subject: Re: [PATCH] drm/amdkfd: Check bounds in allocate_event_notification_slot

Quoted 19 lines · David Francis

The valid event ids go from 0 to signal_mapped_size / 8 (usually 256).

allocate_event_notification_slot has an option to specify an event id to allocate at, used by CRIU. We weren't checking the bounds on that value.

Check them.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
index e9be798c0a2b..5a4fe68a7986 100644

drivers/gpu/drm/amd/amdkfd/kfd_events.c static int allocate_event_notification_slot(struct kfd_process *p, · line 115

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +3 -0
@@ -107,6 +107,9 @@ static int allocate_event_notification_slot(struct kfd_process *p,
      }
 
      if (restore_id) {
+             if (*restore_id < 0 || *restore_id >= p->signal_mapped_size / 8)
+                     return -EINVAL;
+
              id = idr_alloc(&p->event_idr, ev, *restore_id, *restore_id + 1,
                              GFP_KERNEL);
      } else {

This may be a dumb question, but the use of the * means "restore_id" is dereferenced. If the conditions are evaluated, either will result in restore_id being dereferenced, even if both conditions are false. Then can *restore_id still be safely dereferenced in the idr_alloc call?