AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/28 15:52 flow patch-triage
3m Args:
null

Results:
{
  "EnableConfigs": [
    "TUN",
    "INET"
  ],
  "FocusSymbols": [
    "tun_copy_filter",
    "tun_attach_filter",
    "tun_detach_filter",
    "tun_attach",
    "sk_attach_filter_kern",
    "bpf_prog_validate_classic",
    "tun_chr_ioctl"
  ],
  "KMSANReasoning": "The patch introduces a kernel stack information leak to userspace through structure padding.\n\nSpecifically:\n1. On 64-bit architectures like amd64, `struct sock_fprog` consists of an `unsigned short len` (2 bytes), followed by 6 bytes of structure alignment padding, and a pointer `filter` (8 bytes), totaling 16 bytes.\n2. In `__tun_chr_ioctl` for `TUNATTACHFILTER`, `struct sock_fprog fprog;` is allocated on the kernel stack without zero-initialization.\n3. In `tun_copy_filter()`, only the individual fields are assigned (`fprog-\u003elen = fp.len;` and `fprog-\u003efilter = fp.filter;`), leaving the 6 bytes of padding uninitialized stack memory.\n4. The entire struct is copied to the heap structure (`tun-\u003efprog = fprog;`), retaining the uninitialized padding bytes.\n5. In the `TUNGETFILTER` ioctl, `copy_to_user(argp, \u0026tun-\u003efprog, sizeof(tun-\u003efprog))` copies all 16 bytes to userspace, leaking 6 uninitialized bytes of kernel stack data. Prior to this patch, `TUNATTACHFILTER` directly did `copy_from_user(\u0026tun-\u003efprog, argp, sizeof(tun-\u003efprog))`, fully initializing all 16 bytes.\n\nKASAN only monitors memory bounds, use-after-free, and validity, and cannot detect reads or user copies of uninitialized memory. KMSAN tracks initialization at the byte level and checks for uninitialized bytes in `copy_to_user()`, making it uniquely suited to detect this vulnerability.",
  "NeedsKMSAN": true,
  "Reasoning": "The patch modifies the TUN/TAP driver and core socket filter subsystem to copy classic BPF socket filters into kernel memory and validate them upon TUNATTACHFILTER ioctl, allowing persistent filters across queue reattachments. It adds kernel helpers sk_attach_filter_kern() and bpf_prog_validate_classic(), and updates tun_attach(), tun_attach_filter(), and error-handling/rollback paths. This code is fully reachable via standard /dev/net/tun ioctl operations and warrants fuzzing for memory safety, concurrency, and rollback edge cases.",
  "WorthFuzzing": true
}

