]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
vfio: selftests: Introduce snprintf_assert()
authorRaghavendra Rao Ananta <rananta@google.com>
Tue, 5 May 2026 21:28:32 +0000 (21:28 +0000)
committerAlex Williamson <alex@shazbot.org>
Wed, 20 May 2026 17:54:10 +0000 (11:54 -0600)
Introduce snprintf_assert() to protect the users of snprintf() to fail
if the requested operation was truncated due to buffer limits. VFIO
tests and libraries, including a new sysfs library that will be introduced
by an upcoming patch, rely quite heavily on snprintf()s to build PCI
sysfs paths. Having a protection against this will be helpful to prevent
false test failures.

Signed-off-by: Raghavendra Rao Ananta <rananta@google.com>
Reviewed-by: David Matlack <dmatlack@google.com>
Reviewed-by: Vipin Sharma <vipinsh@google.com>
Tested-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20260505212838.1698034-3-rananta@google.com
Signed-off-by: Alex Williamson <alex@shazbot.org>
tools/testing/selftests/vfio/lib/include/libvfio/assert.h
tools/testing/selftests/vfio/lib/vfio_pci_device.c
tools/testing/selftests/vfio/vfio_dma_mapping_test.c
tools/testing/selftests/vfio/vfio_pci_device_test.c

index f4ebd122d9b6fc7077af526e30656b0fcaa6a896..77b68c7129a648cda004ae77d728d61409eff720 100644 (file)
@@ -51,4 +51,9 @@
        VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \
 } while (0)
 
+#define snprintf_assert(_s, _size, _fmt, ...) do {                      \
+       int __ret = snprintf(_s, _size, _fmt, ##__VA_ARGS__);           \
+       VFIO_ASSERT_LT(__ret, _size);                                   \
+} while (0)
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */
index fc75e04ef010cf9374b3abe22b6c804227585083..efbf9ab63cea75f068fe564aa0e5ce73124b5c69 100644 (file)
@@ -211,7 +211,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
        unsigned int group;
        int ret;
 
-       snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf);
+       snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf);
 
        ret = readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group_path));
        VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n", bdf);
@@ -231,7 +231,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
        int group;
 
        group = vfio_pci_get_group_from_dev(bdf);
-       snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group);
+       snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group);
 
        device->group_fd = open(group_path, O_RDWR);
        VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path);
@@ -302,7 +302,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
        cdev_path = calloc(PATH_MAX, 1);
        VFIO_ASSERT_NOT_NULL(cdev_path);
 
-       snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
+       snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
 
        dir = opendir(dir_path);
        VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path);
@@ -312,7 +312,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
                if (strncmp("vfio", entry->d_name, 4))
                        continue;
 
-               snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name);
+               snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name);
                break;
        }
 
index abb170bdcef7ef83eb513cde97ea9144e523cd8a..7d0de8c79de13f398e67bd6ac840aa59e5b64205 100644 (file)
@@ -44,9 +44,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 iova,
        FILE *file;
        char *rest;
 
-       snprintf(iommu_mapping_path, sizeof(iommu_mapping_path),
-                "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct",
-                bdf);
+       snprintf_assert(iommu_mapping_path, sizeof(iommu_mapping_path),
+                       "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct",
+                       bdf);
 
        printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path);
 
index 7c0fe8ce3a61f04865c3b0e43dee23f35b02cc6c..93c11fd5e0818255745a887f379a41e3aa67e2f8 100644 (file)
@@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test)
        iommu_cleanup(self->iommu);
 }
 
-#define read_pci_id_from_sysfs(_file) ({                                                       \
-       char __sysfs_path[PATH_MAX];                                                            \
-       char __buf[32];                                                                         \
-       int __fd;                                                                               \
-                                                                                               \
-       snprintf(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", device_bdf, _file);      \
-       ASSERT_GT((__fd = open(__sysfs_path, O_RDONLY)), 0);                                    \
-       ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0);                                     \
-       ASSERT_EQ(0, close(__fd));                                                              \
-       (u16)strtoul(__buf, NULL, 0);                                                           \
+#define read_pci_id_from_sysfs(_file) ({                                       \
+       char __sysfs_path[PATH_MAX];                                            \
+       char __buf[32];                                                         \
+       int __fd;                                                               \
+                                                                               \
+       snprintf_assert(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s",   \
+                       device_bdf, _file);                                     \
+       ASSERT_GT((__fd = open(__sysfs_path, O_RDONLY)), 0);                    \
+       ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0);                     \
+       ASSERT_EQ(0, close(__fd));                                              \
+       (u16)strtoul(__buf, NULL, 0);                                           \
 })
 
 TEST_F(vfio_pci_device_test, config_space_read_write)