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
6 changes: 6 additions & 0 deletions display/d.mon/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ int check_mon(const char *name)
/* list related commands for given monitor */
void list_cmd(const char *name, FILE *fd_out)
{
if (strlen(name) >= GPATH_MAX) {
G_fatal_error(_("Monitor name <%s> is too long."), name);
}
nilason marked this conversation as resolved.
Show resolved Hide resolved
char *mon_path;
char cmd_file[GPATH_MAX], buf[4096];
FILE *fd;
Expand Down Expand Up @@ -136,6 +139,9 @@ void list_files(const char *name, FILE *fd_out)
strcat(tmpdir, "MONITORS");
strcat(tmpdir, "/");
strcat(tmpdir, name);
if (strlen(tmpdir) >= GPATH_MAX) {
G_fatal_error(_("The path for monitor <%s> is too long."), name);
}
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved

G_file_name(mon_path, tmpdir, NULL, G_mapset());
fprintf(fd_out, "path=%s\n", mon_path);
Expand Down
10 changes: 8 additions & 2 deletions display/d.mon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "proto.h"

#define MONITOR_NAME_LIMIT (GPATH_MAX - strlen("/MONITORS/") - 1)

int main(int argc, char *argv[])
{
struct GModule *module;
Expand Down Expand Up @@ -176,7 +178,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) < MONITOR_NAME_LIMIT) {
nilason marked this conversation as resolved.
Show resolved Hide resolved
if (selected_flag->answer) {
G_verbose_message(_("Currently selected monitor:"));
fprintf(stdout, "%s\n", mon);
Expand All @@ -194,8 +196,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
4 changes: 4 additions & 0 deletions display/d.mon/stop.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ static int stop(const char *);

int stop_mon(const char *name)
{
if (strlen(name) >= GPATH_MAX) {
G_fatal_error(_("Monitor name <%s> is too long."), name);
}

nilason marked this conversation as resolved.
Show resolved Hide resolved
if (!check_mon(name)) {
G_fatal_error(_("Monitor <%s> is not running"), name);
}
Expand Down
Loading