summaryrefslogtreecommitdiffstats
path: root/sbin
diff options
context:
space:
mode:
authorfenner <fenner@FreeBSD.org>1996-08-09 06:00:53 +0000
committerfenner <fenner@FreeBSD.org>1996-08-09 06:00:53 +0000
commit679c08513a643e60de352cbffdafb1bda8b05000 (patch)
tree80efc4c03bc4735309d7ed128353200a207a1302 /sbin
parentdc9c4c1ea7990ee7407f670e55b78b9d0ce44658 (diff)
downloadFreeBSD-src-679c08513a643e60de352cbffdafb1bda8b05000.zip
FreeBSD-src-679c08513a643e60de352cbffdafb1bda8b05000.tar.gz
Drop setuid ASAP, to minimize code executed as root.
Reviewed by: pst
Diffstat (limited to 'sbin')
-rw-r--r--sbin/ping/ping.c20
1 files changed, 17 insertions, 3 deletions
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);
}
OpenPOWER on IntegriCloud