drm/amdgpu: validate the parameters of amdgpu_vm_bo_clear_mappings
Subsystems (2)
PATCH 1/1 drm/amdgpu: validate the parameters of amdgpu_vm_bo_clear_mappings
Ensure there is no address overlapping.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -0
@@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,LIST_HEAD (removed);uint64_t eaddr; +/* validate the parameters */ +if (saddr & ~PAGE_MASK || size & ~PAGE_MASK ) +return -EINVAL ; +if (saddr + size <= saddr) +return -EINVAL ; + eaddr = saddr + size -1 ; saddr /=AMDGPU_GPU_PAGE_SIZE ; eaddr /=AMDGPU_GPU_PAGE_SIZE ;
Signature · 1 line
2.34.1
Am 11.04.24 um 05:28 schrieb xinhui pan:
Quoted 13 lines
Ensure there is no address overlapping.
Reported-by Vlad Stolyarov <[email protected]>Signed-off-by xinhui pan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1860
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6
-0@@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,LIST_HEAD (removed);uint64_t eaddr; +/* validate the parameters */ +if (saddr & ~PAGE_MASK || size & ~PAGE_MASK ) +return -EINVAL ; +if (saddr + size <= saddr) +return -EINVAL ; +
Why the heck should we do that? Looks invalid to me.
Regards,
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0eaddr = saddr + size -1 ; saddr /=AMDGPU_GPU_PAGE_SIZE ; eaddr /=AMDGPU_GPU_PAGE_SIZE ;
Christian König
maintainerAm 11.04.24 um 05:28 schrieb xinhui pan:
Quoted 13 lines
Ensure there is no address overlapping.
Reported-by Vlad Stolyarov <[email protected]>Signed-off-by xinhui pan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1857
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0@@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,LIST_HEAD (removed);uint64_t eaddr; +/* validate the parameters */ +if (saddr & ~PAGE_MASK || size & ~PAGE_MASK ) +return -EINVAL ;
Well as general rule: *never* use PAGE_MASK and other PAGE_* macros here. This is GPUVM and not related to the CPUVM space.
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1860
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0+if (saddr + size <= saddr) +return -EINVAL ; +
Mhm, so basically size is not checked for a wraparound?
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1863
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0eaddr = saddr + size -1 ; saddr /=AMDGPU_GPU_PAGE_SIZE ; eaddr /=AMDGPU_GPU_PAGE_SIZE ;
If that's the case then I would rather check for saddr < eaddr here.
But that actually shouldn't matter since this code here:
/* Now gather all removed mappings */
tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); while (tmp) {
Then shouldn't return anything, so the operation is basically a NO-OP then.
Regards,
Christian.
Quoted 24 lines · Christian König
Am 11.04.24 um 05:28 schrieb xinhui pan:
Quoted 13 lines
Ensure there is no address overlapping.
Reported-by Vlad Stolyarov <[email protected]>Signed-off-by xinhui pan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1857
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0@@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,LIST_HEAD (removed);uint64_t eaddr; +/* validate the parameters */ +if (saddr & ~PAGE_MASK || size & ~PAGE_MASK ) +return -EINVAL ;Well as general rule: *never* use
PAGE_MASKand other PAGE_* macros here. This is GPUVM and not related to the CPUVM space.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1860
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0+if (saddr + size <= saddr) +return -EINVAL ; +Mhm, so basically size is not checked for a wraparound?
Yeah, exactly.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1863
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0eaddr = saddr + size -1 ; saddr /=AMDGPU_GPU_PAGE_SIZE ; eaddr /=AMDGPU_GPU_PAGE_SIZE ;If that's the case then I would rather check for saddr < eaddr here.
FWIW, it would probably a good idea to keep the added check analogous to other functions called from amdgpu_gem_va_ioctl() like amdgpu_vm_bo_replace_map(), which also checks "if (saddr + size <= saddr || offset + size <= offset)" before the division.
Quoted 7 lines
But that actually shouldn't matter since this code here:
/* Now gather all removed mappings */ tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); while (tmp) {Then shouldn't return anything, so the operation is basically a NO-OP then.
That's not how it works; the interval tree is not designed to be fed bogus ranges that end before they start. (Or at least I don't think it is - if it is, it is buggy.) I think basically if the specified start and end addresses are both within an rbtree node, this rbtree node is returned from the lookup, even if the specified end address is before the specified start address.
A more verbose example:
Let's assume the interval tree contains a single entry from address A to address D.
Looking at the _iter_first implementation in interval_tree_generic.h, when it is called with a start address C which is between A and D, and an end address B (result of an addition that wraps around to an address below C but above A), it does the following:
1. bails out if "node->ITSUBTREE < start" (meaning if the specified start address C is after the range covered by the root node - which is not the case)
2. bails out if "ITSTART(leftmost) > last" (meaning if the specified end address is smaller than the entry start address A - which is not the case)
3. enters _subtree_search. in there:
4. the root node has no children, so "node->ITRB.rb_left" is NULL
5. the specified end address B is after the node's start address A, so "Cond1" is fulfilled
6. the specified start address C is before the node's end address D, so "Cond2" is fulfilled
7. the root node is returned from the lookup
Christian König
maintainerAm 11.04.24 um 17:44 schrieb Jann Horn:
Quoted 31 lines
Quoted 23 lines · Christian König
Am 11.04.24 um 05:28 schrieb xinhui pan:
Quoted 13 lines
Ensure there is no address overlapping.
Reported-by Vlad Stolyarov <[email protected]>Signed-off-by xinhui pan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1857
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0@@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,LIST_HEAD (removed);uint64_t eaddr; +/* validate the parameters */ +if (saddr & ~PAGE_MASK || size & ~PAGE_MASK ) +return -EINVAL ;Well as general rule: *never* use
PAGE_MASKand other PAGE_* macros here. This is GPUVM and not related to the CPUVM space.Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1860
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0+if (saddr + size <= saddr) +return -EINVAL ; +Mhm, so basically size is not checked for a wraparound?
Yeah, exactly.
Quoted 8 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1863
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0eaddr = saddr + size -1 ; saddr /=AMDGPU_GPU_PAGE_SIZE ; eaddr /=AMDGPU_GPU_PAGE_SIZE ;If that's the case then I would rather check for saddr < eaddr here.
FWIW, it would probably a good idea to keep the added check analogous to other functions called from
amdgpu_gem_va_ioctl()likeamdgpu_vm_bo_replace_map(), which also checks "if (saddr + size <= saddr || offset + size <= offset)" before the division.
I would also change that function as well.
The eaddr needs to be checked against the max_pfn as well and we currently shift that around for this check which looks quite ugly.
Only the overflow check can probably be before it.
Quoted 7 lines
Quoted 7 lines
But that actually shouldn't matter since this code here:
/* Now gather all removed mappings */ tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); while (tmp) {
Quoted 7 lines
Quoted 1 line
Then shouldn't return anything, so the operation is basically a NO-OP then.
That's not how it works; the interval tree is not designed to be fed bogus ranges that end before they start. (Or at least I don't think it is - if it is, it is buggy.) I think basically if the specified start and end addresses are both within an rbtree node, this rbtree node is returned from the lookup, even if the specified end address is before the specified start address.
Ah, yeah that makes sense. The search functions checks if a node only partially intersects with start and end and not if it is covered by it.
Thanks,
Christian.
Quoted 22 lines
A more verbose example:
Let's assume the interval tree contains a single entry from address A to address D.
Looking at the_iter_firstimplementation ininterval_tree_generic.h, when it is called with a start address C which is between A and D, and an end address B (result of an addition that wraps around to an address below C but above A), it does the following:1. bails out if "
node->ITSUBTREE< start" (meaning if the specified start address C is after the range covered by the root node - which is not the case)
2. bails out if "ITSTART(leftmost) > last" (meaning if the specified end address is smaller than the entry start address A - which is not the case)
3. enters_subtree_search. in there:
4. the root node has no children, so "node->ITRB.rb_left" is NULL
5. the specified end address B is after the node's start address A, so "Cond1" is fulfilled
6. the specified start address C is before the node's end address D, so "Cond2" is fulfilled
7. the root node is returned from the lookup
+Jann Horn <[email protected]> for his thoughts
Quoted 49 lines · Christian König
Am 11.04.24 um 05:28 schrieb xinhui pan:
Review
Quoted 9 lines
Ensure there is no address overlapping.
Reported-by Vlad Stolyarov <[email protected]>Signed-off-by xinhui pan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 ++++++ 1 file changed, 6 insertions(+)(quoted diff) +0
-0
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.cdrivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct
Quoted 4 lines
index
8af3f0fd3073..f1315a854192100644drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0@@ -1852,6 +1852,12 @@ int amdgpu_vm_bo_clear_mappings(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1857
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0LIST_HEAD (removed);uint64_t eaddr; +/* validate the parameters */ +if (saddr & ~PAGE_MASK || size & ~PAGE_MASK ) +return -EINVAL ;Well as general rule: *never* use
PAGE_MASKand other PAGE_* macros here. This is GPUVM and not related to the CPUVM space.drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1860
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0+if (saddr + size <= saddr) +return -EINVAL ; +Mhm, so basically size is not checked for a wraparound?
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, · line 1863
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0eaddr = saddr + size -1 ; saddr /=AMDGPU_GPU_PAGE_SIZE ; eaddr /=AMDGPU_GPU_PAGE_SIZE ;If that's the case then I would rather check for saddr < eaddr here.
But that actually shouldn't matter since this code here:
/* Now gather all removed mappings */ tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); while (tmp) {Then shouldn't return anything, so the operation is basically a NO-OP then.
Regards,
Christian.