From cf7b18f15eff39cd8b0ee1073faa3aeee8fa1bc1 Mon Sep 17 00:00:00 2001 From: Robert Watson Date: Fri, 24 Apr 2009 09:54:46 +0000 Subject: [PATCH] Relocate permissions checking code in in_control() to before the body of the implementation of ioctls. This makes the mapping of ioctls to specific privileges more explicit, and also simplifies the implementation by reducing the use of FALLTHROUGH handling in switch. While this is not intended to be a functional change, it does mean that certain privilege checks are now performed earlier, so EPERM might be returned in preference to EADDRNOTAVAIL for management ioctls that could have failed for both reasons. MFC after: 3 weeks --- sys/netinet/in.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/sys/netinet/in.c b/sys/netinet/in.c index e9e9d611992d..8d5f946799f8 100644 --- a/sys/netinet/in.c +++ b/sys/netinet/in.c @@ -279,6 +279,31 @@ in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, if (ifp == NULL) return (EADDRNOTAVAIL); + /* + * Security checks before we get involved in any work. + */ + switch (cmd) { + case SIOCAIFADDR: + case SIOCSIFADDR: + case SIOCSIFBRDADDR: + case SIOCSIFNETMASK: + case SIOCSIFDSTADDR: + if (td != NULL) { + error = priv_check(td, PRIV_NET_ADDIFADDR); + if (error) + return (error); + } + break; + + case SIOCDIFADDR: + if (td != NULL) { + error = priv_check(td, PRIV_NET_DELIFADDR); + if (error) + return (error); + } + break; + } + /* * Find address for this interface, if it exists. * @@ -334,13 +359,6 @@ in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, case SIOCSIFADDR: case SIOCSIFNETMASK: case SIOCSIFDSTADDR: - if (td != NULL) { - error = priv_check(td, (cmd == SIOCDIFADDR) ? - PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); - if (error) - return (error); - } - if (ia == NULL) { ia = (struct in_ifaddr *) malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); @@ -376,13 +394,6 @@ in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, break; case SIOCSIFBRDADDR: - if (td != NULL) { - error = priv_check(td, PRIV_NET_ADDIFADDR); - if (error) - return (error); - } - /* FALLTHROUGH */ - case SIOCGIFADDR: case SIOCGIFNETMASK: case SIOCGIFDSTADDR: