Skip to content

Commit 3b1499f

Browse files
committed
Fix buffer overflows when argv[0] or env variables are longer than PATH_MAX
1 parent 47b6655 commit 3b1499f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/runtime/runtime.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,10 +1344,18 @@ int main(int argc, char* argv[]) {
13441344
*/
13451345
if (getenv("TARGET_APPIMAGE") == NULL) {
13461346
strcpy(appimage_path, "/proc/self/exe");
1347-
strcpy(argv0_path, argv[0]);
1347+
char *res = memccpy(argv0_path, argv[0], '\0', sizeof(argv0_path));
1348+
if (res == NULL) {
1349+
fprintf(stderr, "Program name too big\n");
1350+
exit(EXIT_EXECERROR);
1351+
}
13481352
} else {
1349-
strcpy(appimage_path, getenv("TARGET_APPIMAGE"));
1350-
strcpy(argv0_path, getenv("TARGET_APPIMAGE"));
1353+
char *res1 = memccpy(appimage_path, getenv("TARGET_APPIMAGE"), '\0', sizeof(appimage_path));
1354+
char *res2 = memccpy(argv0_path, getenv("TARGET_APPIMAGE"), '\0', sizeof(argv0_path));
1355+
if (res1 == NULL || res2 == NULL) {
1356+
fprintf(stderr, "TARGET_APPIMAGE environment variable too big\n");
1357+
exit(EXIT_EXECERROR);
1358+
}
13511359
}
13521360

13531361
// temporary directories are required in a few places
@@ -1356,8 +1364,13 @@ int main(int argc, char* argv[]) {
13561364

13571365
{
13581366
const char* const TMPDIR = getenv("TMPDIR");
1359-
if (TMPDIR != NULL)
1360-
strcpy(temp_base, getenv("TMPDIR"));
1367+
if (TMPDIR != NULL) {
1368+
char *res = memccpy(temp_base, TMPDIR, '\0', sizeof(temp_base));
1369+
if (res == NULL) {
1370+
fprintf(stderr, "TMPDIR environemnt variable too big\n");
1371+
exit(EXIT_EXECERROR);
1372+
}
1373+
}
13611374
}
13621375

13631376
fs_offset = appimage_get_elf_size(appimage_path);

0 commit comments

Comments
 (0)