Skip to content

Commit fd4d5cd

Browse files
nicolincgregkh
authored andcommitted
iommufd: Fix protection fault in iommufd_test_syz_conv_iova
[ Upstream commit cf7c278 ] Syzkaller reported the following bug: general protection fault, probably for non-canonical address 0xdffffc0000000038: 0000 [#1] SMP KASAN KASAN: null-ptr-deref in range [0x00000000000001c0-0x00000000000001c7] Call Trace: lock_acquire lock_acquire+0x1ce/0x4f0 down_read+0x93/0x4a0 iommufd_test_syz_conv_iova+0x56/0x1f0 iommufd_test_access_rw.isra.0+0x2ec/0x390 iommufd_test+0x1058/0x1e30 iommufd_fops_ioctl+0x381/0x510 vfs_ioctl __do_sys_ioctl __se_sys_ioctl __x64_sys_ioctl+0x170/0x1e0 do_syscall_x64 do_syscall_64+0x71/0x140 This is because the new iommufd_access_change_ioas() sets access->ioas to NULL during its process, so the lock might be gone in a concurrent racing context. Fix this by doing the same access->ioas sanity as iommufd_access_rw() and iommufd_access_pin_pages() functions do. Cc: [email protected] Fixes: 9227da7 ("iommufd: Add iommufd_access_change_ioas(_id) helpers") Link: https://lore.kernel.org/r/3f1932acaf1dd494d404c04364d73ce8f57f3e5e.1708636627.git.nicolinc@nvidia.com Reported-by: Jason Gunthorpe <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> (cherry picked from commit cf7c278) [Harshit: CVE-2024-26785; Resolve conflicts due to missing commit: bd7a282 ("iommufd: Add iommufd_ctx to iommufd_put_object()") in 6.6.y] Signed-off-by: Harshit Mogalapalli <[email protected]> Signed-off-by: Vegard Nossum <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 69a1e2d commit fd4d5cd

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

drivers/iommu/iommufd/selftest.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ enum {
4444
* In syzkaller mode the 64 bit IOVA is converted into an nth area and offset
4545
* value. This has a much smaller randomization space and syzkaller can hit it.
4646
*/
47-
static unsigned long iommufd_test_syz_conv_iova(struct io_pagetable *iopt,
48-
u64 *iova)
47+
static unsigned long __iommufd_test_syz_conv_iova(struct io_pagetable *iopt,
48+
u64 *iova)
4949
{
5050
struct syz_layout {
5151
__u32 nth_area;
@@ -69,6 +69,21 @@ static unsigned long iommufd_test_syz_conv_iova(struct io_pagetable *iopt,
6969
return 0;
7070
}
7171

72+
static unsigned long iommufd_test_syz_conv_iova(struct iommufd_access *access,
73+
u64 *iova)
74+
{
75+
unsigned long ret;
76+
77+
mutex_lock(&access->ioas_lock);
78+
if (!access->ioas) {
79+
mutex_unlock(&access->ioas_lock);
80+
return 0;
81+
}
82+
ret = __iommufd_test_syz_conv_iova(&access->ioas->iopt, iova);
83+
mutex_unlock(&access->ioas_lock);
84+
return ret;
85+
}
86+
7287
void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
7388
unsigned int ioas_id, u64 *iova, u32 *flags)
7489
{
@@ -81,7 +96,7 @@ void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
8196
ioas = iommufd_get_ioas(ucmd->ictx, ioas_id);
8297
if (IS_ERR(ioas))
8398
return;
84-
*iova = iommufd_test_syz_conv_iova(&ioas->iopt, iova);
99+
*iova = __iommufd_test_syz_conv_iova(&ioas->iopt, iova);
85100
iommufd_put_object(&ioas->obj);
86101
}
87102

@@ -852,7 +867,7 @@ static int iommufd_test_access_pages(struct iommufd_ucmd *ucmd,
852867
}
853868

854869
if (flags & MOCK_FLAGS_ACCESS_SYZ)
855-
iova = iommufd_test_syz_conv_iova(&staccess->access->ioas->iopt,
870+
iova = iommufd_test_syz_conv_iova(staccess->access,
856871
&cmd->access_pages.iova);
857872

858873
npages = (ALIGN(iova + length, PAGE_SIZE) -
@@ -954,8 +969,8 @@ static int iommufd_test_access_rw(struct iommufd_ucmd *ucmd,
954969
}
955970

956971
if (flags & MOCK_FLAGS_ACCESS_SYZ)
957-
iova = iommufd_test_syz_conv_iova(&staccess->access->ioas->iopt,
958-
&cmd->access_rw.iova);
972+
iova = iommufd_test_syz_conv_iova(staccess->access,
973+
&cmd->access_rw.iova);
959974

960975
rc = iommufd_access_rw(staccess->access, iova, tmp, length, flags);
961976
if (rc)

0 commit comments

Comments
 (0)