Skip to content

Commit f2c47d4

Browse files
LibretroAdminLibretroAdmin
authored andcommitted
Fix buffer overflow vulnerability in system_property_get
Add size parameter to system_property_get() function to prevent buffer overflow when copying command output to caller's buffer. BUFFER OVERFLOW VULNERABILITY FIX: - system_property_get() was writing to output buffer without size checking - Function could overflow caller's buffer with large command output - Added value_size parameter to all function calls - Added bounds checking to prevent overflow - Uses sizeof() for buffer sizes at call sites SECURITY IMPACT: - Prevents potential memory corruption via malicious command output - Eliminates buffer overflow attack vector in Android property reading - Maintains same functionality with added safety This vulnerability could be exploited if an attacker could control the output of the 'getprop' command to exceed the caller's buffer size.
1 parent c3ac4a6 commit f2c47d4

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

frontend/drivers/platform_unix.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ typedef struct inotify_data
167167
#endif
168168

169169
int system_property_get(const char *command,
170-
const char *args, char *value)
170+
const char *args, char *value, size_t value_size)
171171
{
172172
FILE *pipe;
173173
char buffer[BUFSIZ];
@@ -176,6 +176,9 @@ int system_property_get(const char *command,
176176
size_t __len = 0;
177177
size_t _len = strlcpy(cmd, command, sizeof(cmd));
178178

179+
if (value_size == 0)
180+
return 0;
181+
179182
cmd[ _len] = ' ';
180183
cmd[++_len] = '\0';
181184

@@ -194,6 +197,20 @@ int system_property_get(const char *command,
194197
if (fgets(buffer, sizeof(buffer), pipe))
195198
{
196199
size_t _len = strlen(buffer);
200+
201+
/* Prevent buffer overflow by checking available space */
202+
if (__len + _len >= value_size - 1)
203+
{
204+
/* Copy only what fits, leaving space for null terminator */
205+
size_t remaining = value_size - __len - 1;
206+
if (remaining > 0)
207+
{
208+
memcpy(pos, buffer, remaining);
209+
pos += remaining;
210+
__len += remaining;
211+
}
212+
break;
213+
}
197214

198215
memcpy(pos, buffer, _len);
199216

@@ -562,15 +579,15 @@ void ANativeActivity_onCreate(ANativeActivity* activity,
562579

563580
void frontend_android_get_name(char *s, size_t len)
564581
{
565-
system_property_get("getprop", "ro.product.model", s);
582+
system_property_get("getprop", "ro.product.model", s, len);
566583
}
567584

568585
static void frontend_android_get_version(int32_t *major,
569586
int32_t *minor, int32_t *rel)
570587
{
571588
char os_version_str[PROP_VALUE_MAX] = {0};
572589
system_property_get("getprop", "ro.build.version.release",
573-
os_version_str);
590+
os_version_str, sizeof(os_version_str));
574591
*major = 0;
575592
*minor = 0;
576593
*rel = 0;
@@ -598,7 +615,7 @@ static void frontend_android_get_version(int32_t *major,
598615
void frontend_android_get_version_sdk(int32_t *sdk)
599616
{
600617
char os_version_str[PROP_VALUE_MAX] = {0};
601-
system_property_get("getprop", "ro.build.version.sdk", os_version_str);
618+
system_property_get("getprop", "ro.build.version.sdk", os_version_str, sizeof(os_version_str));
602619
*sdk = 0;
603620
if (os_version_str[0])
604621
*sdk = (int32_t)strtol(os_version_str, NULL, 10);
@@ -1883,7 +1900,7 @@ static void frontend_unix_get_env(int *argc,
18831900
}
18841901
}
18851902

1886-
system_property_get("getprop", "ro.product.model", device_model);
1903+
system_property_get("getprop", "ro.product.model", device_model, sizeof(device_model));
18871904

18881905
/* Set automatic default values per device */
18891906
if (g_platform_android_flags & PLAT_ANDROID_FLAG_XPERIA_PLAY_DEVICE)
@@ -2377,7 +2394,7 @@ static void frontend_unix_init(void *data)
23772394
g_platform_android_flags |= PLAT_ANDROID_FLAG_ANDROID_TV_DEVICE;
23782395
}
23792396

2380-
system_property_get("getprop", "ro.product.model", device_model);
2397+
system_property_get("getprop", "ro.product.model", device_model, sizeof(device_model));
23812398

23822399
/* Check if we are a game console device */
23832400
if (device_is_game_console(device_model))

0 commit comments

Comments
 (0)