Skip to contentSkip to conversation

kselftests: dmabuf-heaps: Ensure the driver name is null-terminated

PATCH Superseded Zenghui Yu 2 participants 3 messages Jul 8, 2024 – Jul 9, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zenghui Yu

author
#0 lore raw

PATCH 1/1 kselftests: dmabuf-heaps: Ensure the driver name is null-terminated

Even if a vgem device is configured in, we will skip the import_vgem_fd() test almost every time.

TAP version 13
1..11
# Testing heap: system
# =======================================
# Testing allocation and importing:
ok 1 # SKIP Could not open vgem -1

The problem is that we use the DRM_IOCTL_VERSION ioctl to query the driver version information but leave the name field a non-null-terminated string. Terminate it properly to actually test against the vgem device.

---
 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +2 -0
index 5f541522364f..2fcc74998fa9 100644
@@ -32,6 +32,8 @@ static int check_vgem(int fd)
 	if (ret)
 		return 0;
 
+	name[4] = '\0';
+
 	return !strcmp(name, "vgem");
 }
 
Signature · 1 line
2.33.0
reviewer
#1 lore raw
Quoted 11 lines · Zenghui Yu

Even if a vgem device is configured in, we will skip the import_vgem_fd() test almost every time.

TAP version 13
1..11
# Testing heap: system
# =======================================
# Testing allocation and importing:
ok 1 # SKIP Could not open vgem -1

Quoted 3 lines

The problem is that we use the DRM_IOCTL_VERSION ioctl to query the driver version information but leave the name field a non-null-terminated string. Terminate it properly to actually test against the vgem device.

Hm yeah. Looks like drm_copy_field resets version.name to the actual size of the name in the case of truncation, so maybe worth checking that too in case there is a name like "vgemfoo" that gets converted to "vgem\0" by this?

Quoted 22 lines
---
 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +2 -1
index 5f541522364f..2fcc74998fa9 100644
@@ -32,6 +32,8 @@ static int check_vgem(int fd)
        if (ret)
                return 0;
 
+       name[4] = '\0';
+
        return !strcmp(name, "vgem");
 }
 
--

2.33.0

author
#2 lore raw
Quoted 12 lines · T.J. Mercier
Quoted 11 lines · Zenghui Yu

Even if a vgem device is configured in, we will skip the import_vgem_fd() test almost every time.

TAP version 13
1..11
# Testing heap: system
# =======================================
# Testing allocation and importing:
ok 1 # SKIP Could not open vgem -1

Quoted 8 lines
Quoted 3 lines

The problem is that we use the DRM_IOCTL_VERSION ioctl to query the driver version information but leave the name field a non-null-terminated string. Terminate it properly to actually test against the vgem device.

Hm yeah. Looks like drm_copy_field resets version.name to the actual size of the name in the case of truncation, so maybe worth checking that too in case there is a name like "vgemfoo" that gets converted to "vgem\0" by this?

Given that drm_copy_field() would set version.name_len to the exact length of the driver name, this can be addressed by

tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +1 -1
index 2fcc74998fa9..5d0a809dc2df 100644
@@ -29,7 +29,7 @@ static int check_vgem(int fd)
 	version.name = name;
 
 	ret = ioctl(fd, DRM_IOCTL_VERSION, &version);
-	if (ret)
+	if (ret || version.name_len != 4)
 		return 0;
 
 	name[4] = '\0';

on top of this patch. What do you think?

Thanks,
Zenghui