netfilter: nfnetlink_log: fix config error handling, SIT headroom OOB read, and nlmsg rollback
Subsystems (3)
PATCH 1/1 netfilter: nfnetlink_log: fix config error handling, SIT headroom OOB read, and nlmsg rollback
Fix three issues in net/netfilter/nfnetlink_log.c:
1. In __build_packet_message(), subtracting ETH_HLEN from skb_mac_header() for ARPHRD_SIT tunnels can read before skb->head or past skb_tail_pointer(skb), and nla_put_failure returns -1 without calling nlmsg_cancel(inst->skb, nlh). Remove the unsafe ETH_HLEN subtraction, verify hwhdrp + hard_header_len <= skb_tail_pointer(skb), and cancel the partial nlmsg on error.
2. In nfulnl_log_packet(), only increment inst->qlen after __build_packet_message() succeeds.
3. In nfulnl_recv_config(), propagate errors from nfulnl_set_mode() and nfulnl_set_nlbufsiz() and destroy the newly created instance if NFULNL_CFG_CMD_BIND was processed in the same message.
---
net/netfilter/nfnetlink_log.c +19 -10
@@ -610,10 +610,8 @@ __build_packet_message(struct nfnl_log_net *log, hwhdrp =skb_mac_header (skb); -if (skb->dev->type ==ARPHRD_SIT ) - hwhdrp -=ETH_HLEN ; -if (hwhdrp >= skb->head && + hwhdrp + skb->dev->hard_header_len <=skb_tail_pointer (skb) &&nla_put (inst->skb,NFULA_HWHEADER , skb->dev->hard_header_len, hwhdrp)) goto nla_put_failure;
@@ -688,6 +686,7 @@ __build_packet_message(struct nfnl_log_net *log,return 0 ; nla_put_failure: +nlmsg_cancel (inst->skb, nlh);PRINTR (KERN_ERR "nfnetlink_log: error creating log nlmsg\n" );return -1 ; }
@@ -832,11 +831,12 @@ nfulnl_log_packet(struct net *net, goto alloc_failure; } - inst->qlen++; +if (__build_packet_message (log, inst, skb, data_len, pf, + hooknum, in, out, prefix, plen, + nfnl_ct, ct, ctinfo) <0 ) + goto unlock_and_release; -__build_packet_message (log, inst, skb, data_len, pf, - hooknum, in, out, prefix, plen, - nfnl_ct, ct, ctinfo); + inst->qlen++;if (inst->qlen >= qthreshold)__nfulnl_flush (inst);
@@ -1002,8 +1002,10 @@ static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,struct nfulnl_msg_config_mode *params =nla_data (nfula[NFULA_CFG_MODE ]); -nfulnl_set_mode (inst, params->copy_mode, -ntohl (params->copy_range)); + ret =nfulnl_set_mode (inst, params->copy_mode, +ntohl (params->copy_range)); +if (ret <0 ) + goto err_destroy; }if (nfula[NFULA_CFG_TIMEOUT ]) {
@@ -1015,7 +1017,9 @@ static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,if (nfula[NFULA_CFG_NLBUFSIZ ]) {__be32 nlbufsiz =nla_get_be32 (nfula[NFULA_CFG_NLBUFSIZ ]); -nfulnl_set_nlbufsiz (inst,ntohl (nlbufsiz)); + ret =nfulnl_set_nlbufsiz (inst,ntohl (nlbufsiz)); +if (ret <0 ) + goto err_destroy; }if (nfula[NFULA_CFG_QTHRESH ]) {
@@ -1031,6 +1035,11 @@ static int nfulnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,instance_put (inst); out:return ret; + +err_destroy: +if (cmd && cmd->command ==NFULNL_CFG_CMD_BIND ) +instance_destroy (log, inst); + goto out_put; }static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX ] = {
Florian Westphal
maintainerQuoted 6 lines · Hui Peng
Fix three issues in
net/netfilter/nfnetlink_log.c:1. In
__build_packet_message(), subtractingETH_HLENfromskb_mac_header()forARPHRD_SITtunnels can read beforeskb->heador pastskb_tail_pointer(skb), andnla_put_failurereturns -1 without callingnlmsg_cancel(inst->skb, nlh). Remove the unsafeETH_HLENsubtraction,
That made no sense to me. The entire ARPHRD_SIT check is dead code nowadays and it will be removed in -next.
But how is it 'unsafe'? If it moves to before skb->head, no read will occur.
Quoted 2 lines
verify hwhdrp +
hard_header_len<=skb_tail_pointer(skb), and cancel the partial nlmsg on error.
I don't think its expected to ever fail. There is a space-remaining check *before* __build_packet_message() is called.
Quoted 2 lines
2. In
nfulnl_log_packet(), only incrementinst->qlenafter__build_packet_message()succeeds.
See above.
Quoted 3 lines
3. In
nfulnl_recv_config(), propagate errors fromnfulnl_set_mode()andnfulnl_set_nlbufsiz()and destroy the newly created instance ifNFULNL_CFG_CMD_BINDwas processed in the same message.
Not sure about this. Could cause existing (working!) userspace to fail.
Quoted 1 line
Fixes 0c36b48b36dc ("netfilter: nfnetlink_log: fix mac address for 6in4 tunnels")
Not really. Most of these 'fixes' are day-0 bugs.
I suggest we wait for the ARPHRD_SIT removal series to land in nf-next.
Then at least some of the changes here could land as cleanups.