1/1 2026/10/28 15:52 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 2a3430a0ba742101ec930c293df26c9c26409ea7\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Wed Oct 28 15:52:14 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/drivers/net/tun.c b/drivers/net/tun.c\nindex 439565a264ee79..ea6743e8ae6005 100644\n--- a/drivers/net/tun.c\n+++ b/drivers/net/tun.c\n@@ -197,6 +197,7 @@ struct tun_struct {\n \tint\t\t\tsndbuf;\n \tstruct tap_filter\ttxflt;\n \tstruct sock_fprog\tfprog;\n+\tstruct sock_fprog_kern\tfprog_kern;\n \t/* protected by rtnl lock */\n \tbool\t\t\tfilter_attached;\n \tu32\t\t\tmsg_enable;\n@@ -726,12 +727,67 @@ static void tun_force_wake_queue(struct tun_struct *tun,\n \tspin_unlock_bh(\u0026tfile-\u003etx_ring.consumer_lock);\n }\n \n+/* Copy the filter that @argp points at into the kernel, so that it can be\n+ * installed again later, independent of the ioctl caller's address space.\n+ * @fprog_kern receives the copy, which belongs to the caller until it is\n+ * installed or freed, and @fprog receives the header, with the user pointer\n+ * kept for TUNGETFILTER.\n+ */\n+static int tun_copy_filter(struct sock_fprog __user *argp,\n+\t\t\t   struct sock_fprog *fprog,\n+\t\t\t   struct sock_fprog_kern *fprog_kern)\n+{\n+\tstruct sock_fprog fp;\n+\tstruct sock_filter *insns;\n+\tint err;\n+\n+\tif (copy_from_user(\u0026fp, argp, sizeof(fp)))\n+\t\treturn -EFAULT;\n+\n+\tif (!fp.len || fp.len \u003e BPF_MAXINSNS)\n+\t\treturn -EINVAL;\n+\n+\tinsns = kmalloc_array(fp.len, sizeof(struct sock_filter),\n+\t\t\t      GFP_KERNEL_ACCOUNT);\n+\tif (!insns)\n+\t\treturn -ENOMEM;\n+\n+\tif (copy_from_user(insns, fp.filter,\n+\t\t\t   fp.len * sizeof(struct sock_filter))) {\n+\t\tkfree(insns);\n+\t\treturn -EFAULT;\n+\t}\n+\n+\t/* The instructions are only verified when a program is built from\n+\t * them, which happens when the filter is attached to a socket. Run\n+\t * the classic verifier here as well, so that a bad program is\n+\t * refused before it is saved and before any queue is touched.\n+\t * bpf_prog_create() can still refuse the program later, when it is\n+\t * converted to eBPF or when no runtime can be selected for it; the\n+\t * attach path returns that error, and the saved program is not\n+\t * replaced.\n+\t */\n+\terr = bpf_prog_validate_classic(insns, fp.len);\n+\tif (err) {\n+\t\tkfree(insns);\n+\t\treturn err;\n+\t}\n+\n+\tfprog-\u003elen = fp.len;\n+\tfprog-\u003efilter = fp.filter;\n+\tfprog_kern-\u003elen = fp.len;\n+\tfprog_kern-\u003efilter = insns;\n+\n+\treturn 0;\n+}\n+\n static int tun_attach(struct tun_struct *tun, struct file *file,\n \t\t      bool skip_filter, bool napi, bool napi_frags,\n \t\t      bool publish_tun)\n {\n \tstruct tun_file *tfile = file-\u003eprivate_data;\n \tstruct net_device *dev = tun-\u003edev;\n+\tbool rollback_filter = false;\n \tint err;\n \n \terr = security_tun_dev_attach(tfile-\u003esocket.sk, tun-\u003esecurity);\n@@ -756,10 +812,11 @@ static int tun_attach(struct tun_struct *tun, struct file *file,\n \t/* Re-attach the filter to persist device */\n \tif (!skip_filter \u0026\u0026 (tun-\u003efilter_attached == true)) {\n \t\tlock_sock(tfile-\u003esocket.sk);\n-\t\terr = sk_attach_filter(\u0026tun-\u003efprog, tfile-\u003esocket.sk);\n+\t\terr = sk_attach_filter_kern(\u0026tun-\u003efprog_kern, tfile-\u003esocket.sk);\n \t\trelease_sock(tfile-\u003esocket.sk);\n-\t\tif (!err)\n+\t\tif (err)\n \t\t\tgoto out;\n+\t\trollback_filter = true;\n \t}\n \n \tif (!tfile-\u003edetached \u0026\u0026\n@@ -821,6 +878,17 @@ static int tun_attach(struct tun_struct *tun, struct file *file,\n \tWRITE_ONCE(tun-\u003enumqueues, tun-\u003enumqueues + 1);\n \ttun_set_real_num_queues(tun);\n out:\n+\tif (err \u0026\u0026 rollback_filter) {\n+\t\t/* The filter was installed before the rest of the attach\n+\t\t * ran, so drop it again if a later step failed. Any\n+\t\t * program it replaced is already gone: this leaves the\n+\t\t * queue without a filter rather than restoring the\n+\t\t * previous one.\n+\t\t */\n+\t\tlock_sock(tfile-\u003esocket.sk);\n+\t\tsk_detach_filter(tfile-\u003esocket.sk);\n+\t\trelease_sock(tfile-\u003esocket.sk);\n+\t}\n \treturn err;\n }\n \n@@ -2406,6 +2474,7 @@ static void tun_free_netdev(struct net_device *dev)\n \tsecurity_tun_dev_free_security(tun-\u003esecurity);\n \t__tun_set_ebpf(tun, \u0026tun-\u003esteering_prog, NULL);\n \t__tun_set_ebpf(tun, \u0026tun-\u003efilter_prog, NULL);\n+\tkfree(tun-\u003efprog_kern.filter);\n }\n \n static void tun_setup(struct net_device *dev)\n@@ -3070,10 +3139,14 @@ static void tun_detach_filter(struct tun_struct *tun, int n)\n \t\trelease_sock(tfile-\u003esocket.sk);\n \t}\n \n+\tkfree(tun-\u003efprog_kern.filter);\n+\ttun-\u003efprog_kern.filter = NULL;\n+\ttun-\u003efprog_kern.len = 0;\n \ttun-\u003efilter_attached = false;\n }\n \n-static int tun_attach_filter(struct tun_struct *tun)\n+static int tun_attach_filter(struct tun_struct *tun,\n+\t\t\t     struct sock_fprog_kern *fprog_kern)\n {\n \tint i, ret = 0;\n \tstruct tun_file *tfile;\n@@ -3081,15 +3154,29 @@ static int tun_attach_filter(struct tun_struct *tun)\n \tfor (i = 0; i \u003c tun-\u003enumqueues; i++) {\n \t\ttfile = rtnl_dereference(tun-\u003etfiles[i]);\n \t\tlock_sock(tfile-\u003esocket.sk);\n-\t\tret = sk_attach_filter(\u0026tun-\u003efprog, tfile-\u003esocket.sk);\n+\t\tret = sk_attach_filter_kern(fprog_kern, tfile-\u003esocket.sk);\n \t\trelease_sock(tfile-\u003esocket.sk);\n-\t\tif (ret) {\n-\t\t\ttun_detach_filter(tun, i);\n-\t\t\treturn ret;\n-\t\t}\n+\t\tif (ret)\n+\t\t\tgoto out_detach;\n \t}\n \n \ttun-\u003efilter_attached = true;\n+\treturn 0;\n+\n+out_detach:\n+\t/* Drop the copies already installed on queues 0..i-1, which leaves\n+\t * them without a filter: their previous sk_filter, if any, was\n+\t * already replaced and released. The caller does not replace the\n+\t * saved program when this fails, so tun-\u003efprog, tun-\u003efprog_kern and\n+\t * filter_attached keep their previous values.\n+\t */\n+\twhile (i--) {\n+\t\ttfile = rtnl_dereference(tun-\u003etfiles[i]);\n+\t\tlock_sock(tfile-\u003esocket.sk);\n+\t\tsk_detach_filter(tfile-\u003esocket.sk);\n+\t\trelease_sock(tfile-\u003esocket.sk);\n+\t}\n+\n \treturn ret;\n }\n \n@@ -3424,17 +3511,31 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,\n \t\ttun_set_sndbuf(tun);\n \t\tbreak;\n \n-\tcase TUNATTACHFILTER:\n+\tcase TUNATTACHFILTER: {\n+\t\tstruct sock_fprog_kern fprog_kern;\n+\t\tstruct sock_fprog fprog;\n+\n \t\t/* Can be set only for TAPs */\n \t\tret = -EINVAL;\n \t\tif ((tun-\u003eflags \u0026 TUN_TYPE_MASK) != IFF_TAP)\n \t\t\tbreak;\n-\t\tret = -EFAULT;\n-\t\tif (copy_from_user(\u0026tun-\u003efprog, argp, sizeof(tun-\u003efprog)))\n+\n+\t\tret = tun_copy_filter(argp, \u0026fprog, \u0026fprog_kern);\n+\t\tif (ret)\n+\t\t\tbreak;\n+\n+\t\tret = tun_attach_filter(tun, \u0026fprog_kern);\n+\t\tif (ret) {\n+\t\t\tkfree(fprog_kern.filter);\n \t\t\tbreak;\n+\t\t}\n \n-\t\tret = tun_attach_filter(tun);\n+\t\t/* Install the copy only after every queue accepted it. */\n+\t\tkfree(tun-\u003efprog_kern.filter);\n+\t\ttun-\u003efprog_kern = fprog_kern;\n+\t\ttun-\u003efprog = fprog;\n \t\tbreak;\n+\t}\n \n \tcase TUNDETACHFILTER:\n \t\t/* Can be set only for TAPs */\ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex e0218e677a1dcd..7169c03610b445 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -1257,8 +1257,10 @@ int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);\n int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,\n \t\t\t      bpf_aux_classic_check_t trans, bool save_orig);\n void bpf_prog_destroy(struct bpf_prog *fp);\n+int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len);\n \n int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);\n+int sk_attach_filter_kern(struct sock_fprog_kern *fprog, struct sock *sk);\n int sk_attach_bpf(u32 ufd, struct sock *sk);\n int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);\n int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);\ndiff --git a/net/core/filter.c b/net/core/filter.c\nindex b7ac145b68b1f8..61b2d223c44563 100644\n--- a/net/core/filter.c\n+++ b/net/core/filter.c\n@@ -1476,6 +1476,32 @@ void bpf_prog_destroy(struct bpf_prog *fp)\n }\n EXPORT_SYMBOL_GPL(bpf_prog_destroy);\n \n+/**\n+ * bpf_prog_validate_classic - run the classic BPF verifier on kernel memory\n+ * @insns: instruction array\n+ * @len: number of instructions in @insns\n+ *\n+ * Run bpf_check_basics_ok() and bpf_check_classic() on a program that is\n+ * already in kernel memory, without building a struct bpf_prog.  These are\n+ * the checks bpf_prog_create() runs before it converts the program, so\n+ * callers can reject a bad program when it is configured instead of when it\n+ * is first attached to a socket.\n+ *\n+ * A program that passes here can still be refused by bpf_prog_create(), when\n+ * it is converted to eBPF or when no runtime can be selected for it.\n+ *\n+ * Return: 0 if the program passes the classic verifier, -EINVAL if it does\n+ * not, or -ENOMEM if the verifier cannot allocate its state.\n+ */\n+int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len)\n+{\n+\tif (!bpf_check_basics_ok(insns, len))\n+\t\treturn -EINVAL;\n+\n+\treturn bpf_check_classic(insns, len);\n+}\n+EXPORT_SYMBOL_GPL(bpf_prog_validate_classic);\n+\n static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)\n {\n \tstruct sk_filter *fp, *old_fp;\n@@ -1567,6 +1593,47 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)\n }\n EXPORT_SYMBOL_GPL(sk_attach_filter);\n \n+/**\n+ * sk_attach_filter_kern - attach a classic BPF filter held in kernel memory\n+ * @fprog: program to attach, with @fprog-\u003efilter pointing at kernel memory\n+ * @sk: socket to attach the filter to\n+ *\n+ * Attach a copy of a classic BPF program that the kernel already holds, the\n+ * way sk_attach_filter() does for one that still lives in user space. The\n+ * caller must hold the socket lock; SOCK_FILTER_LOCKED is checked here.\n+ *\n+ * The program is built by bpf_prog_create(), which does not keep an original\n+ * copy of the instructions, so sk_get_filter() returns -EACCES and sock_diag\n+ * omits the filter for a socket that uses this helper.\n+ *\n+ * Return: 0 on success, or a negative errno: -EPERM if @sk has\n+ * SOCK_FILTER_LOCKED set, otherwise whatever bpf_prog_create() or\n+ * __sk_attach_prog() returned (for example -EINVAL for an invalid classic\n+ * BPF program, -ENOMEM, or -ENOTSUPP when a JIT is required but is not\n+ * available).\n+ */\n+int sk_attach_filter_kern(struct sock_fprog_kern *fprog, struct sock *sk)\n+{\n+\tstruct bpf_prog *prog;\n+\tint err;\n+\n+\tif (sock_flag(sk, SOCK_FILTER_LOCKED))\n+\t\treturn -EPERM;\n+\n+\terr = bpf_prog_create(\u0026prog, fprog);\n+\tif (err)\n+\t\treturn err;\n+\n+\terr = __sk_attach_prog(prog, sk);\n+\tif (err \u003c 0) {\n+\t\t__bpf_prog_release(prog);\n+\t\treturn err;\n+\t}\n+\n+\treturn 0;\n+}\n+EXPORT_SYMBOL_GPL(sk_attach_filter_kern);\n+\n int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)\n {\n \tstruct bpf_prog *prog = __get_filter(fprog, sk);\ndiff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c\nindex 34de2580e32eca..3730fba859a46e 100644\n--- a/tools/testing/selftests/net/tun.c\n+++ b/tools/testing/selftests/net/tun.c\n@@ -9,9 +9,11 @@\n #include \u003cstdlib.h\u003e\n #include \u003cstring.h\u003e\n #include \u003cunistd.h\u003e\n+#include \u003clinux/filter.h\u003e\n #include \u003clinux/if_tun.h\u003e\n #include \u003cnetinet/tcp.h\u003e\n #include \u003csys/ioctl.h\u003e\n+#include \u003csys/mman.h\u003e\n #include \u003csys/socket.h\u003e\n \n #include \"kselftest_harness.h\"\n@@ -548,6 +550,253 @@ TEST_F(tun, reattach_close_delete)\n \tEXPECT_EQ(tun_delete(self-\u003eifname), 0);\n }\n \n+/* accept: return skb-\u003elen.  Three instructions on purpose, so that the\n+ * length read back through TUNGETFILTER tells it apart from a rejected\n+ * program that is shorter.\n+ */\n+static const struct sock_filter filter_accept[] = {\n+\tBPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),\n+\tBPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0),\n+\tBPF_STMT(BPF_RET | BPF_A, 0),\n+};\n+\n+/* Put the instructions in an anonymous mapping, so that the test can make the\n+ * address unreadable afterwards.\n+ */\n+static void *filter_alloc(const struct sock_filter *insns, unsigned int len)\n+{\n+\tvoid *p;\n+\n+\tp = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,\n+\t\t MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n+\tif (p == MAP_FAILED)\n+\t\treturn NULL;\n+\n+\tmemcpy(p, insns, len * sizeof(*insns));\n+\n+\treturn p;\n+}\n+\n+static int filter_attach(int fd, const struct sock_filter *insns, unsigned int len)\n+{\n+\tstruct sock_fprog fp = {\n+\t\t.len = len,\n+\t\t.filter = (struct sock_filter *)insns,\n+\t};\n+\n+\treturn ioctl(fd, TUNATTACHFILTER, (void *)\u0026fp);\n+}\n+\n+static int filter_get(int fd, struct sock_fprog *fp)\n+{\n+\treturn ioctl(fd, TUNGETFILTER, (void *)fp);\n+}\n+\n+static int tun_get_iff_flags(int fd)\n+{\n+\tstruct ifreq ifr = { 0 };\n+\n+\tif (ioctl(fd, TUNGETIFF, (void *)\u0026ifr) \u003c 0)\n+\t\treturn -1;\n+\n+\treturn ifr.ifr_flags;\n+}\n+\n+/*\n+ * A queue can be attached long after the filter was configured, from a\n+ * process that does not necessarily map the buffer the program was copied\n+ * from, so the kernel has to keep its own copy of the program.  Here the\n+ * mapping is made unreadable before the queue is attached again, and a\n+ * second queue is then attached from a fresh socket, which starts without a\n+ * filter of its own.\n+ */\n+TEST_F(tun, reattach_filter_without_user_buffer)\n+{\n+\tstruct sock_fprog gf = { 0 };\n+\tstruct ifreq ifr = { 0 };\n+\tshort flags = 0;\n+\tvoid *prog;\n+\tint fd, ret;\n+\n+\tprog = filter_alloc(filter_accept, ARRAY_SIZE(filter_accept));\n+\tASSERT_NE(prog, NULL);\n+\tASSERT_EQ(filter_attach(self-\u003efd, prog, ARRAY_SIZE(filter_accept)), 0);\n+\n+\tEXPECT_EQ(tun_detach(self-\u003efd, self-\u003eifname), 0);\n+\n+\t/* This memory is no longer readable */\n+\tASSERT_EQ(mprotect(prog, getpagesize(), PROT_NONE), 0);\n+\n+\tret = tun_attach(self-\u003efd, self-\u003eifname);\n+\tEXPECT_EQ(ret, 0);\n+\n+\tflags = tun_get_iff_flags(self-\u003efd);\n+\tEXPECT_GE(flags, 0);\n+\t/* The queue is attached again, not left disabled */\n+\tEXPECT_EQ(flags \u0026 IFF_DETACH_QUEUE, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, 0);\n+\n+\t/* The two checks below document the expected state, they cannot\n+\t * tell a reinstalled filter from no filter: tun_detach() leaves\n+\t * sk_filter alone, and TUNGETFILTER only returns the header.\n+\t */\n+\tEXPECT_EQ(filter_get(self-\u003efd, \u0026gf), 0);\n+\tEXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));\n+\n+\t/* A new queue starts without a filter, so a clear IFF_NOFILTER shows\n+\t * that the filter was installed again from the kernel copy.\n+\t */\n+\tfd = open(\"/dev/net/tun\", O_RDWR);\n+\tASSERT_GE(fd, 0);\n+\n+\tstrcpy(ifr.ifr_name, self-\u003eifname);\n+\tifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;\n+\tEXPECT_GE(ioctl(fd, TUNSETIFF, (void *)\u0026ifr), 0);\n+\n+\tflags = tun_get_iff_flags(fd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, 0);\n+\n+\tclose(fd);\n+\n+\tASSERT_EQ(mprotect(prog, getpagesize(), PROT_READ | PROT_WRITE), 0);\n+\tASSERT_EQ(munmap(prog, getpagesize()), 0);\n+}\n+\n+/* A new queue attached with IFF_NOFILTER must not get the filter that is\n+ * configured on the device.\n+ */\n+TEST_F(tun, attach_filter_nofilter_flag)\n+{\n+\tstruct ifreq ifr = { 0 };\n+\tshort flags = 0;\n+\tint fd;\n+\n+\tASSERT_EQ(filter_attach(self-\u003efd, filter_accept, ARRAY_SIZE(filter_accept)), 0);\n+\n+\tfd = open(\"/dev/net/tun\", O_RDWR);\n+\tASSERT_GE(fd, 0);\n+\n+\tstrcpy(ifr.ifr_name, self-\u003eifname);\n+\tifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE | IFF_NOFILTER;\n+\tEXPECT_GE(ioctl(fd, TUNSETIFF, (void *)\u0026ifr), 0);\n+\n+\tflags = tun_get_iff_flags(fd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, IFF_NOFILTER);\n+\n+\tclose(fd);\n+}\n+\n+/* After TUNDETACHFILTER a later attach must not install the filter again */\n+TEST_F(tun, detach_filter_clears_reattach)\n+{\n+\tshort flags = 0;\n+\n+\tASSERT_EQ(filter_attach(self-\u003efd, filter_accept, ARRAY_SIZE(filter_accept)), 0);\n+\tEXPECT_EQ(ioctl(self-\u003efd, TUNDETACHFILTER, 0), 0);\n+\n+\tflags = tun_get_iff_flags(self-\u003efd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, IFF_NOFILTER);\n+\n+\tEXPECT_EQ(tun_detach(self-\u003efd, self-\u003eifname), 0);\n+\tEXPECT_EQ(tun_attach(self-\u003efd, self-\u003eifname), 0);\n+\n+\tflags = tun_get_iff_flags(self-\u003efd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, IFF_NOFILTER);\n+}\n+\n+/* A TUNATTACHFILTER with a bad length must leave the installed program and the\n+ * filter state that TUNGETIFF reports alone.\n+ */\n+TEST_F(tun, attach_filter_bad_len_keeps_program)\n+{\n+\tstruct sock_fprog gf = { 0 };\n+\tstruct ifreq ifr = { 0 };\n+\tshort flags = 0;\n+\tint fd;\n+\n+\tASSERT_EQ(filter_attach(self-\u003efd, filter_accept, ARRAY_SIZE(filter_accept)), 0);\n+\n+\terrno = 0;\n+\tEXPECT_EQ(filter_attach(self-\u003efd, filter_accept, 0), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\tEXPECT_EQ(filter_get(self-\u003efd, \u0026gf), 0);\n+\tEXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));\n+\n+\tflags = tun_get_iff_flags(self-\u003efd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, 0);\n+\n+\t/* A queue attached afterwards still gets the filter, so the rejected\n+\t * ioctl left the saved program and filter_attached alone.\n+\t */\n+\tfd = open(\"/dev/net/tun\", O_RDWR);\n+\tASSERT_GE(fd, 0);\n+\n+\tstrcpy(ifr.ifr_name, self-\u003eifname);\n+\tifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;\n+\tEXPECT_GE(ioctl(fd, TUNSETIFF, (void *)\u0026ifr), 0);\n+\n+\tflags = tun_get_iff_flags(fd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, 0);\n+\n+\tclose(fd);\n+}\n+\n+/* A TUNATTACHFILTER with instructions that bpf_check_classic() rejects must\n+ * be refused, and must leave the program installed already in place.\n+ */\n+TEST_F(tun, attach_filter_bad_prog_keeps_program)\n+{\n+\tstruct sock_fprog gf = { 0 };\n+\tstruct sock_filter bad[] = {\n+\t\tBPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),\n+\t\tBPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),\n+\t};\n+\tstruct ifreq ifr = { 0 };\n+\tshort flags = 0;\n+\tint fd;\n+\n+\tASSERT_EQ(filter_attach(self-\u003efd, filter_accept,\n+\t\t\t\tARRAY_SIZE(filter_accept)), 0);\n+\n+\terrno = 0;\n+\tEXPECT_EQ(filter_attach(self-\u003efd, bad, ARRAY_SIZE(bad)), -1);\n+\tEXPECT_EQ(errno, EINVAL);\n+\n+\t/* the program attached before is untouched */\n+\tEXPECT_EQ(filter_get(self-\u003efd, \u0026gf), 0);\n+\tEXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));\n+\n+\t/* IFF_NOFILTER comes from the socket's sk_filter, not from the saved\n+\t * header, so a clear flag shows the filter is still installed.\n+\t */\n+\tflags = tun_get_iff_flags(self-\u003efd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, 0);\n+\n+\t/* A queue attached afterwards still gets the filter, so the rejected\n+\t * program never replaced the saved one.\n+\t */\n+\tfd = open(\"/dev/net/tun\", O_RDWR);\n+\tASSERT_GE(fd, 0);\n+\n+\tstrcpy(ifr.ifr_name, self-\u003eifname);\n+\tifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;\n+\tEXPECT_GE(ioctl(fd, TUNSETIFF, (void *)\u0026ifr), 0);\n+\n+\tflags = tun_get_iff_flags(fd);\n+\tEXPECT_GE(flags, 0);\n+\tEXPECT_EQ(flags \u0026 IFF_NOFILTER, 0);\n+\n+\tclose(fd);\n+}\n+\n FIXTURE(tun_vnet_gso)\n {\n \tchar ifname[IFNAMSIZ];\n"
}

