xshared: Fix response to unprivileged users
Subsystems (2)
Phil Sutter
reviewerPATCH 1/1 xshared: Fix response to unprivileged users
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything else
With iptables-nft, this was broken basically from day 1. Shared use of do_parse() then somewhat broke legacy: it started complaining about inability to create a lock file.
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
--- iptables/nft.c | 5 ++ .../testcases/iptables/0008-unprivileged_0 | 60 +++++++++++++++++++ iptables/xshared.c | 3 +- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100755 iptables/tests/shell/testcases/iptables/0008-unprivileged_0
iptables/nft.c +5 -0
@@ -3312,6 +3312,11 @@ int nft_compatible_revision(const char *name, uint8_t rev, int opt) err:mnl_socket_close (nl); +/* pretend revision 0 is valid if not permitted to check - +* this is required for printing extension help texts as user */ +if (ret <0 && errno ==EPERM && rev ==0 ) +return 1 ; +return ret <0 ?0 :1 ; }
iptables/tests/shell/testcases/iptables/0008-unprivileged_0 new file +60 -0
60 lines not on this page · raw message
iptables/xshared.c +1 -2
@@ -1473,8 +1473,7 @@ void do_parse(int argc, char *argv[], XTF_TRY_LOAD, &cs->matches); xt_params->print_help (cs->matches); - p->command =CMD_NONE ; -return ; +exit (0 );/* * Option selection
Signature · 1 line
2.34.1
Quoted 10 lines · Phil Sutter
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything elseWith iptables-nft, this was broken basically from day 1. Shared use of
do_parse()then somewhat broke legacy: it started complaining about inability to create a lock file.
Quoted 5 lines
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
Thanks!
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 10 lines · Phil Sutter
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything elseWith iptables-nft, this was broken basically from day 1. Shared use of
do_parse()then somewhat broke legacy: it started complaining about inability to create a lock file.
Quoted 5 lines
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
On top of this patch, it should be possible to allow for some nfnetlink command to be used from unpriviledged process.
I'm attaching a sketch patch, it skips module autoload which is should not be triggered by an unpriviledged process.
This should allow for better help with -m/-j if the module is present.
include/linux/netfilter/nfnetlink.h +1 -0
@@ -28,6 +28,7 @@ struct nfnl_callback {const struct nla_policy *policy;enum nfnl_callback_type type;__u16 attr_count; +bool unpriviledged; };enum nfnl_abort_action {
net/netfilter/nfnetlink.c +15 -7
@@ -211,21 +211,24 @@ EXPORT_SYMBOL_GPL(nfnetlink_broadcast);static int nfnetlink_rcv_msg (struct sk_buff *skb,struct nlmsghdr *nlh,struct netlink_ext_ack *extack) { +const struct nfnetlink_subsystem *ss;struct net *net =sock_net (skb->sk);const struct nfnl_callback *nc; -const struct nfnetlink_subsystem *ss; +bool cap_net_admin;int type, err;/* All the messages must at least contain nfgenmsg */ if (nlmsg_len (nlh) <sizeof (struct nfgenmsg ))return 0 ; + cap_net_admin =netlink_net_capable (skb,CAP_NET_ADMIN ); + type = nlh->nlmsg_type; replay:rcu_read_lock (); ss =nfnetlink_get_subsys (type); -if (!ss) { +if (!ss && cap_net_admin) {#ifdef CONFIG_MODULES rcu_read_unlock ();request_module ("nfnetlink-subsys-%d" ,NFNL_SUBSYS_ID (type));
@@ -245,6 +248,11 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,return -EINVAL ; } +if (cap_net_admin && !nc->unpriviledged) { +rcu_read_unlock (); +return -EPERM ; + } + {int min_len =nlmsg_total_size (sizeof (struct nfgenmsg ));struct nfnl_net *nfnlnet =nfnl_pernet (net);
@@ -607,6 +615,11 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)u32 gen_id =0 ;u16 res_id; +if (!netlink_net_capable (skb,CAP_NET_ADMIN )) { +netlink_ack (skb, nlh, -EPERM ,NULL ); +return ; + } + msglen =NLMSG_ALIGN (nlh->nlmsg_len);if (msglen > skb->len) msglen = skb->len;
@@ -643,11 +656,6 @@ static void nfnetlink_rcv(struct sk_buff *skb) skb->len < nlh->nlmsg_len)return ; -if (!netlink_net_capable (skb,CAP_NET_ADMIN )) { -netlink_ack (skb, nlh, -EPERM ,NULL ); -return ; - } -if (nlh->nlmsg_type ==NFNL_MSG_BATCH_BEGIN )nfnetlink_rcv_skb_batch (skb, nlh);else
net/netfilter/nft_compat.c +9 -5
@@ -677,10 +677,13 @@ static int nfnl_compat_get_rcu(struct sk_buff *skb,return -EINVAL ;rcu_read_unlock (); -try_then_request_module (xt_find_revision (family, name, rev, target, &ret), - fmt, name); -if (ret <0 ) - goto out_put; +if (netlink_net_capable (skb,CAP_NET_ADMIN )) { +try_then_request_module (xt_find_revision (family, name, rev, + target, &ret), + fmt, name); +if (ret <0 ) + goto out_put; + } skb2 =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );if (skb2 ==NULL ) {
@@ -718,7 +721,8 @@ static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = { .call = nfnl_compat_get_rcu, .type = NFNL_CB_RCU, .attr_count = NFTA_COMPAT_MAX, - .policy = nfnl_compat_policy_get + .policy = nfnl_compat_policy_get, + .unpriviledged = true, }, };
Phil Sutter
reviewerHi Pablo,
Quoted 17 lines · Pablo Neira Ayuso
Quoted 10 lines · Phil Sutter
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything elseWith iptables-nft, this was broken basically from day 1. Shared use of
do_parse()then somewhat broke legacy: it started complaining about inability to create a lock file.Quoted 5 lines
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
Quoted 7 lines
On top of this patch, it should be possible to allow for some nfnetlink command to be used from unpriviledged process.
I'm attaching a sketch patch, it skips module autoload which is should not be triggered by an unpriviledged process.
This should allow for better help with -m/-j if the module is present.
That's interesting. What's the use-case? With my patch, extension help text printing works fine as unprivileged user. Does it allow to drop the "revision == 0 && EPERM" hack?
Thanks, Phil
Pablo Neira Ayuso
maintainerQuoted 23 lines · Phil Sutter
Hi Pablo,
Quoted 20 lines · Pablo Neira Ayuso
Quoted 10 lines · Phil Sutter
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything elseWith iptables-nft, this was broken basically from day 1. Shared use of
do_parse()then somewhat broke legacy: it started complaining about inability to create a lock file.Quoted 5 lines
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
On top of this patch, it should be possible to allow for some nfnetlink command to be used from unpriviledged process.
Quoted 8 lines
Quoted 4 lines
I'm attaching a sketch patch, it skips module autoload which is should not be triggered by an unpriviledged process.
This should allow for better help with -m/-j if the module is present.
That's interesting. What's the use-case? With my patch, extension help text printing works fine as unprivileged user. Does it allow to drop the "revision == 0 && EPERM" hack?
Your patch is needed because we have to deal with older kernels.
You assume revision 0 in case of EPERM. My patch provides better help if the module is present since there is no need to assume revision 0.
Anyway, I think your approach is fine for the unpriviledged scenario you describe. I just wanted to write here that there is room to extend nfnetlink to support for unpriviledged requests.
Phil Sutter
reviewerQuoted 29 lines · Pablo Neira Ayuso
Quoted 28 lines · Phil Sutter
Hi Pablo,
Quoted 24 lines · Pablo Neira Ayuso
Quoted 10 lines · Phil Sutter
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything elseWith iptables-nft, this was broken basically from day 1. Shared use of
do_parse()then somewhat broke legacy: it started complaining about inability to create a lock file.Quoted 5 lines
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
On top of this patch, it should be possible to allow for some nfnetlink command to be used from unpriviledged process.
I'm attaching a sketch patch, it skips module autoload which is should not be triggered by an unpriviledged process.
This should allow for better help with -m/-j if the module is present.
Quoted 8 lines
Quoted 3 lines
That's interesting. What's the use-case? With my patch, extension help text printing works fine as unprivileged user. Does it allow to drop the "revision == 0 && EPERM" hack?
Your patch is needed because we have to deal with older kernels.
You assume revision 0 in case of EPERM. My patch provides better help if the module is present since there is no need to assume revision 0.
Ah, that's a good point. Users always see rev0 help texts, which are naturally the most limited ones.
Quoted 3 lines
Anyway, I think your approach is fine for the unpriviledged scenario you describe. I just wanted to write here that there is room to extend nfnetlink to support for unpriviledged requests.
I see, thanks. Yet your approach works only if the module is loaded already, right?
Unless it's useful elsewhere as well, I don't think it's worth the effort for iptables alone - requesting extension help as non-root is quite a corner-case IMHO.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 42 lines · Phil Sutter
Quoted 29 lines · Pablo Neira Ayuso
Quoted 28 lines · Phil Sutter
Hi Pablo,
Quoted 24 lines · Pablo Neira Ayuso
Quoted 10 lines · Phil Sutter
Expected behaviour in both variants is:
* Print help without error, append extension help if -m and/or -j options are present
* Indicate lack of permissions in an error message for anything elseWith iptables-nft, this was broken basically from day 1. Shared use of
do_parse()then somewhat broke legacy: it started complaining about inability to create a lock file.Quoted 5 lines
Fix this by making iptables-nft assume extension revision 0 is present if permissions don't allow to verify. This is consistent with legacy.
Second part is to exit directly after printing help - this avoids having to make the following code "nop-aware" to prevent privileged actions.
On top of this patch, it should be possible to allow for some nfnetlink command to be used from unpriviledged process.
I'm attaching a sketch patch, it skips module autoload which is should not be triggered by an unpriviledged process.
This should allow for better help with -m/-j if the module is present.
Quoted 8 lines
Quoted 3 lines
That's interesting. What's the use-case? With my patch, extension help text printing works fine as unprivileged user. Does it allow to drop the "revision == 0 && EPERM" hack?
Your patch is needed because we have to deal with older kernels.
You assume revision 0 in case of EPERM. My patch provides better help if the module is present since there is no need to assume revision 0.
Ah, that's a good point. Users always see rev0 help texts, which are naturally the most limited ones.
Quoted 6 lines
Quoted 3 lines
Anyway, I think your approach is fine for the unpriviledged scenario you describe. I just wanted to write here that there is room to extend nfnetlink to support for unpriviledged requests.
I see, thanks. Yet your approach works only if the module is loaded already, right?
Yes, I don't think we should allow to autoload modules for non-CAP_NET_ADMIN.
Quoted 3 lines
Unless it's useful elsewhere as well, I don't think it's worth the effort for iptables alone - requesting extension help as non-root is quite a corner-case IMHO.
Agreed.