From patchwork Wed Sep 19 12:08:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 10605751 X-Patchwork-Delegate: johannes@sipsolutions.net Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 14615161F for ; Wed, 19 Sep 2018 12:09:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0BBBA2B610 for ; Wed, 19 Sep 2018 12:09:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0005C2B62F; Wed, 19 Sep 2018 12:09:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78CFB2B610 for ; Wed, 19 Sep 2018 12:09:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731504AbeISRrE (ORCPT ); Wed, 19 Sep 2018 13:47:04 -0400 Received: from s3.sipsolutions.net ([144.76.43.62]:53234 "EHLO sipsolutions.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731436AbeISRqx (ORCPT ); Wed, 19 Sep 2018 13:46:53 -0400 Received: by sipsolutions.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.91) (envelope-from ) id 1g2bIA-0007lf-81; Wed, 19 Sep 2018 14:09:14 +0200 From: Johannes Berg To: linux-wireless@vger.kernel.org, netdev@vger.kernel.org Cc: Johannes Berg Subject: [PATCH 4/7] netlink: combine validate/parse functions Date: Wed, 19 Sep 2018 14:08:57 +0200 Message-Id: <20180919120900.28708-5-johannes@sipsolutions.net> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180919120900.28708-1-johannes@sipsolutions.net> References: <20180919120900.28708-1-johannes@sipsolutions.net> Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Johannes Berg The parse function of course contains validate, but it's implemented a second time, sharing just the validation of a single attribute. Introduce nla_validate_parse() that can be used for both parsing/validation and only validation, to share code. Signed-off-by: Johannes Berg --- lib/nlattr.c | 76 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/lib/nlattr.c b/lib/nlattr.c index 33404745bec4..966cd3dcf31b 100644 --- a/lib/nlattr.c +++ b/lib/nlattr.c @@ -158,6 +158,37 @@ static int validate_nla(const struct nlattr *nla, int maxtype, return 0; } +static int nla_validate_parse(const struct nlattr *head, int len, int maxtype, + const struct nla_policy *policy, + struct netlink_ext_ack *extack, + struct nlattr **tb) +{ + const struct nlattr *nla; + int rem; + + nla_for_each_attr(nla, head, len, rem) { + static const char _msg[] = "Attribute failed policy validation"; + const char *msg = _msg; + u16 type = nla_type(nla); + + if (policy) { + int err = validate_nla(nla, maxtype, policy, &msg); + + if (err < 0) { + if (extack) + extack->_msg = msg; + NL_SET_BAD_ATTR(extack, nla); + return err; + } + } + + if (tb && type > 0 && type <= maxtype) + tb[type] = (struct nlattr *)nla; + } + + return rem; +} + /** * nla_validate - Validate a stream of attributes * @head: head of attribute stream @@ -176,21 +207,12 @@ int nla_validate(const struct nlattr *head, int len, int maxtype, const struct nla_policy *policy, struct netlink_ext_ack *extack) { - const struct nlattr *nla; int rem; - nla_for_each_attr(nla, head, len, rem) { - static const char _msg[] = "Attribute failed policy validation"; - const char *msg = _msg; - int err = validate_nla(nla, maxtype, policy, &msg); + rem = nla_validate_parse(head, len, maxtype, policy, extack, NULL); - if (err < 0) { - if (extack) - extack->_msg = msg; - NL_SET_BAD_ATTR(extack, nla); - return err; - } - } + if (rem < 0) + return rem; return 0; } @@ -244,39 +266,19 @@ int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, int len, const struct nla_policy *policy, struct netlink_ext_ack *extack) { - const struct nlattr *nla; - int rem, err; + int rem; memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); - nla_for_each_attr(nla, head, len, rem) { - u16 type = nla_type(nla); - - if (type > 0 && type <= maxtype) { - static const char _msg[] = "Attribute failed policy validation"; - const char *msg = _msg; - - if (policy) { - err = validate_nla(nla, maxtype, policy, &msg); - if (err < 0) { - NL_SET_BAD_ATTR(extack, nla); - if (extack) - extack->_msg = msg; - goto errout; - } - } - - tb[type] = (struct nlattr *)nla; - } - } + rem = nla_validate_parse(head, len, maxtype, policy, extack, tb); + if (rem < 0) + return rem; if (unlikely(rem > 0)) pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", rem, current->comm); - err = 0; -errout: - return err; + return 0; } EXPORT_SYMBOL(nla_parse);