Sun Oct 6 00:27:50 2019 UTC ()
sprintf() -> snprintf(), and adjust a buffer size to avoid any
potential for overflow.


(mrg)
diff -r1.25 -r1.26 src/usr.bin/netstat/mroute.c

cvs diff -r1.25 -r1.26 src/usr.bin/netstat/mroute.c (expand / switch to unified diff)

--- src/usr.bin/netstat/mroute.c 2014/11/06 21:30:09 1.25
+++ src/usr.bin/netstat/mroute.c 2019/10/06 00:27:50 1.26
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: mroute.c,v 1.25 2014/11/06 21:30:09 christos Exp $ */ 1/* $NetBSD: mroute.c,v 1.26 2019/10/06 00:27:50 mrg Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Stephen Deering of Stanford University. 8 * Stephen Deering of Stanford University.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -66,27 +66,27 @@ @@ -66,27 +66,27 @@
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE. 69 * SUCH DAMAGE.
70 * 70 *
71 * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93 71 * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93
72 */ 72 */
73 73
74#include <sys/cdefs.h> 74#include <sys/cdefs.h>
75#ifndef lint 75#ifndef lint
76#if 0 76#if 0
77static char sccsid[] = "from: @(#)mroute.c 8.1 (Berkeley) 6/6/93"; 77static char sccsid[] = "from: @(#)mroute.c 8.1 (Berkeley) 6/6/93";
78#else 78#else
79__RCSID("$NetBSD: mroute.c,v 1.25 2014/11/06 21:30:09 christos Exp $"); 79__RCSID("$NetBSD: mroute.c,v 1.26 2019/10/06 00:27:50 mrg Exp $");
80#endif 80#endif
81#endif /* not lint */ 81#endif /* not lint */
82 82
83/* 83/*
84 * Print multicast routing structures and statistics. 84 * Print multicast routing structures and statistics.
85 * 85 *
86 * MROUTING 1.0 86 * MROUTING 1.0
87 */ 87 */
88 88
89#include <sys/param.h> 89#include <sys/param.h>
90#include <sys/socket.h> 90#include <sys/socket.h>
91#include <sys/socketvar.h> 91#include <sys/socketvar.h>
92#include <sys/protosw.h> 92#include <sys/protosw.h>
@@ -259,90 +259,90 @@ mroutepr(u_long mrpaddr, u_long mfchasht @@ -259,90 +259,90 @@ mroutepr(u_long mrpaddr, u_long mfchasht
259 } 259 }
260 if (!banner_printed) 260 if (!banner_printed)
261 printf("\nMulticast Forwarding Cache is empty\n"); 261 printf("\nMulticast Forwarding Cache is empty\n");
262 else 262 else
263 printf("\nTotal no. of entries in cache: %d\n", nmfc); 263 printf("\nTotal no. of entries in cache: %d\n", nmfc);
264 264
265 printf("\n"); 265 printf("\n");
266 numeric_addr = saved_numeric_addr; 266 numeric_addr = saved_numeric_addr;
267} 267}
268 268
269static void 269static void
270print_bw_meter(struct bw_meter *bw_meter, int *banner_printed) 270print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
271{ 271{
272 char s0[256], s1[256], s2[256], s3[256]; 272 char s0[256*3], s1[256], s2[256], s3[256];
273 struct timeval now, end, delta; 273 struct timeval now, end, delta;
274 274
275 gettimeofday(&now, NULL); 275 gettimeofday(&now, NULL);
276 276
277 if (! *banner_printed) { 277 if (! *banner_printed) {
278 printf(" Bandwidth Meters\n"); 278 printf(" Bandwidth Meters\n");
279 printf(" %-30s", "Measured(Start|Packets|Bytes)"); 279 printf(" %-30s", "Measured(Start|Packets|Bytes)");
280 printf(" %s", "Type"); 280 printf(" %s", "Type");
281 printf(" %-30s", "Thresh(Interval|Packets|Bytes)"); 281 printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
282 printf(" Remain"); 282 printf(" Remain");
283 printf("\n"); 283 printf("\n");
284 *banner_printed = 1; 284 *banner_printed = 1;
285 } 285 }
286 286
287 /* The measured values */ 287 /* The measured values */
288 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 288 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
289 sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets); 289 snprintf(s1, sizeof s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets);
290 else 290 else
291 sprintf(s1, "?"); 291 snprintf(s1, sizeof s1, "?");
292 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 292 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
293 sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes); 293 snprintf(s2, sizeof s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes);
294 else 294 else
295 sprintf(s2, "?"); 295 snprintf(s2, sizeof s2, "?");
296 sprintf(s0, "%lld.%ld|%s|%s", 296 snprintf(s0, sizeof s0, "%lld.%ld|%s|%s",
297 (long long)bw_meter->bm_start_time.tv_sec, 297 (long long)bw_meter->bm_start_time.tv_sec,
298 (long)bw_meter->bm_start_time.tv_usec, 298 (long)bw_meter->bm_start_time.tv_usec,
299 s1, s2); 299 s1, s2);
300 printf(" %-30s", s0); 300 printf(" %-30s", s0);
301 301
302 /* The type of entry */ 302 /* The type of entry */
303 sprintf(s0, "%s", "?"); 303 snprintf(s0, sizeof s0, "%s", "?");
304 if (bw_meter->bm_flags & BW_METER_GEQ) 304 if (bw_meter->bm_flags & BW_METER_GEQ)
305 sprintf(s0, "%s", ">="); 305 snprintf(s0, sizeof s0, "%s", ">=");
306 else if (bw_meter->bm_flags & BW_METER_LEQ) 306 else if (bw_meter->bm_flags & BW_METER_LEQ)
307 sprintf(s0, "%s", "<="); 307 snprintf(s0, sizeof s0, "%s", "<=");
308 printf(" %-3s", s0); 308 printf(" %-3s", s0);
309 309
310 /* The threshold values */ 310 /* The threshold values */
311 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 311 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
312 sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets); 312 snprintf(s1, sizeof s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets);
313 else 313 else
314 sprintf(s1, "?"); 314 snprintf(s1, sizeof s1, "?");
315 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 315 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
316 sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes); 316 snprintf(s2, sizeof s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes);
317 else 317 else
318 sprintf(s2, "?"); 318 snprintf(s2, sizeof s2, "?");
319 sprintf(s0, "%lld.%ld|%s|%s", 319 snprintf(s0, sizeof s0, "%lld.%ld|%s|%s",
320 (long long)bw_meter->bm_threshold.b_time.tv_sec, 320 (long long)bw_meter->bm_threshold.b_time.tv_sec,
321 (long)bw_meter->bm_threshold.b_time.tv_usec, 321 (long)bw_meter->bm_threshold.b_time.tv_usec,
322 s1, s2); 322 s1, s2);
323 printf(" %-30s", s0); 323 printf(" %-30s", s0);
324 324
325 /* Remaining time */ 325 /* Remaining time */
326 timeradd(&bw_meter->bm_start_time, 326 timeradd(&bw_meter->bm_start_time,
327 &bw_meter->bm_threshold.b_time, &end); 327 &bw_meter->bm_threshold.b_time, &end);
328 if (timercmp(&now, &end, <=)) { 328 if (timercmp(&now, &end, <=)) {
329 timersub(&end, &now, &delta); 329 timersub(&end, &now, &delta);
330 sprintf(s3, "%lld.%ld", 330 snprintf(s3, sizeof s3, "%lld.%ld",
331 (long long)delta.tv_sec, (long)delta.tv_usec); 331 (long long)delta.tv_sec, (long)delta.tv_usec);
332 } else { 332 } else {
333 /* Negative time */ 333 /* Negative time */
334 timersub(&now, &end, &delta); 334 timersub(&now, &end, &delta);
335 sprintf(s3, "-%lld.%ld", 335 snprintf(s3, sizeof s3, "-%lld.%ld",
336 (long long)delta.tv_sec, (long)delta.tv_usec); 336 (long long)delta.tv_sec, (long)delta.tv_usec);
337 } 337 }
338 printf(" %s", s3); 338 printf(" %s", s3);
339 339
340 printf("\n"); 340 printf("\n");
341} 341}
342 342
343void 343void
344mrt_stats(u_long mrpaddr, u_long mstaddr) 344mrt_stats(u_long mrpaddr, u_long mstaddr)
345{ 345{
346 u_int mrtproto; 346 u_int mrtproto;
347 struct mrtstat mrtstat; 347 struct mrtstat mrtstat;
348 348