acpi: battery: Sanitise model_number by dropping unprintable characters
PATCH v2 1/1 acpi: battery: Sanitise model_number by dropping unprintable characters
The battery Embedded Controller (EC) may return the model name with trailing unprintable or non-ASCII characters. For example, on some systems:
$ cat /sys/class/power_supply/BAT0/model_name LNV-5B10W51864��
If a non-ASCII or an unprintable character is found, it will be replaced with '\0' to ensure the model_number is a valid string. If left intact, the malformed string prevents udev rules and hwdb working correctly.
--- drivers/acpi/battery.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/acpi/battery.c +14 -0
@@ -10,6 +10,7 @@#define pr_fmt (fmt) "ACPI: battery: " fmt +#include <linux/ctype.h> #include <linux/delay.h> #include <linux/dmi.h> #include <linux/jiffies.h>
@@ -483,6 +484,15 @@ static int acpi_battery_get_status(struct acpi_battery *battery)return 0 ; } +static void acpi_battery_clean_unprintable_chars (char *str,size_t length) +{ +for (unsigned int i =0 ; i < length; i++) { +if (!isascii (str[i]) || !isprint (str[i])) { + str[i] ='\0' ; +break ; + } + } +} static int extract_battery_info(const int use_bix, struct acpi_battery *battery,
@@ -524,6 +534,10 @@ static int extract_battery_info(const int use_bix, battery->capacity_now > battery->full_charge_capacity) battery->capacity_now = battery->full_charge_capacity; +if (!result) +acpi_battery_clean_unprintable_chars (battery->model_number, +ARRAY_SIZE (battery->model_number)); +return result; }
Signature · 1 line
2.55.0
Quoted 59 lines · Kate Hsuan
The battery Embedded Controller (EC) may return the model name with trailing unprintable or non-ASCII characters. For example, on some systems:
$ cat /sys/class/power_supply/BAT0/model_name LNV-5B10W51864��If a non-ASCII or an unprintable character is found, it will be replaced with '\0' to ensure the
model_numberis a valid string. If left intact, the malformed string prevents udev rules and hwdb working correctly.Signed-off-by Kate Hsuan <[email protected]>--- drivers/acpi/battery.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/acpi/battery.c +10
-0@@ -10,6 +10,7 @@#define pr_fmt (fmt) "ACPI: battery: " fmt +#include <linux/ctype.h> #include <linux/delay.h> #include <linux/dmi.h> #include <linux/jiffies.h> @@ -483,6 +484,15 @@ static int acpi_battery_get_status(struct acpi_battery *battery)return 0 ; } +static void acpi_battery_clean_unprintable_chars (char *str,size_t length) +{ +for (unsigned int i =0 ; i < length; i++) { +if (!isascii (str[i]) || !isprint (str[i])) { + str[i] ='\0' ; +break ; + } + } +} static int extract_battery_info(const int use_bix,drivers/acpi/battery.c +4
-0struct acpi_battery *battery,@@ -524,6 +534,10 @@ static int extract_battery_info(const int use_bix, battery->capacity_now > battery->full_charge_capacity) battery->capacity_now = battery->full_charge_capacity; +if (!result) +acpi_battery_clean_unprintable_chars (battery->model_number, +ARRAY_SIZE (battery->model_number)); +return result; }Signature · 1 line
2.55.0
Thanks Kate - I know this impacts a few of our platforms and it will be great to have this fix.
Implementation looks good to me.
Mark
Quoted 59 lines · Mark Pearson
Quoted 57 lines · Kate Hsuan
The battery Embedded Controller (EC) may return the model name with trailing unprintable or non-ASCII characters. For example, on some systems:
$ cat /sys/class/power_supply/BAT0/model_name LNV-5B10W51864��If a non-ASCII or an unprintable character is found, it will be replaced with '\0' to ensure the
model_numberis a valid string. If left intact, the malformed string prevents udev rules and hwdb working correctly.Signed-off-by Kate Hsuan <[email protected]>--- drivers/acpi/battery.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/acpi/battery.c +10
-0@@ -10,6 +10,7 @@#define pr_fmt (fmt) "ACPI: battery: " fmt +#include <linux/ctype.h> #include <linux/delay.h> #include <linux/dmi.h> #include <linux/jiffies.h> @@ -483,6 +484,15 @@ static int acpi_battery_get_status(struct acpi_battery *battery)return 0 ; } +static void acpi_battery_clean_unprintable_chars (char *str,size_t length) +{ +for (unsigned int i =0 ; i < length; i++) { +if (!isascii (str[i]) || !isprint (str[i])) { + str[i] ='\0' ; +break ; + } + } +} static int extract_battery_info(const int use_bix,struct
acpi_battery*battery,drivers/acpi/battery.c +4
-0@@ -524,6 +534,10 @@ static int extract_battery_info(const int use_bix, battery->capacity_now > battery->full_charge_capacity) battery->capacity_now = battery->full_charge_capacity; +if (!result) +acpi_battery_clean_unprintable_chars (battery->model_number, +ARRAY_SIZE (battery->model_number)); +return result; }
Quoted 7 lines
Quoted 2 lines
--
2.55.0Thanks Kate - I know this impacts a few of our platforms and it will be great to have this fix.
Implementation looks good to me.
Reviewed-by Mark Pearson <[email protected]>
Applied as 7.3 material, thanks!