From patchwork Mon Apr 15 17:52:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Herbert X-Patchwork-Id: 1085857 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herbertland.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=herbertland-com.20150623.gappssmtp.com header.i=@herbertland-com.20150623.gappssmtp.com header.b="ZNJz6F84"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44jbh14bd2z9s71 for ; Tue, 16 Apr 2019 03:53:05 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727866AbfDORxE (ORCPT ); Mon, 15 Apr 2019 13:53:04 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:35044 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727783AbfDORxD (ORCPT ); Mon, 15 Apr 2019 13:53:03 -0400 Received: by mail-pl1-f196.google.com with SMTP id w24so8944319plp.2 for ; Mon, 15 Apr 2019 10:53:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=herbertland-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=exEH8AAk1DdJFl0za7kp0y8Laame4Vj1M2tPQwyTAxA=; b=ZNJz6F84xmGHhZRHLoZ/ccofM0AyojPdN6Oq+NUg+UR4g8mX45TIgeoZVtVSoSIIFX ydFBUS0nrvvv/UGQ4wUl3FoXhT7cRVOsZ47Uxd5DXyDBU3iRN7Bw5fDjdGPUMNwIpyzZ nrs4flMzSOXrQqzZz6eRilatcNaojL+ax21idQIGC9VV4L/kNuFM4gfMfF9VO85KNkNQ /84OnOwJqKVRAsb5EMj3Ldd8tWwr6UDXJBIX69QZk4wykKHYeGadmqfo81+zd0z6iy7t 7wKTJUbBC+u1zQLwiUaOxbuvTHmxQAQz1bC7ibrnGAlXtt6pIKQIn/P85w7MAuDDSNoz uN+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=exEH8AAk1DdJFl0za7kp0y8Laame4Vj1M2tPQwyTAxA=; b=L8kirbAhfpEQuZJysjIPlgw/iw51Dm+flYsT7DwMHG/kLkHR9qdWstOgeBpvg2ZTyT TecDa6aE2uKh/u14jiWw9k/hZjA7b2FLvyOqmfPM68DLOT6+jwv2UVHdG1vM/1JqzD87 pV/Xpqdyp0J3MGoCI/sT16oYX/it3MLrrLWMe9TWYwjgENIGbuDDMi7Pio3OXMJcEPOH iQ/k0DG8zl5oQ7H6MQABfN1A0RpzebJRnJ1pV4IsJzF9dwORJdX3ae/5Y1oB60UI6kD1 PyDetuf5892csa37PUjbcRCXkP2uk+1Y5jhG3d+Umv4VSV1WVx6e8fsr7zjxpmkad6VM LP/Q== X-Gm-Message-State: APjAAAUCSXW7DiYlG29SUSFOyc8R1qaXUyGnWeg+c+DzPQX/h9SB8uNn UHvXXS4zBrtc+r//vJQOE5sIRg== X-Google-Smtp-Source: APXvYqyfiY8sEDNX5firoqyf86YlaaI4wJgOFu+sR1GITIT8hwa6AgFcYHjHWESBgtkU4+3rfhOBXA== X-Received: by 2002:a17:902:d83:: with SMTP id 3mr52907010plv.125.1555350781242; Mon, 15 Apr 2019 10:53:01 -0700 (PDT) Received: from localhost.localdomain (c-73-223-249-119.hsd1.ca.comcast.net. [73.223.249.119]) by smtp.gmail.com with ESMTPSA id t24sm58751170pfe.110.2019.04.15.10.53.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Apr 2019 10:53:00 -0700 (PDT) From: Tom Herbert X-Google-Original-From: Tom Herbert To: davem@davemloft.net, netdev@vger.kernel.org Cc: Tom Herbert Subject: [PATCH v3 net-next 7/7] ip6tlvs: Validation of TX Destination and Hop-by-Hop options Date: Mon, 15 Apr 2019 10:52:20 -0700 Message-Id: <1555350740-23490-8-git-send-email-tom@quantonium.net> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1555350740-23490-1-git-send-email-tom@quantonium.net> References: <1555350740-23490-1-git-send-email-tom@quantonium.net> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Validate Destination and Hop-by-Hop options. This uses the information in the TLV parameters table to validate various aspects of both individual TLVs as well as a list of TLVs in an extension header. There are two levels of validation that can be performed: simple checks and deep checks. Simple checks validate only the most basic properties such as that the TLV list fits into the EH. Deep checks do a fine grained validation that includes perferred ordering, length limits, and length alignment. With proper permissions set in the TLV parameter table, this patch allows non-privileged users to send TLVs. Given that TLVs are open ended and potentially a source of DOS attack, deep checks are performed to limit the format that a non-privileged user can send. If deep checks are enabled, a canonical format for sending TLVs is enforced (in adherence with the robustness principle). A TLV must be well ordered with respect to the preferred order for the TLV. Each TLV must be aligned as described in the parameter table. Minimal padding (one padding TLV) is used to align TLVs. The length of the extension header as well as the count of non-padding TLVs is checked against max_*_opts_len and max_*_opts_cnt. For individual TLVs, length limits and length alignment is checked. --- include/net/ipv6.h | 23 +++ net/ipv6/datagram.c | 27 +++- net/ipv6/exthdrs_core.c | 373 +++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/ipv6_sockglue.c | 43 ++---- 4 files changed, 434 insertions(+), 32 deletions(-) diff --git a/include/net/ipv6.h b/include/net/ipv6.h index a1a0af2..f7f079f 100644 --- a/include/net/ipv6.h +++ b/include/net/ipv6.h @@ -472,6 +472,20 @@ int exthdrs_init(struct tlv_param_table *tlv_param_table, int num_init_params); void exthdrs_fini(struct tlv_param_table *tlv_param_table); +int ipv6_opt_validate_tlvs(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_opt_hdr *opt, + unsigned int optname, bool admin, + unsigned int max_len, unsigned int max_cnt); +int ipv6_opt_validate_single_tlv(struct net *net, + struct tlv_param_table *tlv_param_table, + unsigned int optname, + unsigned char *tlv, size_t len, + bool deleting, bool admin); +int ipv6_opt_check_perm(struct net *net, + struct tlv_param_table *tlv_param_table, + struct sock *sk, int optname, bool admin); + /* tlv_get_proc assumes rcu_read_lock is held */ static inline struct tlv_proc *tlv_get_proc( struct tlv_param_table *tlv_param_table, @@ -488,6 +502,15 @@ bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, struct ipv6_txoptions *ipv6_update_options(struct sock *sk, struct ipv6_txoptions *opt); +struct ipv6_txoptions *txoptions_from_opt(struct sock *sk, + struct tlv_param_table + *tlv_param_table, + struct ipv6_txoptions *opt, + int optname, char __user *optval, + unsigned int optlen, + unsigned int max_len, + unsigned int max_cnt); + static inline bool ipv6_accept_ra(struct inet6_dev *idev) { /* If forwarding is enabled, RA are not accepted unless the special diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c index f4742db..1d37ac1 100644 --- a/net/ipv6/datagram.c +++ b/net/ipv6/datagram.c @@ -844,7 +844,10 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, case IPV6_HOPOPTS: case IPV6_2292DSTOPTS: case IPV6_DSTOPTS: - case IPV6_RTHDRDSTOPTS: + case IPV6_RTHDRDSTOPTS: { + int type = IPV6_DSTOPTS; + unsigned int max_len, max_cnt; + if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { err = -EINVAL; goto exit_f; @@ -856,11 +859,26 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, err = -EINVAL; goto exit_f; } - if (!ns_capable(net->user_ns, CAP_NET_RAW)) { - err = -EPERM; - goto exit_f; + + if (cmsg->cmsg_type == IPV6_2292HOPOPTS || + cmsg->cmsg_type == IPV6_HOPOPTS) { + type = IPV6_HOPOPTS; + max_len = net->ipv6.sysctl.max_hbh_opts_len; + max_cnt = net->ipv6.sysctl.max_hbh_opts_cnt; + } else { + type = IPV6_DSTOPTS; + max_len = net->ipv6.sysctl.max_dst_opts_len; + max_cnt = net->ipv6.sysctl.max_dst_opts_cnt; } + err = ipv6_opt_validate_tlvs(net, &ipv6_tlv_param_table, + hdr, type, + ns_capable(net->user_ns, + CAP_NET_RAW), + max_len, max_cnt); + if (err < 0) + goto exit_f; + switch (cmsg->cmsg_type) { case IPV6_2292HOPOPTS: case IPV6_HOPOPTS: @@ -888,6 +906,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk, } break; + } case IPV6_2292RTHDR: case IPV6_RTHDR: diff --git a/net/ipv6/exthdrs_core.c b/net/ipv6/exthdrs_core.c index 0b56f7a..ef8e250 100644 --- a/net/ipv6/exthdrs_core.c +++ b/net/ipv6/exthdrs_core.c @@ -421,6 +421,309 @@ struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, } EXPORT_SYMBOL_GPL(ipv6_fixup_options); +/* TLV validation functions */ + +/* Validate a single non-padding TLV */ +static int __ipv6_opt_validate_single_tlv(struct net *net, unsigned char *tlv, + struct tlv_tx_params *tptx, + unsigned int class, bool *deep_check, + bool deleting, bool admin) +{ + if (tlv[0] < 2) /* Must be non-padding */ + return -EINVAL; + + /* Check permissions */ + switch (admin ? tptx->admin_perm : tptx->user_perm) { + case IPV6_TLV_PERM_NO_CHECK: + /* Allowed with no deep checks */ + *deep_check = false; + return 0; + case IPV6_TLV_PERM_WITH_CHECK: + /* Allowed with deep checks */ + *deep_check = true; + break; + default: + /* No permission */ + return -EPERM; + } + + /* Perform deep checks on the TLV */ + + /* Check class */ + if ((tptx->class & class) != class) + return -EINVAL; + + /* Don't bother checking lengths when deleting, the TLV is only + * needed here for lookup + */ + if (deleting) { + /* Don't bother with deep checks when deleting */ + *deep_check = false; + } else { + /* Check length */ + if (tlv[1] < tptx->min_data_len || tlv[1] > tptx->max_data_len) + return -EINVAL; + + /* Check length alignment */ + if ((tlv[1] % (tptx->data_len_mult + 1)) != tptx->data_len_off) + return -EINVAL; + } + + return 0; +} + +static unsigned int optname_to_tlv_class(int optname) +{ + switch (optname) { + case IPV6_HOPOPTS: + return IPV6_TLV_CLASS_FLAG_HOPOPT; + case IPV6_RTHDRDSTOPTS: + return IPV6_TLV_CLASS_FLAG_RTRDSTOPT; + case IPV6_DSTOPTS: + return IPV6_TLV_CLASS_FLAG_DSTOPT; + default: + return -1U; + } +} + +static int __ipv6_opt_validate_tlvs(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_opt_hdr *opt, + unsigned int optname, bool deleting, + bool admin, unsigned int max_len, + unsigned int max_cnt) +{ + unsigned char *tlv = (unsigned char *)opt; + bool deep_check, did_deep_check = false; + unsigned int opt_len, tlv_len, offset; + unsigned int padding = 0, numpad = 0; + unsigned char prev_tlv_order = 0; + unsigned int class, cnt = 0; + struct tlv_tx_params *tptx; + int retc, ret = -EINVAL; + struct tlv_proc *tproc; + + opt_len = ipv6_optlen(opt); + offset = sizeof(*opt); + + class = optname_to_tlv_class(optname); + + rcu_read_lock(); + + while (offset < opt_len) { + switch (tlv[offset]) { + case IPV6_TLV_PAD1: + tlv_len = 1; + padding++; + numpad++; + break; + case IPV6_TLV_PADN: + if (offset + 1 >= opt_len) + goto out; + + tlv_len = tlv[offset + 1] + 2; + + if (offset + tlv_len > opt_len) + goto out; + + padding += tlv_len; + numpad++; + break; + default: + if (offset + 1 >= opt_len) + goto out; + + tlv_len = tlv[offset + 1] + 2; + + if (offset + tlv_len > opt_len) + goto out; + + tproc = tlv_get_proc(tlv_param_table, tlv[offset]); + tptx = &tproc->params.t; + + retc = __ipv6_opt_validate_single_tlv(net, &tlv[offset], + tptx, class, + &deep_check, + deleting, admin); + if (retc < 0) { + ret = retc; + goto out; + } + + if (deep_check) { + /* Check for too many options */ + if (++cnt > max_cnt) { + ret = -E2BIG; + goto out; + } + + /* Check order */ + if (tptx->preferred_order < prev_tlv_order) + goto out; + + /* Check alignment */ + if ((offset % (tptx->align_mult + 1)) != + tptx->align_off) + goto out; + + /* Check for right amount of padding */ + if (numpad > 1 || padding > tptx->align_mult) + goto out; + + prev_tlv_order = tptx->preferred_order; + + did_deep_check = true; + } + padding = 0; + numpad = 0; + } + offset += tlv_len; + } + + /* If we did at least one deep check apply length limit */ + if (did_deep_check && opt_len > max_len) { + ret = -EMSGSIZE; + goto out; + } + + /* All good */ + ret = 0; +out: + rcu_read_unlock(); + + return ret; +} + +/** + * ipv6_opt_validate_tlvs - Validate TLVs. + * @net: Current net + * @tlv_param_table: TLV parameter table + * @opt: The option header + * @optname: IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, or IPV6_DSTOPTS + * @admin: Set for privileged user + * @max_len: Maximum length for TLV + * @max_cnt: Maximum number of non-padding TLVs + * + * Description: + * Walks the TLVs in a list to verify that the TLV lengths and other + * parameters are in bounds for a Destination or Hop-by-Hop option. + * Return -EINVAL is there is a problem, zero otherwise. + */ +int ipv6_opt_validate_tlvs(struct net *net, + struct tlv_param_table *tlv_param_table, + struct ipv6_opt_hdr *opt, unsigned int optname, + bool admin, unsigned int max_len, + unsigned int max_cnt) +{ + return __ipv6_opt_validate_tlvs(net, tlv_param_table, opt, optname, + false, admin, max_len, max_cnt); +} +EXPORT_SYMBOL(ipv6_opt_validate_tlvs); + +/** + * ipv6_opt_validate_single_tlv - Check that a single TLV is valid. + * @net: Current net + * @tlv_param_table: TLV parameter table + * @optname: IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, or IPV6_DSTOPTS + * @tlv: The TLV as array of bytes + * @len: Length of buffer holding TLV + * @deleting: TLV is being deleted + * @admin: Set for privileged user + * + * Description: + * Validates a single TLV. The TLV must be non-padding type. The length + * of the TLV (as determined by the second byte that gives length of the + * option data) must match @len. + */ +int ipv6_opt_validate_single_tlv(struct net *net, + struct tlv_param_table *tlv_param_table, + unsigned int optname, + unsigned char *tlv, size_t len, + bool deleting, bool admin) +{ + struct tlv_tx_params *tptx; + struct tlv_proc *tproc; + unsigned int class; + bool deep_check; + int ret = 0; + + class = optname_to_tlv_class(optname); + + if (tlv[0] < 2) + return -EINVAL; + + if (len < 2) + return -EINVAL; + + if (tlv[1] + 2 != len) + return -EINVAL; + + rcu_read_lock(); + + tproc = tlv_get_proc(tlv_param_table, tlv[0]); + tptx = &tproc->params.t; + + ret = __ipv6_opt_validate_single_tlv(net, tlv, tptx, class, + &deep_check, deleting, admin); + + rcu_read_unlock(); + + return ret; +} +EXPORT_SYMBOL(ipv6_opt_validate_single_tlv); + +/** + * ipv6_opt_check_perm - Check that current capabilities allows modifying + * txopts. + * @net: Current net + * @tlv_param_table: TLV parameter table + * @sk: the socket + * @optname: IPV6_HOPOPTS, IPV6_RTHDRDSTOPTS, or IPV6_DSTOPTS + * @admin: Set for privileged user + * + * Description: + * + * Checks whether the permissions of TLV that are set on a socket permit + * modificationr. + * + */ +int ipv6_opt_check_perm(struct net *net, + struct tlv_param_table *tlv_param_table, + struct sock *sk, int optname, bool admin) +{ + struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)); + struct ipv6_opt_hdr *opt; + int retv = -EPERM; + + if (!old) + return 0; + + switch (optname) { + case IPV6_HOPOPTS: + opt = old->hopopt; + break; + case IPV6_RTHDRDSTOPTS: + opt = old->dst0opt; + break; + case IPV6_DSTOPTS: + opt = old->dst1opt; + break; + default: + goto out; + } + + /* Just call the validate function on the options as being + * deleted. + */ + retv = __ipv6_opt_validate_tlvs(net, tlv_param_table, opt, optname, + true, admin, -1U, -1U); + +out: + txopt_put(old); + return retv; +} +EXPORT_SYMBOL(ipv6_opt_check_perm); + /* TLV parameter table functions and structures */ static void tlv_param_table_release(struct rcu_head *rcu) @@ -659,6 +962,76 @@ int tlv_unset_params(struct tlv_param_table *tlv_param_table, } EXPORT_SYMBOL(tlv_unset_params); +/* Utility function tp create TX options from a setsockopt that is setting + * options on a socket. + */ +struct ipv6_txoptions *txoptions_from_opt(struct sock *sk, + struct tlv_param_table + *tlv_param_table, + struct ipv6_txoptions *opt, + int optname, char __user *optval, + unsigned int optlen, + unsigned int max_len, + unsigned int max_cnt) +{ + struct ipv6_opt_hdr *new = NULL; + struct net *net = sock_net(sk); + int retv; + + /* remove any sticky options header with a zero option + * length, per RFC3542. + */ + if (optlen == 0) { + optval = NULL; + } else if (!optval) { + return ERR_PTR(-EINVAL); + } else if (optlen < sizeof(struct ipv6_opt_hdr) || + optlen & 0x7 || optlen > 8 * 255) { + return ERR_PTR(-EINVAL); + } else { + new = memdup_user(optval, optlen); + if (IS_ERR(new)) + return (struct ipv6_txoptions *)new; + if (unlikely(ipv6_optlen(new) > optlen)) { + kfree(new); + return ERR_PTR(-EINVAL); + } + } + + if (optname != IPV6_RTHDR) { + bool cap = ns_capable(net->user_ns, CAP_NET_RAW); + + /* First check if we have permission to delete + * the existing options on the socket. + */ + retv = ipv6_opt_check_perm(net, tlv_param_table, + sk, optname, cap); + if (retv < 0) { + kfree(new); + return ERR_PTR(retv); + } + + /* Check permissions and other validations on new + * TLVs + */ + if (new) { + retv = ipv6_opt_validate_tlvs(net, tlv_param_table, + new, optname, cap, + max_len, max_cnt); + if (retv < 0) { + kfree(new); + return ERR_PTR(retv); + } + } + } + + opt = ipv6_renew_options(sk, opt, optname, new); + kfree(new); + + return opt; +} +EXPORT_SYMBOL(txoptions_from_opt); + const struct nla_policy tlv_nl_policy[IPV6_TLV_ATTR_MAX + 1] = { [IPV6_TLV_ATTR_TYPE] = { .type = NLA_U8, }, [IPV6_TLV_ATTR_ORDER] = { .type = NLA_U8, }, diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index 40f21fe..14c4051 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -398,39 +398,26 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, case IPV6_DSTOPTS: { struct ipv6_txoptions *opt; - struct ipv6_opt_hdr *new = NULL; + unsigned int max_len = -1U, max_cnt = -1U; - /* hop-by-hop / destination options are privileged option */ - retv = -EPERM; - if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW)) + switch (optname) { + case IPV6_HOPOPTS: + max_len = net->ipv6.sysctl.max_hbh_opts_len; + max_cnt = net->ipv6.sysctl.max_hbh_opts_cnt; break; - - /* remove any sticky options header with a zero option - * length, per RFC3542. - */ - if (optlen == 0) - optval = NULL; - else if (!optval) - goto e_inval; - else if (optlen < sizeof(struct ipv6_opt_hdr) || - optlen & 0x7 || optlen > 8 * 255) - goto e_inval; - else { - new = memdup_user(optval, optlen); - if (IS_ERR(new)) { - retv = PTR_ERR(new); + case IPV6_RTHDRDSTOPTS: + case IPV6_DSTOPTS: + max_len = net->ipv6.sysctl.max_dst_opts_len; + max_cnt = net->ipv6.sysctl.max_dst_opts_cnt; break; - } - if (unlikely(ipv6_optlen(new) > optlen)) { - kfree(new); - goto e_inval; - } } - opt = rcu_dereference_protected(np->opt, - lockdep_sock_is_held(sk)); - opt = ipv6_renew_options(sk, opt, optname, new); - kfree(new); + opt = txoptions_from_opt(sk, &ipv6_tlv_param_table, + rcu_dereference_protected(np->opt, + lockdep_sock_is_held(sk)), + optname, optval, optlen, + max_len, max_cnt); + if (IS_ERR(opt)) { retv = PTR_ERR(opt); break;