From: Michal Kubecek <hidden> Date: 2019-05-02 12:48:06
Three follow-up patches for recent strict netlink validation series.
Patch 1 fixes dump handling for genetlink families which validate and parse
messages themselves (e.g. because they need different policies for diferent
commands).
Patch 2 sets bad_attr in extack in one place where this was omitted.
Patch 3 adds new NL_VALIDATE_NESTED flags for strict validation to enable
checking that NLA_F_NESTED value in received messages matches expectations
and includes this flag in NL_VALIDATE_STRICT. This would change userspace
visible behavior but the previous switching to NL_VALIDATE_STRICT for new
code is still only in net-next at the moment.
Michal Kubecek (3):
genetlink: do not validate dump requests if there is no policy
netlink: set bad attribute also on maxtype check
netlink: add validation of NLA_F_NESTED flag
include/net/netlink.h | 10 +++++++++-
lib/nlattr.c | 18 +++++++++++++++++-
net/netlink/genetlink.c | 24 ++++++++++++++----------
3 files changed, 40 insertions(+), 12 deletions(-)
--
2.21.0
From: Michal Kubecek <hidden> Date: 2019-05-02 12:48:04
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 <redacted>
---
include/net/netlink.h | 10 +++++++++-
lib/nlattr.c | 15 +++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
From: Michal Kubecek <hidden> Date: 2019-05-02 12:48:07
Unlike do requests, dump genetlink requests now perform strict validation
by default even if the genetlink family does not set policy and maxtype
because it does validation and parsing on its own (e.g. because it wants to
allow different message format for different commands). While the null
policy will be ignored, maxtype (which would be zero) is still checked so
that any attribute will fail validation.
The solution is to only call __nla_validate() from genl_family_rcv_msg()
if family->maxtype is set.
Fixes: ef6243acb478 ("genetlink: optionally validate strictly/dumps")
Signed-off-by: Michal Kubecek <redacted>
---
net/netlink/genetlink.c | 24 ++++++++++++++----------
1 file changed, 14 insertions(+), 10 deletions(-)
From: Michal Kubecek <hidden> Date: 2019-05-02 12:48:09
The check that attribute type is within 0...maxtype range in
__nla_validate_parse() sets only error message but not bad_attr in extack.
Set also bad_attr to tell userspace which attribute failed validation.
Signed-off-by: Michal Kubecek <redacted>
---
lib/nlattr.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
From: Johannes Berg <johannes@sipsolutions.net> Date: 2019-05-02 12:51:41
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
Unlike do requests, dump genetlink requests now perform strict validation
by default even if the genetlink family does not set policy and maxtype
because it does validation and parsing on its own (e.g. because it wants to
allow different message format for different commands). While the null
policy will be ignored, maxtype (which would be zero) is still checked so
that any attribute will fail validation.
The solution is to only call __nla_validate() from genl_family_rcv_msg()
if family->maxtype is set.
D'oh. Which family was it that you found this on? I checked only ones
with policy I guess.
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
johannes
From: Johannes Berg <johannes@sipsolutions.net> Date: 2019-05-02 12:52:16
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
The check that attribute type is within 0...maxtype range in
__nla_validate_parse() sets only error message but not bad_attr in extack.
Set also bad_attr to tell userspace which attribute failed validation.
Good catch, we actually do have an attribute in this case.
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
johannes
From: Johannes Berg <johannes@sipsolutions.net> Date: 2019-05-02 12:55:04
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
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()
This is fine _right now_, but in general we cannot keep adding here
after the next release :-)
quoted hunk
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");
Maybe reword that to say "NLA_F_NESTED is missing" or so? The "nested
attribute expected" could result in a lot of headscratching (without
looking at the code) because it looks nested if you do nla_nest_start()
etc.
@@ -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;
Same comment here wrt. the messages, I think they should more explicitly
refer to the flag.
johannes
(PS: if you CC me on this address I generally can respond quicker)
From: Michal Kubecek <hidden> Date: 2019-05-02 13:10:27
On Thu, May 02, 2019 at 02:51:33PM +0200, Johannes Berg wrote:
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
quoted
Unlike do requests, dump genetlink requests now perform strict validation
by default even if the genetlink family does not set policy and maxtype
because it does validation and parsing on its own (e.g. because it wants to
allow different message format for different commands). While the null
policy will be ignored, maxtype (which would be zero) is still checked so
that any attribute will fail validation.
The solution is to only call __nla_validate() from genl_family_rcv_msg()
if family->maxtype is set.
D'oh. Which family was it that you found this on? I checked only ones
with policy I guess.
It was with my ethtool netlink series (still work in progress).
Michal
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
johannes
From: Johannes Berg <johannes@sipsolutions.net> Date: 2019-05-02 13:13:11
On Thu, 2019-05-02 at 15:10 +0200, Michal Kubecek wrote:
On Thu, May 02, 2019 at 02:51:33PM +0200, Johannes Berg wrote:
quoted
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
quoted
Unlike do requests, dump genetlink requests now perform strict validation
by default even if the genetlink family does not set policy and maxtype
because it does validation and parsing on its own (e.g. because it wants to
allow different message format for different commands). While the null
policy will be ignored, maxtype (which would be zero) is still checked so
that any attribute will fail validation.
The solution is to only call __nla_validate() from genl_family_rcv_msg()
if family->maxtype is set.
D'oh. Which family was it that you found this on? I checked only ones
with policy I guess.
It was with my ethtool netlink series (still work in progress).
Then you should probably *have* a policy to get all the other goodies
like automatic policy export (once I repost those patches)
johannes
From: Michal Kubecek <hidden> Date: 2019-05-02 13:14:20
On Thu, May 02, 2019 at 02:54:56PM +0200, Johannes Berg wrote:
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
quoted
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()
This is fine _right now_, but in general we cannot keep adding here
after the next release :-)
Right, that's why I would like to get this into the same cycle as your
series.
quoted
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");
Maybe reword that to say "NLA_F_NESTED is missing" or so? The "nested
attribute expected" could result in a lot of headscratching (without
looking at the code) because it looks nested if you do nla_nest_start()
etc.
How about "NLA_F_NESTED is missing" and "NLA_F_NESTED not expected"?
@@ -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;
Same comment here wrt. the messages, I think they should more explicitly
refer to the flag.
johannes
(PS: if you CC me on this address I generally can respond quicker)
From: Michal Kubecek <hidden> Date: 2019-05-02 13:32:34
On Thu, May 02, 2019 at 03:13:00PM +0200, Johannes Berg wrote:
On Thu, 2019-05-02 at 15:10 +0200, Michal Kubecek wrote:
quoted
On Thu, May 02, 2019 at 02:51:33PM +0200, Johannes Berg wrote:
quoted
On Thu, 2019-05-02 at 12:48 +0000, Michal Kubecek wrote:
quoted
Unlike do requests, dump genetlink requests now perform strict validation
by default even if the genetlink family does not set policy and maxtype
because it does validation and parsing on its own (e.g. because it wants to
allow different message format for different commands). While the null
policy will be ignored, maxtype (which would be zero) is still checked so
that any attribute will fail validation.
The solution is to only call __nla_validate() from genl_family_rcv_msg()
if family->maxtype is set.
D'oh. Which family was it that you found this on? I checked only ones
with policy I guess.
It was with my ethtool netlink series (still work in progress).
Then you should probably *have* a policy to get all the other goodies
like automatic policy export (once I repost those patches)
Wouldn't it mean effecitvely ending up with only one command (in
genetlink sense) and having to distinguish actual commands with
atributes? Even if I wanted to have just "get" and "set" command, common
policy wouldn't allow me to say which attributes are allowed for each of
them.
Michal
From: David Ahern <hidden> Date: 2019-05-02 13:36:42
On 5/2/19 7:32 AM, Michal Kubecek wrote:
Wouldn't it mean effecitvely ending up with only one command (in
genetlink sense) and having to distinguish actual commands with
atributes? Even if I wanted to have just "get" and "set" command, common
policy wouldn't allow me to say which attributes are allowed for each of
them.
yes, I have been stuck on that as well.
There are a number of RTA attributes that are only valid for GET
requests or only used in the response or only valid in NEW requests.
Right now there is no discriminator when validating policies and the
patch set to expose the policies to userspace
From: David Ahern <hidden> Date: 2019-05-02 13:37:31
On 5/2/19 6:48 AM, Michal Kubecek wrote:
The check that attribute type is within 0...maxtype range in
__nla_validate_parse() sets only error message but not bad_attr in extack.
Set also bad_attr to tell userspace which attribute failed validation.
Signed-off-by: Michal Kubecek <redacted>
---
lib/nlattr.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
From: David Ahern <hidden> Date: 2019-05-02 13:40:30
On 5/2/19 7:14 AM, Michal Kubecek wrote:
quoted
quoted
@@ -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");
Maybe reword that to say "NLA_F_NESTED is missing" or so? The "nested
attribute expected" could result in a lot of headscratching (without
looking at the code) because it looks nested if you do nla_nest_start()
etc.
How about "NLA_F_NESTED is missing" and "NLA_F_NESTED not expected"?