Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommu/vt-d: Replace snprintf with scnprintf in dmar_latency_snapshot()

snprintf() returns the number of bytes that would have been written, not
the number actually written. Using this for offset tracking can cause
buffer overruns if truncation occurs.

Replace snprintf() with scnprintf() to ensure the offset stays within
bounds.

Since scnprintf() never returns a negative value, and zero is not possible
in this context because 'bytes' starts at 0 and 'size - bytes' is
DEBUG_BUFFER_SIZE in the first call, which is large enough to hold the
string literals used, the return value is always positive. An integer
overflow is also completely out of reach here due to the small and fixed
buffer size. The error check in latency_show_one() is therefore
unnecessary. Remove it and make dmar_latency_snapshot() return void.

Signed-off-by: Seyediman Seyedarab <ImanDevel@gmail.com>
Link: https://lore.kernel.org/r/20250731225048.131364-1-ImanDevel@gmail.com
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>

authored by

Seyediman Seyedarab and committed by
Joerg Roedel
75c02a03 1b237f19

+8 -17
+2 -8
drivers/iommu/intel/debugfs.c
··· 648 648 static void latency_show_one(struct seq_file *m, struct intel_iommu *iommu, 649 649 struct dmar_drhd_unit *drhd) 650 650 { 651 - int ret; 652 - 653 651 seq_printf(m, "IOMMU: %s Register Base Address: %llx\n", 654 652 iommu->name, drhd->reg_base_addr); 655 653 656 - ret = dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE); 657 - if (ret < 0) 658 - seq_puts(m, "Failed to get latency snapshot"); 659 - else 660 - seq_puts(m, debug_buf); 661 - seq_puts(m, "\n"); 654 + dmar_latency_snapshot(iommu, debug_buf, DEBUG_BUFFER_SIZE); 655 + seq_printf(m, "%s\n", debug_buf); 662 656 } 663 657 664 658 static int latency_show(struct seq_file *m, void *v)
+4 -6
drivers/iommu/intel/perf.c
··· 113 113 " svm_prq" 114 114 }; 115 115 116 - int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size) 116 + void dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size) 117 117 { 118 118 struct latency_statistic *lstat = iommu->perf_statistic; 119 119 unsigned long flags; ··· 122 122 memset(str, 0, size); 123 123 124 124 for (i = 0; i < COUNTS_NUM; i++) 125 - bytes += snprintf(str + bytes, size - bytes, 125 + bytes += scnprintf(str + bytes, size - bytes, 126 126 "%s", latency_counter_names[i]); 127 127 128 128 spin_lock_irqsave(&latency_lock, flags); ··· 130 130 if (!dmar_latency_enabled(iommu, i)) 131 131 continue; 132 132 133 - bytes += snprintf(str + bytes, size - bytes, 133 + bytes += scnprintf(str + bytes, size - bytes, 134 134 "\n%s", latency_type_names[i]); 135 135 136 136 for (j = 0; j < COUNTS_NUM; j++) { ··· 156 156 break; 157 157 } 158 158 159 - bytes += snprintf(str + bytes, size - bytes, 159 + bytes += scnprintf(str + bytes, size - bytes, 160 160 "%12lld", val); 161 161 } 162 162 } 163 163 spin_unlock_irqrestore(&latency_lock, flags); 164 - 165 - return bytes; 166 164 }
+2 -3
drivers/iommu/intel/perf.h
··· 40 40 bool dmar_latency_enabled(struct intel_iommu *iommu, enum latency_type type); 41 41 void dmar_latency_update(struct intel_iommu *iommu, enum latency_type type, 42 42 u64 latency); 43 - int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size); 43 + void dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size); 44 44 #else 45 45 static inline int 46 46 dmar_latency_enable(struct intel_iommu *iommu, enum latency_type type) ··· 64 64 { 65 65 } 66 66 67 - static inline int 67 + static inline void 68 68 dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size) 69 69 { 70 - return 0; 71 70 } 72 71 #endif /* CONFIG_DMAR_PERF */