Skip to content

Commit ec8f303

Browse files
committed
Fix security vulnerability CVE-2025-31160 (#334)
Atop will not connect to the TCP port of 'atopgpud' daemon any more by default. The flag -k can be used explicitly when 'atopgpud' is active. Also the code to parse the received strings is improved to avoid future issues with heap corruption. The flag -K has been implemented to connect to netatop/netatop-bpf.
1 parent e530cfd commit ec8f303

File tree

5 files changed

+210
-103
lines changed

5 files changed

+210
-103
lines changed

atop.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,12 @@ char displaymode = 'T'; /* 'T' = text, 'D' = draw */
185185
char barmono = 0; /* boolean: bar without categories? */
186186
/* name in case of parseable output */
187187

188-
char prependenv = 0; /* boolean: prepend selected */
188+
char prependenv = 0; /* boolean: prepend selected */
189189
/* environment variables to cmdline */
190+
191+
char connectgpud = 0; /* boolean: connect to atopgpud */
192+
char connectnetatop = 0; /* boolean: connect to netatop(bpf) */
193+
190194
regex_t envregex;
191195

192196
unsigned short hertz;
@@ -493,6 +497,14 @@ main(int argc, char *argv[])
493497
prependenv = 1;
494498
break;
495499

500+
case 'k': /* try to open TCP connection to atopgpud */
501+
connectgpud = 1;
502+
break;
503+
504+
case 'K': /* try to open connection to netatop/netatop-bpf */
505+
connectnetatop = 1;
506+
break;
507+
496508
default: /* gather other flags */
497509
flaglist[i++] = c;
498510
}
@@ -621,7 +633,8 @@ main(int argc, char *argv[])
621633
/*
622634
** open socket to the IP layer to issue getsockopt() calls later on
623635
*/
624-
netatop_ipopen();
636+
if (connectnetatop)
637+
netatop_ipopen();
625638

626639
/*
627640
** since privileged activities are finished now, there is no
@@ -735,11 +748,15 @@ engine(void)
735748

736749
/*
737750
** open socket to the atopgpud daemon for GPU statistics
751+
** if explicitly required
738752
*/
739-
nrgpus = gpud_init();
753+
if (connectgpud)
754+
{
755+
nrgpus = gpud_init();
740756

741-
if (nrgpus)
742-
supportflags |= GPUSTAT;
757+
if (nrgpus)
758+
supportflags |= GPUSTAT;
759+
}
743760

744761
/*
745762
** MAIN-LOOP:
@@ -786,7 +803,10 @@ engine(void)
786803
** send request for statistics to atopgpud
787804
*/
788805
if (nrgpus)
789-
gpupending = gpud_statrequest();
806+
{
807+
if ((gpupending = gpud_statrequest()) == 0)
808+
nrgpus = 0;
809+
}
790810

791811
/*
792812
** take a snapshot of the current system-level metrics
@@ -818,28 +838,8 @@ engine(void)
818838
// connection lost or timeout on receive?
819839
if (nrgpuproc == -1)
820840
{
821-
int ng;
822-
823-
// try to reconnect
824-
ng = gpud_init();
825-
826-
if (ng != nrgpus) // no success
827-
nrgpus = 0;
828-
829-
if (nrgpus)
830-
{
831-
// request for stats again
832-
if (gpud_statrequest())
833-
{
834-
// receive stats response
835-
nrgpuproc = gpud_statresponse(nrgpus,
836-
cursstat->gpu.gpu, &gp);
837-
838-
// persistent failure?
839-
if (nrgpuproc == -1)
840-
nrgpus = 0;
841-
}
842-
}
841+
nrgpus = 0;
842+
supportflags &= ~GPUSTAT;
843843
}
844844

845845
cursstat->gpu.nrgpus = nrgpus;
@@ -929,7 +929,7 @@ engine(void)
929929
/*
930930
** merge GPU per-process stats with other per-process stats
931931
*/
932-
if (nrgpus && nrgpuproc)
932+
if (nrgpus && nrgpuproc > 0)
933933
gpumergeproc(curtpres, ntaskpres,
934934
curpexit, nprocexit,
935935
gp, nrgpuproc);
@@ -976,8 +976,8 @@ engine(void)
976976
if ((supportflags & NETATOPD) && (nprocexitnet > 0))
977977
netatop_exiterase();
978978

979-
if (gp)
980-
free(gp);
979+
free(gp);
980+
gp = NULL; // avoid double free
981981

982982
if (lastcmd == MRESET) /* reset requested ? */
983983
{
@@ -1033,6 +1033,9 @@ prusage(char *myname)
10331033
"non-screen output\n");
10341034
printf("\t -z prepend regex matching environment variables to "
10351035
"command line\n");
1036+
printf("\t -k try to connect to external atopgpud daemon (default: do not connect)\n");
1037+
printf("\t -K try to connect to netatop/netatop-bpf interface (default: do not connect)\n");
1038+
10361039

10371040
if (vis.show_usage)
10381041
(*vis.show_usage)();

atop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ extern char calcpss;
9999
extern char getwchan;
100100
extern char rawname[];
101101
extern char rawreadflag;
102+
extern char connectnetatop;
102103
extern char rmspaces;
103104
extern time_t begintime, endtime, cursortime; // epoch or time in day
104105
extern char flaglist[];

0 commit comments

Comments
 (0)