]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
Validate argument to port option - bug 18695
authorShawn Routhier <sar@isc.org>
Tue, 6 Jan 2009 00:32:19 +0000 (00:32 +0000)
committerShawn Routhier <sar@isc.org>
Tue, 6 Jan 2009 00:32:19 +0000 (00:32 +0000)
RELNOTES
client/dhclient.c
common/inet.c
includes/dhcpd.h
relay/dhcrelay.c
server/dhcpd.c

index 72859b9f42f70b903d483cae82da859969194b44..fd2d051551698aa855354f3c595628516c9e2849 100644 (file)
--- a/RELNOTES
+++ b/RELNOTES
@@ -47,6 +47,10 @@ The system has only been tested on Linux, FreeBSD, and Solaris, and may not
 work on other platforms. Please report any problems and suggested fixes to
 <dhcp-users@isc.org>.
 
+                       Changes since 4.1.0 (bug fixes)
+
+- Validate the argument to the -p option.
+
                         Changes since 4.1.0b1
 
 - A missing "else" in dhcrelay.c could have caused an interface not to
index 54e971cc784f1738334b8f702b0a0fc540728451..c71003997a73cd13cf19aba6a5c0f67bf88cc035 100644 (file)
@@ -172,7 +172,7 @@ main(int argc, char **argv) {
                } 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")) {
index 7bee088811b03f604a0d25813230aeb90ff59605..1d50d40e205b0ad43386cef07fbd2771bdf42762 100644 (file)
@@ -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,2007 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 2004,2005,2007,2008 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
@@ -604,3 +604,26 @@ piaddrcidr(const struct iaddr *addr, unsigned int bits) {
        return ret;
 }
 
+/* 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);
+}
index 3867bd08d2321a73627e42448bdf8e675f3b7261..d1ca0fe21b8fa5c80624f589176f9a8a67b44be1 100644 (file)
@@ -40,6 +40,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <arpa/inet.h>
+#include <errno.h>
 
 #include <netdb.h>
 #else
@@ -2450,6 +2451,7 @@ isc_result_t free_iaddrcidrnetlist(struct iaddrcidrnetlist **result);
 const char *piaddr PROTO ((struct iaddr));
 char *piaddrmask(struct iaddr *, struct iaddr *);
 char *piaddrcidr(const struct iaddr *, unsigned int);
+u_int16_t validate_port(char *);
 
 /* dhclient.c */
 extern int nowait;
index 93bfb4ed516785f3d93437f898ecffc4567ca124..e585c900b613e27c10b67383a622504f93dc7959 100644 (file)
@@ -222,7 +222,7 @@ main(int argc, char **argv) {
                } 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], "-c")) {
index ff256fe688b37caca9ba0f5d82bba2fa3025b813..3d4d8c0b7ef879d78e725c9ef3e14c060c9bb1a4 100644 (file)
@@ -298,15 +298,7 @@ main(int argc, char **argv) {
                if (!strcmp (argv [i], "-p")) {
                        if (++i == argc)
                                usage ();
-                       for (s = argv [i]; *s; s++)
-                               if (!isdigit ((unsigned char)*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")) {
@@ -531,7 +523,7 @@ main(int argc, char **argv) {
        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 {