Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7184123yba; Thu, 2 May 2019 05:51:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqzq9j6/gm/szGz71Pcq3gEF+y8k86TXmjfRTqxUAMhnyMjbuFfMcz9vo34wLfzs/+bzF5qu X-Received: by 2002:aa7:8a53:: with SMTP id n19mr3910265pfa.11.1556801492558; Thu, 02 May 2019 05:51:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556801492; cv=none; d=google.com; s=arc-20160816; b=YaX5kLaQzVae65Am50Qzb4jENA++f+79maTf1YcSs1U8L1uV7u1HTJKs6sL7BoUstC iBM3fiK+rPDwfnxfqAw1/xuP+JpMr3zDh3bY2yKbkdf8F5kZ7qeewMacQ0G3bbcUGrHJ FpefgNFDwHHdYbiNOdC5I1b5Y+19GMJU1BSMB5TRzPhccSvH5wj/JohZcUUiYpSteMvb PyRAptGuxUU1j1//eWMZSUvn95evjpsUtF5gs7PlBUmaJYl01S0nDIgFYy+CmHOOc0kW EN5Y+DOR/NLwpZSgrLwhpsDZAO2f1KzLx4EDws0DuJZEdCawGBRXV/YKUVMsZf2JOh/W 3cHw== 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=+2KDDrJicMoKfQJ81Pocbwwb/lVqbYGFyd3fyrK55Lc=; b=1HXtHGkmizEFjjoAzvEnsWzWowSuvOkRqPeyiUxpmB0xfM+LNRJab78KAQ/xyI33s4 3DiXWagQ+pntheYD+UL67wrJuDeER0I4yG3UxWhfnto2x2pg7U7sNG6LYq5lhYhVQZnU 4F3ZQQCCdn38b0ADC9H9k2fItb+gwnIBKPuCA4J9lo4Xf829ALgs66Vq8dBsR5QyslZE aefaUwm9dtc8fmjNJ+KkCTzse7HJNE/qdm89eqNWnUK/L6fCvdkOSavMXZQbpYg963r1 0q3vyZ1n69KqQ+YHetMmkt54L3rO0DuqPP+wfw4x+xG2hvizwTTJSbdJoSQTexJV3aEJ jh5w== 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 ci9si25364047plb.408.2019.05.02.05.51.16; Thu, 02 May 2019 05:51:32 -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 S1726458AbfEBMsC (ORCPT + 99 others); Thu, 2 May 2019 08:48:02 -0400 Received: from mx2.suse.de ([195.135.220.15]:33934 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726267AbfEBMsC (ORCPT ); Thu, 2 May 2019 08:48:02 -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 0565CADEA; Thu, 2 May 2019 12:48:00 +0000 (UTC) Received: by unicorn.suse.cz (Postfix, from userid 1000) id 662ABE0157; Thu, 2 May 2019 14:48:00 +0200 (CEST) Message-Id: <75a0887b3eb70005c272685d8ef9a712f37d7a54.1556798793.git.mkubecek@suse.cz> In-Reply-To: References: From: Michal Kubecek Subject: [PATCH net-next 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 14:48:00 +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 --- include/net/netlink.h | 10 +++++++++- lib/nlattr.c | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/net/netlink.h b/include/net/netlink.h index 679f649748d4..55f68e00fb6e 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,10 @@ 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, "nested attribute expected"); + 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..92da65cb6637 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, + "nested attribute expected"); + 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, + "nested attribute not expected"); + return -EINVAL; + } + } + switch (pt->type) { case NLA_EXACT_LEN: if (attrlen != pt->len) -- 2.21.0