All of lore.kernel.org
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [iptables PATCH 1/4] nft: cache: Fix nft_release_cache() under stress
Date: Mon,  2 Mar 2020 18:53:55 +0100	[thread overview]
Message-ID: <20200302175358.27796-2-phil@nwl.cc> (raw)
In-Reply-To: <20200302175358.27796-1-phil@nwl.cc>

iptables-nft-restore calls nft_action(h, NFT_COMPAT_COMMIT) for each
COMMIT line in input. When restoring a dump containing multiple large
tables, chances are nft_rebuild_cache() has to run multiple times.

If the above happens, consecutive table contents are added to __cache[1]
which nft_rebuild_cache() then frees, so next commit attempt accesses
invalid memory.

Fix this by making nft_release_cache() (called after each successful
commit) return things into pre-rebuild state again, but keeping the
fresh cache copy.

Fixes: f6ad231d698c7 ("nft: keep original cache in case of ERESTART")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 iptables/nft-cache.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/iptables/nft-cache.c b/iptables/nft-cache.c
index 7345a27e2894b..6f21f2283e0fb 100644
--- a/iptables/nft-cache.c
+++ b/iptables/nft-cache.c
@@ -647,8 +647,14 @@ void nft_rebuild_cache(struct nft_handle *h)
 
 void nft_release_cache(struct nft_handle *h)
 {
-	if (h->cache_index)
-		flush_cache(h, &h->__cache[0], NULL);
+	if (!h->cache_index)
+		return;
+
+	flush_cache(h, &h->__cache[0], NULL);
+	memcpy(&h->__cache[0], &h->__cache[1], sizeof(h->__cache[0]));
+	memset(&h->__cache[1], 0, sizeof(h->__cache[1]));
+	h->cache_index = 0;
+	h->cache = &h->__cache[0];
 }
 
 struct nftnl_table_list *nftnl_table_list_get(struct nft_handle *h)
-- 
2.25.1


  reply	other threads:[~2020-03-02 17:54 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-02 17:53 [iptables PATCH 0/4] Fix for iptables-nft-restore under pressure Phil Sutter
2020-03-02 17:53 ` Phil Sutter [this message]
2020-03-02 19:19   ` [iptables PATCH 1/4] nft: cache: Fix nft_release_cache() under stress Pablo Neira Ayuso
2020-03-03  1:02     ` Phil Sutter
2020-03-03 20:55       ` Pablo Neira Ayuso
2020-03-04  2:13         ` Phil Sutter
2020-03-04 17:02           ` Pablo Neira Ayuso
2020-03-02 17:53 ` [iptables PATCH 2/4] nft: cache: Make nft_rebuild_cache() respect fake cache Phil Sutter
2020-03-02 19:26   ` Pablo Neira Ayuso
2020-03-03  1:15     ` Phil Sutter
2020-03-02 17:53 ` [iptables PATCH 3/4] nft: cache: Simplify chain list allocation Phil Sutter
2020-03-02 17:53 ` [iptables PATCH 4/4] nft: cache: Review flush_cache() Phil Sutter
2020-03-02 19:22   ` Pablo Neira Ayuso
2020-03-03  1:22     ` Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200302175358.27796-2-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.