* [PATCH v3] netfilter: nf_tables: fix memory leak during stateful obj update
@ 2022-02-21 12:31 Florian Westphal
2022-02-21 14:51 ` Pablo Neira Ayuso
2022-02-21 17:54 ` kernel test robot
0 siblings, 2 replies; 3+ messages in thread
From: Florian Westphal @ 2022-02-21 12:31 UTC (permalink / raw)
To: netfilter-devel; +Cc: Florian Westphal, Fernando Fernandez Mancera
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 <ffmancera@riseup.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
---
v3: increment module refcount -> no need for special release function.
net/netfilter/nf_tables_api.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 3081c4399f10..49060f281342 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -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--;
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v3] netfilter: nf_tables: fix memory leak during stateful obj update
2022-02-21 12:31 [PATCH v3] netfilter: nf_tables: fix memory leak during stateful obj update Florian Westphal
@ 2022-02-21 14:51 ` Pablo Neira Ayuso
2022-02-21 17:54 ` kernel test robot
1 sibling, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2022-02-21 14:51 UTC (permalink / raw)
To: Florian Westphal; +Cc: netfilter-devel, Fernando Fernandez Mancera
On Mon, Feb 21, 2022 at 01:31:49PM +0100, Florian Westphal wrote:
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] netfilter: nf_tables: fix memory leak during stateful obj update
2022-02-21 12:31 [PATCH v3] netfilter: nf_tables: fix memory leak during stateful obj update Florian Westphal
2022-02-21 14:51 ` Pablo Neira Ayuso
@ 2022-02-21 17:54 ` kernel test robot
1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2022-02-21 17:54 UTC (permalink / raw)
To: Florian Westphal; +Cc: llvm, kbuild-all
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/202202220115.4cv9qUQN-lkp@intel.com/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
Reported-by: kernel test robot <lkp@intel.com>
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/kbuild-all@lists.01.org
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-02-21 17:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-21 12:31 [PATCH v3] netfilter: nf_tables: fix memory leak during stateful obj update Florian Westphal
2022-02-21 14:51 ` Pablo Neira Ayuso
2022-02-21 17:54 ` kernel test robot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.