File: fix-cve-2025-31160

package info (click to toggle)
atop 2.8.1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,112 kB
  • sloc: ansic: 27,274; python: 253; sh: 231; makefile: 170
file content (625 lines) | stat: -rw-r--r-- 14,017 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
--- a/atop.c
+++ b/atop.c
@@ -324,6 +324,8 @@ char      	calcpss    = 0;  /* boolean:
 char      	getwchan   = 0;  /* boolean: obtain wchan string         */
 char      	rmspaces   = 0;  /* boolean: remove spaces from command  */
 		                 /* name in case of parseable output     */
+char		connectgpud    = 0; /* boolean: connect to atopgpud      */
+char		connectnetatop = 0; /* boolean: connect to netatop(bpf)  */
 
 unsigned short	hertz;
 unsigned int	pidwidth;
@@ -619,6 +621,14 @@ main(int argc, char *argv[])
 				rmspaces = 1;
 				break;
 
+                           case 'k':		/* try to open TCP connection to atopgpud */
+				connectgpud = 1;
+				break;
+
+                           case 'K':		/* try to open connection to netatop/netatop-bpf */
+				connectnetatop = 1;
+				break;
+
 			   default:		/* gather other flags */
 				flaglist[i++] = c;
 			}
@@ -736,7 +746,8 @@ main(int argc, char *argv[])
 	/*
  	** open socket to the IP layer to issue getsockopt() calls later on
 	*/
-	netatop_ipopen();
+	if (connectnetatop)
+		netatop_ipopen();
 
 	/*
 	** since privileged activities are finished now, there is no
@@ -839,11 +850,15 @@ engine(void)
 
 	/*
  	** open socket to the atopgpud daemon for GPU statistics
+	** if explicitly required
 	*/
-        nrgpus = gpud_init();
+	if (connectgpud)
+	{
+		nrgpus = gpud_init();
 
-	if (nrgpus)
-		supportflags |= GPUSTAT;
+		if (nrgpus)
+			supportflags |= GPUSTAT;
+	}
 
 	/*
 	** MAIN-LOOP:
@@ -890,7 +905,10 @@ engine(void)
 		** send request for statistics to atopgpud 
 		*/
 		if (nrgpus)
-			gpupending = gpud_statrequest();
+		{
+			if ((gpupending = gpud_statrequest()) == 0)
+				nrgpus = 0;
+		}
 
 		/*
 		** take a snapshot of the current system-level statistics 
@@ -915,28 +933,8 @@ engine(void)
 			// connection lost or timeout on receive?
 			if (nrgpuproc == -1)
 			{
-				int ng;
-
-				// try to reconnect
-        			ng = gpud_init();
-
-				if (ng != nrgpus)	// no success
-					nrgpus = 0;
-
-				if (nrgpus)
-				{
-					// request for stats again
-					if (gpud_statrequest())
-					{
-						// receive stats response
-						nrgpuproc = gpud_statresponse(nrgpus,
-						     cursstat->gpu.gpu, &gp);
-
-						// persistent failure?
-						if (nrgpuproc == -1)
-							nrgpus = 0;
-					}
-				}
+				nrgpus = 0;
+				supportflags &= ~GPUSTAT;
 			}
 
 			cursstat->gpu.nrgpus = nrgpus;
@@ -1025,7 +1023,7 @@ engine(void)
 		/*
  		** merge GPU per-process stats with other per-process stats
 		*/
-		if (nrgpus && nrgpuproc)
+		if (nrgpus && nrgpuproc > 0)
 			gpumergeproc(curtpres, ntaskpres,
 		                     curpexit, nprocexit,
 		 	             gp,       nrgpuproc);
@@ -1056,8 +1054,8 @@ engine(void)
 		if (nprocexitnet > 0)
 			netatop_exiterase();
 
-		if (gp)
-			 free(gp);
+		free(gp);
+		gp = NULL;      // avoid double free
 
 		if (lastcmd == 'r')	/* reset requested ? */
 		{
@@ -1102,6 +1100,8 @@ prusage(char *myname)
 			MRMSPACES);
 	printf("\t  -L  alternate line length (default 80) in case of "
 			"non-screen output\n");
+	printf("\t  -k  try to connect to external atopgpud daemon (default: do not connect)\n");
+	printf("\t  -K  try to connect to netatop/netatop-bpf interface (default: do not connect)\n");
 
 	if (vis.show_usage)
 		(*vis.show_usage)();
--- a/atop.h
+++ b/atop.h
@@ -89,6 +89,7 @@ extern char		calcpss;
 extern char		getwchan;
 extern char		rawname[];
 extern char		rawreadflag;
+extern char		connectnetatop;
 extern char		rmspaces;
 extern time_t		begintime, endtime, cursortime;	// epoch or time in day
 extern char		flaglist[];