2/1 2026/10/28 15:52 agent patch-evaluator
1m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "TUN",
    "INET"
  ],
  "FocusSymbols": [
    "tun_copy_filter",
    "tun_attach_filter",
    "tun_detach_filter",
    "tun_attach",
    "sk_attach_filter_kern",
    "bpf_prog_validate_classic",
    "tun_chr_ioctl"
  ],
  "Reasoning": "The patch modifies the TUN/TAP driver and core socket filter subsystem to copy classic BPF socket filters into kernel memory and validate them upon TUNATTACHFILTER ioctl, allowing persistent filters across queue reattachments. It adds kernel helpers sk_attach_filter_kern() and bpf_prog_validate_classic(), and updates tun_attach(), tun_attach_filter(), and error-handling/rollback paths. This code is fully reachable via standard /dev/net/tun ioctl operations and warrants fuzzing for memory safety, concurrency, and rollback edge cases.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 2a3430a0ba742101ec930c293df26c9c26409ea7
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 28 15:52:14 2026 +0000

    syz-cluster: applied patch under review

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 439565a264ee79..ea6743e8ae6005 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -197,6 +197,7 @@ struct tun_struct {
 	int			sndbuf;
 	struct tap_filter	txflt;
 	struct sock_fprog	fprog;
+	struct sock_fprog_kern	fprog_kern;
 	/* protected by rtnl lock */
 	bool			filter_attached;
 	u32			msg_enable;
@@ -726,12 +727,67 @@ static void tun_force_wake_queue(struct tun_struct *tun,
 	spin_unlock_bh(&tfile->tx_ring.consumer_lock);
 }
 
+/* Copy the filter that @argp points at into the kernel, so that it can be
+ * installed again later, independent of the ioctl caller's address space.
+ * @fprog_kern receives the copy, which belongs to the caller until it is
+ * installed or freed, and @fprog receives the header, with the user pointer
+ * kept for TUNGETFILTER.
+ */
+static int tun_copy_filter(struct sock_fprog __user *argp,
+			   struct sock_fprog *fprog,
+			   struct sock_fprog_kern *fprog_kern)
+{
+	struct sock_fprog fp;
+	struct sock_filter *insns;
+	int err;
+
+	if (copy_from_user(&fp, argp, sizeof(fp)))
+		return -EFAULT;
+
+	if (!fp.len || fp.len > BPF_MAXINSNS)
+		return -EINVAL;
+
+	insns = kmalloc_array(fp.len, sizeof(struct sock_filter),
+			      GFP_KERNEL_ACCOUNT);
+	if (!insns)
+		return -ENOMEM;
+
+	if (copy_from_user(insns, fp.filter,
+			   fp.len * sizeof(struct sock_filter))) {
+		kfree(insns);
+		return -EFAULT;
+	}
+
+	/* The instructions are only verified when a program is built from
+	 * them, which happens when the filter is attached to a socket. Run
+	 * the classic verifier here as well, so that a bad program is
+	 * refused before it is saved and before any queue is touched.
+	 * bpf_prog_create() can still refuse the program later, when it is
+	 * converted to eBPF or when no runtime can be selected for it; the
+	 * attach path returns that error, and the saved program is not
+	 * replaced.
+	 */
+	err = bpf_prog_validate_classic(insns, fp.len);
+	if (err) {
+		kfree(insns);
+		return err;
+	}
+
+	fprog->len = fp.len;
+	fprog->filter = fp.filter;
+	fprog_kern->len = fp.len;
+	fprog_kern->filter = insns;
+
+	return 0;
+}
+
 static int tun_attach(struct tun_struct *tun, struct file *file,
 		      bool skip_filter, bool napi, bool napi_frags,
 		      bool publish_tun)
 {
 	struct tun_file *tfile = file->private_data;
 	struct net_device *dev = tun->dev;
+	bool rollback_filter = false;
 	int err;
 
 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
@@ -756,10 +812,11 @@ static int tun_attach(struct tun_struct *tun, struct file *file,
 	/* Re-attach the filter to persist device */
 	if (!skip_filter && (tun->filter_attached == true)) {
 		lock_sock(tfile->socket.sk);
-		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
+		err = sk_attach_filter_kern(&tun->fprog_kern, tfile->socket.sk);
 		release_sock(tfile->socket.sk);
-		if (!err)
+		if (err)
 			goto out;
+		rollback_filter = true;
 	}
 
 	if (!tfile->detached &&
@@ -821,6 +878,17 @@ static int tun_attach(struct tun_struct *tun, struct file *file,
 	WRITE_ONCE(tun->numqueues, tun->numqueues + 1);
 	tun_set_real_num_queues(tun);
 out:
+	if (err && rollback_filter) {
+		/* The filter was installed before the rest of the attach
+		 * ran, so drop it again if a later step failed. Any
+		 * program it replaced is already gone: this leaves the
+		 * queue without a filter rather than restoring the
+		 * previous one.
+		 */
+		lock_sock(tfile->socket.sk);
+		sk_detach_filter(tfile->socket.sk);
+		release_sock(tfile->socket.sk);
+	}
 	return err;
 }
 
@@ -2406,6 +2474,7 @@ static void tun_free_netdev(struct net_device *dev)
 	security_tun_dev_free_security(tun->security);
 	__tun_set_ebpf(tun, &tun->steering_prog, NULL);
 	__tun_set_ebpf(tun, &tun->filter_prog, NULL);
+	kfree(tun->fprog_kern.filter);
 }
 
 static void tun_setup(struct net_device *dev)
@@ -3070,10 +3139,14 @@ static void tun_detach_filter(struct tun_struct *tun, int n)
 		release_sock(tfile->socket.sk);
 	}
 
+	kfree(tun->fprog_kern.filter);
+	tun->fprog_kern.filter = NULL;
+	tun->fprog_kern.len = 0;
 	tun->filter_attached = false;
 }
 
-static int tun_attach_filter(struct tun_struct *tun)
+static int tun_attach_filter(struct tun_struct *tun,
+			     struct sock_fprog_kern *fprog_kern)
 {
 	int i, ret = 0;
 	struct tun_file *tfile;
@@ -3081,15 +3154,29 @@ static int tun_attach_filter(struct tun_struct *tun)
 	for (i = 0; i < tun->numqueues; i++) {
 		tfile = rtnl_dereference(tun->tfiles[i]);
 		lock_sock(tfile->socket.sk);
-		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
+		ret = sk_attach_filter_kern(fprog_kern, tfile->socket.sk);
 		release_sock(tfile->socket.sk);
-		if (ret) {
-			tun_detach_filter(tun, i);
-			return ret;
-		}
+		if (ret)
+			goto out_detach;
 	}
 
 	tun->filter_attached = true;
+	return 0;
+
+out_detach:
+	/* Drop the copies already installed on queues 0..i-1, which leaves
+	 * them without a filter: their previous sk_filter, if any, was
+	 * already replaced and released. The caller does not replace the
+	 * saved program when this fails, so tun->fprog, tun->fprog_kern and
+	 * filter_attached keep their previous values.
+	 */
+	while (i--) {
+		tfile = rtnl_dereference(tun->tfiles[i]);
+		lock_sock(tfile->socket.sk);
+		sk_detach_filter(tfile->socket.sk);
+		release_sock(tfile->socket.sk);
+	}
+
 	return ret;
 }
 
@@ -3424,17 +3511,31 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		tun_set_sndbuf(tun);
 		break;
 
-	case TUNATTACHFILTER:
+	case TUNATTACHFILTER: {
+		struct sock_fprog_kern fprog_kern;
+		struct sock_fprog fprog;
+
 		/* Can be set only for TAPs */
 		ret = -EINVAL;
 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
 			break;
-		ret = -EFAULT;
-		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
+
+		ret = tun_copy_filter(argp, &fprog, &fprog_kern);
+		if (ret)
+			break;
+
+		ret = tun_attach_filter(tun, &fprog_kern);
+		if (ret) {
+			kfree(fprog_kern.filter);
 			break;
+		}
 
-		ret = tun_attach_filter(tun);
+		/* Install the copy only after every queue accepted it. */
+		kfree(tun->fprog_kern.filter);
+		tun->fprog_kern = fprog_kern;
+		tun->fprog = fprog;
 		break;
+	}
 
 	case TUNDETACHFILTER:
 		/* Can be set only for TAPs */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index e0218e677a1dcd..7169c03610b445 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1257,8 +1257,10 @@ int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
 			      bpf_aux_classic_check_t trans, bool save_orig);
 void bpf_prog_destroy(struct bpf_prog *fp);
+int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len);
 
 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
+int sk_attach_filter_kern(struct sock_fprog_kern *fprog, struct sock *sk);
 int sk_attach_bpf(u32 ufd, struct sock *sk);
 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
diff --git a/net/core/filter.c b/net/core/filter.c
index b7ac145b68b1f8..61b2d223c44563 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -1476,6 +1476,32 @@ void bpf_prog_destroy(struct bpf_prog *fp)
 }
 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
 
