From 679c08513a643e60de352cbffdafb1bda8b05000 Mon Sep 17 00:00:00 2001 From: fenner Date: Fri, 9 Aug 1996 06:00:53 +0000 Subject: Drop setuid ASAP, to minimize code executed as root. Reviewed by: pst --- sbin/ping/ping.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'sbin') diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index de19a45..dfd1c2a 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -154,13 +154,26 @@ main(argc, argv) struct protoent *proto; struct termios ts; register int i; - int ch, fdmask, hold, packlen, preload; + int ch, fdmask, hold, packlen, preload, sockerrno; u_char *datap, *packet; char *target, hnamebuf[MAXHOSTNAMELEN], *malloc(); #ifdef IP_OPTIONS char rspace[3 + 4 * NROUTES + 1]; /* record route space */ #endif + /* + * Do the stuff that we need root priv's for *first*, and + * then drop our setuid bit. Save error reporting for + * after arg parsing. + */ + proto = getprotobyname("icmp"); + if (proto) { + s = socket(AF_INET, SOCK_RAW, proto->p_proto); + sockerrno = errno; + } + + setuid(getuid()); + preload = 0; if (tcgetattr (0, &ts) != -1) { reset_kerninfo = !(ts.c_lflag & NOKERNINFO); @@ -288,11 +301,12 @@ main(argc, argv) ident = getpid() & 0xFFFF; - if (!(proto = getprotobyname("icmp"))) { + if (!proto) { (void)fprintf(stderr, "ping: unknown protocol icmp.\n"); exit(1); } - if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0) { + if (s < 0) { + errno = sockerrno; perror("ping: socket"); exit(1); } -- cgit v1.1