|
| 1 | +From 22fdffbdde9476b27988b3ee0a4013a4453784c9 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Andrei Vagin <avagin@gmail.com> |
| 3 | +Date: Mon, 21 Apr 2025 06:33:41 +0000 |
| 4 | +Subject: [PATCH] net: nftables: avoid restore failure if the CRIU nft table |
| 5 | + already exist |
| 6 | + |
| 7 | +CRIU locks the network during restore in an "empty" network namespace. |
| 8 | +However, "empty" in this context means CRIU isn't restoring the |
| 9 | +namespace. This network namespace can be the same namespace where |
| 10 | +processes have been dumped and so the network is already locked in it. |
| 11 | + |
| 12 | +Fixes #2650 |
| 13 | + |
| 14 | +Signed-off-by: Andrei Vagin <avagin@gmail.com> |
| 15 | +--- |
| 16 | + criu/cr-restore.c | 2 +- |
| 17 | + criu/include/net.h | 2 +- |
| 18 | + criu/net.c | 30 +++++++++++++++++------------- |
| 19 | + 3 files changed, 19 insertions(+), 15 deletions(-) |
| 20 | + |
| 21 | +diff --git a/criu/cr-restore.c b/criu/cr-restore.c |
| 22 | +index 583b446e0b..30932f60a2 100644 |
| 23 | +--- a/criu/cr-restore.c |
| 24 | ++++ b/criu/cr-restore.c |
| 25 | +@@ -2119,7 +2119,7 @@ static int restore_root_task(struct pstree_item *init) |
| 26 | + * the '--empty-ns net' mode no iptables C/R is done and we |
| 27 | + * need to return these rules by hands. |
| 28 | + */ |
| 29 | +- ret = network_lock_internal(); |
| 30 | ++ ret = network_lock_internal(/* restore = */ true); |
| 31 | + if (ret) |
| 32 | + goto out_kill; |
| 33 | + } |
| 34 | +diff --git a/criu/include/net.h b/criu/include/net.h |
| 35 | +index 5e8a848620..7c5ede21e1 100644 |
| 36 | +--- a/criu/include/net.h |
| 37 | ++++ b/criu/include/net.h |
| 38 | +@@ -31,7 +31,7 @@ extern int collect_net_namespaces(bool for_dump); |
| 39 | + |
| 40 | + extern int network_lock(void); |
| 41 | + extern void network_unlock(void); |
| 42 | +-extern int network_lock_internal(void); |
| 43 | ++extern int network_lock_internal(bool restore); |
| 44 | + |
| 45 | + extern struct ns_desc net_ns_desc; |
| 46 | + |
| 47 | +diff --git a/criu/net.c b/criu/net.c |
| 48 | +index ee46f1c495..300df480b0 100644 |
| 49 | +--- a/criu/net.c |
| 50 | ++++ b/criu/net.c |
| 51 | +@@ -3206,12 +3206,12 @@ static inline FILE *redirect_nftables_output(struct nft_ctx *nft) |
| 52 | + } |
| 53 | + #endif |
| 54 | + |
| 55 | +-static inline int nftables_lock_network_internal(void) |
| 56 | ++static inline int nftables_lock_network_internal(bool restore) |
| 57 | + { |
| 58 | + #if defined(CONFIG_HAS_NFTABLES_LIB_API_0) || defined(CONFIG_HAS_NFTABLES_LIB_API_1) |
| 59 | + cleanup_file FILE *fp = NULL; |
| 60 | + struct nft_ctx *nft; |
| 61 | +- int ret = 0; |
| 62 | ++ int ret = 0, exit_code = -1; |
| 63 | + char table[32]; |
| 64 | + char buf[128]; |
| 65 | + |
| 66 | +@@ -3224,11 +3224,16 @@ static inline int nftables_lock_network_internal(void) |
| 67 | + |
| 68 | + fp = redirect_nftables_output(nft); |
| 69 | + if (!fp) |
| 70 | +- goto out; |
| 71 | ++ goto err2; |
| 72 | + |
| 73 | + snprintf(buf, sizeof(buf), "create table %s", table); |
| 74 | +- if (NFT_RUN_CMD(nft, buf)) |
| 75 | ++ ret = NFT_RUN_CMD(nft, buf); |
| 76 | ++ if (ret) { |
| 77 | ++ /* The network has been locked on dump. */ |
| 78 | ++ if (restore && errno == EEXIST) |
| 79 | ++ return 0; |
| 80 | + goto err2; |
| 81 | ++ } |
| 82 | + |
| 83 | + snprintf(buf, sizeof(buf), "add chain %s output { type filter hook output priority 0; policy drop; }", table); |
| 84 | + if (NFT_RUN_CMD(nft, buf)) |
| 85 | +@@ -3246,17 +3251,16 @@ static inline int nftables_lock_network_internal(void) |
| 86 | + if (NFT_RUN_CMD(nft, buf)) |
| 87 | + goto err1; |
| 88 | + |
| 89 | +- goto out; |
| 90 | +- |
| 91 | ++ exit_code = 0; |
| 92 | ++out: |
| 93 | ++ nft_ctx_free(nft); |
| 94 | ++ return exit_code; |
| 95 | + err1: |
| 96 | + snprintf(buf, sizeof(buf), "delete table %s", table); |
| 97 | + NFT_RUN_CMD(nft, buf); |
| 98 | + err2: |
| 99 | +- ret = -1; |
| 100 | + pr_err("Locking network failed using nftables\n"); |
| 101 | +-out: |
| 102 | +- nft_ctx_free(nft); |
| 103 | +- return ret; |
| 104 | ++ goto out; |
| 105 | + #else |
| 106 | + pr_err("CRIU was built without libnftables support\n"); |
| 107 | + return -1; |
| 108 | +@@ -3288,7 +3292,7 @@ static int iptables_network_lock_internal(void) |
| 109 | + return ret; |
| 110 | + } |
| 111 | + |
| 112 | +-int network_lock_internal(void) |
| 113 | ++int network_lock_internal(bool restore) |
| 114 | + { |
| 115 | + int ret = 0, nsret; |
| 116 | + |
| 117 | +@@ -3301,7 +3305,7 @@ int network_lock_internal(void) |
| 118 | + if (opts.network_lock_method == NETWORK_LOCK_IPTABLES) |
| 119 | + ret = iptables_network_lock_internal(); |
| 120 | + else if (opts.network_lock_method == NETWORK_LOCK_NFTABLES) |
| 121 | +- ret = nftables_lock_network_internal(); |
| 122 | ++ ret = nftables_lock_network_internal(restore); |
| 123 | + |
| 124 | + if (restore_ns(nsret, &net_ns_desc)) |
| 125 | + ret = -1; |
| 126 | +@@ -3427,7 +3431,7 @@ int network_lock(void) |
| 127 | + if (run_scripts(ACT_NET_LOCK)) |
| 128 | + return -1; |
| 129 | + |
| 130 | +- return network_lock_internal(); |
| 131 | ++ return network_lock_internal(false); |
| 132 | + } |
| 133 | + |
| 134 | + void network_unlock(void) |
0 commit comments