Skip to contentSkip to conversation

tools/power/acpi: Replace strcpy/strcat with snprintf in osunixdir.c

PATCH In review Sumeet Pawnikar 2 participants 2 messages Feb 1 – Feb 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/power/acpi: Replace strcpy/strcat with snprintf in osunixdir.c

Replace unsafe strcpy() and strcat() calls with snprintf() in osunixdir.c to prevent potential buffer overflow vulnerabilities when constructing file paths.

The snprintf() function performs automatic bounds checking to ensure the destination buffer is not overflowed.

No functional change.

---
 .../power/acpi/os_specific/service_layers/osunixdir.c  | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/acpi/os_specific/service_layers/osunixdir.c +5 -5
index b9bb83116549..5dc960c6b376 100644
@@ -113,9 +113,8 @@ char *acpi_os_get_next_filename(void *dir_handle)
 				return (NULL);
 			}
 
-			strcpy(temp_str, external_info->dir_pathname);
-			strcat(temp_str, "/");
-			strcat(temp_str, dir_entry->d_name);
+			snprintf(temp_str, str_len, "%s/%s",
+				 external_info->dir_pathname, dir_entry->d_name);
 
 			err = stat(temp_str, &temp_stat);
 			if (err == -1) {
@@ -137,8 +136,9 @@ char *acpi_os_get_next_filename(void *dir_handle)
 
 				/* copy to a temp buffer because dir_entry struct is on the stack */
 
-				strcpy(external_info->temp_buffer,
-				       dir_entry->d_name);
+				snprintf(external_info->temp_buffer,
+					 sizeof(external_info->temp_buffer),
+					 "%s", dir_entry->d_name);
 				return (external_info->temp_buffer);
 			}
 		}
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Sumeet Pawnikar

Replace unsafe strcpy() and strcat() calls with snprintf() in osunixdir.c to prevent potential buffer overflow vulnerabilities when constructing file paths.

The snprintf() function performs automatic bounds checking to ensure the destination buffer is not overflowed.

No functional change.

The ACPI tools come from ACPICA, so if you want to make changes to them, you need to do that in upstream ACPICA.

Refer to Documentation/driver-api/acpi/linuxized-acpica.rst for more information.

Thanks!

Quoted 36 lines
---
 .../power/acpi/os_specific/service_layers/osunixdir.c  | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/power/acpi/os_specific/service_layers/osunixdir.c +5 -6
index b9bb83116549..5dc960c6b376 100644
@@ -113,9 +113,8 @@ char *acpi_os_get_next_filename(void *dir_handle)
                                return (NULL);
                        }
 
-                       strcpy(temp_str, external_info->dir_pathname);
-                       strcat(temp_str, "/");
-                       strcat(temp_str, dir_entry->d_name);
+                       snprintf(temp_str, str_len, "%s/%s",
+                                external_info->dir_pathname, dir_entry->d_name);
 
                        err = stat(temp_str, &temp_stat);
                        if (err == -1) {
@@ -137,8 +136,9 @@ char *acpi_os_get_next_filename(void *dir_handle)
 
                                /* copy to a temp buffer because dir_entry struct is on the stack */
 
-                               strcpy(external_info->temp_buffer,
-                                      dir_entry->d_name);
+                               snprintf(external_info->temp_buffer,
+                                        sizeof(external_info->temp_buffer),
+                                        "%s", dir_entry->d_name);
                                return (external_info->temp_buffer);
                        }
                }
--

2.43.0