Skip to contentSkip to conversation

netfilter: nf_tables: fix memory leak during stateful obj update

PATCH In review Florian Westphal 3 participants 3 messages Feb 21, 2022

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH v3 1/1 netfilter: nf_tables: fix memory leak during stateful obj update

stateful objects can be updated from the control plane. The transaction logic allocates a temporary object for this purpose.

The ->init function was called for this object, so plain kfree() leaks resources. We must call ->destroy function of the object.

nft_obj_destroy does this, but it also decrements the module refcount, but the update path doesn't increment it.

To avoid special-casing the update object release, do module_get for the update case too and release it via nft_obj_destroy().

Fixes d62d0ba97b58 ("netfilter: nf_tables: Introduce stateful object update operation")
Cc Fernando Fernandez Mancera <[email protected]>

---
v3: increment module refcount -> no need for special release function.

 net/netfilter/nf_tables_api.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +8 -3
index 3081c4399f10..49060f281342 100644
@@ -6553,10 +6553,13 @@ static int nf_tables_updobj(const struct nft_ctx *ctx,
 	struct nft_trans *trans;
 	int err;
 
+	if (!try_module_get(type->owner))
+		return -ENOENT;
+
 	trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
 				sizeof(struct nft_trans_obj));
 	if (!trans)
-		return -ENOMEM;
+		goto err_trans;
 
 	newobj = nft_obj_init(ctx, type, attr);
 	if (IS_ERR(newobj)) {
@@ -6573,6 +6576,8 @@ static int nf_tables_updobj(const struct nft_ctx *ctx,
 
 err_free_trans:
 	kfree(trans);
+err_trans:
+	module_put(type->owner);
 	return err;
 }
 
@@ -8185,7 +8190,7 @@ static void nft_obj_commit_update(struct nft_trans *trans)
 	if (obj->ops->update)
 		obj->ops->update(obj, newobj);
 
-	kfree(newobj);
+	nft_obj_destroy(&trans->ctx, newobj);
 }
 
 static void nft_commit_release(struct nft_trans *trans)
@@ -8976,7 +8981,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
 			break;
 		case NFT_MSG_NEWOBJ:
 			if (nft_trans_obj_update(trans)) {
-				kfree(nft_trans_obj_newobj(trans));
+				nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans));
 				nft_trans_destroy(trans);
 			} else {
 				trans->ctx.table->use--;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 11 lines · Florian Westphal

stateful objects can be updated from the control plane. The transaction logic allocates a temporary object for this purpose.

The ->init function was called for this object, so plain kfree() leaks resources. We must call ->destroy function of the object.

nft_obj_destroy does this, but it also decrements the module refcount, but the update path doesn't increment it.

To avoid special-casing the update object release, do module_get for the update case too and release it via nft_obj_destroy().

Also applied, thanks

#2 lore raw
In reply to Florian Westphal
I love your patch! Perhaps something to improve:

Hi Florian,

I love your patch! Perhaps something to improve:

[auto build test WARNING on nf-next/master]
[also build test WARNING on linus/master v5.17-rc5 next-20220217] [cannot apply to nf/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Florian-Westphal/netfilter-nf_tables-fix-memory-leak-during-stateful-obj-update/20220221-203309
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master
config: i386-randconfig-a012-20220221 (https://download.01.org/0day-ci/archive/20220222/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project d271fc04d5b97b12e6b797c6067d3c96a8d7470e)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/c462078317a686231967b4edac900241eafbbf13
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Florian-Westphal/netfilter-nf_tables-fix-memory-leak-during-stateful-obj-update/20220221-203309 git checkout c462078317a686231967b4edac900241eafbbf13
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash net/netfilter/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> net/netfilter/nf_tables_api.c:6561:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
           if (!trans)
               ^~~~~~
   net/netfilter/nf_tables_api.c:6581:9: note: uninitialized use occurs here
           return err;
                  ^~~
   net/netfilter/nf_tables_api.c:6561:2: note: remove the 'if' if its condition is always false
           if (!trans)
           ^~~~~~~~~~~
   net/netfilter/nf_tables_api.c:6554:9: note: initialize the variable 'err' to silence this warning
           int err;
                  ^
                   = 0

1 warning generated.

vim +6561 net/netfilter/nf_tables_api.c

e50092404c1bc7 Pablo Neira Ayuso          2016-11-28  6546  
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6547  static int nf_tables_updobj(const struct nft_ctx *ctx,
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6548  			    const struct nft_object_type *type,
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6549  			    const struct nlattr *attr,
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6550  			    struct nft_object *obj)
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6551  {
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6552  	struct nft_object *newobj;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6553  	struct nft_trans *trans;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6554  	int err;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6555  
c462078317a686 Florian Westphal           2022-02-21  6556  	if (!try_module_get(type->owner))
c462078317a686 Florian Westphal           2022-02-21  6557  		return -ENOENT;
c462078317a686 Florian Westphal           2022-02-21  6558  
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6559  	trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6560  				sizeof(struct nft_trans_obj));
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26 @6561  	if (!trans)
c462078317a686 Florian Westphal           2022-02-21  6562  		goto err_trans;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6563  
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6564  	newobj = nft_obj_init(ctx, type, attr);
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6565  	if (IS_ERR(newobj)) {
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6566  		err = PTR_ERR(newobj);
b74ae9618b15de Dan Carpenter              2019-09-06  6567  		goto err_free_trans;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6568  	}
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6569  
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6570  	nft_trans_obj(trans) = obj;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6571  	nft_trans_obj_update(trans) = true;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6572  	nft_trans_obj_newobj(trans) = newobj;
0854db2aaef3fc Florian Westphal           2021-04-01  6573  	nft_trans_commit_list_add_tail(ctx->net, trans);
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6574  
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6575  	return 0;
b74ae9618b15de Dan Carpenter              2019-09-06  6576  
b74ae9618b15de Dan Carpenter              2019-09-06  6577  err_free_trans:
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6578  	kfree(trans);
c462078317a686 Florian Westphal           2022-02-21  6579  err_trans:
c462078317a686 Florian Westphal           2022-02-21  6580  	module_put(type->owner);
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6581  	return err;
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6582  }
d62d0ba97b5803 Fernando Fernandez Mancera 2019-08-26  6583  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]