Skip to contentSkip to conversation

drm/amdgpu: reject mapping info for unmapped BOs

PATCH In review Yousef Alhouseen 3 participants 10 messages Jun 24 – Jun 29

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

PATCH 1/1 drm/amdgpu: reject mapping info for unmapped BOs

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the BO's VM mapping and then iterates the valid and invalid mapping lists unconditionally. A GEM BO can be queried before it has been mapped into the file VM, in which case amdgpu_vm_bo_find() returns NULL and the list walk dereferences it.

Return -ENOENT for an unmapped BO, matching the VA operation path that already rejects missing BO-VA state before touching the mapping lists.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
index 212c14d99..4b2699931 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 		struct drm_amdgpu_gem_vm_entry *vm_entries;
 		struct amdgpu_bo_va_mapping *mapping;
 		int num_mappings = 0;
+
+		if (!bo_va) {
+			r = -ENOENT;
+			goto out_exec;
+		}
+
 		/*
 		 * num_entries is set as an input to the size of the user-allocated array of
 		 * drm_amdgpu_gem_vm_entry stored at args->value.
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [Low] Duplicate if (!bo_va) check introduces redundant logic.

Pre-existing issues:
- [High] Deadlock risk due to GFP_KERNEL allocation with dma_resv locks held.
- [High] Denial of Service risk via unverified large kernel memory allocation. --

commit 3feae89c84a776461a942a018d277330e825a587
Author: Yousef Alhouseen <[email protected]>

drm/amdgpu: reject mapping info for unmapped BOs

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the BO's VM mapping and then iterates the valid and invalid mapping lists unconditionally. A GEM BO can be queried before it has been mapped into the file VM, in which case amdgpu_vm_bo_find() returns NULL and the list walk dereferences it.

Return -ENOENT for an unmapped BO, matching the VA operation path that already rejects missing BO-VA state before touching the mapping lists.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 28f89ae8ca745..4ef08737390a2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, · line 1095

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 		struct drm_amdgpu_gem_vm_entry *vm_entries;
 		struct amdgpu_bo_va_mapping *mapping;
 		int num_mappings = 0;
+
+		if (!bo_va) {
+			r = -ENOENT;
+			goto out_exec;
+		}
+

[Severity: Low]
Does this introduce a redundant check? There appears to be an identical check immediately following the comment block in amdgpu_gem_op_ioctl():

		/*
		 * num_entries is set as an input to the size of the user-allocated array of
		 * drm_amdgpu_gem_vm_entry stored at args->value.
		 * num_entries is sent back as output as the number of mappings the bo has.
		 * If that number is larger than the size of the array, the ioctl must
		 * be retried.
		 */
		if (!bo_va) {
			r = -ENOENT;
			goto out_exec;
		}

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, · line 1098

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
 		/*
 		 * num_entries is set as an input to the size of the user-allocated array of
 		 * drm_amdgpu_gem_vm_entry stored at args->value.

[Severity: High]
This is a pre-existing issue, but does this allocation risk a deadlock when memory pressure occurs?

amdgpu_gem_op_ioctl() {
	...
	vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
	...
}

Using GFP_KERNEL here allows direct memory reclaim, which can invoke the DRM shrinker. If the shrinker attempts to acquire dma_resv locks to evict buffer objects, it could cause a circular locking dependency (ABBA deadlock) since gobj's dma_resv is already locked earlier in this path via drm_exec_lock_obj().

[Severity: High]
This is another pre-existing issue, but does this same allocation allow for an unbounded memory allocation that could lead to a denial of service?

amdgpu_gem_op_ioctl() {
	...
	if (args->num_entries > INT_MAX / sizeof(*vm_entries)) {
		r = -EINVAL;
		goto out_exec;
	}

	vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
	...
}

An unprivileged user could request an allocation size up to INT_MAX (~2GB) because the kernel does not verify the actual number of mappings before allocating the vm_entries array based solely on the user-supplied args->num_entries.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Yousef Alhouseen
Quoted 4 lines · Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the BO's VM mapping and then iterates the valid and invalid mapping lists unconditionally. A GEM BO can be queried before it has been mapped into the file VM, in which case amdgpu_vm_bo_find() returns NULL and the list walk dereferences it.

Mhm, that is not correct at all.

The bo_va is created when the handle is opened inside the filp and not when the first mapping is created.

Do you have a test case to reproduce the issue?

Thanks,
Christian.

Quoted 29 lines

Return -ENOENT for an unmapped BO, matching the VA operation path that already rejects missing BO-VA state before touching the mapping lists.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
index 212c14d99..4b2699931 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
                struct drm_amdgpu_gem_vm_entry *vm_entries;
                struct amdgpu_bo_va_mapping *mapping;
                int num_mappings = 0;
+
+               if (!bo_va) {
+                       r = -ENOENT;
+                       goto out_exec;
+               }
+
                /*
                 * num_entries is set as an input to the size of the user-allocated array of
                 * drm_amdgpu_gem_vm_entry stored at args->value.
--

2.54.0

#3 lore raw

Hi Christian,

You're right. I rechecked the handle-open path and bo_va should already be created for this file, so I do not have a valid reproducer for the NULL case.

Please drop this patch.

Thanks,
Yousef

Quoted 44 lines · Christian König
Quoted 4 lines · Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the BO's VM mapping and then iterates the valid and invalid mapping lists unconditionally. A GEM BO can be queried before it has been mapped into the file VM, in which case amdgpu_vm_bo_find() returns NULL and the list walk dereferences it.

Mhm, that is not correct at all.

The bo_va is created when the handle is opened inside the filp and not when the first mapping is created.

Do you have a test case to reproduce the issue?

Thanks,
Christian.

Quoted 29 lines

Return -ENOENT for an unmapped BO, matching the VA operation path that already rejects missing BO-VA state before touching the mapping lists.

---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++

1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
index 212c14d99..4b2699931 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 struct drm_amdgpu_gem_vm_entry *vm_entries;
 struct amdgpu_bo_va_mapping *mapping;
 int num_mappings = 0;
+
+ if (!bo_va) {
+ r = -ENOENT;
+ goto out_exec;
+ }
+
 /*
 * num_entries is set as an input to the size of the user-allocated array of
 * drm_amdgpu_gem_vm_entry stored at args->value.
--

2.54.0

maintainer
#4 lore raw
Quoted 7 lines · Yousef Alhouseen

Hi Christian,

You're right. I rechecked the handle-open path and bo_va should already be created for this file, so I do not have a valid reproducer for the NULL case.

Please drop this patch.

Well wait a second, I think you stumbled over something here. It's just that your bug description is not correct.

As far as I can see it is possible that between drm_gem_object_lookup() and drm_exec_lock_obj() the handle will be closed. So in this case bo_va will then be NULL.

It's a rather small race window, that's why I asked if you can reproduce this because we would then add this case to our IGT tests.

But if you can't reproduce this then well it should still be fixed. Just update the commit message and maybe return -EINVAL instead of -ENOENT.

Regards,
Christian.

Quoted 50 lines

Thanks,
Yousef

Quoted 44 lines · Christian König
Quoted 4 lines · Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the BO's VM mapping and then iterates the valid and invalid mapping lists unconditionally. A GEM BO can be queried before it has been mapped into the file VM, in which case amdgpu_vm_bo_find() returns NULL and the list walk dereferences it.

Mhm, that is not correct at all.

The bo_va is created when the handle is opened inside the filp and not when the first mapping is created.

Do you have a test case to reproduce the issue?

Thanks,
Christian.

Quoted 29 lines

Return -ENOENT for an unmapped BO, matching the VA operation path that already rejects missing BO-VA state before touching the mapping lists.

---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++

1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
index 212c14d99..4b2699931 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 struct drm_amdgpu_gem_vm_entry *vm_entries;
 struct amdgpu_bo_va_mapping *mapping;
 int num_mappings = 0;
+
+ if (!bo_va) {
+ r = -ENOENT;
+ goto out_exec;
+ }
+
 /*
 * num_entries is set as an input to the size of the user-allocated array of
 * drm_amdgpu_gem_vm_entry stored at args->value.
--

2.54.0

#5 lore raw

PATCH v2 1/1 drm/amdgpu: reject mapping info when BO VA is gone

In reply to Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the GEM object from the file handle and then locks the object and VM before resolving the BO-VA. The GEM object reference keeps the BO alive, but it does not keep the per-file handle open.

If a racing close drops the last handle reference in that window, amdgpu_gem_object_close() can remove the BO-VA before amdgpu_vm_bo_find() runs. The ioctl then walks the BO-VA mapping lists unconditionally.

Return -EINVAL if the BO is no longer associated with this VM.

---
Changes in v2:
- Describe the handle-close race instead of an initially unmapped BO.
- Return -EINVAL instead of -ENOENT.

 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
index 212c14d99..6f5b6f4c2 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 		struct drm_amdgpu_gem_vm_entry *vm_entries;
 		struct amdgpu_bo_va_mapping *mapping;
 		int num_mappings = 0;
+
+		if (!bo_va) {
+			r = -EINVAL;
+			goto out_exec;
+		}
+
 		/*
 		 * num_entries is set as an input to the size of the user-allocated array of
 		 * drm_amdgpu_gem_vm_entry stored at args->value.
Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH v2] drm/amdgpu: reject mapping info when BO VA is gone

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Medium] Duplicate check for !bo_va introduces dead code and changes the returned error code without justification, while the commit message incorrectly claims the list was walked unconditionally.

Pre-existing issues:
- [High] Memory allocation using GFP_KERNEL while holding dma_resv locks, risking AB-BA deadlock with memory reclaim. --

commit 6e50ec4c3e751796a8ad26f809bc3ef0737b1ddc
Author: Yousef Alhouseen <[email protected]>

drm/amdgpu: reject mapping info when BO VA is gone

The commit adds a check to return -EINVAL if the BO-VA mapping is missing during AMDGPU_GEM_OP_GET_MAPPING_INFO. It claims this prevents unconditionally walking the lists after a racing close.

Quoted 4 lines

If a racing close drops the last handle reference in that window, amdgpu_gem_object_close() can remove the BO-VA before amdgpu_vm_bo_find() runs. The ioctl then walks the BO-VA mapping lists unconditionally.

Is it accurate that the ioctl walks the BO-VA mapping lists unconditionally?

Looking at amdgpu_gem_op_ioctl(), there is already a pre-existing check for !bo_va that returns -ENOENT before any lists are walked.

[ ... ]

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 28f89ae8ca745..f048bbc4c672e 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, · line 1095

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 		struct drm_amdgpu_gem_vm_entry *vm_entries;
 		struct amdgpu_bo_va_mapping *mapping;
 		int num_mappings = 0;
+
+		if (!bo_va) {
+			r = -EINVAL;
+			goto out_exec;
+		}
+

[Severity: Medium]
Does adding this check introduce unreachable dead code just below it?

A few lines further down in amdgpu_gem_op_ioctl(), there is another !bo_va check:

		if (!bo_va) {
			r = -ENOENT;
			goto out_exec;
		}

With the newly added check returning -EINVAL, the pre-existing -ENOENT check becomes unreachable. Is there a specific reason to change the error code from -ENOENT to -EINVAL?

[Severity: High]
This is a pre-existing issue, but can this function trigger an AB-BA deadlock during memory reclaim?

Further down in amdgpu_gem_op_ioctl() for this same op:

		vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);

This memory allocation occurs while holding the dma_resv locks after drm_exec_until_all_locked() has successfully acquired them.

Allocating with GFP_KERNEL under a dma_resv lock allows direct memory reclaim, which can invoke the graphics shrinkers. Shrinkers often attempt to acquire dma_resv locks to evict buffers, causing a deadlock if the thread already holds them.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
 		/*
 		 * num_entries is set as an input to the size of the user-allocated array of
 		 * drm_amdgpu_gem_vm_entry stored at args->value.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw

Re: [PATCH v2] drm/amdgpu: reject mapping info when BO VA is gone

In reply to Yousef Alhouseen

Hi Alex, Christian,

Please drop this v2 as well.

I rechecked the target tree and missed the existing !bo_va check in AMDGPU_GEM_OP_GET_MAPPING_INFO. The close-race case is already handled before the mapping list walks, so this patch only makes the later check unreachable and changes the errno.

Sorry for the churn.

Thanks,
Yousef

Quoted 41 lines · Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the GEM object from the file handle and then locks the object and VM before resolving the BO-VA. The GEM object reference keeps the BO alive, but it does not keep the per-file handle open.

If a racing close drops the last handle reference in that window, amdgpu_gem_object_close() can remove the BO-VA before amdgpu_vm_bo_find() runs. The ioctl then walks the BO-VA mapping lists unconditionally.

Return -EINVAL if the BO is no longer associated with this VM.

---
Changes in v2:
- Describe the handle-close race instead of an initially unmapped BO.
- Return -EINVAL instead of -ENOENT.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++

1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
index 212c14d99..6f5b6f4c2 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 struct drm_amdgpu_gem_vm_entry *vm_entries;
 struct amdgpu_bo_va_mapping *mapping;
 int num_mappings = 0;
+
+ if (!bo_va) {
+ r = -EINVAL;
+ goto out_exec;
+ }
+
 /*
 * num_entries is set as an input to the size of the user-allocated array of
 * drm_amdgpu_gem_vm_entry stored at args->value.
--

2.54.0

maintainer
#8 lore raw

Re: [PATCH v2] drm/amdgpu: reject mapping info when BO VA is gone

Quoted 6 lines · Yousef Alhouseen

Hi Alex, Christian,

Please drop this v2 as well.

I rechecked the target tree and missed the existing !bo_va check in AMDGPU_GEM_OP_GET_MAPPING_INFO.

Am I blind? As far as I can see at least the code in drm-misc-next doesn't have such a check.

Regards,
Christian.

Quoted 52 lines

The close-race case is already handled
before the mapping list walks, so this patch only makes the later check unreachable and changes the errno.

Sorry for the churn.

Thanks,
Yousef

Quoted 41 lines · Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the GEM object from the file handle and then locks the object and VM before resolving the BO-VA. The GEM object reference keeps the BO alive, but it does not keep the per-file handle open.

If a racing close drops the last handle reference in that window, amdgpu_gem_object_close() can remove the BO-VA before amdgpu_vm_bo_find() runs. The ioctl then walks the BO-VA mapping lists unconditionally.

Return -EINVAL if the BO is no longer associated with this VM.

---
Changes in v2:
- Describe the handle-close race instead of an initially unmapped BO.
- Return -EINVAL instead of -ENOENT.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++

1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
index 212c14d99..6f5b6f4c2 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 struct drm_amdgpu_gem_vm_entry *vm_entries;
 struct amdgpu_bo_va_mapping *mapping;
 int num_mappings = 0;
+
+ if (!bo_va) {
+ r = -EINVAL;
+ goto out_exec;
+ }
+
 /*
 * num_entries is set as an input to the size of the user-allocated array of
 * drm_amdgpu_gem_vm_entry stored at args->value.
--

2.54.0

#9 lore raw

Re: [PATCH v2] drm/amdgpu: reject mapping info when BO VA is gone

No, you are not blind. I generated v2 from a local branch that still contained v1, so the v2 diff added a second check on top of the intended one. I then mistook that local state for the target tree when withdrawing it.

The close-handle race you identified still calls for the single check, but neither of my postings is suitable as sent. I will prepare a clean v3 against the target branch.

Sorry for the confusion.

Thanks,
Yousef

Quoted 65 lines · Christian König
Quoted 6 lines · Yousef Alhouseen

Hi Alex, Christian,

Please drop this v2 as well.

I rechecked the target tree and missed the existing !bo_va check in AMDGPU_GEM_OP_GET_MAPPING_INFO.

Am I blind? As far as I can see at least the code in drm-misc-next doesn't have such a check.

Regards,
Christian.

Quoted 52 lines

The close-race case is already handled
before the mapping list walks, so this patch only makes the later check unreachable and changes the errno.

Sorry for the churn.

Thanks,
Yousef

Quoted 41 lines · Yousef Alhouseen

AMDGPU_GEM_OP_GET_MAPPING_INFO looks up the GEM object from the file handle and then locks the object and VM before resolving the BO-VA. The GEM object reference keeps the BO alive, but it does not keep the per-file handle open.

If a racing close drops the last handle reference in that window, amdgpu_gem_object_close() can remove the BO-VA before amdgpu_vm_bo_find() runs. The ioctl then walks the BO-VA mapping lists unconditionally.

Return -EINVAL if the BO is no longer associated with this VM.

---
Changes in v2:
- Describe the handle-close race instead of an initially unmapped BO.
- Return -EINVAL instead of -ENOENT.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++++

1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
index 212c14d99..6f5b6f4c2 100644
@@ -1087,6 +1087,12 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 struct drm_amdgpu_gem_vm_entry *vm_entries;
 struct amdgpu_bo_va_mapping *mapping;
 int num_mappings = 0;
+
+ if (!bo_va) {
+ r = -EINVAL;
+ goto out_exec;
+ }
+
 /*
 * num_entries is set as an input to the size of the user-allocated array of
 * drm_amdgpu_gem_vm_entry stored at args->value.
--

2.54.0