Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7278695yba; Thu, 2 May 2019 07:19:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZMqsx+QOWdGcx1nULmOGCNqUliA8tNF3Iap7ZpGtOeB2lMkFhot5X01ubwaXobTviwNJS X-Received: by 2002:a17:902:7689:: with SMTP id m9mr4026908pll.274.1556806749074; Thu, 02 May 2019 07:19:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556806749; cv=none; d=google.com; s=arc-20160816; b=tnhddU5+j1n1Dd6YX0kDs1CaCj7IklD+zCb5gUuK1Jd1Ky8+XijsFWNM3VynllHMHX YiIZhmWV0XWp2cTRwnAoxWoOHflvFkfdMyggh7qwM6MSTrc5DzN8NjyTYIkA7kNEXWZg sgv0dhtLqn01TwgdCKPvhlpgVdXPkcQmCjymkqCexQx0SSLXwyE1AUOyQP04CBlxolLu 1xGf8IuTDYzzFLfB67FFaiKH94oFpE4Ewq7XIzlg6eN14cp0zZOxUsfZXRwK5MvhJdCK 25T0wZ8Jk+IaV7Ia54YcaO9vl37BibOMwarF+LCS+r52Qr073q6xUeavq6jjBZ0I7P6f 9t0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id; bh=Pmoy5F3BeBGpJ6Y8dgqzwM8KKfiojb1+htbxGgrwAFs=; b=uFAGuOzNOVO9ts9a9Hjz01Dv84CDUwkcepVywspk5txUKkVaUkPyklx5elFHv4nRUu ay1qVZ/vQFFjdOhC6kw9nKjHcBxQRAR1wGMDBIsO41QZkFVe0hpAjS9Uk56q4uEwI7Zk fP2Fpkkv9BNW6H/d/WoCxijQvEDimVQeNIqI5gOfT8S9ZUACau3zZs2zl2aRVfPXGLg1 N6ly9JJSNw79cTABXQfPh+tNjBWKgdIa/FIQmjirzJE+dKahaiJrC5hMk4Lr/2LtvHI8 sBsMo+5S+ml7YWtnYt1EU6fRgenUvuLhTAfGwKVmJS7jMWJ4jKdS+kbYMksymjq8B0CE wUAw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 14si43835283ple.218.2019.05.02.07.18.53; Thu, 02 May 2019 07:19:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726524AbfEBOPk (ORCPT + 99 others); Thu, 2 May 2019 10:15:40 -0400 Received: from mx2.suse.de ([195.135.220.15]:51398 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726320AbfEBOPM (ORCPT ); Thu, 2 May 2019 10:15:12 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 785D9AD7E; Thu, 2 May 2019 14:15:11 +0000 (UTC) Received: by unicorn.suse.cz (Postfix, from userid 1000) id D29B5E0157; Thu, 2 May 2019 16:15:10 +0200 (CEST) Message-Id: <6b6ead21c5d8436470b82ab40355f6bd7dbbf14b.1556806084.git.mkubecek@suse.cz> In-Reply-To: References: From: Michal Kubecek Subject: [PATCH net-next v2 3/3] netlink: add validation of NLA_F_NESTED flag To: "David S. Miller" Cc: netdev@vger.kernel.org, Johannes Berg , David Ahern , linux-kernel@vger.kernel.org Date: Thu, 2 May 2019 16:15:10 +0200 (CEST) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add new validation flag NL_VALIDATE_NESTED which adds three consistency checks of NLA_F_NESTED_FLAG: - the flag is set on attributes with NLA_NESTED{,_ARRAY} policy - the flag is not set on attributes with other policies except NLA_UNSPEC - the flag is set on attribute passed to nla_parse_nested() Signed-off-by: Michal Kubecek v2: change error messages to mention NLA_F_NESTED explicitly --- include/net/netlink.h | 11 ++++++++++- lib/nlattr.c | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/net/netlink.h b/include/net/netlink.h index 679f649748d4..395b4406f4b0 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -401,6 +401,8 @@ struct nl_info { * are enforced going forward. * @NL_VALIDATE_STRICT_ATTRS: strict attribute policy parsing (e.g. * U8, U16, U32 must have exact size, etc.) + * @NL_VALIDATE_NESTED: Check that NLA_F_NESTED is set for NLA_NESTED(_ARRAY) + * and unset for other policies. */ enum netlink_validation { NL_VALIDATE_LIBERAL = 0, @@ -408,6 +410,7 @@ enum netlink_validation { NL_VALIDATE_MAXTYPE = BIT(1), NL_VALIDATE_UNSPEC = BIT(2), NL_VALIDATE_STRICT_ATTRS = BIT(3), + NL_VALIDATE_NESTED = BIT(4), }; #define NL_VALIDATE_DEPRECATED_STRICT (NL_VALIDATE_TRAILING |\ @@ -415,7 +418,8 @@ enum netlink_validation { #define NL_VALIDATE_STRICT (NL_VALIDATE_TRAILING |\ NL_VALIDATE_MAXTYPE |\ NL_VALIDATE_UNSPEC |\ - NL_VALIDATE_STRICT_ATTRS) + NL_VALIDATE_STRICT_ATTRS |\ + NL_VALIDATE_NESTED) int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, struct nlmsghdr *, @@ -1132,6 +1136,11 @@ static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, const struct nla_policy *policy, struct netlink_ext_ack *extack) { + if (!(nla->nla_type & NLA_F_NESTED)) { + NL_SET_ERR_MSG_ATTR(extack, nla, "NLA_F_NESTED is missing"); + return -EINVAL; + } + return __nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy, NL_VALIDATE_STRICT, extack); } diff --git a/lib/nlattr.c b/lib/nlattr.c index adc919b32bf9..cace9b307781 100644 --- a/lib/nlattr.c +++ b/lib/nlattr.c @@ -184,6 +184,21 @@ static int validate_nla(const struct nlattr *nla, int maxtype, } } + if (validate & NL_VALIDATE_NESTED) { + if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) && + !(nla->nla_type & NLA_F_NESTED)) { + NL_SET_ERR_MSG_ATTR(extack, nla, + "NLA_F_NESTED is missing"); + return -EINVAL; + } + if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY && + pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) { + NL_SET_ERR_MSG_ATTR(extack, nla, + "NLA_F_NESTED not expected"); + return -EINVAL; + } + } + switch (pt->type) { case NLA_EXACT_LEN: if (attrlen != pt->len) -- 2.21.0