summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn Routhier <sar@isc.org>2009-01-06 00:51:24 +0000
committerShawn Routhier <sar@isc.org>2009-01-06 00:51:24 +0000
commitecb2f312ba4c6372831ff66c9924f68b067cf18d (patch)
treeabf0e3a7540cdb656e3f83676395f2a915a3f0cb
parenta2477d70b060adfdf3383e2bd94615b017c2bfb6 (diff)
downloadisc-dhcp-ecb2f312ba4c6372831ff66c9924f68b067cf18d.tar.gz
Validate argument to port option - bug 18695
-rw-r--r--RELNOTES4
-rw-r--r--client/dhclient.c4
-rw-r--r--common/inet.c29
-rw-r--r--includes/dhcpd.h2
-rw-r--r--relay/dhcrelay.c4
-rw-r--r--server/dhcpd.c14
6 files changed, 39 insertions, 18 deletions
diff --git a/RELNOTES b/RELNOTES
index e07e5d55..295b2352 100644
--- a/RELNOTES
+++ b/RELNOTES
@@ -38,6 +38,10 @@ as well as how to find documentation and report bugs, please consult
the README file.
+ Changes since 3.1.2 (bug fixes)
+
+- Validate the argument to the -p option.
+
Changes since 3.1.2rc1
- None.
diff --git a/client/dhclient.c b/client/dhclient.c
index 3180d0ad..867eaa9a 100644
--- a/client/dhclient.c
+++ b/client/dhclient.c
@@ -32,7 +32,7 @@
#ifndef lint
static char ocopyright[] =
-"$Id: dhclient.c,v 1.143.2.10 2008/06/11 20:20:31 dhankins Exp $ Copyright (c) 2004-2008 Internet Systems Consortium. All rights reserved.\n";
+"$Id: dhclient.c,v 1.143.2.11 2009/01/06 00:51:24 sar Exp $ Copyright (c) 2004-2008 Internet Systems Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
@@ -151,7 +151,7 @@ int main (argc, argv, envp)
} else if (!strcmp (argv [i], "-p")) {
if (++i == argc)
usage ();
- local_port = htons (atoi (argv [i]));
+ local_port = validate_port (argv [i]);
log_debug ("binding to user-specified port %d",
ntohs (local_port));
} else if (!strcmp (argv [i], "-d")) {
diff --git a/common/inet.c b/common/inet.c
index 934eba19..470c51b4 100644
--- a/common/inet.c
+++ b/common/inet.c
@@ -1,10 +1,10 @@
/* inet.c
- Subroutines to manipulate internet addresses in a safely portable
+ Subroutines to manipulate internet addresses and ports in a safely portable
way... */
/*
- * Copyright (c) 2004-2005 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 2004-2005,2009 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
@@ -35,7 +35,7 @@
#ifndef lint
static char copyright[] =
-"$Id: inet.c,v 1.11 2006/05/15 15:07:49 dhankins Exp $ Copyright (c) 2004-2005 Internet Systems Consortium. All rights reserved.\n";
+"$Id: inet.c,v 1.11.84.1 2009/01/06 00:51:24 sar Exp $ Copyright (c) 2004-2005 Internet Systems Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
@@ -270,3 +270,26 @@ char *piaddrmask (struct iaddr addr, struct iaddr mask,
return s;
}
+/* Validate that the string represents a valid port number and
+ * return it in network byte order
+ */
+
+u_int16_t
+validate_port(char *port) {
+ int local_port = 0;
+ int lower = 1;
+ int upper = 65535;
+ char *endptr;
+
+ errno = 0;
+ local_port = strtol(port, &endptr, 10);
+
+ if ((*endptr != '\0') || (errno == ERANGE) || (errno == EINVAL))
+ log_fatal ("Invalid port number specification: %s", port);
+
+ if (local_port < lower || local_port > upper)
+ log_fatal("Port number specified is out of range (%d-%d).",
+ lower, upper);
+
+ return htons(local_port);
+}
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
index 3f4187cf..cf3b5979 100644
--- a/includes/dhcpd.h
+++ b/includes/dhcpd.h
@@ -38,6 +38,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
+#include <errno.h>
#include <netdb.h>
#else
@@ -2013,6 +2014,7 @@ int addr_eq PROTO ((struct iaddr, struct iaddr));
int addr_match(struct iaddr *, struct iaddrmatch *);
char *piaddr PROTO ((struct iaddr));
char *piaddrmask (struct iaddr, struct iaddr, const char *, int);
+u_int16_t validate_port(char *);
/* dhclient.c */
extern const char *path_dhclient_conf;
diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c
index b7b4ead5..907087f0 100644
--- a/relay/dhcrelay.c
+++ b/relay/dhcrelay.c
@@ -34,7 +34,7 @@
#ifndef lint
static char ocopyright[] =
-"$Id: dhcrelay.c,v 1.59.2.4 2008/07/16 18:14:56 each Exp $ Copyright (c) 2004-2008 Internet Systems Consortium. All rights reserved.\n";
+"$Id: dhcrelay.c,v 1.59.2.5 2009/01/06 00:51:24 sar Exp $ Copyright (c) 2004-2008 Internet Systems Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
@@ -156,7 +156,7 @@ int main (argc, argv, envp)
if (!strcmp (argv [i], "-p")) {
if (++i == argc)
usage ();
- local_port = htons (atoi (argv [i]));
+ local_port = validate_port (argv [i]);
log_debug ("binding to user-specified port %d",
ntohs (local_port));
} else if (!strcmp (argv [i], "-d")) {
diff --git a/server/dhcpd.c b/server/dhcpd.c
index 710f4e3c..6d096b24 100644
--- a/server/dhcpd.c
+++ b/server/dhcpd.c
@@ -34,7 +34,7 @@
#ifndef lint
static char ocopyright[] =
-"$Id: dhcpd.c,v 1.121.42.5 2008/03/18 18:30:20 dhankins Exp $ Copyright 2004-2008 Internet Systems Consortium.";
+"$Id: dhcpd.c,v 1.121.42.6 2009/01/06 00:51:24 sar Exp $ Copyright 2004-2008 Internet Systems Consortium.";
#endif
static char copyright[] =
@@ -266,15 +266,7 @@ int main (argc, argv, envp)
if (!strcmp (argv [i], "-p")) {
if (++i == argc)
usage ();
- for (s = argv [i]; *s; s++)
- if (!isdigit (*s))
- log_fatal ("%s: not a valid UDP port",
- argv [i]);
- status = atoi (argv [i]);
- if (status < 1 || status > 65535)
- log_fatal ("%s: not a valid UDP port",
- argv [i]);
- local_port = htons (status);
+ local_port = validate_port (argv [i]);
log_debug ("binding to user-specified port %d",
ntohs (local_port));
} else if (!strcmp (argv [i], "-f")) {
@@ -397,7 +389,7 @@ int main (argc, argv, envp)
if (!local_port)
{
if ((s = getenv ("DHCPD_PORT"))) {
- local_port = htons (atoi (s));
+ local_port = validate_port (s);
log_debug ("binding to environment-specified port %d",
ntohs (local_port));
} else {