--- a/deviate.c
+++ b/deviate.c
@@ -1053,7 +1053,7 @@ deviatsyst(struct sstat *cur, struct sst
 		/*
 		** determine new j
 		*/
-		if (pre->dsk.dsk[j].name)	// existing matching entry
+		if (pre->dsk.dsk[j].name[0])	// existing matching entry
 			j++;
 		else
 			j = realj;		// empty entry: stick to old j
@@ -1122,7 +1122,7 @@ deviatsyst(struct sstat *cur, struct sst
 		/*
 		** determine new j
 		*/
-		if (pre->dsk.mdd[j].name)	// existing matching entry
+		if (pre->dsk.mdd[j].name[0])	// existing matching entry
 			j++;
 		else
 			j = realj;		// empty entry: stick to old j
@@ -1191,7 +1191,7 @@ deviatsyst(struct sstat *cur, struct sst
 		/*
 		** determine new j
 		*/
-		if (pre->dsk.lvm[j].name)	// existing matching entry
+		if (pre->dsk.lvm[j].name[0])	// existing matching entry
 			j++;
 		else
 			j = realj;		// empty entry: stick to old j
--- a/gpucom.c
+++ b/gpucom.c
@@ -43,12 +43,12 @@
 
 #define	GPUDPORT	59123
 
-static void	gputype_parse(char *);
+static int	gputype_parse(char *);
 
-static void	gpustat_parse(int, char *, int,
+static int	gpustat_parse(int, char *, int,
 		                      struct pergpu *, struct gpupidstat *);
-static void	gpuparse(int, char *, struct pergpu *);
-static void	pidparse(int, char *, struct gpupidstat *);
+static int	gpuparse(int, char *, struct pergpu *);
+static int	pidparse(int, char *, struct gpupidstat *);
 static int	rcvuntil(int, char *, int);
 
 static int	actsock = -1;
@@ -150,20 +150,24 @@ gpud_init(void)
 	if ( rcvuntil(actsock, buf, length) == -1)
 	{
 		perror("receive type request from atopgpud");
+		free(buf);
 		goto close_and_return;
 	}
 
 	buf[length] = '\0';
 
-	gputype_parse(buf);
-
-        numgpus = numgpus <= MAXGPU ? numgpus : MAXGPU;
+	if (! gputype_parse(buf))
+	{
+		free(buf);
+		goto close_and_return;
+	}
 
 	return numgpus;
 
     close_and_return:
 	close(actsock);
 	actsock = -1;
+	numgpus = 0;
 	return 0;
 }
 
@@ -176,7 +180,7 @@ gpud_init(void)
 **
 ** Return value:
 ** 	0 in case of failure
-** 	1 in case of success
+** 	1 in case of success (request pending)
 */
 int
 gpud_statrequest(void)
@@ -190,6 +194,7 @@ gpud_statrequest(void)
 	{
 		close(actsock);
 		actsock = -1;
+		numgpus = 0;
 		return 0;
 	}
 
@@ -216,7 +221,7 @@ gpud_statresponse(int maxgpu, struct per
 	uint32_t	prelude;
 	char		*buf = NULL, *p;
 	int		version, length;
-	int		pids = 0;
+	int		maxprocs = 0, nrprocs;
 
 	if (actsock == -1)
 		return -1;
@@ -269,22 +274,22 @@ gpud_statresponse(int maxgpu, struct per
 	*(buf+length) = '\0';
 
 	/*
-	** determine number of per-process stats
-	** and malloc space to parse these stats
+	** determine number of per-process stats in string
+	** and malloc space to store these stats
 	*/
 	for (p=buf; *p; p++)
 	{
 		if (*p == PIDDELIM)
-			pids++;
+			maxprocs++;
 	}
 
 	if (gps)
 	{
-		if (pids)
+		if (maxprocs)
 		{
-			*gps = malloc(pids * sizeof(struct gpupidstat));
-			ptrverify(gps, "Malloc failed for gpu pidstats\n");
-			memset(*gps, 0, pids * sizeof(struct gpupidstat));
+			*gps = malloc(maxprocs * sizeof(struct gpupidstat));
+			ptrverify(*gps, "Malloc failed for gpu pidstats\n");
+			memset(*gps, 0, maxprocs * sizeof(struct gpupidstat));
 		}
 		else
 		{
@@ -295,18 +300,27 @@ gpud_statresponse(int maxgpu, struct per
 	/*
 	** parse stats string for per-gpu stats
 	*/
-	gpustat_parse(version, buf, maxgpu, ggs, gps ? *gps : NULL);
+	if ( (nrprocs =  gpustat_parse(version, buf, maxgpu, ggs, gps ? *gps : NULL)) == -1)
+	{
+		if (gps)
+		{
+			free(*gps);
+			*gps = NULL;    // avoid double free later on
+		}
+
+		goto close_and_return; // inconsistent data received from atopgpud
+	}
 
 	free(buf);
 
-	return pids;
+	return nrprocs;
 
     close_and_return:
-	if (buf)
-		free(buf);
+	free(buf);
 
 	close(actsock);
 	actsock = -1;
+	numgpus = 0;
 	return -1;
 }
 
@@ -314,6 +328,9 @@ gpud_statresponse(int maxgpu, struct per
 /*
 ** Receive given number of bytes from given socket
 ** into given buffer address
+**
+** Return value:  number of bytes received
+**                -1 - failed (including end-of-connection)
 */
 static int
 rcvuntil(int sock, char *buf, int size)
@@ -339,23 +356,27 @@ rcvuntil(int sock, char *buf, int size)
 **
 ** Store the type, busid and tasksupport of every GPU in
 ** static pointer tables
+**
+** Return value:  1 - success
+**                0 - failed
 */
-static void
+static int
 gputype_parse(char *buf)
 {
-	char	*p, *start, **bp, **tp, *cp;
+	char	*p, *start, **bp, **tp, *cp, fails=0;
 
 	/*
 	** determine number of GPUs
 	*/
 	if ( sscanf(buf, "%d@", &numgpus) != 1)
-	{
-		close(actsock);
-		actsock = -1;
-		return;
-	}
+		return 0;
+
+        numgpus = numgpus <= MAXGPU ? numgpus : MAXGPU;
 
-	for (p=buf; *p; p++)	// search for first delimiter
+	/*
+	** search for first GPU delimiter (@)
+	*/
+	for (p=buf; *p; p++)
 	{
 		if (*p == GPUDELIM)
 		{
@@ -364,6 +385,9 @@ gputype_parse(char *buf)
 		}
 	}
 
+	if (*p == 0)	// no delimiter or no data behind delimeter?
+		return 0;
+
 	/*
 	** parse GPU info and build arrays of pointers to the
 	** busid strings, type strings and tasksupport strings.
@@ -380,27 +404,47 @@ gputype_parse(char *buf)
 		ptrverify(gputypes, "Malloc failed for gpu types\n");
 		ptrverify(gputasks, "Malloc failed for gpu tasksup\n");
 
-		for (field=0, start=p; ; p++)
+		for (field=0, start=p; fails == 0; p++)
 		{
 			if (*p == ' ' || *p == '\0' || *p == GPUDELIM)
 			{
 				switch(field)
 				{
 				   case 0:
+					if (bp - gpubusid >= numgpus)
+					{
+						fails++;
+						break; 	// inconsistent with number of GPUs
+					}
+
 					if (p-start <= MAXGPUBUS)
 						*bp++ = start;
 					else
 						*bp++ = p - MAXGPUBUS;
 					break;
 				   case 1:
+					if (tp - gputypes >= numgpus)
+					{
+						fails++;
+						break; 	// inconsistent with number of GPUs
+					}
+
 					if (p-start <= MAXGPUTYPE)
 						*tp++ = start;
 					else
 						*tp++ = p - MAXGPUTYPE;
 					break;
 				   case 2:
+					if (cp - gputasks >= numgpus)
+					{
+						fails++;
+						break; 	// inconsistent with number of GPUs
+					}
+
 					*cp++ = *start;
 					break;
+				   default:
+					fails++;
 				}
 
 				field++;
@@ -418,7 +462,25 @@ gputype_parse(char *buf)
 
 		*bp = NULL;
 		*tp = NULL;
+
+		/*
+		** verify if number of GPUs and supplied per-GPU information
+		** appears to be inconsistent
+		*/
+		if (fails || bp - gpubusid != numgpus || tp - gputypes != numgpus || cp - gputasks != numgpus)
+		{
+			free(gpubusid);
+			free(gputypes);
+			free(gputasks);
+			return 0;
+		}
 	}
+	else
+	{
+		return 0;
+	}
+
+	return 1;
 }
 
 
@@ -429,106 +491,146 @@ gputype_parse(char *buf)
 ** with a '@' delimiter.
 ** Every series with counters on process level is introduced
 ** with a '#' delimiter (last part of the GPU level data).
+**
+** Return value:  valid number of processes 
+**                -1 - failed
 */
-static void
+static int
 gpustat_parse(int version, char *buf, int maxgpu, 
 		struct pergpu *gg, struct gpupidstat *gp)
 {
-	char	*p, *start, delimlast;
-	int	gpunum = 0;
+	char		*p, *pp, *start;
+	int		gpunum, nrprocs = 0;
 
 	/*
 	** parse stats string
 	*/
-	for (p=start=buf, delimlast=DUMMY; gpunum <= maxgpu; p++)
+	for (p=buf; *p && *p != GPUDELIM; p++)	// find first GPU deimiter
+		; 
+
+	if (*p == 0)	// string without GPU delimiter
+		return -1;
+
+	for (p++, start=p, gpunum=0; gpunum < maxgpu; p++)
 	{
-		char delimnow;
+		char delimnext;
 
-		if (*p != '\0' && *p != GPUDELIM && *p != PIDDELIM)
+		// search next GPU delimiter
+		//
+		if (*p && *p != GPUDELIM)
 			continue;
 
 		/*
-		** next delimiter or end-of-string found
+		** next GPU delimiter or end-of-string found
 		*/
-		delimnow = *p;
-		*p       = 0;
-
- 		switch (delimlast)
-		{
-		   case DUMMY:
-			break;
-
-		   case GPUDELIM:
-			gpuparse(version, start, gg);
+		delimnext = *p;
+		*p        = 0;
 
-			strcpy(gg->type,  gputypes[gpunum]);
-			strcpy(gg->busid, gpubusid[gpunum]);
+		/*
+		** parse GPU itself
+		*/
+		if (! gpuparse(version, start, gg))
+			return -1;
 
-			gpunum++;
-			gg++;
-			break;
+		strncpy(gg->type,  gputypes[gpunum], MAXGPUTYPE);
+		strncpy(gg->busid, gpubusid[gpunum], MAXGPUBUS);
 
-		   case PIDDELIM:
-			if (gp)
+		/*
+		** continue searching for per-process stats for this GPU
+		*/
+		if (gp)
+		{
+			for (pp = start; pp < p; pp++)
 			{
-				pidparse(version, start, gp);
+				if (*pp != PIDDELIM)
+					continue;
+
+				// new PID delimiter (#) found
+				//
+				if (! pidparse(version, pp+1, gp))
+					return -1;
 
 				gp->gpu.nrgpus++;
-				gp->gpu.gpulist = 1<<(gpunum-1);
+				gp->gpu.gpulist = 1<<gpunum;
 				gp++;
 
-				(gg-1)->nrprocs++;
+				gg->nrprocs++;	// per GPU
+				nrprocs++;	// total
 			}
 		}
 
-		if (delimnow == 0 || *(p+1) == 0)
+		gpunum++;
+		gg++;
+
+		if (delimnext == 0 || *(p+1) == 0)
 			break;
 
-		start     = p+1;
-		delimlast = delimnow;
+		start = p+1;
 	}
+
+	return nrprocs;
 }
 
 
 /*
 ** Parse GPU statistics string
+**
+** Return value:  1 - success
+**                0 - failed
 */
-static void
+static int
 gpuparse(int version, char *p, struct pergpu *gg)
 {
+	int nr;
+
 	switch (version)
 	{
 	   case 1:
-		(void) sscanf(p, "%d %d %lld %lld %lld %lld %lld %lld", 
+		nr = sscanf(p, "%d %d %lld %lld %lld %lld %lld %lld", 
 			&(gg->gpupercnow), &(gg->mempercnow),
 			&(gg->memtotnow),  &(gg->memusenow),
 			&(gg->samples),    &(gg->gpuperccum),
 			&(gg->memperccum), &(gg->memusecum));
 
+		if (nr < 8)	// parse error: unexpected data
+			return 0;
+
 		gg->nrprocs = 0;
 
 		break;
 	}
+
+	return 1;
 }
 
 
 /*
 ** Parse PID statistics string
+**
+** Return value:  1 - success
+**                0 - failed
 */
-static void
+static int
 pidparse(int version, char *p, struct gpupidstat *gp)
 {
+	int nr;
+
 	switch (version)
 	{
 	   case 1:
-		(void) sscanf(p, "%c %ld %d %d %lld %lld %lld %lld",
+		nr = sscanf(p, "%c %ld %d %d %lld %lld %lld %lld",
 			&(gp->gpu.state),   &(gp->pid),    
 			&(gp->gpu.gpubusy), &(gp->gpu.membusy),
 			&(gp->gpu.timems),
 			&(gp->gpu.memnow), &(gp->gpu.memcum),
 		        &(gp->gpu.sample));
+
+		if (nr < 8)	// parse error: unexpected data
+	 		return 0;
 		break;
 	}
+
+	return 1;
 }
 
 
--- a/photoproc.c
+++ b/photoproc.c
@@ -136,7 +136,8 @@ photoproc(struct tstat *tasklist, int ma
 	*/
 	regainrootprivs();
 
-	netatop_probe();
+	if (connectnetatop)
+		netatop_probe();
 
 	if (supportflags & CGROUPV2)
 		wipecgroupv2();