Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: fix network unlock with iptables-nft #2323

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions criu/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -3178,19 +3178,53 @@ static inline int nftables_network_unlock(void)
#endif
}

static int iptables_has_criu_jump_target(void)
rst0git marked this conversation as resolved.
Show resolved Hide resolved
{
int fd, ret;
char *argv[4] = { "sh", "-c", "iptables -C INPUT -j CRIU", NULL };

fd = open("/dev/null", O_RDWR);
if (fd < 0) {
fd = -1;
pr_perror("failed to open /dev/null, using log fd");
}

ret = cr_system(fd, fd, fd, "sh", argv, CRS_CAN_FAIL);
close_safe(&fd);
return ret;
}

static int iptables_network_unlock_internal(void)
{
char conf[] = "*filter\n"
":CRIU - [0:0]\n"
"-D INPUT -j CRIU\n"
"-D OUTPUT -j CRIU\n"
"-X CRIU\n"
"COMMIT\n";
char delete_jump_targets[] = "*filter\n"
":CRIU - [0:0]\n"
"-D INPUT -j CRIU\n"
"-D OUTPUT -j CRIU\n"
"COMMIT\n";

char delete_criu_chain[] = "*filter\n"
":CRIU - [0:0]\n"
"-X CRIU\n"
"COMMIT\n";

int ret = 0;

ret |= iptables_restore(false, conf, sizeof(conf) - 1);
ret |= iptables_restore(false, delete_jump_targets, sizeof(delete_jump_targets) - 1);
if (kdat.ipv6)
ret |= iptables_restore(true, conf, sizeof(conf) - 1);
ret |= iptables_restore(true, delete_jump_targets, sizeof(delete_jump_targets) - 1);

/* For compatibility with iptables-nft backend, we need to make sure that all jump
* targets have been removed before deleting the CRIU chain.
*/
if (!iptables_has_criu_jump_target()) {
ret |= iptables_restore(false, delete_jump_targets, sizeof(delete_jump_targets) - 1);
if (kdat.ipv6)
ret |= iptables_restore(true, delete_jump_targets, sizeof(delete_jump_targets) - 1);
}

ret |= iptables_restore(false, delete_criu_chain, sizeof(delete_criu_chain) - 1);
if (kdat.ipv6)
ret |= iptables_restore(true, delete_criu_chain, sizeof(delete_criu_chain) - 1);

return ret;
}
Expand Down
1 change: 1 addition & 0 deletions scripts/build/Dockerfile.alpine
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RUN make mrproper && date && make -j $(nproc) CC="$CC" && date
RUN apk add \
ip6tables \
iptables \
iptables-legacy \
nftables \
iproute2 \
tar \
Expand Down
8 changes: 5 additions & 3 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ TST_NOFILE := \
socket-tcp4v6 \
socket-tcp-local \
socket-tcp-reuseport \
socket-tcp-nfconntrack \
socket-tcp-ipt-nfconntrack \
socket-tcp-nft-nfconntrack \
socket-tcp6-local \
socket-tcp4v6-local \
socket-tcpbuf \
Expand Down Expand Up @@ -277,7 +278,7 @@ pkg-config-check = $(shell sh -c '$(PKG_CONFIG) $(1) && echo y')
ifeq ($(call pkg-config-check,libbpf),y)
TST_NOFILE += \
bpf_hash \
bpf_array
bpf_array
endif

ifneq ($(ARCH),arm)
Expand Down Expand Up @@ -598,7 +599,8 @@ socket-tcpbuf6-local: CFLAGS += -D ZDTM_TCP_LOCAL -D ZDTM_IPV6
socket-tcp6-local: CFLAGS += -D ZDTM_TCP_LOCAL -D ZDTM_IPV6
socket-tcp4v6-local: CFLAGS += -D ZDTM_TCP_LOCAL -D ZDTM_IPV4V6
socket-tcp-local: CFLAGS += -D ZDTM_TCP_LOCAL
socket-tcp-nfconntrack: CFLAGS += -D ZDTM_TCP_LOCAL -DZDTM_CONNTRACK
socket-tcp-ipt-nfconntrack: CFLAGS += -D ZDTM_TCP_LOCAL -DZDTM_IPT_CONNTRACK
socket-tcp-nft-nfconntrack: CFLAGS += -D ZDTM_TCP_LOCAL -DZDTM_NFT_CONNTRACK
socket_listen6: CFLAGS += -D ZDTM_IPV6
socket_listen4v6: CFLAGS += -D ZDTM_IPV4V6
socket-tcp6-closed: CFLAGS += -D ZDTM_IPV6
Expand Down
6 changes: 6 additions & 0 deletions test/zdtm/static/socket-tcp-ipt-nfconntrack.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
'feature': 'has_ipt_legacy',
'flavor': 'h',
'opts': '--tcp-established',
'flags': 'suid'
}
1 change: 0 additions & 1 deletion test/zdtm/static/socket-tcp-nfconntrack.desc

This file was deleted.

1 change: 1 addition & 0 deletions test/zdtm/static/socket-tcp-nft-nfconntrack.c
7 changes: 7 additions & 0 deletions test/zdtm/static/socket-tcp-nft-nfconntrack.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
'flavor': 'h',
'feature': 'network_lock_nftables',
'opts': '--tcp-established',
'dopts': '--network-lock nftables',
'flags': 'suid'
}
27 changes: 24 additions & 3 deletions test/zdtm/static/socket-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,38 @@ int main(int argc, char **argv)
int val;
socklen_t optlen;

#ifdef ZDTM_CONNTRACK
#ifdef ZDTM_IPT_CONNTRACK
if (unshare(CLONE_NEWNET)) {
pr_perror("unshare");
return 1;
}
if (system("ip link set up dev lo"))
return 1;
if (system("iptables -w -A INPUT -i lo -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT"))

if (system("iptables-legacy -w -A INPUT -i lo -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT"))
return 1;
if (system("iptables-legacy -w -A INPUT -j DROP"))
return 1;

#endif

#ifdef ZDTM_NFT_CONNTRACK
if (unshare(CLONE_NEWNET)) {
pr_perror("unshare");
return 1;
if (system("iptables -w -A INPUT -j DROP"))
}
if (system("ip link set up dev lo"))
return 1;

if (system("nft add table ip filter"))
return 1;
if (system("nft add chain ip filter INPUT"))
rst0git marked this conversation as resolved.
Show resolved Hide resolved
return 1;
if (system("nft add rule ip filter INPUT iifname \"lo\" ip protocol tcp ct state new,established counter accept"))
return 1;
if (system("nft add rule ip filter INPUT counter drop"))
return 1;

#endif

#ifdef ZDTM_TCP_LOCAL
Expand Down