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

ACPI: sysfs: Fix create_pnp_modalias() and create_of_modalias()

snprintf() does not return negative values on error.

To know if the buffer was too small, the returned value needs to be
compared with the length of the passed buffer. If it is greater or
equal, the output has been truncated, so add checks for the truncation
to create_pnp_modalias() and create_of_modalias(). Also make them
return -ENOMEM in that case, as they already do that elsewhere.

Moreover, the remaining size of the buffer used by snprintf() needs to
be updated after the first write to avoid out-of-bounds access as
already done correctly in create_pnp_modalias(), but not in
create_of_modalias(), so change the latter accordingly.

Fixes: 8765c5ba1949 ("ACPI / scan: Rework modalias creation when "compatible" is present")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
[ rjw: Merge two patches into one, combine changelogs, add subject ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

authored by

Christophe JAILLET and committed by
Rafael J. Wysocki
48cf49d3 05d3ef8b

+6 -4
+6 -4
drivers/acpi/device_sysfs.c
··· 158 158 return 0; 159 159 160 160 len = snprintf(modalias, size, "acpi:"); 161 - if (len <= 0) 162 - return len; 161 + if (len >= size) 162 + return -ENOMEM; 163 163 164 164 size -= len; 165 165 ··· 212 212 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer); 213 213 ACPI_FREE(buf.pointer); 214 214 215 - if (len <= 0) 216 - return len; 215 + if (len >= size) 216 + return -ENOMEM; 217 + 218 + size -= len; 217 219 218 220 of_compatible = acpi_dev->data.of_compatible; 219 221 if (of_compatible->type == ACPI_TYPE_PACKAGE) {