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

d.mon: Fix Unbounded Source Buffer in main.c file of d.mon module #4260

Merged
merged 12 commits into from
Sep 19, 2024
Merged
54 changes: 54 additions & 0 deletions display/d.mon/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,40 @@
char *get_path(const char *name, int fpath)
{
char tmpdir[GPATH_MAX];
int available_space;

G_temp_element(tmpdir);
available_space = GPATH_MAX - strlen(tmpdir) - 1;
if (available_space < strlen("/")) {
G_fatal_error(
_("Insufficient space to append / to the path for <%s>."),
name);
ShubhamDesai marked this conversation as resolved.
Show resolved Hide resolved
}
strcat(tmpdir, "/");
available_space -= strlen("/");
if (available_space < strlen("MONITORS")) {
G_fatal_error(
_("Insufficient space to append MONITORS to the path for <%s>."),
name);
}
strcat(tmpdir, "MONITORS");
available_space -= strlen("MONITORS");
if (name) {
if (available_space < strlen("/")) {
G_fatal_error(
_("Insufficient space to append / to the path for <%s>."),
name);
}
strcat(tmpdir, "/");
available_space -= strlen("/");

if (available_space < strlen(name)) {
G_fatal_error(_("Insufficient space to append the monitor name "
"<%s> to the path."),
name);
}
strcat(tmpdir, name);
available_space -= strlen(name);
}

if (fpath) {
Expand Down Expand Up @@ -130,12 +157,39 @@ void list_files(const char *name, FILE *fd_out)
char tmpdir[GPATH_MAX], mon_path[GPATH_MAX];
struct dirent *dp;
DIR *dirp;
int available_space;

G_temp_element(tmpdir);
available_space = GPATH_MAX - strlen(tmpdir) - 1;
if (available_space < strlen("/")) {
G_fatal_error(
_("Insufficient space to append / to the path for <%s>."),
name);
ShubhamDesai marked this conversation as resolved.
Show resolved Hide resolved
}
strcat(tmpdir, "/");
available_space -= strlen("/");
if (available_space < strlen("MONITORS")) {
G_fatal_error(
_("Insufficient space to append MONITORS to the path for <%s>."),
name);
}
strcat(tmpdir, "MONITORS");
available_space -= strlen("MONITORS");

if (available_space < strlen("/")) {
G_fatal_error(_("Insufficient space to append / to the path for <%s>."),
name);
}
strcat(tmpdir, "/");
available_space -= strlen("/");

if (available_space < strlen(name)) {
G_fatal_error(_("Insufficient space to append the monitor name <%s> to "
"the path."),
name);
}
strcat(tmpdir, name);
available_space -= strlen(name);

G_file_name(mon_path, tmpdir, NULL, G_mapset());
fprintf(fd_out, "path=%s\n", mon_path);
Expand Down
8 changes: 6 additions & 2 deletions display/d.mon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ int main(int argc, char *argv[])
if (list_flag->answer)
G_warning(_("Flag -%c ignored"), list_flag->key);
mon = G_getenv_nofatal("MONITOR");
if (mon) {
if (mon && strlen(mon) < GNAME_MAX) {
if (selected_flag->answer) {
G_verbose_message(_("Currently selected monitor:"));
fprintf(stdout, "%s\n", mon);
Expand All @@ -194,8 +194,12 @@ int main(int argc, char *argv[])
ret = stop_mon(mon);
}
}
else
else if (!mon) {
G_important_message(_("No monitor selected"));
}
else {
G_important_message(_("Monitor name is too long"));
}

exit(EXIT_SUCCESS);
}
Expand Down
Loading