Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buffer overflows when argv[0] or env variables are longer than PATH_MAX #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ char* appimage_hexlify(const char* bytes, const size_t numBytes) {
}

int main(int argc, char* argv[]) {
char appimage_path[PATH_MAX];
char appimage_path[PATH_MAX] = "/proc/self/exe";
char argv0_path[PATH_MAX];
char* arg;

Expand All @@ -1342,12 +1342,20 @@ int main(int argc, char* argv[]) {
* change any time. Do not rely on it being present. We might even limit this
* functionality specifically for builds used by appimaged.
*/
if (getenv("TARGET_APPIMAGE") == NULL) {
strcpy(appimage_path, "/proc/self/exe");
strcpy(argv0_path, argv[0]);
const char* const TARGET_APPIMAGE = getenv("TARGET_APPIMAGE");
if (TARGET_APPIMAGE == NULL) {
char *res = memccpy(argv0_path, argv[0], '\0', sizeof(argv0_path));
if (res == NULL) {
fprintf(stderr, "Program name too big\n");
exit(EXIT_EXECERROR);
}
} else {
strcpy(appimage_path, getenv("TARGET_APPIMAGE"));
strcpy(argv0_path, getenv("TARGET_APPIMAGE"));
char *res1 = memccpy(appimage_path, TARGET_APPIMAGE, '\0', sizeof(appimage_path));
char *res2 = memccpy(argv0_path, TARGET_APPIMAGE, '\0', sizeof(argv0_path));
if (res1 == NULL || res2 == NULL) {
fprintf(stderr, "TARGET_APPIMAGE environment variable too big\n");
exit(EXIT_EXECERROR);
}
}

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

{
const char* const TMPDIR = getenv("TMPDIR");
if (TMPDIR != NULL)
strcpy(temp_base, getenv("TMPDIR"));
if (TMPDIR != NULL) {
char *res = memccpy(temp_base, TMPDIR, '\0', sizeof(temp_base));
if (res == NULL) {
fprintf(stderr, "TMPDIR environemnt variable too big\n");
exit(EXIT_EXECERROR);
}
}
}

fs_offset = appimage_get_elf_size(appimage_path);
Expand Down
Loading