From nobody Tue Dec 16 20:46:00 2025 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7D93285C91 for ; Wed, 10 Dec 2025 18:14:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390464; cv=none; b=DpXJJDu79FvUFfpzC0TmMrnATzY1O32c9RGZifIrjkXei3ps8ud2A476dHsdDe4Sfq9C3uTHofnrudrD8XLMjva0FwkPIO7VUGHJXjGLxNB5aVAjIrFRJ+6Yb5cKI/Ih7nNTzMK120THpNYNT/uutVnInrZ6g/uv9ZOwa/m+PIc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390464; c=relaxed/simple; bh=lhAXB7A8G2v17fgR7GievtgMw7aHmutfq6cVjY4fsC0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WLtu8ACtrLW7wg4Cn2aGoJEU9/1dOr+L/jkpsBNl0nM+148p2DAWxUqdKf4peocS3VoYwW5cinUSHGXOu6eJrxdrWY+KWw+lJQJt40SVl+I0kZvV4K2AH3GAYIGXh73iE3G4mnikVy1DW0aTO55iO2T3Xkoa137YSsk4c08lNKA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0KfjAsuP; arc=none smtp.client-ip=209.85.161.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0KfjAsuP" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-657537cef7cso131533eaf.1 for ; Wed, 10 Dec 2025 10:14:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390462; x=1765995262; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TA+dFiCFfE/neZMvBvdBl7grrAyeiSJc0c8DLMfcbps=; b=0KfjAsuPORKwXQd0WlfyxrATau9rPcSTrVXqhEcNzGHUuQ4TJQ6fBYl52uGeFEyFqE nBYoeajrCHwzVJ9TR4DjuI7Z0lb9u6ZfUeuNigIP73KBy9nwUgbsapU56jWtqWFKubfJ 1tukrWic+a0IndMRtCv3EGHGivYFDgrlvssK0IUD2W+JrdW7TVxi1s+mPpeN5u+hSt2H 70XlufZMsuxK6xDWJOpqPpeMfKx3ZkmgI7Q2qOgWyf588D21EDlq8UEWrVBT5T4bJmYN JkIVV+l3W9SDORAeJ4aVS5NnZhHh+5tCnFyFAyyqm81A+FMF3vi9IQsnt0APaQ89CO3v jt9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390462; x=1765995262; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TA+dFiCFfE/neZMvBvdBl7grrAyeiSJc0c8DLMfcbps=; b=nJPtqHu+6269Nlbr/EXnV7qjfWMcn70aoUXzsy1MBmwsEamzZlL8WF07sdLODIvZOv PdjjDddD/1Nw2FILGFqagVld7SKDNPM1IK7SSR82RiaPDNI3aCWiDFiDPx2yUO2R/Z7r ebat5EMmcOID0scMt7R5+x2CQD1Iuy6+7gKo1Wn8nn6ZB/K8m7DxihH7TprX4bVGa75c CuuHCTvPzKbPvMan7lX92xApqarY1VL64qqiT7LCQiNXc6q/+MFFY04T4lrp26c4c8Ko c4rhLDErZAryyUQN+Ty5Bo9DpHMd/Nu0oKwlfFcv2Qda5hz4EMD/rxt8/IBUYs0hb5Pj ge6A== X-Forwarded-Encrypted: i=1; AJvYcCXZYRfV5MjINXsdnyX47SXaAktWYeZfgpFqGidD5KO2gWj1MZJFX9n5bT4mIShxniepCpe9qS11bBX7a1w=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5lXc/I04AaSCPNFCv+kBEDWsyPHBX52bYcSUDZI7/IwtPOXol Aeps8f5wgw0vpHx4IKZtYElZW8l/sKaHkx0jPDjhLiwWhYjU+2+JVZ+/JvmjWCg8pwRgA032sLM enlGTqAT1/g== X-Google-Smtp-Source: AGHT+IGWhLSFoMUjQWfjmNMZkoR8qR5Wr00Wt6Ecah3pTSGPB9crDFJhJUsYYCKig83u1qz0VeKbkBJ3k3Zh X-Received: from ilbee24.prod.google.com ([2002:a05:6e02:4918:b0:438:237b:ed42]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:905:b0:659:9a49:900d with SMTP id 006d021491bc7-65b2ad632a6mr2034002eaf.58.1765390461916; Wed, 10 Dec 2025 10:14:21 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:12 +0000 In-Reply-To: <20251210181417.3677674-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-2-rananta@google.com> Subject: [PATCH v2 1/6] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- .../vfio/lib/include/libvfio/assert.h | 5 +++++ .../selftests/vfio/lib/vfio_pci_device.c | 8 +++---- .../selftests/vfio/vfio_dma_mapping_test.c | 6 +++--- .../selftests/vfio/vfio_pci_device_test.c | 21 ++++++++++--------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h b/to= ols/testing/selftests/vfio/lib/include/libvfio/assert.h index f4ebd122d9b6f..77b68c7129a64 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h @@ -51,4 +51,9 @@ VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #= _arg, __ret); \ } while (0) =20 +#define snprintf_assert(_s, _size, _fmt, ...) do { \ + int __ret =3D snprintf(_s, _size, _fmt, ##__VA_ARGS__); \ + VFIO_ASSERT_LT(__ret, _size); \ +} while (0) + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 13fdb4b0b10f3..64a19481b734f 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -188,7 +188,7 @@ static unsigned int vfio_pci_get_group_from_dev(const c= har *bdf) unsigned int group; int ret; =20 - 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); =20 ret =3D 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); @@ -208,7 +208,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf int group; =20 group =3D 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); =20 device->group_fd =3D open(group_path, O_RDWR); VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); @@ -279,7 +279,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) cdev_path =3D calloc(PATH_MAX, 1); VFIO_ASSERT_NOT_NULL(cdev_path); =20 - 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); =20 dir =3D opendir(dir_path); VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); @@ -289,7 +289,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) if (strncmp("vfio", entry->d_name, 4)) continue; =20 - snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); + snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_na= me); break; } =20 diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index 5397822c3dd4b..3a0bea5e26480 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -45,9 +45,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 i= ova, FILE *file; char *rest; =20 - 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); =20 printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path); =20 diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/te= sting/selftests/vfio/vfio_pci_device_test.c index ecbb669b37651..723b56b485f67 100644 --- a/tools/testing/selftests/vfio/vfio_pci_device_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c @@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test) iommu_cleanup(self->iommu); } =20 -#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 =3D 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 =3D 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); \ }) =20 TEST_F(vfio_pci_device_test, config_space_read_write) --=20 2.52.0.239.gd5f0c6e74e-goog