+/**
+ * bpf_prog_validate_classic - run the classic BPF verifier on kernel memory
+ * @insns: instruction array
+ * @len: number of instructions in @insns
+ *
+ * Run bpf_check_basics_ok() and bpf_check_classic() on a program that is
+ * already in kernel memory, without building a struct bpf_prog.  These are
+ * the checks bpf_prog_create() runs before it converts the program, so
+ * callers can reject a bad program when it is configured instead of when it
+ * is first attached to a socket.
+ *
+ * A program that passes here can still be refused by bpf_prog_create(), when
+ * it is converted to eBPF or when no runtime can be selected for it.
+ *
+ * Return: 0 if the program passes the classic verifier, -EINVAL if it does
+ * not, or -ENOMEM if the verifier cannot allocate its state.
+ */
+int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len)
+{
+	if (!bpf_check_basics_ok(insns, len))
+		return -EINVAL;
+
+	return bpf_check_classic(insns, len);
+}
+EXPORT_SYMBOL_GPL(bpf_prog_validate_classic);
+
 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
 {
 	struct sk_filter *fp, *old_fp;
@@ -1567,6 +1593,47 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(sk_attach_filter);
 
+/**
+ * sk_attach_filter_kern - attach a classic BPF filter held in kernel memory
+ * @fprog: program to attach, with @fprog->filter pointing at kernel memory
+ * @sk: socket to attach the filter to
+ *
+ * Attach a copy of a classic BPF program that the kernel already holds, the
+ * way sk_attach_filter() does for one that still lives in user space. The
+ * caller must hold the socket lock; SOCK_FILTER_LOCKED is checked here.
+ *
+ * The program is built by bpf_prog_create(), which does not keep an original
+ * copy of the instructions, so sk_get_filter() returns -EACCES and sock_diag
+ * omits the filter for a socket that uses this helper.
+ *
+ * Return: 0 on success, or a negative errno: -EPERM if @sk has
+ * SOCK_FILTER_LOCKED set, otherwise whatever bpf_prog_create() or
+ * __sk_attach_prog() returned (for example -EINVAL for an invalid classic
+ * BPF program, -ENOMEM, or -ENOTSUPP when a JIT is required but is not
+ * available).
+ */
+int sk_attach_filter_kern(struct sock_fprog_kern *fprog, struct sock *sk)
+{
+	struct bpf_prog *prog;
+	int err;
+
+	if (sock_flag(sk, SOCK_FILTER_LOCKED))
+		return -EPERM;
+
+	err = bpf_prog_create(&prog, fprog);
+	if (err)
+		return err;
+
+	err = __sk_attach_prog(prog, sk);
+	if (err < 0) {
+		__bpf_prog_release(prog);
+		return err;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sk_attach_filter_kern);
+
 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
 {
 	struct bpf_prog *prog = __get_filter(fprog, sk);
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 34de2580e32eca..3730fba859a46e 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -9,9 +9,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <linux/filter.h>
 #include <linux/if_tun.h>
 #include <netinet/tcp.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 #include <sys/socket.h>
 
 #include "kselftest_harness.h"
@@ -548,6 +550,253 @@ TEST_F(tun, reattach_close_delete)
 	EXPECT_EQ(tun_delete(self->ifname), 0);
 }
 
+/* accept: return skb->len.  Three instructions on purpose, so that the
+ * length read back through TUNGETFILTER tells it apart from a rejected
+ * program that is shorter.
+ */
+static const struct sock_filter filter_accept[] = {
+	BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
+	BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0),
+	BPF_STMT(BPF_RET | BPF_A, 0),
+};
+
+/* Put the instructions in an anonymous mapping, so that the test can make the
+ * address unreadable afterwards.
+ */
+static void *filter_alloc(const struct sock_filter *insns, unsigned int len)
+{
+	void *p;
+
+	p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+		 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (p == MAP_FAILED)
+		return NULL;
+
+	memcpy(p, insns, len * sizeof(*insns));
+
+	return p;
+}
+
+static int filter_attach(int fd, const struct sock_filter *insns, unsigned int len)
+{
+	struct sock_fprog fp = {
+		.len = len,
+		.filter = (struct sock_filter *)insns,
+	};
+
+	return ioctl(fd, TUNATTACHFILTER, (void *)&fp);
+}
+
+static int filter_get(int fd, struct sock_fprog *fp)
+{
+	return ioctl(fd, TUNGETFILTER, (void *)fp);
+}
+
+static int tun_get_iff_flags(int fd)
+{
+	struct ifreq ifr = { 0 };
+
+	if (ioctl(fd, TUNGETIFF, (void *)&ifr) < 0)
+		return -1;
+
+	return ifr.ifr_flags;
+}
+
+/*
+ * A queue can be attached long after the filter was configured, from a
+ * process that does not necessarily map the buffer the program was copied
+ * from, so the kernel has to keep its own copy of the program.  Here the
+ * mapping is made unreadable before the queue is attached again, and a
+ * second queue is then attached from a fresh socket, which starts without a
+ * filter of its own.
+ */
+TEST_F(tun, reattach_filter_without_user_buffer)
+{
+	struct sock_fprog gf = { 0 };
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	void *prog;
+	int fd, ret;
+
+	prog = filter_alloc(filter_accept, ARRAY_SIZE(filter_accept));
+	ASSERT_NE(prog, NULL);
+	ASSERT_EQ(filter_attach(self->fd, prog, ARRAY_SIZE(filter_accept)), 0);
+
+	EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
+
+	/* This memory is no longer readable */
+	ASSERT_EQ(mprotect(prog, getpagesize(), PROT_NONE), 0);
+
+	ret = tun_attach(self->fd, self->ifname);
+	EXPECT_EQ(ret, 0);
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	/* The queue is attached again, not left disabled */
+	EXPECT_EQ(flags & IFF_DETACH_QUEUE, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	/* The two checks below document the expected state, they cannot
+	 * tell a reinstalled filter from no filter: tun_detach() leaves
+	 * sk_filter alone, and TUNGETFILTER only returns the header.
+	 */
+	EXPECT_EQ(filter_get(self->fd, &gf), 0);
+	EXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));
+
+	/* A new queue starts without a filter, so a clear IFF_NOFILTER shows
+	 * that the filter was installed again from the kernel copy.
+	 */
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	close(fd);
+
+	ASSERT_EQ(mprotect(prog, getpagesize(), PROT_READ | PROT_WRITE), 0);
+	ASSERT_EQ(munmap(prog, getpagesize()), 0);
+}
+
+/* A new queue attached with IFF_NOFILTER must not get the filter that is
+ * configured on the device.
+ */
+TEST_F(tun, attach_filter_nofilter_flag)
+{
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	int fd;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept, ARRAY_SIZE(filter_accept)), 0);
+
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE | IFF_NOFILTER;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, IFF_NOFILTER);
+
+	close(fd);
+}
+
+/* After TUNDETACHFILTER a later attach must not install the filter again */
+TEST_F(tun, detach_filter_clears_reattach)
+{
+	short flags = 0;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept, ARRAY_SIZE(filter_accept)), 0);
+	EXPECT_EQ(ioctl(self->fd, TUNDETACHFILTER, 0), 0);
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, IFF_NOFILTER);
+
+	EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
+	EXPECT_EQ(tun_attach(self->fd, self->ifname), 0);
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, IFF_NOFILTER);
+}
+
+/* A TUNATTACHFILTER with a bad length must leave the installed program and the
+ * filter state that TUNGETIFF reports alone.
+ */
+TEST_F(tun, attach_filter_bad_len_keeps_program)
+{
+	struct sock_fprog gf = { 0 };
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	int fd;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept, ARRAY_SIZE(filter_accept)), 0);
+
+	errno = 0;
+	EXPECT_EQ(filter_attach(self->fd, filter_accept, 0), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	EXPECT_EQ(filter_get(self->fd, &gf), 0);
+	EXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	/* A queue attached afterwards still gets the filter, so the rejected
+	 * ioctl left the saved program and filter_attached alone.
+	 */
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	close(fd);
+}
+
+/* A TUNATTACHFILTER with instructions that bpf_check_classic() rejects must
+ * be refused, and must leave the program installed already in place.
+ */
+TEST_F(tun, attach_filter_bad_prog_keeps_program)
+{
+	struct sock_fprog gf = { 0 };
+	struct sock_filter bad[] = {
+		BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
+		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
+	};
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	int fd;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept,
+				ARRAY_SIZE(filter_accept)), 0);
+
+	errno = 0;
+	EXPECT_EQ(filter_attach(self->fd, bad, ARRAY_SIZE(bad)), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	/* the program attached before is untouched */
+	EXPECT_EQ(filter_get(self->fd, &gf), 0);
+	EXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));
+
+	/* IFF_NOFILTER comes from the socket's sk_filter, not from the saved
+	 * header, so a clear flag shows the filter is still installed.
+	 */
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	/* A queue attached afterwards still gets the filter, so the rejected
+	 * program never replaced the saved one.
+	 */
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	close(fd);
+}
+
 FIXTURE(tun_vnet_gso)
 {
 	char ifname[IFNAMSIZ];

3/2 2026/10/28 15:52 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 9817
							output: 31
							thoughts: 731
						

Thoughts:
**Analyzing a Patch for Fuzzing Potential**

Okay, let's break this down. My initial assessment of this patch is that it's worth a closer look from a fuzzing perspective. It involves modifications to `drivers/net/tun.c`, `include/linux/filter.h`, `net/core/filter.c`, and even some selftests in `tools/testing/selftests/net/tun.c`. The core of the change seems to be around adding kernel-side BPF filter support for TUN devices. Specifically, they're introducing `struct sock_fprog_kern fprog_kern` within the `tun_struct`, along with associated functions like `tun_copy_filter`, `tun_attach_filter`, and `tun_detach_filter`. There's also some interesting additions of BPF validation and attachment functions.

My immediate thought is that this has definite fuzzing potential because of how it modifies memory handling, especially copying the filter to the kernel, managing the lifetime of that kernel filter pointer (`tun->fprog_kern.filter`), and the inclusion of attach/detach error handling. The use of `sk_attach_filter_kern` suggests integration with the networking stack, which always warrants a thorough examination. The fact that it's touching `net/core/filter.c` is especially promising, because that's where a lot of potentially exploitable logic lives.

Now, let's evaluate its reachability. Crucially, TUN/TAP devices are ubiquitous in virtualized environments like QEMU and GCE, which I know are heavily targeted by fuzzing efforts. The patch modifies code that is accessible via ioctls like `TUNATTACHFILTER`, `TUNDETACHFILTER`, and `TUNSETIFF`. This is a big win, as these ioctls are a well-understood attack surface.

The potential for finding bugs is high. I see opportunities for use-after-free, double frees, memory leaks, and even race conditions between ioctl operations and the attach/detach functions. The classic BPF validation logic adds another layer of complexity that could be a source of vulnerabilities.

Finally, I need to verify that the necessary configurations are enabled. I'll need to check the Kconfig files for `drivers/net/tun.c` and `net/core/filter.c` and grep for any relevant CONFIG options related to TUN and BPF.



4/2 2026/10/28 15:52 tool grepper
0m Args:
{
  "Expression": "config TUN\\b",
  "PathPrefixes": [
    "drivers/net/Kconfig"
  ]
}

Results:
{
  "Output": "drivers/net/Kconfig=386=config RIONET_RX_SIZE\n--\ndrivers/net/Kconfig-390-\ndrivers/net/Kconfig:391:config TUN\ndrivers/net/Kconfig-392-\ttristate \"Universal TUN/TAP device driver support\"\n"
}

5/2 2026/10/28 15:52 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 11173
							output: 34
							thoughts: 26
						

Thoughts:
Here's a summary of my thought process:

**Diving into the Kernel Network Configuration**

Okay, time to dig into this networking issue. My gut feeling tells me the problem likely resides in the configuration, so I'm going to jump straight to the relevant Kconfig file. I'm thinking `drivers/net/Kconfig` is the place to start, specifically around line 391. I'll probably be using `read-file` to quickly check that specific section, or maybe even fire up `grepper` if I need something a little more powerful to search the surrounding code. Need to check what's there and if that area is configured in the expected way. Gotta stay efficient.



6/2 2026/10/28 15:52 tool read-file
0m Args:
{
  "File": "drivers/net/Kconfig",
  "FirstLine": 390,
  "LineCount": 20
}

Results:
{
  "Contents": " 390:\t\n 391:\tconfig TUN\n 392:\t\ttristate \"Universal TUN/TAP device driver support\"\n 393:\t\tdepends on INET\n 394:\t\tselect CRC32\n 395:\t\thelp\n 396:\t\t  TUN/TAP provides packet reception and transmission for user space\n 397:\t\t  programs.  It can be viewed as a simple Point-to-Point or Ethernet\n 398:\t\t  device, which instead of receiving packets from a physical media,\n 399:\t\t  receives them from user space program and instead of sending packets\n 400:\t\t  via physical media writes them to the user space program.\n 401:\t\n 402:\t\t  When a program opens /dev/net/tun, driver creates and registers\n 403:\t\t  corresponding net device tunX or tapX.  After a program closed above\n 404:\t\t  devices, driver will automatically delete tunXX or tapXX device and\n 405:\t\t  all routes corresponding to it.\n 406:\t\n 407:\t\t  Please read \u003cfile:Documentation/networking/tuntap.rst\u003e for more\n 408:\t\t  information.\n 409:\t\n"
}

7/2 2026/10/28 15:52 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 11700
							output: 29
							thoughts: 44
						

Thoughts:
**Investigating the Dependency Chain for `TUN`**

Okay, so `TUN` is definitely dependent on `INET`. That's the starting point. Now, a nagging thought popped into my head – what about `net/core/filter.c`? It feels related, perhaps a key piece in this puzzle. I've got a hunch it plays a role in how this all works together, potentially influencing how the network traffic is filtered or processed for the `TUN` interface.

To confirm and understand the build process of `filter.c` in relation to everything, I need to check the `net/core/Makefile`. That's where I can trace how `filter.c` is compiled and linked. This should reveal how it’s integrated with `TUN` and `INET`, if at all. This should provide some solid information.



8/2 2026/10/28 15:52 tool grepper
0m Args:
{
  "Expression": "filter\\.o",
  "PathPrefixes": [
    "net/core/Makefile"
  ]
}

Results:
{
  "Output": "net/core/Makefile=12=obj-y\t\t     += dev.o dev_api.o dev_addr_lists.o dst.o netevent.o \\\nnet/core/Makefile:13:\t\t\tneighbour.o rtnetlink.o utils.o link_watch.o filter.o \\\nnet/core/Makefile-14-\t\t\tsock_diag.o dev_ioctl.o tso.o sock_reuseport.o \\\n"
}

9/2 2026/10/28 15:52 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 12064
							output: 31
							thoughts: 131
						

Thoughts:
**My Configuration Dependency Analysis**

Okay, so I'm looking at how these kernel configurations relate to each other, specifically concerning networking features. It seems pretty straightforward: `filter.o` *always* gets built into `net/core` if `CONFIG_NET` (networking) is active. That makes sense, a foundational component. And then, `CONFIG_TUN` *absolutely* requires `CONFIG_INET` (the internet protocol suite) to function. So, based on that direct dependency, my "EnableConfigs" list, the configurations I *must* enable to support a certain feature set, should definitely include "TUN" and "INET." No surprises there, really.

Now, I need to dig deeper, since I'm concerned about BPF (Berkeley Packet Filter) configurations, and how they might relate. I need to make sure I don't miss anything. Particularly, I need to check if `bpf_prog_validate_classic` in `net/core/filter.c` has any *specific* configuration dependencies of its own. Let's see... looking at the source, I see `bpf_check_basics_ok` and `bpf_check_classic` are both defined in `net/core/filter.c`. I need to review those definitions to get the full picture. My goal is to be thorough.



10/2 2026/10/28 15:53 tool grepper
0m Args:
{
  "Expression": "bpf_check_classic",
  "PathPrefixes": [
    "net/core/"
  ]
}

Results:
{
  "Output": "net/core/filter.c-16- * Andi Kleen - Fix a few bad bugs and races.\nnet/core/filter.c:17: * Kris Katterjohn - Added many additional checks in bpf_check_classic()\nnet/core/filter.c-18- */\n--\nnet/core/filter.c=1054=static bool bpf_check_basics_ok(const struct sock_filter *filter,\n--\nnet/core/filter.c-1065-/**\nnet/core/filter.c:1066: *\tbpf_check_classic - verify socket filter code\nnet/core/filter.c-1067- *\t@filter: filter to verify\n--\nnet/core/filter.c-1078- */\nnet/core/filter.c:1079:static int bpf_check_classic(const struct sock_filter *filter,\nnet/core/filter.c-1080-\t\t\t     unsigned int flen)\n--\nnet/core/filter.c=1336=static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,\n--\nnet/core/filter.c-1343-\nnet/core/filter.c:1344:\terr = bpf_check_classic(fp-\u003einsns, fp-\u003elen);\nnet/core/filter.c-1345-\tif (err) {\n--\nnet/core/filter.c=1477=EXPORT_SYMBOL_GPL(bpf_prog_destroy);\n--\nnet/core/filter.c-1483- *\nnet/core/filter.c:1484: * Run bpf_check_basics_ok() and bpf_check_classic() on a program that is\nnet/core/filter.c-1485- * already in kernel memory, without building a struct bpf_prog.  These are\n--\nnet/core/filter.c=1496=int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len)\n--\nnet/core/filter.c-1500-\nnet/core/filter.c:1501:\treturn bpf_check_classic(insns, len);\nnet/core/filter.c-1502-}\n"
}

11/2 2026/10/28 15:53 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 13082
							output: 33
							thoughts: 458
						

Thoughts:
**Focusing on Key Symbols for Fuzzing**

Okay, so `bpf_check_classic` is right there in `net/core/filter.c`. Now, the important bit: the `FocusSymbols` list. What exactly *should* be in this thing? Remember Section 4: it's all about guiding the fuzzer effectively. We need to avoid the ubiquitous functions – those memory allocators, deallocators, core synchronization primitives, the common stuff. The goal is to pinpoint the logic that *we're* actually changing. We need to target specific feature logic, entry points, and, crucially, how to handle those static inline functions hiding in the headers. Compiler inlining makes them invisible, so we need to find their non-static, calling functions.

Alright, let's see what the diff has given us. In `net/core/filter.c`, we have `bpf_prog_validate_classic` and `sk_attach_filter_kern`. Those seem pretty targeted. Then, in `drivers/net/tun.c`: there's `tun_copy_filter`, `tun_attach_filter`, `tun_detach_filter`, and `tun_attach` and `tun_free_netdev`. Also, there's `__tun_chr_ioctl`.

Hold on, though. Those `tun_*` functions in `tun.c` are all marked static. So, are they inlined, or do they have distinct symbols? Let's check the code around `tun_chr_ioctl` and `__tun_chr_ioctl` in `tun.c` and make sure. 



12/2 2026/10/28 15:53 tool grepper
0m Args:
{
  "Expression": "tun_chr_ioctl",
  "PathPrefixes": [
    "drivers/net/tun.c"
  ]
}

Results:
{
  "Output": "drivers/net/tun.c=3251=static unsigned char tun_get_addr_len(unsigned short type)\n--\ndrivers/net/tun.c-3288-\ndrivers/net/tun.c:3289:static long __tun_chr_ioctl(struct file *file, unsigned int cmd,\ndrivers/net/tun.c-3290-\t\t\t    unsigned long arg, int ifreq_len)\n--\ndrivers/net/tun.c-3365-\ndrivers/net/tun.c:3366:\tnetif_info(tun, drv, tun-\u003edev, \"tun_chr_ioctl cmd %u\\n\", cmd);\ndrivers/net/tun.c-3367-\n--\ndrivers/net/tun.c-3596-\ndrivers/net/tun.c:3597:static long tun_chr_ioctl(struct file *file,\ndrivers/net/tun.c-3598-\t\t\t  unsigned int cmd, unsigned long arg)\ndrivers/net/tun.c-3599-{\ndrivers/net/tun.c:3600:\treturn __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));\ndrivers/net/tun.c-3601-}\n--\ndrivers/net/tun.c=3604=static long tun_chr_compat_ioctl(struct file *file,\n--\ndrivers/net/tun.c-3627-\t */\ndrivers/net/tun.c:3628:\treturn __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));\ndrivers/net/tun.c-3629-}\n--\ndrivers/net/tun.c=3726=static const struct file_operations tun_fops = {\n--\ndrivers/net/tun.c-3730-\t.poll\t= tun_chr_poll,\ndrivers/net/tun.c:3731:\t.unlocked_ioctl\t= tun_chr_ioctl,\ndrivers/net/tun.c-3732-#ifdef CONFIG_COMPAT\n"
}

13/2 2026/10/28 15:53 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 14444
							output: 32
							thoughts: 38
						

Thoughts:
Here's my analysis of the provided information, as though it were my train of thought:

**Diving into the `tun_chr_ioctl` Mystery**

Alright, so `tun_chr_ioctl`… that's the function handling the `ioctl` calls for the TUN/TAP device nodes, specifically the `/dev/net/tun` interface. It's either a main entry point for those ioctl commands or it's not a static function, which means it's called from somewhere else. That's a key distinction and something I need to understand. Let me see where `tun_attach` is used. I'll check its call chain to get a better understanding of how `tun_chr_ioctl` is called.



14/2 2026/10/28 15:53 tool grepper
0m Args:
{
  "Expression": "tun_attach\\(",
  "PathPrefixes": [
    "drivers/net/tun.c"
  ]
}

Results:
{
  "Output": "drivers/net/tun.c=736=static int tun_copy_filter(struct sock_fprog __user *argp,\n--\ndrivers/net/tun.c-783-\ndrivers/net/tun.c:784:static int tun_attach(struct tun_struct *tun, struct file *file,\ndrivers/net/tun.c-785-\t\t      bool skip_filter, bool napi, bool napi_frags,\n--\ndrivers/net/tun.c=1021=static int tun_net_init(struct net_device *dev)\n--\ndrivers/net/tun.c-1049-\tINIT_LIST_HEAD(\u0026tun-\u003edisabled);\ndrivers/net/tun.c:1050:\terr = tun_attach(tun, tun-\u003efile, false, ifr-\u003eifr_flags \u0026 IFF_NAPI,\ndrivers/net/tun.c-1051-\t\t\t ifr-\u003eifr_flags \u0026 IFF_NAPI_FRAGS, false);\n--\ndrivers/net/tun.c=2920=static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)\n--\ndrivers/net/tun.c-2960-\ndrivers/net/tun.c:2961:\t\terr = tun_attach(tun, file, ifr-\u003eifr_flags \u0026 IFF_NOFILTER,\ndrivers/net/tun.c-2962-\t\t\t\t ifr-\u003eifr_flags \u0026 IFF_NAPI,\n--\ndrivers/net/tun.c=3194=static int tun_set_queue(struct file *file, struct ifreq *ifr)\n--\ndrivers/net/tun.c-3210-\t\t\tgoto unlock;\ndrivers/net/tun.c:3211:\t\tret = tun_attach(tun, file, false, tun-\u003eflags \u0026 IFF_NAPI,\ndrivers/net/tun.c-3212-\t\t\t\t tun-\u003eflags \u0026 IFF_NAPI_FRAGS, true);\n"
}

15/2 2026/10/28 15:53 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 15170
							output: 36
							thoughts: 21
						

Thoughts:
Here's my take on those thoughts:

**Analyzing `tun_attach_filter` and `tun_detach_filter` Callers**

Okay, time to dig into the `tun_attach_filter` and `tun_detach_filter` functions. I need to understand their usage patterns, who's calling them, and in what context. This gives me a picture of the control plane interactions and helps to identify potential bottlenecks, security vulnerabilities, or areas ripe for optimization within the TUN device driver. I'm looking for where these filters are being applied, removed, and what impact that's having on the data path. Let's see what the code reveals.



16/2 2026/10/28 15:53 tool grepper
0m Args:
{
  "Expression": "tun_(attach|detach)_filter\\(",
  "PathPrefixes": [
    "drivers/net/tun.c"
  ]
}

Results:
{
  "Output": "drivers/net/tun.c=3076=static int set_offload(struct tun_struct *tun, unsigned long arg)\n--\ndrivers/net/tun.c-3129-\ndrivers/net/tun.c:3130:static void tun_detach_filter(struct tun_struct *tun, int n)\ndrivers/net/tun.c-3131-{\n--\ndrivers/net/tun.c-3147-\ndrivers/net/tun.c:3148:static int tun_attach_filter(struct tun_struct *tun,\ndrivers/net/tun.c-3149-\t\t\t     struct sock_fprog_kern *fprog_kern)\n--\ndrivers/net/tun.c=3289=static long __tun_chr_ioctl(struct file *file, unsigned int cmd,\n--\ndrivers/net/tun.c-3526-\ndrivers/net/tun.c:3527:\t\tret = tun_attach_filter(tun, \u0026fprog_kern);\ndrivers/net/tun.c-3528-\t\tif (ret) {\n--\ndrivers/net/tun.c-3545-\t\tret = 0;\ndrivers/net/tun.c:3546:\t\ttun_detach_filter(tun, tun-\u003enumqueues);\ndrivers/net/tun.c-3547-\t\tbreak;\n"
}

17/2 2026/10/28 15:53 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 15717
							output: 210
							thoughts: 1307
						

Thoughts:
**Analysis of TUN Device Filter Implementation for Fuzzing**

Okay, let's break this down for fuzzing. `bpf_prog_validate_classic` and `sk_attach_filter_kern` are clearly exported symbols, so those are easy targets. `tun_attach` looks like a key function in `tun.c` (and it's a decent size), so it's probably not inlined and worth including, though, if inlined, it's called by the ioctls, which we'll include, too. The `tun_chr_ioctl` is the ioctl handler, so it's definitely in scope. I also see `tun_attach_filter`, `tun_detach_filter`, and `tun_copy_filter` as static functions in `tun.c`. Static functions *can* have symbols in the vmlinux if not inlined. In order to ensure full coverage, even in the inlined case, these are all critical alongside the entrypoints.

