From nobody Thu Apr 2 06:31:34 2026 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 242F339769D for ; Tue, 3 Mar 2026 19:38:30 +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=1772566713; cv=none; b=BtytrGYtaK7P9E/yP5JCGqRNn93iYy9ZZMJ3aY8UgTgCRPaJyDv79mqi23NOSO8mc2wg4oakR97gNxn+xYDTdkjqLvrz82Vm5+pzyEGYCz4XuhHGg/hHNwiLc/CyiVDhUKiMrMc8/QSbv8M98T1w1/vdC3ygcWFZc6NXMePqXo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566713; c=relaxed/simple; bh=BjDIIe88AEsckrtwAjj93CmAX57GI3uuDWU+eakcZrQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OwiPerxjd8lyh2S/g1qH4SO7lr6GDgzZs+rmdoHTEuXdTCruWoV4o11lAEwSkMxIqKLRWZSEsRcfrV7P6b/c8xrJIvKFDW1SGKPT220gTLnezYXA0t4kH7gQuctMu/LxKlDcoNnXW5RBOUJQF/ck4pb88J5t9r2JtsokpaTxlUw= 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=WM4Oq/RD; 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="WM4Oq/RD" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-672c40f3873so109842638eaf.2 for ; Tue, 03 Mar 2026 11:38:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566710; x=1773171510; 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=wjWzLo2XlCuyIDdW74QDqar0cCQChKUuTAH2m9cJEjM=; b=WM4Oq/RDzRMfliaOa/8fOEudwv/BZK/LYMBe2VgEoiKWx4fLJU+bmvPpgT4k7DQwtN nl0KqwzYRaBAa5hNwBslUo9qFVfz6i3LauamUj7Y/62d9veQjhJNYt3+32U+SHOOzML8 O8TzdQ2BK3BGwz50vQcqtdByU3kOCX91r28LpBa2zsrAesasV2tpS/NdZruJyIGlMWtl lsggJzWSwGE6OEhkLvq5mReQ5kTBJFza9YCFmCETCcY2lRllquBysNHZdbYBa2MKskXo xj0InyYKHtg46EZqV76LwL1lyBXVB6O1Ww8E0PE0SYJjFXoaYJ+omSnv+TZUsylG8AEv QVhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566710; x=1773171510; 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=wjWzLo2XlCuyIDdW74QDqar0cCQChKUuTAH2m9cJEjM=; b=jfFiW62dwLnjgRl0vjB8kRx7a8l9ISPruXZ0Zs5d7oq4EiR84BPf7AvVq9yrg8k0xA 9r9eHciX90rZb4iw1ErOHUTaKkRzXVGmHjoyjvKVs40qpZOH4WB4gmWN4sC7CPiMUAtb QF0YlQmrDbfwaHFuCtcuWCTz0tBWafs96fKR4b0twf7BgtUnY8hTKEETTP01xgP+3mX8 sYC/gYMxig4kSQPSxF/sO8OmYwW0Y1yPCcJ6YMVMHPtfMet8Sr0X/e/jztDDYgZ1E9lr gZ7kD6zb4BD+UaWpANaHSVj5nHD9av31RrD/HXNpEsZBInvsCqqTEq7D0YROZUcDzbIH piOQ== X-Forwarded-Encrypted: i=1; AJvYcCV4dgrTKQnURJumUD4MV/cfr5R064rEoguoQ+l0K/SaKPNFtfGRkVjN3L15P+WpJ+gF4GHSQheKmmDMzxM=@vger.kernel.org X-Gm-Message-State: AOJu0YzOUNfPT/mHfDEPgduHYNgx7q6/IXoaIejhYQDQlReR+WtPyJ4n eEZy6efAOiKptg4nRb+oKZsiKmzU5+GIPV47viiS35pKPmaCySlxvmEnTg4NXUw34SHBrtFYKJs lUUf/StFP7g== X-Received: from ilkg5.prod.google.com ([2002:a92:c7c5:0:b0:48b:c9f5:33ef]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2107:b0:679:7aed:9529 with SMTP id 006d021491bc7-679fadb732emr10456053eaf.1.1772566709726; Tue, 03 Mar 2026 11:38:29 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:16 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-3-rananta@google.com> Subject: [PATCH v6 2/8] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , 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 Reviewed-by: David Matlack --- .../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 4e5871f1ebc3b..1538d2b30ae59 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -209,7 +209,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); @@ -229,7 +229,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); @@ -300,7 +300,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); @@ -310,7 +310,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 abb170bdcef7e..7d0de8c79de13 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -44,9 +44,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 7c0fe8ce3a61f..93c11fd5e0818 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.53.0.473.g4a7958ca14-goog