]> git.itanic.dy.fi Git - linux-stable/commitdiff
ACPI: sysfs: Fix create_pnp_modalias() and create_of_modalias()
authorChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Mon, 23 Oct 2023 18:32:54 +0000 (20:32 +0200)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Tue, 24 Oct 2023 19:31:26 +0000 (21:31 +0200)
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>
drivers/acpi/device_sysfs.c

index b9bbf0746199215b77b0a769fdefd92ab350eb53..a34d8578b3da6cd159b1e10f035b9a7e96475d16 100644 (file)
@@ -158,8 +158,8 @@ static int create_pnp_modalias(const struct acpi_device *acpi_dev, char *modalia
                return 0;
 
        len = snprintf(modalias, size, "acpi:");
-       if (len <= 0)
-               return len;
+       if (len >= size)
+               return -ENOMEM;
 
        size -= len;
 
@@ -212,8 +212,10 @@ static int create_of_modalias(const struct acpi_device *acpi_dev, char *modalias
        len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
        ACPI_FREE(buf.pointer);
 
-       if (len <= 0)
-               return len;
+       if (len >= size)
+               return -ENOMEM;
+
+       size -= len;
 
        of_compatible = acpi_dev->data.of_compatible;
        if (of_compatible->type == ACPI_TYPE_PACKAGE) {