So, the critical functions to focus on are:
*   `sk_attach_filter_kern`
*   `bpf_prog_validate_classic`
*   `tun_copy_filter`
*   `tun_attach_filter`
*   `tun_detach_filter`
*   `tun_attach`
*   `tun_chr_ioctl`

Let's quickly check `tun_free_netdev`. Ah, that's the `ndo_init` / `priv_destructor` – good to know. Now, is `tun_chr_ioctl` ubiquitous? No, it's only for `/dev/net/tun` ioctls. Same for `sk_attach_filter_kern` and `bpf_prog_validate_classic` which is newly introduced and called from `tun_copy_filter`. `tun_set_iff` is that ioctl for creating/attaching tun devices, and `tun_set_queue` attaches queues to multi-queue tun devices.

Let's carefully review the patch changes. Okay, in `drivers/net/tun.c`:

1.  `tun_copy_filter`: Allocates memory, copies the instructions from userspace, runs validation (`bpf_prog_validate_classic`), and saves the filter to `fprog` and `fprog_kern`.
2.  `tun_attach`: Calls `sk_attach_filter_kern` now. It rolls back filter changes via `sk_detach_filter` if problems occur.
3.  `tun_free_netdev`: Frees the `tun->fprog_kern.filter`.
4.  `tun_detach_filter`: Frees `tun->fprog_kern.filter` and resets `tun->fprog_kern`.
5.  `tun_attach_filter`: Takes a `struct sock_fprog_kern *fprog_kern`, calls `sk_attach_filter_kern` and rolls back any failures from prior queues.
6.  `__tun_chr_ioctl`: Inside `TUNATTACHFILTER`, calls `tun_copy_filter`, then `tun_attach_filter`, updating `tun->fprog_kern` and `tun->fprog`.

In `include/linux/filter.h`: `bpf_prog_validate_classic` and `sk_attach_filter_kern` are exported.

And in `net/core/filter.c`: `bpf_prog_validate_classic` runs validation and `sk_attach_filter_kern` attaches the BPF filter from kernel memory.

The selftests in `tools/testing/selftests/net/tun.c` are also testing reattaching filters, as well as several edge cases, which is very helpful.

Is this fuzz-worthy? Absolutely!
*   TUN is easily accessible in standard environments like QEMU/GCE via `/dev/net/tun`.
*   It impacts filter allocation, copying, validation, attachment, rollback, and destruction in the core TUN driver and network filtering code.
*   Syzkaller has descriptions for related tun ioctls.
*   This introduces prime opportunity to discover memory safety issues, refcount bugs, error path bugs, or race conditions.

Let's double-check the `FocusSymbols`:
*   `tun_copy_filter`
*   `tun_attach_filter`
*   `tun_detach_filter`
*   `tun_attach`
*   `sk_attach_filter_kern`
*   `bpf_prog_validate_classic`
*   `tun_chr_ioctl`

And the configurations:
*   `CONFIG_TUN` ("TUN")
*   


18/2 2026/10/28 15:53 tool set-results
0m Args:
{
  "EnableConfigs": [
    "TUN",
    "INET"
  ],
  "FocusSymbols": [
    "tun_copy_filter",
    "tun_attach_filter",
    "tun_detach_filter",
    "tun_attach",
    "sk_attach_filter_kern",
    "bpf_prog_validate_classic",
    "tun_chr_ioctl"
  ],
  "Reasoning": "The patch modifies the TUN/TAP driver and core socket filter subsystem to copy classic BPF socket filters into kernel memory and validate them upon TUNATTACHFILTER ioctl, allowing persistent filters across queue reattachments. It adds kernel helpers sk_attach_filter_kern() and bpf_prog_validate_classic(), and updates tun_attach(), tun_attach_filter(), and error-handling/rollback paths. This code is fully reachable via standard /dev/net/tun ioctl operations and warrants fuzzing for memory safety, concurrency, and rollback edge cases.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "TUN",
    "INET"
  ],
  "FocusSymbols": [
    "tun_copy_filter",
    "tun_attach_filter",
    "tun_detach_filter",
    "tun_attach",
    "sk_attach_filter_kern",
    "bpf_prog_validate_classic",
    "tun_chr_ioctl"
  ],
  "Reasoning": "The patch modifies the TUN/TAP driver and core socket filter subsystem to copy classic BPF socket filters into kernel memory and validate them upon TUNATTACHFILTER ioctl, allowing persistent filters across queue reattachments. It adds kernel helpers sk_attach_filter_kern() and bpf_prog_validate_classic(), and updates tun_attach(), tun_attach_filter(), and error-handling/rollback paths. This code is fully reachable via standard /dev/net/tun ioctl operations and warrants fuzzing for memory safety, concurrency, and rollback edge cases.",
  "WorthFuzzing": true
}

19/1 2026/10/28 15:53 action If
1m Args:
{
  "WorthFuzzing": true
}

Results:
null

20/2 2026/10/28 15:53 agent kmsan-evaluator
1m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch introduces a kernel stack information leak to userspace through structure padding.\n\nSpecifically:\n1. On 64-bit architectures like amd64, `struct sock_fprog` consists of an `unsigned short len` (2 bytes), followed by 6 bytes of structure alignment padding, and a pointer `filter` (8 bytes), totaling 16 bytes.\n2. In `__tun_chr_ioctl` for `TUNATTACHFILTER`, `struct sock_fprog fprog;` is allocated on the kernel stack without zero-initialization.\n3. In `tun_copy_filter()`, only the individual fields are assigned (`fprog-\u003elen = fp.len;` and `fprog-\u003efilter = fp.filter;`), leaving the 6 bytes of padding uninitialized stack memory.\n4. The entire struct is copied to the heap structure (`tun-\u003efprog = fprog;`), retaining the uninitialized padding bytes.\n5. In the `TUNGETFILTER` ioctl, `copy_to_user(argp, \u0026tun-\u003efprog, sizeof(tun-\u003efprog))` copies all 16 bytes to userspace, leaking 6 uninitialized bytes of kernel stack data. Prior to this patch, `TUNATTACHFILTER` directly did `copy_from_user(\u0026tun-\u003efprog, argp, sizeof(tun-\u003efprog))`, fully initializing all 16 bytes.\n\nKASAN only monitors memory bounds, use-after-free, and validity, and cannot detect reads or user copies of uninitialized memory. KMSAN tracks initialization at the byte level and checks for uninitialized bytes in `copy_to_user()`, making it uniquely suited to detect this vulnerability.",
  "NeedsKMSAN": true
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 2a3430a0ba742101ec930c293df26c9c26409ea7
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Oct 28 15:52:14 2026 +0000

    syz-cluster: applied patch under review

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 439565a264ee79..ea6743e8ae6005 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -197,6 +197,7 @@ struct tun_struct {
 	int			sndbuf;
 	struct tap_filter	txflt;
 	struct sock_fprog	fprog;
+	struct sock_fprog_kern	fprog_kern;
 	/* protected by rtnl lock */
 	bool			filter_attached;
 	u32			msg_enable;
@@ -726,12 +727,67 @@ static void tun_force_wake_queue(struct tun_struct *tun,
 	spin_unlock_bh(&tfile->tx_ring.consumer_lock);
 }
 
+/* Copy the filter that @argp points at into the kernel, so that it can be
+ * installed again later, independent of the ioctl caller's address space.
+ * @fprog_kern receives the copy, which belongs to the caller until it is
+ * installed or freed, and @fprog receives the header, with the user pointer
+ * kept for TUNGETFILTER.
+ */
+static int tun_copy_filter(struct sock_fprog __user *argp,
+			   struct sock_fprog *fprog,
+			   struct sock_fprog_kern *fprog_kern)
+{
+	struct sock_fprog fp;
+	struct sock_filter *insns;
+	int err;
+
+	if (copy_from_user(&fp, argp, sizeof(fp)))
+		return -EFAULT;
+
+	if (!fp.len || fp.len > BPF_MAXINSNS)
+		return -EINVAL;
+
+	insns = kmalloc_array(fp.len, sizeof(struct sock_filter),
+			      GFP_KERNEL_ACCOUNT);
+	if (!insns)
+		return -ENOMEM;
+
+	if (copy_from_user(insns, fp.filter,
+			   fp.len * sizeof(struct sock_filter))) {
+		kfree(insns);
+		return -EFAULT;
+	}
+
+	/* The instructions are only verified when a program is built from
+	 * them, which happens when the filter is attached to a socket. Run
+	 * the classic verifier here as well, so that a bad program is
+	 * refused before it is saved and before any queue is touched.
+	 * bpf_prog_create() can still refuse the program later, when it is
+	 * converted to eBPF or when no runtime can be selected for it; the
+	 * attach path returns that error, and the saved program is not
+	 * replaced.
+	 */
+	err = bpf_prog_validate_classic(insns, fp.len);
+	if (err) {
+		kfree(insns);
+		return err;
+	}
+
+	fprog->len = fp.len;
+	fprog->filter = fp.filter;
+	fprog_kern->len = fp.len;
+	fprog_kern->filter = insns;
+
+	return 0;
+}
+
 static int tun_attach(struct tun_struct *tun, struct file *file,
 		      bool skip_filter, bool napi, bool napi_frags,
 		      bool publish_tun)
 {
 	struct tun_file *tfile = file->private_data;
 	struct net_device *dev = tun->dev;
+	bool rollback_filter = false;
 	int err;
 
 	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
@@ -756,10 +812,11 @@ static int tun_attach(struct tun_struct *tun, struct file *file,
 	/* Re-attach the filter to persist device */
 	if (!skip_filter && (tun->filter_attached == true)) {
 		lock_sock(tfile->socket.sk);
-		err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
+		err = sk_attach_filter_kern(&tun->fprog_kern, tfile->socket.sk);
 		release_sock(tfile->socket.sk);
-		if (!err)
+		if (err)
 			goto out;
+		rollback_filter = true;
 	}
 
 	if (!tfile->detached &&
@@ -821,6 +878,17 @@ static int tun_attach(struct tun_struct *tun, struct file *file,
 	WRITE_ONCE(tun->numqueues, tun->numqueues + 1);
 	tun_set_real_num_queues(tun);
 out:
+	if (err && rollback_filter) {
+		/* The filter was installed before the rest of the attach
+		 * ran, so drop it again if a later step failed. Any
+		 * program it replaced is already gone: this leaves the
+		 * queue without a filter rather than restoring the
+		 * previous one.
+		 */
+		lock_sock(tfile->socket.sk);
+		sk_detach_filter(tfile->socket.sk);
+		release_sock(tfile->socket.sk);
+	}
 	return err;
 }
 
@@ -2406,6 +2474,7 @@ static void tun_free_netdev(struct net_device *dev)
 	security_tun_dev_free_security(tun->security);
 	__tun_set_ebpf(tun, &tun->steering_prog, NULL);
 	__tun_set_ebpf(tun, &tun->filter_prog, NULL);
+	kfree(tun->fprog_kern.filter);
 }
 
 static void tun_setup(struct net_device *dev)
@@ -3070,10 +3139,14 @@ static void tun_detach_filter(struct tun_struct *tun, int n)
 		release_sock(tfile->socket.sk);
 	}
 
+	kfree(tun->fprog_kern.filter);
+	tun->fprog_kern.filter = NULL;
+	tun->fprog_kern.len = 0;
 	tun->filter_attached = false;
 }
 
-static int tun_attach_filter(struct tun_struct *tun)
+static int tun_attach_filter(struct tun_struct *tun,
+			     struct sock_fprog_kern *fprog_kern)
 {
 	int i, ret = 0;
 	struct tun_file *tfile;
@@ -3081,15 +3154,29 @@ static int tun_attach_filter(struct tun_struct *tun)
 	for (i = 0; i < tun->numqueues; i++) {
 		tfile = rtnl_dereference(tun->tfiles[i]);
 		lock_sock(tfile->socket.sk);
-		ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
+		ret = sk_attach_filter_kern(fprog_kern, tfile->socket.sk);
 		release_sock(tfile->socket.sk);
-		if (ret) {
-			tun_detach_filter(tun, i);
-			return ret;
-		}
+		if (ret)
+			goto out_detach;
 	}
 
 	tun->filter_attached = true;
+	return 0;
+
+out_detach:
+	/* Drop the copies already installed on queues 0..i-1, which leaves
+	 * them without a filter: their previous sk_filter, if any, was
+	 * already replaced and released. The caller does not replace the
+	 * saved program when this fails, so tun->fprog, tun->fprog_kern and
+	 * filter_attached keep their previous values.
+	 */
+	while (i--) {
+		tfile = rtnl_dereference(tun->tfiles[i]);
+		lock_sock(tfile->socket.sk);
+		sk_detach_filter(tfile->socket.sk);
+		release_sock(tfile->socket.sk);
+	}
+
 	return ret;
 }
 
@@ -3424,17 +3511,31 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
 		tun_set_sndbuf(tun);
 		break;
 
-	case TUNATTACHFILTER:
+	case TUNATTACHFILTER: {
+		struct sock_fprog_kern fprog_kern;
+		struct sock_fprog fprog;
+
 		/* Can be set only for TAPs */
 		ret = -EINVAL;
 		if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
 			break;
-		ret = -EFAULT;
-		if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
+
+		ret = tun_copy_filter(argp, &fprog, &fprog_kern);
+		if (ret)
+			break;
+
+		ret = tun_attach_filter(tun, &fprog_kern);
+		if (ret) {
+			kfree(fprog_kern.filter);
 			break;
+		}
 
-		ret = tun_attach_filter(tun);
+		/* Install the copy only after every queue accepted it. */
+		kfree(tun->fprog_kern.filter);
+		tun->fprog_kern = fprog_kern;
+		tun->fprog = fprog;
 		break;
+	}
 
 	case TUNDETACHFILTER:
 		/* Can be set only for TAPs */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index e0218e677a1dcd..7169c03610b445 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1257,8 +1257,10 @@ int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
 			      bpf_aux_classic_check_t trans, bool save_orig);
 void bpf_prog_destroy(struct bpf_prog *fp);
+int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len);
 
 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
+int sk_attach_filter_kern(struct sock_fprog_kern *fprog, struct sock *sk);
 int sk_attach_bpf(u32 ufd, struct sock *sk);
 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk);
 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk);
diff --git a/net/core/filter.c b/net/core/filter.c
index b7ac145b68b1f8..61b2d223c44563 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -1476,6 +1476,32 @@ void bpf_prog_destroy(struct bpf_prog *fp)
 }
 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
 
+/**
+ * bpf_prog_validate_classic - run the classic BPF verifier on kernel memory
+ * @insns: instruction array
+ * @len: number of instructions in @insns
+ *
+ * Run bpf_check_basics_ok() and bpf_check_classic() on a program that is
+ * already in kernel memory, without building a struct bpf_prog.  These are
+ * the checks bpf_prog_create() runs before it converts the program, so
+ * callers can reject a bad program when it is configured instead of when it
+ * is first attached to a socket.
+ *
+ * A program that passes here can still be refused by bpf_prog_create(), when
+ * it is converted to eBPF or when no runtime can be selected for it.
+ *
+ * Return: 0 if the program passes the classic verifier, -EINVAL if it does
+ * not, or -ENOMEM if the verifier cannot allocate its state.
+ */
+int bpf_prog_validate_classic(const struct sock_filter *insns, unsigned int len)
+{
+	if (!bpf_check_basics_ok(insns, len))
+		return -EINVAL;
+
+	return bpf_check_classic(insns, len);
+}
+EXPORT_SYMBOL_GPL(bpf_prog_validate_classic);
+
 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
 {
 	struct sk_filter *fp, *old_fp;
@@ -1567,6 +1593,47 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(sk_attach_filter);
 
+/**
+ * sk_attach_filter_kern - attach a classic BPF filter held in kernel memory
+ * @fprog: program to attach, with @fprog->filter pointing at kernel memory
+ * @sk: socket to attach the filter to
+ *
+ * Attach a copy of a classic BPF program that the kernel already holds, the
+ * way sk_attach_filter() does for one that still lives in user space. The
+ * caller must hold the socket lock; SOCK_FILTER_LOCKED is checked here.
+ *
+ * The program is built by bpf_prog_create(), which does not keep an original
+ * copy of the instructions, so sk_get_filter() returns -EACCES and sock_diag
+ * omits the filter for a socket that uses this helper.
+ *
+ * Return: 0 on success, or a negative errno: -EPERM if @sk has
+ * SOCK_FILTER_LOCKED set, otherwise whatever bpf_prog_create() or
+ * __sk_attach_prog() returned (for example -EINVAL for an invalid classic
+ * BPF program, -ENOMEM, or -ENOTSUPP when a JIT is required but is not
+ * available).
+ */
+int sk_attach_filter_kern(struct sock_fprog_kern *fprog, struct sock *sk)
+{
+	struct bpf_prog *prog;
+	int err;
+
+	if (sock_flag(sk, SOCK_FILTER_LOCKED))
+		return -EPERM;
+
+	err = bpf_prog_create(&prog, fprog);
+	if (err)
+		return err;
+
+	err = __sk_attach_prog(prog, sk);
+	if (err < 0) {
+		__bpf_prog_release(prog);
+		return err;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(sk_attach_filter_kern);
+
 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
 {
 	struct bpf_prog *prog = __get_filter(fprog, sk);
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 34de2580e32eca..3730fba859a46e 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -9,9 +9,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <linux/filter.h>
 #include <linux/if_tun.h>
 #include <netinet/tcp.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 #include <sys/socket.h>
 
 #include "kselftest_harness.h"
@@ -548,6 +550,253 @@ TEST_F(tun, reattach_close_delete)
 	EXPECT_EQ(tun_delete(self->ifname), 0);
 }
 
+/* accept: return skb->len.  Three instructions on purpose, so that the
+ * length read back through TUNGETFILTER tells it apart from a rejected
+ * program that is shorter.
+ */
+static const struct sock_filter filter_accept[] = {
+	BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
+	BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0),
+	BPF_STMT(BPF_RET | BPF_A, 0),
+};
+
+/* Put the instructions in an anonymous mapping, so that the test can make the
+ * address unreadable afterwards.
+ */
+static void *filter_alloc(const struct sock_filter *insns, unsigned int len)
+{
+	void *p;
+
+	p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
+		 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (p == MAP_FAILED)
+		return NULL;
+
+	memcpy(p, insns, len * sizeof(*insns));
+
+	return p;
+}
+
+static int filter_attach(int fd, const struct sock_filter *insns, unsigned int len)
+{
+	struct sock_fprog fp = {
+		.len = len,
+		.filter = (struct sock_filter *)insns,
+	};
+
+	return ioctl(fd, TUNATTACHFILTER, (void *)&fp);
+}
+
+static int filter_get(int fd, struct sock_fprog *fp)
+{
+	return ioctl(fd, TUNGETFILTER, (void *)fp);
+}
+
+static int tun_get_iff_flags(int fd)
+{
+	struct ifreq ifr = { 0 };
+
+	if (ioctl(fd, TUNGETIFF, (void *)&ifr) < 0)
+		return -1;
+
+	return ifr.ifr_flags;
+}
+
+/*
+ * A queue can be attached long after the filter was configured, from a
+ * process that does not necessarily map the buffer the program was copied
+ * from, so the kernel has to keep its own copy of the program.  Here the
+ * mapping is made unreadable before the queue is attached again, and a
+ * second queue is then attached from a fresh socket, which starts without a
+ * filter of its own.
+ */
+TEST_F(tun, reattach_filter_without_user_buffer)
+{
+	struct sock_fprog gf = { 0 };
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	void *prog;
+	int fd, ret;
+
+	prog = filter_alloc(filter_accept, ARRAY_SIZE(filter_accept));
+	ASSERT_NE(prog, NULL);
+	ASSERT_EQ(filter_attach(self->fd, prog, ARRAY_SIZE(filter_accept)), 0);
+
+	EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
+
+	/* This memory is no longer readable */
+	ASSERT_EQ(mprotect(prog, getpagesize(), PROT_NONE), 0);
+
+	ret = tun_attach(self->fd, self->ifname);
+	EXPECT_EQ(ret, 0);
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	/* The queue is attached again, not left disabled */
+	EXPECT_EQ(flags & IFF_DETACH_QUEUE, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	/* The two checks below document the expected state, they cannot
+	 * tell a reinstalled filter from no filter: tun_detach() leaves
+	 * sk_filter alone, and TUNGETFILTER only returns the header.
+	 */
+	EXPECT_EQ(filter_get(self->fd, &gf), 0);
+	EXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));
+
+	/* A new queue starts without a filter, so a clear IFF_NOFILTER shows
+	 * that the filter was installed again from the kernel copy.
+	 */
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	close(fd);
+
+	ASSERT_EQ(mprotect(prog, getpagesize(), PROT_READ | PROT_WRITE), 0);
+	ASSERT_EQ(munmap(prog, getpagesize()), 0);
+}
+
+/* A new queue attached with IFF_NOFILTER must not get the filter that is
+ * configured on the device.
+ */
+TEST_F(tun, attach_filter_nofilter_flag)
+{
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	int fd;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept, ARRAY_SIZE(filter_accept)), 0);
+
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE | IFF_NOFILTER;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, IFF_NOFILTER);
+
+	close(fd);
+}
+
+/* After TUNDETACHFILTER a later attach must not install the filter again */
+TEST_F(tun, detach_filter_clears_reattach)
+{
+	short flags = 0;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept, ARRAY_SIZE(filter_accept)), 0);
+	EXPECT_EQ(ioctl(self->fd, TUNDETACHFILTER, 0), 0);
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, IFF_NOFILTER);
+
+	EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
+	EXPECT_EQ(tun_attach(self->fd, self->ifname), 0);
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, IFF_NOFILTER);
+}
+
+/* A TUNATTACHFILTER with a bad length must leave the installed program and the
+ * filter state that TUNGETIFF reports alone.
+ */
+TEST_F(tun, attach_filter_bad_len_keeps_program)
+{
+	struct sock_fprog gf = { 0 };
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	int fd;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept, ARRAY_SIZE(filter_accept)), 0);
+
+	errno = 0;
+	EXPECT_EQ(filter_attach(self->fd, filter_accept, 0), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	EXPECT_EQ(filter_get(self->fd, &gf), 0);
+	EXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));
+
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	/* A queue attached afterwards still gets the filter, so the rejected
+	 * ioctl left the saved program and filter_attached alone.
+	 */
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	close(fd);
+}
+
+/* A TUNATTACHFILTER with instructions that bpf_check_classic() rejects must
+ * be refused, and must leave the program installed already in place.
+ */
+TEST_F(tun, attach_filter_bad_prog_keeps_program)
+{
+	struct sock_fprog gf = { 0 };
+	struct sock_filter bad[] = {
+		BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
+		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
+	};
+	struct ifreq ifr = { 0 };
+	short flags = 0;
+	int fd;
+
+	ASSERT_EQ(filter_attach(self->fd, filter_accept,
+				ARRAY_SIZE(filter_accept)), 0);
+
+	errno = 0;
+	EXPECT_EQ(filter_attach(self->fd, bad, ARRAY_SIZE(bad)), -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	/* the program attached before is untouched */
+	EXPECT_EQ(filter_get(self->fd, &gf), 0);
+	EXPECT_EQ(gf.len, ARRAY_SIZE(filter_accept));
+
+	/* IFF_NOFILTER comes from the socket's sk_filter, not from the saved
+	 * header, so a clear flag shows the filter is still installed.
+	 */
+	flags = tun_get_iff_flags(self->fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	/* A queue attached afterwards still gets the filter, so the rejected
+	 * program never replaced the saved one.
+	 */
+	fd = open("/dev/net/tun", O_RDWR);
+	ASSERT_GE(fd, 0);
+
+	strcpy(ifr.ifr_name, self->ifname);
+	ifr.ifr_flags = IFF_TAP | IFF_MULTI_QUEUE;
+	EXPECT_GE(ioctl(fd, TUNSETIFF, (void *)&ifr), 0);
+
+	flags = tun_get_iff_flags(fd);
+	EXPECT_GE(flags, 0);
+	EXPECT_EQ(flags & IFF_NOFILTER, 0);
+
+	close(fd);
+}
+
 FIXTURE(tun_vnet_gso)
 {
 	char ifname[IFNAMSIZ];

21/3 2026/10/28 15:53 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 9029
							output: 31
							thoughts: 116
						

Thoughts:
Okay, let's break this down.

**Evaluating the KMSAN Fuzzing Justification**

I need to quickly assess if this patch series warrants firing up a dedicated KMSAN fuzzing session. First, a quick code dive is in order. It looks like this touches a few key areas: `drivers/net/tun.c`, `include/linux/filter.h`, `net/core/filter.c`, and a self-test in `tools/testing/selftests/net/tun.c`. I'll want to focus my attention on the `drivers/net/tun.c` file, specifically around the `TUNATTACHFILTER`, `TUNGETFILTER`, and `tun_copy_filter` functions. My gut tells me this area has potential for interesting interactions, so let's start there.



22/3 2026/10/28 15:53 tool grepper
0m Args:
{
  "Expression": "TUNGETFILTER",
  "PathPrefixes": [
    "drivers/net/tun.c"
  ]
}

Results:
{
  "Output": "drivers/net/tun.c=711=static void tun_force_wake_queue(struct tun_struct *tun,\n--\ndrivers/net/tun.c-733- * installed or freed, and @fprog receives the header, with the user pointer\ndrivers/net/tun.c:734: * kept for TUNGETFILTER.\ndrivers/net/tun.c-735- */\n--\ndrivers/net/tun.c=3289=static long __tun_chr_ioctl(struct file *file, unsigned int cmd,\n--\ndrivers/net/tun.c-3548-\ndrivers/net/tun.c:3549:\tcase TUNGETFILTER:\ndrivers/net/tun.c-3550-\t\tret = -EINVAL;\n"
}

23/3 2026/10/28 15:53 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 9549
							output: 36
							thoughts: 27
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Diving into `tun.c` - Line 3549**

Alright, let's get down to brass tacks. I'm going to pull up `drivers/net/tun.c` and zero in on line 3549 using `read-file`. This specific point likely represents a critical piece of the TUN device's functionality. I'm expecting to find something interesting there – perhaps an important data structure access, a crucial function call related to packet handling, or maybe even a subtle race condition that's been nagging at my intuition. The location within the file suggests this is likely related to... well, depending on context, it could be packet processing, device management, or error handling. My gut tells me this could be where the rubber hits the road in terms of either core features or some debugging effort I may have ongoing. I need to get in there and refresh my mental model of this part of the driver. I will be looking out for interactions with the core networking stack, and whether the code addresses some of the recent bug reports that have been circulating. Time to get my hands dirty, and see what the code has to say!



24/3 2026/10/28 15:53 tool read-file
0m Args:
{
  "File": "drivers/net/tun.c",
  "FirstLine": 3540,
  "LineCount": 40
}

Results:
{
  "Contents": "3540:\t\tcase TUNDETACHFILTER:\n3541:\t\t\t/* Can be set only for TAPs */\n3542:\t\t\tret = -EINVAL;\n3543:\t\t\tif ((tun-\u003eflags \u0026 TUN_TYPE_MASK) != IFF_TAP)\n3544:\t\t\t\tbreak;\n3545:\t\t\tret = 0;\n3546:\t\t\ttun_detach_filter(tun, tun-\u003enumqueues);\n3547:\t\t\tbreak;\n3548:\t\n3549:\t\tcase TUNGETFILTER:\n3550:\t\t\tret = -EINVAL;\n3551:\t\t\tif ((tun-\u003eflags \u0026 TUN_TYPE_MASK) != IFF_TAP)\n3552:\t\t\t\tbreak;\n3553:\t\t\tret = -EFAULT;\n3554:\t\t\tif (copy_to_user(argp, \u0026tun-\u003efprog, sizeof(tun-\u003efprog)))\n3555:\t\t\t\tbreak;\n3556:\t\t\tret = 0;\n3557:\t\t\tbreak;\n3558:\t\n3559:\t\tcase TUNSETSTEERINGEBPF:\n3560:\t\t\tret = tun_set_ebpf(tun, \u0026tun-\u003esteering_prog, argp);\n3561:\t\t\tbreak;\n3562:\t\n3563:\t\tcase TUNSETFILTEREBPF:\n3564:\t\t\tret = tun_set_ebpf(tun, \u0026tun-\u003efilter_prog, argp);\n3565:\t\t\tbreak;\n3566:\t\n3567:\t\tcase TUNSETCARRIER:\n3568:\t\t\tret = -EFAULT;\n3569:\t\t\tif (copy_from_user(\u0026carrier, argp, sizeof(carrier)))\n3570:\t\t\t\tgoto unlock;\n3571:\t\n3572:\t\t\tret = tun_net_change_carrier(tun-\u003edev, (bool)carrier);\n3573:\t\t\tbreak;\n3574:\t\n3575:\t\tcase TUNGETDEVNETNS:\n3576:\t\t\tret = -EPERM;\n3577:\t\t\tif (!ns_capable(net-\u003euser_ns, CAP_NET_ADMIN))\n3578:\t\t\t\tgoto unlock;\n3579:\t\t\tret = open_related_ns(\u0026net-\u003ens, get_net_ns);\n"
}

25/3 2026/10/28 15:53 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 10468
							output: 29
							thoughts: 786
						

Thoughts:
**Analyzing the TUN/TAP Filter Handling**

Okay, let's dive into this `tun->fprog` situation. It looks like we're dealing with how BPF filters are managed for TUN/TAP devices, specifically when a user-space application sets or retrieves them. First, I'm checking the `TUNGETFILTER` case, where the kernel copies `tun->fprog` to user-space.  Then I want to look at the differences between the old and new implementation of the `TUNATTACHFILTER` command.

Initially, `TUNATTACHFILTER` would simply `copy_from_user` directly into `tun->fprog`.  Now, it looks like there's a more involved process.  A temporary `struct sock_fprog_kern fprog_kern` is introduced, alongside the `fprog` structure.  I can see that the new implementation copies the filter from user space, validates it, and then installs it. Crucially, the old method directly used the user-provided pointer.

Now, let's get down to brass tacks:  I need to look closely at the `tun_copy_filter` function.  It appears to be handling the copying and validation of the BPF filter from user-space.  It uses `copy_from_user` which is good, but it also allocates kernel memory with `kmalloc_array`.  That is an important detail. It copies the filter instructions themselves and validates them using `bpf_prog_validate_classic`. The function then copies the lengths into two structs: `fprog` and `fprog_kern`. The `fprog_kern` struct is then copied to the tun device, while `fprog` is assigned to `tun->fprog`.

I need to understand what `struct sock_fprog` actually contains. Are there any padding issues I need to be concerned with?  I need to check the definition to make sure I understand how data is passed between kernel and user space.  This is critical to be sure there are no potential vulnerabilities.



26/3 2026/10/28 15:54 tool grepper
0m Args:
{
  "Expression": "struct sock_fprog \\{",
  "PathPrefixes": [
    "include/"
  ]
}

Results:
{
  "Output": "include/uapi/linux/filter.h=24=struct sock_filter {\t/* Filter block */\n--\ninclude/uapi/linux/filter.h-30-\ninclude/uapi/linux/filter.h:31:struct sock_fprog {\t/* Required for SO_ATTACH_FILTER. */\ninclude/uapi/linux/filter.h-32-\tunsigned short\t\tlen;\t/* Number of filter blocks */\n"
}

27/3 2026/10/28 15:54 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 11830
							output: 37
							thoughts: 22
						

Thoughts:
Okay, let's dive into this. Here's my take on those instructions:

**Analyzing `include/uapi/linux/filter.h` around Line 31**

Right, so the task at hand is to scrutinize `include/uapi/linux/filter.h`, specifically around line 31. This suggests we're likely dealing with the definition of BPF (Berkeley Packet Filter) or potentially other packet filtering mechanisms within the Linux kernel. Given my expertise, I'm already anticipating certain structures or macros. I'll be looking for clues on how packet filtering rules are defined, structured, and potentially compiled. I expect this section contains the foundation for defining the instructions a BPF program can take. Things that are of great interest to me are the architecture, endianness, and instruction set. I am looking for the essential parts of any BPF structure, such as the opcode, the source and destination registers, and the immediate value fields for each instruction. I'll be keeping an eye out for any specific constants, enum definitions, or struct declarations. My understanding is that this file is often the source of truth for the user-space interaction with packet filtering capabilities, so this information is invaluable.



28/3 2026/10/28 15:54 tool read-file
0m Args:
{
  "File": "include/uapi/linux/filter.h",
  "FirstLine": 25,
  "LineCount": 20
}

Results:
{
  "Contents": "  25:\t\t__u16\tcode;   /* Actual filter code */\n  26:\t\t__u8\tjt;\t/* Jump true */\n  27:\t\t__u8\tjf;\t/* Jump false */\n  28:\t\t__u32\tk;      /* Generic multiuse field */\n  29:\t};\n  30:\t\n  31:\tstruct sock_fprog {\t/* Required for SO_ATTACH_FILTER. */\n  32:\t\tunsigned short\t\tlen;\t/* Number of filter blocks */\n  33:\t\tstruct sock_filter __user *filter;\n  34:\t};\n  35:\t\n  36:\t/* ret - BPF_K and BPF_X also apply */\n  37:\t#define BPF_RVAL(code)  ((code) \u0026 0x18)\n  38:\t#define         BPF_A           0x10\n  39:\t\n  40:\t/* misc */\n  41:\t#define BPF_MISCOP(code) ((code) \u0026 0xf8)\n  42:\t#define         BPF_TAX         0x00\n  43:\t#define         BPF_TXA         0x80\n  44:\t\n"
}

29/3 2026/10/28 15:54 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 12460
							output: 31
							thoughts: 2096
						

Thoughts:
**Kernel Memory Info-Leak Analysis**

Okay, so I'm diving into this patch, and my initial thought is: *wait a minute... something feels off about how this `struct sock_fprog` is being handled.* Specifically, I'm zeroing in on this interaction between `__tun_chr_ioctl` and `tun_copy_filter`. The core issue looks like this: `struct sock_fprog fprog;` is declared on the stack within `__tun_chr_ioctl` without any initialization. That's a red flag immediately.

Then `tun_copy_filter` is called with that uninitialized `fprog`'s address. Inside `tun_copy_filter`, the code uses `copy_from_user` to initialize a local stack variable `fp`. So we get this user-provided `fp` from user space, which is potentially initialized, but then we don't *copy* all of `fp`'s fields into `fprog`. Instead, `tun_copy_filter` selectively assigns `fprog->len` and `fprog->filter` only. *That's a key detail*.

The architecture here, AMD64, which is the likely target, is crucial. That `struct sock_fprog` is going to have padding between `len` (2 bytes) and `filter` (an 8-byte pointer). *That padding is now uninitialized!* Because of the select assignment, any data already in the memory location becomes part of the `fprog` data structure, specifically in the padding area.

So now back in `__tun_chr_ioctl`, `tun->fprog = fprog;` is executed, and the contents of `fprog` (including that uninitialized padding) are copied into `tun->fprog`.  *This is where the vulnerability lies!*

Fast forward to `TUNGETFILTER`, when the code copies `&tun->fprog` back to user space using `copy_to_user`, the 6 bytes of uninitialized padding are getting leaked. The userspace process can now read data from the kernel stack that it shouldn't have access to, a classic memory information leak.

I checked what was happening before this patch. Before, there was a direct `copy_from_user` from the userspace buffer to `tun->fprog`. *That would have handled the padding correctly*, since `copy_from_user` initializes all the bytes involved in the copy! But now, because of the finer-grained assignment within `tun_copy_filter`, that initialization step is skipped. *This is why this patch introduces the vulnerability.* Even if userspace provided a valid and initialized `fp`, it still would not affect the padding.

Okay, I need to check something else. Let's look at `fprog_kern`. It doesn't look like `struct sock_fprog_kern` is leaking uninitialized memory, but I should double check. The problem is definitely with the uninitialized padding in `fprog` and its subsequent copy to `tun->fprog`.  KMSAN is going to have a field day with this!



30/3 2026/10/28 15:54 tool grepper
0m Args:
{
  "Expression": "struct sock_fprog_kern \\{",
  "PathPrefixes": [
    "include/"
  ]
}

Results:
{
  "Output": "include/linux/filter.h=736=struct compat_sock_fprog {\n--\ninclude/linux/filter.h-740-\ninclude/linux/filter.h:741:struct sock_fprog_kern {\ninclude/linux/filter.h-742-\tu16\t\t\tlen;\n"
}

31/3 2026/10/28 15:54 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 15344
							output: 35
							thoughts: 20
						

Thoughts:
Here's a first-person summary, tailored for an expert audience, based on the provided "thoughts":

**Diving into BPF Program Structure**

Okay, let's pull up the `include/linux/filter.h` header file. Specifically, I need to get my bearings around line 741. I'm guessing that's where we'll find the core data structures that define the shape of BPF programs. I'm going to want to focus on the structure and how it describes the BPF program instructions as well as any auxiliary structures for program metadata. I'm anticipating we'll be looking at things like instruction formats, program contexts, and perhaps some internal representations used by the BPF verifier or JIT compiler. I need to get a clear picture of how this information is represented at a low level.



32/3 2026/10/28 15:54 tool read-file
0m Args:
{
  "File": "include/linux/filter.h",
  "FirstLine": 735,
  "LineCount": 20
}

Results:
{
  "Contents": " 735:\t/* A struct sock_filter is architecture independent. */\n 736:\tstruct compat_sock_fprog {\n 737:\t\tu16\t\tlen;\n 738:\t\tcompat_uptr_t\tfilter;\t/* struct sock_filter * */\n 739:\t};\n 740:\t\n 741:\tstruct sock_fprog_kern {\n 742:\t\tu16\t\t\tlen;\n 743:\t\tstruct sock_filter\t*filter;\n 744:\t};\n 745:\t\n 746:\t/* Some arches need doubleword alignment for their instructions and/or data */\n 747:\t#define BPF_IMAGE_ALIGNMENT 8\n 748:\t\n 749:\tstruct bpf_binary_header {\n 750:\t\tu32 size;\n 751:\t\tu8 image[] __aligned(BPF_IMAGE_ALIGNMENT);\n 752:\t};\n 753:\t\n 754:\tstruct bpf_prog_stats {\n"
}

33/3 2026/10/28 15:54 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 15858
							output: 36
							thoughts: 67
						

Thoughts:
Here's my summary, as though I'm thinking through this process:

**Diving into `bpf_prog_create` and its Interaction with `sock_fprog_kern`**

Okay, so we're looking at `bpf_prog_create(&prog, fprog)`. My initial thought is, what exactly happens under the hood when this function gets called, particularly in relation to the `struct sock_fprog_kern` that’s likely being passed in? I need to understand how the BPF program gets created and initialized from this `fprog` structure.

The next step is clear: let's jump into the code. I need to find the definitive source for `bpf_prog_create`. The best way to do that is to grep for its definition within `net/core/filter.c` since this is the kind of low-level networking code where I'd expect to find it. Time to pull up my terminal and get searching! I'm expecting to find the function definition and then immediately will begin to parse its code, looking for exactly how `struct sock_fprog_kern` is interacted with.



34/3 2026/10/28 15:55 tool grepper
0m Args:
{
  "Expression": "int bpf_prog_create\\(",
  "PathPrefixes": [
    "net/core/filter.c"
  ]
}

Results:
{
  "Output": "net/core/filter.c=1336=static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,\n--\nnet/core/filter.c-1384- */\nnet/core/filter.c:1385:int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)\nnet/core/filter.c-1386-{\n"
}

35/3 2026/10/28 15:55 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 16327
							output: 36
							thoughts: 20
						

Thoughts:
Okay, let's dive into this. Here's my take on those lines of code...

**Delving into `net/core/filter.c` - Line 1385**

Alright, time to get my hands dirty. I'm thinking, let's head over to `net/core/filter.c` and specifically, around line 1385. That's where I need to start my investigation. I'm expecting to find some interesting logic, likely related to packet filtering, perhaps some rule matching, or potentially even some crucial data structure manipulation. Given my experience, I'm already anticipating the typical challenges of debugging network packet processing: concurrency issues, subtle interactions with kernel memory, and the constant need to trace the execution path meticulously. Line 1385… it's almost like a starting point in a treasure hunt! I'm armed with my knowledge of the networking stack and ready to decipher the code's intent. Let's see what secrets it holds.



36/3 2026/10/28 15:55 tool read-file
0m Args:
{
  "File": "net/core/filter.c",
  "FirstLine": 1385,
  "LineCount": 40
}

Results:
{
  "Contents": "1385:\tint bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)\n1386:\t{\n1387:\t\tunsigned int fsize = bpf_classic_proglen(fprog);\n1388:\t\tstruct bpf_prog *fp;\n1389:\t\n1390:\t\t/* Make sure new filter is there and in the right amounts. */\n1391:\t\tif (!bpf_check_basics_ok(fprog-\u003efilter, fprog-\u003elen))\n1392:\t\t\treturn -EINVAL;\n1393:\t\n1394:\t\tfp = bpf_prog_alloc(bpf_prog_size(fprog-\u003elen), 0);\n1395:\t\tif (!fp)\n1396:\t\t\treturn -ENOMEM;\n1397:\t\n1398:\t\tmemcpy(fp-\u003einsns, fprog-\u003efilter, fsize);\n1399:\t\n1400:\t\tfp-\u003elen = fprog-\u003elen;\n1401:\t\t/* Since unattached filters are not copied back to user\n1402:\t\t * space through sk_get_filter(), we do not need to hold\n1403:\t\t * a copy here, and can spare us the work.\n1404:\t\t */\n1405:\t\tfp-\u003eorig_prog = NULL;\n1406:\t\n1407:\t\t/* bpf_prepare_filter() already takes care of freeing\n1408:\t\t * memory in case something goes wrong.\n1409:\t\t */\n1410:\t\tfp = bpf_prepare_filter(fp, NULL);\n1411:\t\tif (IS_ERR(fp))\n1412:\t\t\treturn PTR_ERR(fp);\n1413:\t\n1414:\t\t*pfp = fp;\n1415:\t\treturn 0;\n1416:\t}\n1417:\tEXPORT_SYMBOL_GPL(bpf_prog_create);\n1418:\t\n1419:\t/**\n1420:\t *\tbpf_prog_create_from_user - create an unattached filter from user buffer\n1421:\t *\t@pfp: the unattached filter that is created\n1422:\t *\t@fprog: the filter program\n1423:\t *\t@trans: post-classic verifier transformation handler\n1424:\t *\t@save_orig: save classic BPF program\n"
}

37/3 2026/10/28 15:55 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 17230
							output: 388
							thoughts: 2401
						

Thoughts:
**Analysis of Kernel Memory Leak Vulnerability in `TUNGETFILTER` IOCTL Handler**

Okay, let's break this down. We're looking at `TUNGETFILTER` within the `__tun_chr_ioctl` function. Previously, the `TUNATTACHFILTER` path used `copy_from_user` directly into `tun->fprog`, which would fully initialize the `struct sock_fprog` structure.  Now, the patch introduces a `tun_copy_filter` function and a local stack variable, `struct sock_fprog fprog;` in `__tun_chr_ioctl`. The structure itself, on a 64-bit architecture, is structured like this: `unsigned short len` (2 bytes), followed by 6 bytes of padding (for alignment), and then a `struct sock_filter __user *filter` pointer (8 bytes).  Crucially, this `fprog` *isn't* initialized; no `= {0}` or `memset` is present.

Inside `tun_copy_filter`, the code only initializes the `len` and `filter` fields, leaving that 6-byte padding untouched.  The value of the local uninitialized `fprog` structure is then copied into the global `tun->fprog` structure. This means the 6 bytes of stack padding are now part of the `tun` structure.

The problem arises during a `TUNGETFILTER` ioctl call.  The vulnerable code is: `copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))`.  This copies *the entire* 16-byte structure, including the uninitialized 6-byte padding, to userspace!

Here's where it gets interesting: KASAN won't catch this. KASAN focuses on memory *validity* (bounds, use-after-free, etc.), not the *initialization* of the memory. KASAN is blind to uninitialized stack padding within a valid structure.  However, KMSAN *does* have a hook on `copy_to_user` (`kmsan_copy_to_user`), which actively detects the copying of uninitialized memory to user space.  It will report either a "BUG: KMSAN: kernel-infoleak in ... copy_to_user" or "kmsan: uninit-value in copy_to_user".

This perfectly matches Condition 1 of the criteria for `NeedsKMSAN=true`: "Kernel structures sent to user space (via copy_to_user, put_user, netlink skb attributes, ioctl output arguments, socket options, or BPF buffers) where fields or structure padding might not be fully initialized/zeroed."  The patch introduces a kernel memory leak because of this uninitialized stack variable. The old method of direct `copy_from_user` would initialize all bytes, the new method only initializes parts and leaks a portion. Therefore, we should set `NeedsKMSAN=true`.



38/3 2026/10/28 15:55 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch introduces a kernel stack information leak to userspace through structure padding.\n\nSpecifically:\n1. On 64-bit architectures like amd64, `struct sock_fprog` consists of an `unsigned short len` (2 bytes), followed by 6 bytes of structure alignment padding, and a pointer `filter` (8 bytes), totaling 16 bytes.\n2. In `__tun_chr_ioctl` for `TUNATTACHFILTER`, `struct sock_fprog fprog;` is allocated on the kernel stack without zero-initialization.\n3. In `tun_copy_filter()`, only the individual fields are assigned (`fprog-\u003elen = fp.len;` and `fprog-\u003efilter = fp.filter;`), leaving the 6 bytes of padding uninitialized stack memory.\n4. The entire struct is copied to the heap structure (`tun-\u003efprog = fprog;`), retaining the uninitialized padding bytes.\n5. In the `TUNGETFILTER` ioctl, `copy_to_user(argp, \u0026tun-\u003efprog, sizeof(tun-\u003efprog))` copies all 16 bytes to userspace, leaking 6 uninitialized bytes of kernel stack data. Prior to this patch, `TUNATTACHFILTER` directly did `copy_from_user(\u0026tun-\u003efprog, argp, sizeof(tun-\u003efprog))`, fully initializing all 16 bytes.\n\nKASAN only monitors memory bounds, use-after-free, and validity, and cannot detect reads or user copies of uninitialized memory. KMSAN tracks initialization at the byte level and checks for uninitialized bytes in `copy_to_user()`, making it uniquely suited to detect this vulnerability.",
  "NeedsKMSAN": true
}

Results:
{
  "KMSANReasoning": "The patch introduces a kernel stack information leak to userspace through structure padding.\n\nSpecifically:\n1. On 64-bit architectures like amd64, `struct sock_fprog` consists of an `unsigned short len` (2 bytes), followed by 6 bytes of structure alignment padding, and a pointer `filter` (8 bytes), totaling 16 bytes.\n2. In `__tun_chr_ioctl` for `TUNATTACHFILTER`, `struct sock_fprog fprog;` is allocated on the kernel stack without zero-initialization.\n3. In `tun_copy_filter()`, only the individual fields are assigned (`fprog-\u003elen = fp.len;` and `fprog-\u003efilter = fp.filter;`), leaving the 6 bytes of padding uninitialized stack memory.\n4. The entire struct is copied to the heap structure (`tun-\u003efprog = fprog;`), retaining the uninitialized padding bytes.\n5. In the `TUNGETFILTER` ioctl, `copy_to_user(argp, \u0026tun-\u003efprog, sizeof(tun-\u003efprog))` copies all 16 bytes to userspace, leaking 6 uninitialized bytes of kernel stack data. Prior to this patch, `TUNATTACHFILTER` directly did `copy_from_user(\u0026tun-\u003efprog, argp, sizeof(tun-\u003efprog))`, fully initializing all 16 bytes.\n\nKASAN only monitors memory bounds, use-after-free, and validity, and cannot detect reads or user copies of uninitialized memory. KMSAN tracks initialization at the byte level and checks for uninitialized bytes in `copy_to_user()`, making it uniquely suited to detect this vulnerability.",
  "NeedsKMSAN": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)