| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/24 17:57 | flow | patch-triage |
11mArgs:null Results: {
"EnableConfigs": [
"COREDUMP"
],
"FocusSymbols": [
"coredump_close_files",
"coredump_task_close_files",
"vfs_coredump"
],
"KMSANReasoning": "The patch introduces a new feature to close file descriptors before generating a coredump (COREDUMP_CLOSE_FILES). It modifies file descriptor table management, adds a new empty file descriptor table allocation (which is explicitly zeroed using __GFP_ZERO), and alters the coredump synchronization logic. No new data structures are copied to userspace, and no complex structures are allocated without initialization. The changes primarily involve pointer switching, reference counting, and file closing, which might risk use-after-free, NULL pointer dereferences, or locking issues. These types of bugs are effectively caught by KASAN, LOCKDEP, and standard bug detectors. There is no risk of uninitialized memory usage or info-leaks that would require KMSAN.",
"NeedsKMSAN": false,
"Reasoning": "The patch introduces a new `COREDUMP_CLOSE_FILES` flag to the coredump protocol. When a userspace coredump handler requests this flag, the kernel closes all file descriptors of the coredumping task before generating the coredump. This involves new core kernel logic in `fs/file.c` and `fs/coredump.c` to handle file descriptor table switching and closing. The feature is reachable by crashing a process and having a userspace coredump handler reply with the new flag, making it functionally impactful and worth fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/24 17:57 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 0957d000b45ea469cf234e7cad4c57051ec54716\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Mon Aug 24 17:57:04 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/fs/coredump.c b/fs/coredump.c\nindex ec9359e382ea5..a6e7da0bc16a7 100644\n--- a/fs/coredump.c\n+++ b/fs/coredump.c\n@@ -523,7 +523,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)\n \tstruct task_struct *tsk = current;\n \tint core_waiters = -EBUSY;\n \n-\tinit_completion(\u0026core_state-\u003estartup);\n+\tinit_completion(\u0026core_state-\u003edone);\n \tcore_state-\u003edumper.task = tsk;\n \tcore_state-\u003edumper.next = NULL;\n \n@@ -531,7 +531,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)\n \tif (core_waiters \u003e 0) {\n \t\tstruct core_thread *ptr;\n \n-\t\twait_for_completion_state(\u0026core_state-\u003estartup,\n+\t\twait_for_completion_state(\u0026core_state-\u003edone,\n \t\t\t\t\t TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);\n \t\t/*\n \t\t * Wait for all the threads to become inactive, so that\n@@ -837,7 +837,8 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *\n \t\t.mask\t\t\t= COREDUMP_KERNEL | COREDUMP_USERSPACE |\n \t\t\t\t\t COREDUMP_REJECT | COREDUMP_WAIT |\n \t\t\t\t\t COREDUMP_RECORDS | COREDUMP_SPARSE |\n-\t\t\t\t\t COREDUMP_MEMORY_TYPES,\n+\t\t\t\t\t COREDUMP_MEMORY_TYPES |\n+\t\t\t\t\t COREDUMP_CLOSE_FILES,\n \t\t.size_ack\t\t= sizeof(struct coredump_ack),\n \t\t.memory_types\t\t= cprm-\u003ememory_types,\n \t\t.memory_types_mask\t= COREDUMP_MEMORY_ALL,\n@@ -905,6 +906,12 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *\n \t\treturn false;\n \t}\n \n+\t/* A rejected task exits right away and closes everything anyway. */\n+\tif ((ack.mask \u0026 COREDUMP_CLOSE_FILES) \u0026\u0026 (ack.mask \u0026 COREDUMP_REJECT)) {\n+\t\tcoredump_sock_mark(cprm-\u003efile, COREDUMP_MARK_CONFLICTING);\n+\t\treturn false;\n+\t}\n+\n \tif (ack.mask \u0026 COREDUMP_MEMORY_TYPES) {\n \t\t/* The memory types need the whole field. */\n \t\tif (usize \u003c COREDUMP_ACK_SIZE_VER1) {\n@@ -1225,6 +1232,11 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\n \tif (cprm-\u003emask \u0026 COREDUMP_REJECT)\n \t\treturn;\n \n+\tif ((cprm-\u003emask \u0026 COREDUMP_CLOSE_FILES) \u0026\u0026 !coredump_close_files()) {\n+\t\tcoredump_report_failure(\"Failed to close files, aborting core\");\n+\t\treturn;\n+\t}\n+\n \t/* get us an unshared descriptor table; almost always a no-op */\n \t/* The cell spufs coredump code reads the file descriptor tables */\n \tif (unshare_files())\ndiff --git a/fs/file.c b/fs/file.c\nindex 628ca07dc4b17..3b8fce6e36951 100644\n--- a/fs/file.c\n+++ b/fs/file.c\n@@ -375,21 +375,15 @@ static unsigned int sane_fdtable_size(struct fdtable *fdt, struct fd_range *punc\n \treturn ALIGN(last + 1, BITS_PER_LONG);\n }\n \n-/*\n- * Allocate a new descriptor table and copy contents from the passed in\n- * instance. Returns a pointer to cloned table on success, ERR_PTR()\n- * on failure. For 'punch_hole' see sane_fdtable_size().\n- */\n-struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)\n+/* A table with one reference and the embedded fdtable, nothing copied yet. */\n+static struct files_struct *alloc_files(gfp_t gfp)\n {\n \tstruct files_struct *newf;\n-\tstruct file **old_fds, **new_fds;\n-\tunsigned int open_files, i;\n-\tstruct fdtable *old_fdt, *new_fdt;\n+\tstruct fdtable *new_fdt;\n \n-\tnewf = kmem_cache_alloc(files_cachep, GFP_KERNEL);\n+\tnewf = kmem_cache_alloc(files_cachep, gfp);\n \tif (!newf)\n-\t\treturn ERR_PTR(-ENOMEM);\n+\t\treturn NULL;\n \n \tatomic_set(\u0026newf-\u003ecount, 1);\n \n@@ -404,6 +398,39 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_ho\n \tnew_fdt-\u003efull_fds_bits = newf-\u003efull_fds_bits_init;\n \tnew_fdt-\u003efd = \u0026newf-\u003efd_array[0];\n \n+\treturn newf;\n+}\n+\n+/* An empty descriptor table with one reference. */\n+static struct files_struct *alloc_files_struct(void)\n+{\n+\tstruct files_struct *newf;\n+\n+\tnewf = alloc_files(GFP_KERNEL | __GFP_ZERO);\n+\tif (!newf)\n+\t\treturn NULL;\n+\n+\trcu_assign_pointer(newf-\u003efdt, \u0026newf-\u003efdtab);\n+\treturn newf;\n+}\n+\n+/*\n+ * Allocate a new descriptor table and copy contents from the passed in\n+ * instance. Returns a pointer to cloned table on success, ERR_PTR()\n+ * on failure. For 'punch_hole' see sane_fdtable_size().\n+ */\n+struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)\n+{\n+\tstruct files_struct *newf;\n+\tstruct file **old_fds, **new_fds;\n+\tunsigned int open_files, i;\n+\tstruct fdtable *old_fdt, *new_fdt;\n+\n+\tnewf = alloc_files(GFP_KERNEL);\n+\tif (!newf)\n+\t\treturn ERR_PTR(-ENOMEM);\n+\tnew_fdt = \u0026newf-\u003efdtab;\n+\n \tspin_lock(\u0026oldf-\u003efile_lock);\n \told_fdt = files_fdtable(oldf);\n \topen_files = sane_fdtable_size(old_fdt, punch_hole);\n@@ -480,6 +507,11 @@ static struct fdtable *close_files(struct files_struct * files)\n \t */\n \tstruct fdtable *fdt = rcu_dereference_raw(files-\u003efdt);\n \tunsigned int i, j = 0;\n+\t/*\n+\t * A kernel thread that might be needed to make progress on some\n+\t * umount must not run __fput() itself, see __fput_sync().\n+\t */\n+\tbool sync = !(current-\u003eflags \u0026 PF_KTHREAD);\n \n \tfor (;;) {\n \t\tunsigned long set;\n@@ -491,7 +523,10 @@ static struct fdtable *close_files(struct files_struct * files)\n \t\t\tif (set \u0026 1) {\n \t\t\t\tstruct file *file = fdt-\u003efd[i];\n \t\t\t\tif (file) {\n-\t\t\t\t\tfilp_close(file, files);\n+\t\t\t\t\tif (sync)\n+\t\t\t\t\t\tfilp_close_sync(file, files);\n+\t\t\t\t\telse\n+\t\t\t\t\t\tfilp_close(file, files);\n \t\t\t\t\tcond_resched();\n \t\t\t\t}\n \t\t\t}\n@@ -515,16 +550,97 @@ void put_files_struct(struct files_struct *files)\n \t}\n }\n \n+/* Hand @tsk the table @files, or none, and drop the one it had. */\n+static void switch_files_struct(struct task_struct *tsk,\n+\t\t\t\tstruct files_struct *files)\n+{\n+\tstruct files_struct *old;\n+\n+\ttask_lock(tsk);\n+\told = tsk-\u003efiles;\n+\tif (old) {\n+\t\tif (files)\n+\t\t\tatomic_inc(\u0026files-\u003ecount);\n+\t\ttsk-\u003efiles = files;\n+\t}\n+\ttask_unlock(tsk);\n+\tif (old)\n+\t\tput_files_struct(old);\n+}\n+\n void exit_files(struct task_struct *tsk)\n {\n-\tstruct files_struct * files = tsk-\u003efiles;\n+\tif (!tsk-\u003efiles)\n+\t\treturn;\n+\tswitch_files_struct(tsk, NULL);\n+}\n+\n+/*\n+ * The thread-group is waiting in coredump_task_exit(). Wake them and\n+ * tell them to get rid of their files.\n+ */\n+bool coredump_close_files(void)\n+{\n+\tstruct core_state *core_state = current-\u003esignal-\u003ecore_state;\n+\tstruct files_struct *files;\n+\tstruct core_thread *ct;\n+\tint nr = 0;\n \n-\tif (files) {\n-\t\ttask_lock(tsk);\n-\t\ttsk-\u003efiles = NULL;\n-\t\ttask_unlock(tsk);\n-\t\tput_files_struct(files);\n+\tfiles = alloc_files_struct();\n+\tif (!files)\n+\t\treturn false;\n+\n+\t/* Tasks without a table, vhost workers say, sit the switch out. */\n+\tfor (ct = core_state-\u003edumper.next; ct; ct = ct-\u003enext)\n+\t\tif (ct-\u003etask-\u003efiles)\n+\t\t\tnr++;\n+\tatomic_set(\u0026core_state-\u003enr_threads, nr);\n+\treinit_completion(\u0026core_state-\u003edone);\n+\n+\tfor (ct = core_state-\u003edumper.next; ct; ct = ct-\u003enext) {\n+\t\tif (!ct-\u003etask-\u003efiles)\n+\t\t\tcontinue;\n+\t\t/* Pairs with the acquire in coredump_task_close_files(). */\n+\t\tsmp_store_release(\u0026ct-\u003efiles, files);\n+\t\twake_up_process(ct-\u003etask);\n \t}\n+\n+\t/* The dumper's subjective creds are the dump's, not its own. */\n+\tscoped_with_creds(current_real_cred())\n+\t\tswitch_files_struct(current, files);\n+\n+\tif (nr) {\n+\t\twait_for_completion_state(\u0026core_state-\u003edone,\n+\t\t\t\t\t TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);\n+\t\t/* Back asleep before the notes read their registers. */\n+\t\tfor (ct = core_state-\u003edumper.next; ct; ct = ct-\u003enext)\n+\t\t\twait_task_inactive(ct-\u003etask, TASK_ANY);\n+\t}\n+\n+\tput_files_struct(files);\n+\treturn true;\n+}\n+\n+/*\n+ * Get rid of the files and switch to the empty file descriptor table\n+ * the dumping thread published in this thread's core_thread entry.\n+ */\n+bool coredump_task_close_files(struct core_thread *self)\n+{\n+\tstruct core_state *core_state;\n+\tstruct files_struct *files;\n+\n+\t/* Pairs with the release in coredump_close_files(). */\n+\tfiles = smp_load_acquire(\u0026self-\u003efiles);\n+\tif (!files || !current-\u003efiles || current-\u003efiles == files)\n+\t\treturn false;\n+\n+\t__set_current_state(TASK_RUNNING);\n+\tswitch_files_struct(current, files);\n+\tcore_state = current-\u003esignal-\u003ecore_state;\n+\tif (atomic_dec_and_test(\u0026core_state-\u003enr_threads))\n+\t\tcomplete(\u0026core_state-\u003edone);\n+\treturn true;\n }\n \n struct files_struct init_files = {\ndiff --git a/fs/file_table.c b/fs/file_table.c\nindex c68b8c0a4097a..3c705917646b5 100644\n--- a/fs/file_table.c\n+++ b/fs/file_table.c\n@@ -13,6 +13,7 @@\n #include \u003clinux/module.h\u003e\n #include \u003clinux/fs.h\u003e\n #include \u003clinux/filelock.h\u003e\n+#include \u003clinux/dnotify.h\u003e\n #include \u003clinux/security.h\u003e\n #include \u003clinux/cred.h\u003e\n #include \u003clinux/eventpoll.h\u003e\n@@ -606,15 +607,53 @@ void __fput_sync(struct file *file)\n EXPORT_SYMBOL(__fput_sync);\n \n /*\n- * Equivalent to __fput_sync(), but optimized for being called with the last\n- * reference.\n- *\n- * See file_ref_put_close() for details.\n+ * \"id\" is the POSIX thread ID. We use the\n+ * files pointer for this..\n */\n-void fput_close_sync(struct file *file)\n+static int filp_flush(struct file *filp, fl_owner_t id)\n {\n-\tif (likely(file_ref_put_close(\u0026file-\u003ef_ref)))\n-\t\t__fput(file);\n+\tint retval = 0;\n+\n+\tif (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,\n+\t\t\t\"VFS: Close: file count is 0 (f_op=%ps)\",\n+\t\t\tfilp-\u003ef_op)) {\n+\t\treturn 0;\n+\t}\n+\n+\tif (filp-\u003ef_op-\u003eflush)\n+\t\tretval = filp-\u003ef_op-\u003eflush(filp, id);\n+\n+\tif (likely(!(filp-\u003ef_mode \u0026 FMODE_PATH))) {\n+\t\tdnotify_flush(filp, id);\n+\t\tlocks_remove_posix(filp, id);\n+\t}\n+\treturn retval;\n+}\n+\n+int filp_close(struct file *filp, fl_owner_t id)\n+{\n+\tint retval;\n+\n+\tretval = filp_flush(filp, id);\n+\tfput_close(filp);\n+\n+\treturn retval;\n+}\n+EXPORT_SYMBOL(filp_close);\n+\n+/*\n+ * Like filp_close() but without deferring the release of the last\n+ * reference, see file_ref_put_close() for details.\n+ */\n+int filp_close_sync(struct file *filp, fl_owner_t id)\n+{\n+\tint retval;\n+\n+\tretval = filp_flush(filp, id);\n+\tif (likely(file_ref_put_close(\u0026filp-\u003ef_ref)))\n+\t\t__fput(filp);\n+\n+\treturn retval;\n }\n \n /*\ndiff --git a/fs/internal.h b/fs/internal.h\nindex 355d93f922086..7ba8cd26a698c 100644\n--- a/fs/internal.h\n+++ b/fs/internal.h\n@@ -127,7 +127,7 @@ static inline void put_file_access(struct file *file)\n \t}\n }\n \n-void fput_close_sync(struct file *);\n+int filp_close_sync(struct file *filp, fl_owner_t id);\n void fput_close(struct file *);\n \n /*\n@@ -190,6 +190,7 @@ struct open_flags {\n \tint intent;\n \tint lookup_flags;\n };\n+\n extern struct file *do_file_open(int dfd, struct filename *pathname,\n \t\tconst struct open_flags *op);\n extern struct file *do_file_open_root(const struct path *,\ndiff --git a/fs/open.c b/fs/open.c\nindex 408925d7bd0b7..d036736d5d435 100644\n--- a/fs/open.c\n+++ b/fs/open.c\n@@ -1453,41 +1453,6 @@ SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)\n }\n #endif\n \n-/*\n- * \"id\" is the POSIX thread ID. We use the\n- * files pointer for this..\n- */\n-static int filp_flush(struct file *filp, fl_owner_t id)\n-{\n-\tint retval = 0;\n-\n-\tif (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,\n-\t\t\t\"VFS: Close: file count is 0 (f_op=%ps)\",\n-\t\t\tfilp-\u003ef_op)) {\n-\t\treturn 0;\n-\t}\n-\n-\tif (filp-\u003ef_op-\u003eflush)\n-\t\tretval = filp-\u003ef_op-\u003eflush(filp, id);\n-\n-\tif (likely(!(filp-\u003ef_mode \u0026 FMODE_PATH))) {\n-\t\tdnotify_flush(filp, id);\n-\t\tlocks_remove_posix(filp, id);\n-\t}\n-\treturn retval;\n-}\n-\n-int filp_close(struct file *filp, fl_owner_t id)\n-{\n-\tint retval;\n-\n-\tretval = filp_flush(filp, id);\n-\tfput_close(filp);\n-\n-\treturn retval;\n-}\n-EXPORT_SYMBOL(filp_close);\n-\n /*\n * Careful here! We test whether the file pointer is NULL before\n * releasing the fd. This ensures that one clone task can't release\n@@ -1502,13 +1467,11 @@ SYSCALL_DEFINE1(close, unsigned int, fd)\n \tif (!file)\n \t\treturn -EBADF;\n \n-\tretval = filp_flush(file, current-\u003efiles);\n-\n \t/*\n \t * We're returning to user space. Don't bother\n \t * with any delayed fput() cases.\n \t */\n-\tfput_close_sync(file);\n+\tretval = filp_close_sync(file, current-\u003efiles);\n \n \tif (likely(retval == 0))\n \t\treturn 0;\ndiff --git a/include/linux/fdtable.h b/include/linux/fdtable.h\nindex c45306a9f0072..f0c1003da8ce8 100644\n--- a/include/linux/fdtable.h\n+++ b/include/linux/fdtable.h\n@@ -99,11 +99,16 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *fil\n \n struct task_struct;\n \n+struct core_thread;\n+\n void put_files_struct(struct files_struct *fs);\n+bool coredump_close_files(void);\n+bool coredump_task_close_files(struct core_thread *self);\n int unshare_files(void);\n struct fd_range {\n \tunsigned int from, to;\n };\n+\n struct files_struct *dup_fd(struct files_struct *, struct fd_range *) __latent_entropy;\n void do_close_on_exec(struct files_struct *);\n int iterate_fd(struct files_struct *, unsigned,\ndiff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h\nindex 584ae88b435e9..fed02a427de31 100644\n--- a/include/linux/sched/signal.h\n+++ b/include/linux/sched/signal.h\n@@ -76,12 +76,15 @@ struct multiprocess_signals {\n struct core_thread {\n \tstruct task_struct *task;\n \tstruct core_thread *next;\n+\t/* The empty table to switch to, published by the dumping thread. */\n+\tstruct files_struct *files;\n };\n \n struct core_state {\n \tatomic_t nr_threads;\n \tstruct core_thread dumper;\n-\tstruct completion startup;\n+\t/* Completed by the last thread to park, reused for the table switch. */\n+\tstruct completion done;\n };\n \n /*\ndiff --git a/include/uapi/linux/coredump.h b/include/uapi/linux/coredump.h\nindex 6d0c53b534ea8..ec09d7ab01314 100644\n--- a/include/uapi/linux/coredump.h\n+++ b/include/uapi/linux/coredump.h\n@@ -19,6 +19,9 @@\n * @COREDUMP_MEMORY_TYPES: dump the memory types in\n * coredump_ack-\u003ememory_types instead of the ones\n * the task selected; requires COREDUMP_KERNEL\n+ * @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the\n+ * coredump is generated; incompatible with\n+ * COREDUMP_REJECT\n */\n enum {\n \tCOREDUMP_KERNEL\t\t= (1ULL \u003c\u003c 0),\n@@ -28,6 +31,7 @@ enum {\n \tCOREDUMP_RECORDS\t= (1ULL \u003c\u003c 4),\n \tCOREDUMP_SPARSE\t\t= (1ULL \u003c\u003c 5),\n \tCOREDUMP_MEMORY_TYPES\t= (1ULL \u003c\u003c 6),\n+\tCOREDUMP_CLOSE_FILES\t= (1ULL \u003c\u003c 7),\n };\n \n /**\n@@ -137,6 +141,10 @@ enum {\n * Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't\n * raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of\n * at least COREDUMP_ACK_SIZE_VER1 bytes.\n+ *\n+ * If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file\n+ * descriptors of the coredumping task before it generates the coredump.\n+ * The task ends up with an empty descriptor table.\n */\n struct coredump_ack {\n \t__u32 size;\ndiff --git a/kernel/exit.c b/kernel/exit.c\nindex 2c0b1c02920f4..b651679c12e97 100644\n--- a/kernel/exit.c\n+++ b/kernel/exit.c\n@@ -25,6 +25,7 @@\n #include \u003clinux/acct.h\u003e\n #include \u003clinux/tsacct_kern.h\u003e\n #include \u003clinux/file.h\u003e\n+#include \u003clinux/fdtable.h\u003e\n #include \u003clinux/freezer.h\u003e\n #include \u003clinux/binfmts.h\u003e\n #include \u003clinux/nsproxy.h\u003e\n@@ -432,6 +433,7 @@ static void coredump_task_exit(struct task_struct *tsk,\n \tstruct core_thread self;\n \n \tself.task = tsk;\n+\tself.files = NULL;\n \tif (self.task-\u003eflags \u0026 PF_SIGNALED)\n \t\tself.next = xchg(\u0026core_state-\u003edumper.next, \u0026self);\n \telse\n@@ -441,12 +443,15 @@ static void coredump_task_exit(struct task_struct *tsk,\n \t * to core_state-\u003edumper.\n \t */\n \tif (atomic_dec_and_test(\u0026core_state-\u003enr_threads))\n-\t\tcomplete(\u0026core_state-\u003estartup);\n+\t\tcomplete(\u0026core_state-\u003edone);\n \n \tfor (;;) {\n \t\tset_current_state(TASK_IDLE|TASK_FREEZABLE);\n \t\tif (!self.task) /* see coredump_finish() */\n \t\t\tbreak;\n+\t\t/* see coredump_close_files() */\n+\t\tif (coredump_task_close_files(\u0026self))\n+\t\t\tcontinue;\n \t\tschedule();\n \t}\n \t__set_current_state(TASK_RUNNING);\ndiff --git a/tools/include/uapi/linux/coredump.h b/tools/include/uapi/linux/coredump.h\nindex 6d0c53b534ea8..ec09d7ab01314 100644\n--- a/tools/include/uapi/linux/coredump.h\n+++ b/tools/include/uapi/linux/coredump.h\n@@ -19,6 +19,9 @@\n * @COREDUMP_MEMORY_TYPES: dump the memory types in\n * coredump_ack-\u003ememory_types instead of the ones\n * the task selected; requires COREDUMP_KERNEL\n+ * @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the\n+ * coredump is generated; incompatible with\n+ * COREDUMP_REJECT\n */\n enum {\n \tCOREDUMP_KERNEL\t\t= (1ULL \u003c\u003c 0),\n@@ -28,6 +31,7 @@ enum {\n \tCOREDUMP_RECORDS\t= (1ULL \u003c\u003c 4),\n \tCOREDUMP_SPARSE\t\t= (1ULL \u003c\u003c 5),\n \tCOREDUMP_MEMORY_TYPES\t= (1ULL \u003c\u003c 6),\n+\tCOREDUMP_CLOSE_FILES\t= (1ULL \u003c\u003c 7),\n };\n \n /**\n@@ -137,6 +141,10 @@ enum {\n * Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't\n * raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of\n * at least COREDUMP_ACK_SIZE_VER1 bytes.\n+ *\n+ * If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file\n+ * descriptors of the coredumping task before it generates the coredump.\n+ * The task ends up with an empty descriptor table.\n */\n struct coredump_ack {\n \t__u32 size;\ndiff --git a/tools/testing/selftests/coredump/Makefile b/tools/testing/selftests/coredump/Makefile\nindex dece1a31d561b..dc8489d406184 100644\n--- a/tools/testing/selftests/coredump/Makefile\n+++ b/tools/testing/selftests/coredump/Makefile\n@@ -3,7 +3,8 @@ CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)\n \n TEST_GEN_PROGS := stackdump_test \\\n \t\t coredump_socket_test \\\n-\t\t coredump_socket_protocol_test\n+\t\t coredump_socket_protocol_test \\\n+\t\t coredump_close_files_test\n TEST_FILES := stackdump\n \n include ../lib.mk\n@@ -11,3 +12,4 @@ include ../lib.mk\n $(OUTPUT)/stackdump_test: coredump_test_helpers.c\n $(OUTPUT)/coredump_socket_test: coredump_test_helpers.c\n $(OUTPUT)/coredump_socket_protocol_test: coredump_test_helpers.c\n+$(OUTPUT)/coredump_close_files_test: coredump_test_helpers.c\ndiff --git a/tools/testing/selftests/coredump/coredump_close_files_test.c b/tools/testing/selftests/coredump/coredump_close_files_test.c\nnew file mode 100644\nindex 0000000000000..7a41906b46952\n--- /dev/null\n+++ b/tools/testing/selftests/coredump/coredump_close_files_test.c\n@@ -0,0 +1,592 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003cdirent.h\u003e\n+#include \u003cfcntl.h\u003e\n+#include \u003cpthread.h\u003e\n+#include \u003csys/file.h\u003e\n+#include \u003csys/stat.h\u003e\n+#include \u003csys/socket.h\u003e\n+#include \u003csys/un.h\u003e\n+\n+#include \"coredump_test.h\"\n+\n+#define LOCK_FILE \"/tmp/coredump.lock\"\n+\n+/* Idle threads the multi-threaded crashing child spawns. */\n+#define NUM_CLOSE_THREADS 4\n+\n+/* How the crashing child locks LOCK_FILE. */\n+enum lock_kind {\n+\tLOCK_KIND_POSIX,\n+\tLOCK_KIND_OFD,\n+\tLOCK_KIND_FLOCK,\n+};\n+\n+/* Who else has a handle on the lock when the child crashes. */\n+enum lock_share {\n+\tLOCK_SHARE_NONE,\n+\tLOCK_SHARE_DUP,\t\t/* a second slot in the child's own table */\n+\tLOCK_SHARE_FORK,\t/* a forked process keeps the fd open */\n+\tLOCK_SHARE_FILES,\t/* a CLONE_FILES process shares the table */\n+};\n+\n+struct close_test {\n+\tenum lock_kind kind;\n+\tenum lock_share share;\n+\tbool threads;\n+\tbool close;\t\t/* ack with COREDUMP_CLOSE_FILES */\n+\tbool userspace;\t\t/* COREDUMP_USERSPACE instead of COREDUMP_KERNEL */\n+\tbool released;\t\t/* the lock is gone once the kernel is past the close */\n+};\n+\n+FIXTURE_SETUP(coredump)\n+{\n+\tFILE *file;\n+\tint ret;\n+\n+\tself-\u003epid_coredump_server = -ESRCH;\n+\tself-\u003efd_tmpfs_detached = -1;\n+\tfile = fopen(\"/proc/sys/kernel/core_pattern\", \"r\");\n+\tASSERT_NE(NULL, file);\n+\n+\tret = fread(self-\u003eoriginal_core_pattern, 1, sizeof(self-\u003eoriginal_core_pattern), file);\n+\tASSERT_TRUE(ret || feof(file));\n+\tASSERT_LT(ret, sizeof(self-\u003eoriginal_core_pattern));\n+\n+\tself-\u003eoriginal_core_pattern[ret] = '\\0';\n+\tself-\u003efd_tmpfs_detached = create_detached_tmpfs();\n+\tASSERT_GE(self-\u003efd_tmpfs_detached, 0);\n+\n+\tret = fclose(file);\n+\tASSERT_EQ(0, ret);\n+}\n+\n+FIXTURE_TEARDOWN(coredump)\n+{\n+\tconst char *reason;\n+\tFILE *file;\n+\tint ret, status;\n+\n+\tif (self-\u003epid_coredump_server \u003e 0) {\n+\t\tkill(self-\u003epid_coredump_server, SIGTERM);\n+\t\twaitpid(self-\u003epid_coredump_server, \u0026status, 0);\n+\t}\n+\tunlink(LOCK_FILE);\n+\tunlink(\"/tmp/coredump.socket\");\n+\n+\tfile = fopen(\"/proc/sys/kernel/core_pattern\", \"w\");\n+\tif (!file) {\n+\t\treason = \"Unable to open core_pattern\";\n+\t\tgoto fail;\n+\t}\n+\n+\tret = fprintf(file, \"%s\", self-\u003eoriginal_core_pattern);\n+\tif (ret \u003c 0) {\n+\t\treason = \"Unable to write to core_pattern\";\n+\t\tgoto fail;\n+\t}\n+\n+\tret = fclose(file);\n+\tif (ret) {\n+\t\treason = \"Unable to close core_pattern\";\n+\t\tgoto fail;\n+\t}\n+\n+\tif (self-\u003efd_tmpfs_detached \u003e= 0) {\n+\t\tret = close(self-\u003efd_tmpfs_detached);\n+\t\tif (ret \u003c 0) {\n+\t\t\treason = \"Unable to close detached tmpfs\";\n+\t\t\tgoto fail;\n+\t\t}\n+\t\tself-\u003efd_tmpfs_detached = -1;\n+\t}\n+\n+\treturn;\n+fail:\n+\t/* This should never happen */\n+\tfprintf(stderr, \"Failed to cleanup coredump test: %s\\n\", reason);\n+}\n+\n+/* Write-lock @fd the way @kind says. */\n+static int take_lock(int fd, enum lock_kind kind)\n+{\n+\tstruct flock fl = {\n+\t\t.l_type = F_WRLCK,\n+\t\t.l_whence = SEEK_SET,\n+\t};\n+\n+\tswitch (kind) {\n+\tcase LOCK_KIND_POSIX:\n+\t\treturn fcntl(fd, F_SETLK, \u0026fl);\n+\tcase LOCK_KIND_OFD:\n+\t\treturn fcntl(fd, F_OFD_SETLK, \u0026fl);\n+\tcase LOCK_KIND_FLOCK:\n+\t\treturn flock(fd, LOCK_EX);\n+\t}\n+\n+\treturn -1;\n+}\n+\n+/* Does anyone else hold a write lock on @fd? 1 if so, 0 if not, -1 on error. */\n+static int lock_held(int fd, enum lock_kind kind)\n+{\n+\tstruct flock fl = {\n+\t\t.l_type = F_WRLCK,\n+\t\t.l_whence = SEEK_SET,\n+\t};\n+\n+\tif (kind == LOCK_KIND_FLOCK) {\n+\t\tif (flock(fd, LOCK_EX | LOCK_NB) == 0) {\n+\t\t\tflock(fd, LOCK_UN);\n+\t\t\treturn 0;\n+\t\t}\n+\t\treturn errno == EWOULDBLOCK ? 1 : -1;\n+\t}\n+\n+\t/* F_GETLK reports conflicting OFD locks too. */\n+\tif (fcntl(fd, F_GETLK, \u0026fl) \u003c 0)\n+\t\treturn -1;\n+\treturn fl.l_type != F_UNLCK;\n+}\n+\n+/* Number of entries in /proc/@pid/fd, the lowest one in @first. */\n+static int count_fds(pid_t pid, int *first)\n+{\n+\tchar path[64];\n+\tstruct dirent *de;\n+\tDIR *dir;\n+\tint nr = 0;\n+\n+\tsnprintf(path, sizeof(path), \"/proc/%d/fd\", pid);\n+\tdir = opendir(path);\n+\tif (!dir)\n+\t\treturn -1;\n+\n+\t*first = -1;\n+\twhile ((de = readdir(dir))) {\n+\t\tint fd;\n+\n+\t\tif (de-\u003ed_name[0] == '.')\n+\t\t\tcontinue;\n+\t\tfd = atoi(de-\u003ed_name);\n+\t\tif (*first \u003c 0 || fd \u003c *first)\n+\t\t\t*first = fd;\n+\t\tnr++;\n+\t}\n+\tclosedir(dir);\n+\treturn nr;\n+}\n+\n+/*\n+ * Block until the test hangs up @fd_release, keeping every inherited fd\n+ * open, then report through @fd_result whether @fd is still open.\n+ */\n+static void hold_until_released(int fd, int fd_release, int fd_result)\n+{\n+\tchar c;\n+\n+\tread_nointr(fd_release, \u0026c, 1);\n+\tc = fcntl(fd, F_GETFD) \u003c 0 ? 'C' : 'O';\n+\twrite_nointr(fd_result, \u0026c, 1);\n+\t_exit(EXIT_SUCCESS);\n+}\n+\n+/* Lock LOCK_FILE, share it as requested, then crash. */\n+static void crashing_child_locked(const struct close_test *t, int fd_release,\n+\t\t\t\t int fd_result)\n+{\n+\tpthread_t thread;\n+\tint fd, pidfd, i;\n+\tpid_t pid;\n+\n+\tfd = open(LOCK_FILE, O_RDWR | O_CLOEXEC);\n+\tif (fd \u003c 0)\n+\t\t_exit(EXIT_FAILURE);\n+\n+\tif (take_lock(fd, t-\u003ekind))\n+\t\t_exit(EXIT_FAILURE);\n+\n+\tswitch (t-\u003eshare) {\n+\tcase LOCK_SHARE_NONE:\n+\t\tbreak;\n+\tcase LOCK_SHARE_DUP:\n+\t\tif (dup(fd) \u003c 0)\n+\t\t\t_exit(EXIT_FAILURE);\n+\t\tbreak;\n+\tcase LOCK_SHARE_FORK:\n+\t\tpid = fork();\n+\t\tif (pid \u003c 0)\n+\t\t\t_exit(EXIT_FAILURE);\n+\t\tif (pid == 0)\n+\t\t\thold_until_released(fd, fd_release, fd_result);\n+\t\tbreak;\n+\tcase LOCK_SHARE_FILES:\n+\t\tpid = create_child(\u0026pidfd, CLONE_FILES);\n+\t\tif (pid \u003c 0)\n+\t\t\t_exit(EXIT_FAILURE);\n+\t\tif (pid == 0)\n+\t\t\thold_until_released(fd, fd_release, fd_result);\n+\t\tbreak;\n+\t}\n+\n+\tif (t-\u003ethreads)\n+\t\tfor (i = 0; i \u003c NUM_CLOSE_THREADS; i++)\n+\t\t\tpthread_create(\u0026thread, NULL, do_nothing, NULL);\n+\n+\t/* crash on purpose */\n+\t*(volatile int *)NULL = 0;\n+}\n+\n+/*\n+ * Serve one coredump and look at the task on the way. Before the ack the\n+ * lock is held and the descriptors are there. Once the kernel is past the\n+ * point where it closes them, which is before the first byte of the dump\n+ * or before the hangup in userspace mode, they are gone if we asked for\n+ * it and the lock is in the expected state.\n+ */\n+static int close_server(const struct close_test *t, int fd_ipc)\n+{\n+\tstruct coredump_req req = {};\n+\tstruct pidfd_info info = {};\n+\tint fd_server = -1, fd_coredump = -1, fd_peer_pidfd = -1, fd_lock = -1;\n+\tint exit_code = EXIT_FAILURE;\n+\tint fd, first_fd, nr_fds;\n+\t__u64 mask;\n+\tssize_t bytes;\n+\tchar c;\n+\n+\tfd_lock = open(LOCK_FILE, O_RDWR | O_CLOEXEC);\n+\tif (fd_lock \u003c 0) {\n+\t\tfprintf(stderr, \"%s: open lock file failed: %m\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tfd_server = create_and_listen_unix_socket(\"/tmp/coredump.socket\");\n+\tif (fd_server \u003c 0) {\n+\t\tfprintf(stderr, \"%s: create_and_listen_unix_socket failed: %m\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tif (write_nointr(fd_ipc, \"1\", 1) \u003c 0) {\n+\t\tfprintf(stderr, \"%s: write_nointr to ipc socket failed: %m\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\tclose(fd_ipc);\n+\n+\tfd_coredump = accept4(fd_server, NULL, NULL, SOCK_CLOEXEC);\n+\tif (fd_coredump \u003c 0) {\n+\t\tfprintf(stderr, \"%s: accept4 failed: %m\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tfd_peer_pidfd = get_peer_pidfd(fd_coredump);\n+\tif (fd_peer_pidfd \u003c 0) {\n+\t\tfprintf(stderr, \"%s: get_peer_pidfd failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!get_pidfd_info(fd_peer_pidfd, \u0026info)) {\n+\t\tfprintf(stderr, \"%s: get_pidfd_info failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!read_coredump_req(fd_coredump, \u0026req)) {\n+\t\tfprintf(stderr, \"%s: read_coredump_req failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!check_coredump_req(\u0026req)) {\n+\t\tfprintf(stderr, \"%s: check_coredump_req failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\t/* The task waits for our answer with everything still in place. */\n+\tif (lock_held(fd_lock, t-\u003ekind) != 1) {\n+\t\tfprintf(stderr, \"%s: lock not held during the handshake\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tnr_fds = count_fds(info.pid, \u0026first_fd);\n+\tif (nr_fds \u003c= 0) {\n+\t\tfprintf(stderr, \"%s: no descriptors during the handshake\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tfd = sys_pidfd_getfd(fd_peer_pidfd, first_fd, 0);\n+\tif (fd \u003c 0) {\n+\t\tfprintf(stderr, \"%s: pidfd_getfd during the handshake failed: %m\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\tclose(fd);\n+\n+\tmask = COREDUMP_WAIT;\n+\tmask |= t-\u003euserspace ? COREDUMP_USERSPACE : COREDUMP_KERNEL;\n+\tif (t-\u003eclose)\n+\t\tmask |= COREDUMP_CLOSE_FILES;\n+\n+\tif (!send_coredump_ack(fd_coredump, \u0026req, mask, 0)) {\n+\t\tfprintf(stderr, \"%s: send_coredump_ack failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!read_marker(fd_coredump, COREDUMP_MARK_REQACK)) {\n+\t\tfprintf(stderr, \"%s: read_marker COREDUMP_MARK_REQACK failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tbytes = read_nointr(fd_coredump, \u0026c, 1);\n+\tif (bytes != (t-\u003euserspace ? 0 : 1)) {\n+\t\tfprintf(stderr, \"%s: read after the ack returned %zd: %m\\n\", __func__, bytes);\n+\t\tgoto out;\n+\t}\n+\n+\tif (lock_held(fd_lock, t-\u003ekind) != !t-\u003ereleased) {\n+\t\tfprintf(stderr, \"%s: lock %s while the coredump is generated\\n\",\n+\t\t\t__func__, t-\u003ereleased ? \"still held\" : \"released\");\n+\t\tgoto out;\n+\t}\n+\n+\tnr_fds = count_fds(info.pid, \u0026first_fd);\n+\tif (nr_fds \u003c 0 || !nr_fds != t-\u003eclose) {\n+\t\tfprintf(stderr, \"%s: %d descriptors while the coredump is generated\\n\",\n+\t\t\t__func__, nr_fds);\n+\t\tgoto out;\n+\t}\n+\n+\tfd = sys_pidfd_getfd(fd_peer_pidfd, first_fd, 0);\n+\tif (t-\u003eclose) {\n+\t\tif (fd \u003e= 0 || errno != EBADF) {\n+\t\t\tfprintf(stderr, \"%s: pidfd_getfd after the close returned %d: %m\\n\",\n+\t\t\t\t__func__, fd);\n+\t\t\tgoto out;\n+\t\t}\n+\t} else {\n+\t\tif (fd \u003c 0) {\n+\t\t\tfprintf(stderr, \"%s: pidfd_getfd during the coredump failed: %m\\n\",\n+\t\t\t\t__func__);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tclose(fd);\n+\t}\n+\n+\t/* COREDUMP_WAIT keeps the task around until we hang up. */\n+\tif (!get_pidfd_info(fd_peer_pidfd, \u0026info)) {\n+\t\tfprintf(stderr, \"%s: get_pidfd_info failed\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tif (info.mask \u0026 PIDFD_INFO_EXIT) {\n+\t\tfprintf(stderr, \"%s: task exited before the coredump finished\\n\", __func__);\n+\t\tgoto out;\n+\t}\n+\n+\tfor (;;) {\n+\t\tchar buffer[4096];\n+\n+\t\tbytes = read_nointr(fd_coredump, buffer, sizeof(buffer));\n+\t\tif (bytes \u003c 0) {\n+\t\t\tfprintf(stderr, \"%s: read from coredump socket failed: %m\\n\", __func__);\n+\t\t\tgoto out;\n+\t\t}\n+\n+\t\tif (bytes == 0)\n+\t\t\tbreak;\n+\t}\n+\n+\texit_code = EXIT_SUCCESS;\n+out:\n+\tif (fd_lock \u003e= 0)\n+\t\tclose(fd_lock);\n+\tif (fd_peer_pidfd \u003e= 0)\n+\t\tclose(fd_peer_pidfd);\n+\tif (fd_coredump \u003e= 0)\n+\t\tclose(fd_coredump);\n+\tif (fd_server \u003e= 0)\n+\t\tclose(fd_server);\n+\treturn exit_code;\n+}\n+\n+static void run_close_test(struct __test_metadata *const _metadata,\n+\t\t\t FIXTURE_DATA(coredump) *self,\n+\t\t\t const struct close_test *t)\n+{\n+\tint fd, status, ipc_sockets[2], release_pipe[2], result_pipe[2];\n+\tpid_t pid, pid_coredump_server;\n+\tchar c;\n+\n+\tASSERT_TRUE(set_core_pattern(\"@@/tmp/coredump.socket\"));\n+\n+\tfd = open(LOCK_FILE, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);\n+\tASSERT_GE(fd, 0);\n+\tEXPECT_EQ(close(fd), 0);\n+\n+\tASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets), 0);\n+\n+\tpid_coredump_server = fork();\n+\tASSERT_GE(pid_coredump_server, 0);\n+\tif (pid_coredump_server == 0) {\n+\t\tclose(ipc_sockets[0]);\n+\t\t_exit(close_server(t, ipc_sockets[1]));\n+\t}\n+\tself-\u003epid_coredump_server = pid_coredump_server;\n+\n+\tEXPECT_EQ(close(ipc_sockets[1]), 0);\n+\tASSERT_EQ(read_nointr(ipc_sockets[0], \u0026c, 1), 1);\n+\tEXPECT_EQ(close(ipc_sockets[0]), 0);\n+\n+\t/* Only the crashing child and what it spawns see these pipes. */\n+\tASSERT_EQ(pipe2(release_pipe, O_CLOEXEC), 0);\n+\tASSERT_EQ(pipe2(result_pipe, O_CLOEXEC), 0);\n+\n+\tpid = fork();\n+\tASSERT_GE(pid, 0);\n+\tif (pid == 0) {\n+\t\tclose(release_pipe[1]);\n+\t\tclose(result_pipe[0]);\n+\t\tcrashing_child_locked(t, release_pipe[0], result_pipe[1]);\n+\t}\n+\tEXPECT_EQ(close(release_pipe[0]), 0);\n+\tEXPECT_EQ(close(result_pipe[1]), 0);\n+\n+\twaitpid(pid, \u0026status, 0);\n+\tASSERT_TRUE(WIFSIGNALED(status));\n+\tASSERT_TRUE(WCOREDUMP(status));\n+\n+\twait_and_check_coredump_server(pid_coredump_server, _metadata, self);\n+\n+\t/* Let the process holding the shared lock go. */\n+\tEXPECT_EQ(close(release_pipe[1]), 0);\n+\n+\t/* The crashing child is gone but what it shared with is untouched. */\n+\tif (t-\u003eshare == LOCK_SHARE_FORK || t-\u003eshare == LOCK_SHARE_FILES) {\n+\t\tASSERT_EQ(read_nointr(result_pipe[0], \u0026c, 1), 1);\n+\t\tASSERT_EQ(c, 'O');\n+\t}\n+\tEXPECT_EQ(close(result_pipe[0]), 0);\n+}\n+\n+TEST_F(coredump, close_files_posix)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_POSIX,\n+\t\t.share = LOCK_SHARE_NONE,\n+\t\t.threads = true,\n+\t\t.close = true,\n+\t\t.released = true,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_ofd)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_OFD,\n+\t\t.share = LOCK_SHARE_NONE,\n+\t\t.close = true,\n+\t\t.released = true,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_flock)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_FLOCK,\n+\t\t.share = LOCK_SHARE_NONE,\n+\t\t.threads = true,\n+\t\t.close = true,\n+\t\t.released = true,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_flock_dup)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_FLOCK,\n+\t\t.share = LOCK_SHARE_DUP,\n+\t\t.close = true,\n+\t\t.released = true,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_posix_fork)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_POSIX,\n+\t\t.share = LOCK_SHARE_FORK,\n+\t\t.close = true,\n+\t\t.released = true,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_flock_fork)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_FLOCK,\n+\t\t.share = LOCK_SHARE_FORK,\n+\t\t.close = true,\n+\t\t.released = false,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_ofd_fork)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_OFD,\n+\t\t.share = LOCK_SHARE_FORK,\n+\t\t.close = true,\n+\t\t.released = false,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_posix_shared_table)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_POSIX,\n+\t\t.share = LOCK_SHARE_FILES,\n+\t\t.close = true,\n+\t\t.released = false,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_userspace)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_FLOCK,\n+\t\t.share = LOCK_SHARE_NONE,\n+\t\t.close = true,\n+\t\t.userspace = true,\n+\t\t.released = true,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_F(coredump, close_files_not_requested)\n+{\n+\tconst struct close_test t = {\n+\t\t.kind = LOCK_KIND_FLOCK,\n+\t\t.share = LOCK_SHARE_NONE,\n+\t\t.threads = true,\n+\t\t.close = false,\n+\t\t.released = false,\n+\t};\n+\n+\trun_close_test(_metadata, self, \u0026t);\n+}\n+\n+TEST_HARNESS_MAIN\ndiff --git a/tools/testing/selftests/coredump/coredump_socket_protocol_test.c b/tools/testing/selftests/coredump/coredump_socket_protocol_test.c\nindex f5c9bad875463..beb167698cfdb 100644\n--- a/tools/testing/selftests/coredump/coredump_socket_protocol_test.c\n+++ b/tools/testing/selftests/coredump/coredump_socket_protocol_test.c\n@@ -2377,6 +2377,12 @@ TEST_F(coredump, socket_request_memory_types_without_kernel)\n \tcheck_conflicting_ack(_metadata, self, COREDUMP_USERSPACE | COREDUMP_MEMORY_TYPES);\n }\n \n+/* A rejected task closes everything on its way out anyway. */\n+TEST_F(coredump, socket_request_close_files_reject)\n+{\n+\tcheck_conflicting_ack(_metadata, self, COREDUMP_REJECT | COREDUMP_CLOSE_FILES);\n+}\n+\n /*\n * A server built with the first structs reads the request it knows,\n * discards the rest and acks with the ack it knows. It raises nothing\ndiff --git a/tools/testing/selftests/coredump/coredump_test_helpers.c b/tools/testing/selftests/coredump/coredump_test_helpers.c\nindex 4e36e3e4fb78f..65132a0decedd 100644\n--- a/tools/testing/selftests/coredump/coredump_test_helpers.c\n+++ b/tools/testing/selftests/coredump/coredump_test_helpers.c\n@@ -1612,7 +1612,8 @@ bool send_coredump_ack(int fd, const struct coredump_req *req,\n #define TEST_REQ_MASK_ALL\t\t\t\t\t\\\n \t(COREDUMP_KERNEL | COREDUMP_USERSPACE |\t\t\t\\\n \t COREDUMP_REJECT | COREDUMP_WAIT |\t\t\t\\\n-\t COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES)\n+\t COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES | \\\n+\t COREDUMP_CLOSE_FILES)\n \n bool check_coredump_req(const struct coredump_req *req)\n {\n"
} |
| 2/1 | 2026/08/24 17:57 | agent | patch-evaluator |
11mModel:best-expensive Args: null Results: {
"EnableConfigs": [
"COREDUMP"
],
"FocusSymbols": [
"coredump_close_files",
"coredump_task_close_files",
"vfs_coredump"
],
"Reasoning": "The patch introduces a new `COREDUMP_CLOSE_FILES` flag to the coredump protocol. When a userspace coredump handler requests this flag, the kernel closes all file descriptors of the coredumping task before generating the coredump. This involves new core kernel logic in `fs/file.c` and `fs/coredump.c` to handle file descriptor table switching and closing. The feature is reachable by crashing a process and having a userspace coredump handler reply with the new flag, making it functionally impactful and worth fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a '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: For your convenience, here is the diff of the changes:
commit 0957d000b45ea469cf234e7cad4c57051ec54716
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Aug 24 17:57:04 2026 +0000
syz-cluster: applied patch under review
diff --git a/fs/coredump.c b/fs/coredump.c
index ec9359e382ea5..a6e7da0bc16a7 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -523,7 +523,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
struct task_struct *tsk = current;
int core_waiters = -EBUSY;
- init_completion(&core_state->startup);
+ init_completion(&core_state->done);
core_state->dumper.task = tsk;
core_state->dumper.next = NULL;
@@ -531,7 +531,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
if (core_waiters > 0) {
struct core_thread *ptr;
- wait_for_completion_state(&core_state->startup,
+ wait_for_completion_state(&core_state->done,
TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
/*
* Wait for all the threads to become inactive, so that
@@ -837,7 +837,8 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *
.mask = COREDUMP_KERNEL | COREDUMP_USERSPACE |
COREDUMP_REJECT | COREDUMP_WAIT |
COREDUMP_RECORDS | COREDUMP_SPARSE |
- COREDUMP_MEMORY_TYPES,
+ COREDUMP_MEMORY_TYPES |
+ COREDUMP_CLOSE_FILES,
.size_ack = sizeof(struct coredump_ack),
.memory_types = cprm->memory_types,
.memory_types_mask = COREDUMP_MEMORY_ALL,
@@ -905,6 +906,12 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *
return false;
}
+ /* A rejected task exits right away and closes everything anyway. */
+ if ((ack.mask & COREDUMP_CLOSE_FILES) && (ack.mask & COREDUMP_REJECT)) {
+ coredump_sock_mark(cprm->file, COREDUMP_MARK_CONFLICTING);
+ return false;
+ }
+
if (ack.mask & COREDUMP_MEMORY_TYPES) {
/* The memory types need the whole field. */
if (usize < COREDUMP_ACK_SIZE_VER1) {
@@ -1225,6 +1232,11 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,
if (cprm->mask & COREDUMP_REJECT)
return;
+ if ((cprm->mask & COREDUMP_CLOSE_FILES) && !coredump_close_files()) {
+ coredump_report_failure("Failed to close files, aborting core");
+ return;
+ }
+
/* get us an unshared descriptor table; almost always a no-op */
/* The cell spufs coredump code reads the file descriptor tables */
if (unshare_files())
diff --git a/fs/file.c b/fs/file.c
index 628ca07dc4b17..3b8fce6e36951 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -375,21 +375,15 @@ static unsigned int sane_fdtable_size(struct fdtable *fdt, struct fd_range *punc
return ALIGN(last + 1, BITS_PER_LONG);
}
-/*
- * Allocate a new descriptor table and copy contents from the passed in
- * instance. Returns a pointer to cloned table on success, ERR_PTR()
- * on failure. For 'punch_hole' see sane_fdtable_size().
- */
-struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)
+/* A table with one reference and the embedded fdtable, nothing copied yet. */
+static struct files_struct *alloc_files(gfp_t gfp)
{
struct files_struct *newf;
- struct file **old_fds, **new_fds;
- unsigned int open_files, i;
- struct fdtable *old_fdt, *new_fdt;
+ struct fdtable *new_fdt;
- newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
+ newf = kmem_cache_alloc(files_cachep, gfp);
if (!newf)
- return ERR_PTR(-ENOMEM);
+ return NULL;
atomic_set(&newf->count, 1);
@@ -404,6 +398,39 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_ho
new_fdt->full_fds_bits = newf->full_fds_bits_init;
new_fdt->fd = &newf->fd_array[0];
+ return newf;
+}
+
+/* An empty descriptor table with one reference. */
+static struct files_struct *alloc_files_struct(void)
+{
+ struct files_struct *newf;
+
+ newf = alloc_files(GFP_KERNEL | __GFP_ZERO);
+ if (!newf)
+ return NULL;
+
+ rcu_assign_pointer(newf->fdt, &newf->fdtab);
+ return newf;
+}
+
+/*
+ * Allocate a new descriptor table and copy contents from the passed in
+ * instance. Returns a pointer to cloned table on success, ERR_PTR()
+ * on failure. For 'punch_hole' see sane_fdtable_size().
+ */
+struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)
+{
+ struct files_struct *newf;
+ struct file **old_fds, **new_fds;
+ unsigned int open_files, i;
+ struct fdtable *old_fdt, *new_fdt;
+
+ newf = alloc_files(GFP_KERNEL);
+ if (!newf)
+ return ERR_PTR(-ENOMEM);
+ new_fdt = &newf->fdtab;
+
spin_lock(&oldf->file_lock);
old_fdt = files_fdtable(oldf);
open_files = sane_fdtable_size(old_fdt, punch_hole);
@@ -480,6 +507,11 @@ static struct fdtable *close_files(struct files_struct * files)
*/
struct fdtable *fdt = rcu_dereference_raw(files->fdt);
unsigned int i, j = 0;
+ /*
+ * A kernel thread that might be needed to make progress on some
+ * umount must not run __fput() itself, see __fput_sync().
+ */
+ bool sync = !(current->flags & PF_KTHREAD);
for (;;) {
unsigned long set;
@@ -491,7 +523,10 @@ static struct fdtable *close_files(struct files_struct * files)
if (set & 1) {
struct file *file = fdt->fd[i];
if (file) {
- filp_close(file, files);
+ if (sync)
+ filp_close_sync(file, files);
+ else
+ filp_close(file, files);
cond_resched();
}
}
@@ -515,16 +550,97 @@ void put_files_struct(struct files_struct *files)
}
}
+/* Hand @tsk the table @files, or none, and drop the one it had. */
+static void switch_files_struct(struct task_struct *tsk,
+ struct files_struct *files)
+{
+ struct files_struct *old;
+
+ task_lock(tsk);
+ old = tsk->files;
+ if (old) {
+ if (files)
+ atomic_inc(&files->count);
+ tsk->files = files;
+ }
+ task_unlock(tsk);
+ if (old)
+ put_files_struct(old);
+}
+
void exit_files(struct task_struct *tsk)
{
- struct files_struct * files = tsk->files;
+ if (!tsk->files)
+ return;
+ switch_files_struct(tsk, NULL);
+}
+
+/*
+ * The thread-group is waiting in coredump_task_exit(). Wake them and
+ * tell them to get rid of their files.
+ */
+bool coredump_close_files(void)
+{
+ struct core_state *core_state = current->signal->core_state;
+ struct files_struct *files;
+ struct core_thread *ct;
+ int nr = 0;
- if (files) {
- task_lock(tsk);
- tsk->files = NULL;
- task_unlock(tsk);
- put_files_struct(files);
+ files = alloc_files_struct();
+ if (!files)
+ return false;
+
+ /* Tasks without a table, vhost workers say, sit the switch out. */
+ for (ct = core_state->dumper.next; ct; ct = ct->next)
+ if (ct->task->files)
+ nr++;
+ atomic_set(&core_state->nr_threads, nr);
+ reinit_completion(&core_state->done);
+
+ for (ct = core_state->dumper.next; ct; ct = ct->next) {
+ if (!ct->task->files)
+ continue;
+ /* Pairs with the acquire in coredump_task_close_files(). */
+ smp_store_release(&ct->files, files);
+ wake_up_process(ct->task);
}
+
+ /* The dumper's subjective creds are the dump's, not its own. */
+ scoped_with_creds(current_real_cred())
+ switch_files_struct(current, files);
+
+ if (nr) {
+ wait_for_completion_state(&core_state->done,
+ TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
+ /* Back asleep before the notes read their registers. */
+ for (ct = core_state->dumper.next; ct; ct = ct->next)
+ wait_task_inactive(ct->task, TASK_ANY);
+ }
+
+ put_files_struct(files);
+ return true;
+}
+
+/*
+ * Get rid of the files and switch to the empty file descriptor table
+ * the dumping thread published in this thread's core_thread entry.
+ */
+bool coredump_task_close_files(struct core_thread *self)
+{
+ struct core_state *core_state;
+ struct files_struct *files;
+
+ /* Pairs with the release in coredump_close_files(). */
+ files = smp_load_acquire(&self->files);
+ if (!files || !current->files || current->files == files)
+ return false;
+
+ __set_current_state(TASK_RUNNING);
+ switch_files_struct(current, files);
+ core_state = current->signal->core_state;
+ if (atomic_dec_and_test(&core_state->nr_threads))
+ complete(&core_state->done);
+ return true;
}
struct files_struct init_files = {
diff --git a/fs/file_table.c b/fs/file_table.c
index c68b8c0a4097a..3c705917646b5 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -13,6 +13,7 @@
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/filelock.h>
+#include <linux/dnotify.h>
#include <linux/security.h>
#include <linux/cred.h>
#include <linux/eventpoll.h>
@@ -606,15 +607,53 @@ void __fput_sync(struct file *file)
EXPORT_SYMBOL(__fput_sync);
/*
- * Equivalent to __fput_sync(), but optimized for being called with the last
- * reference.
- *
- * See file_ref_put_close() for details.
+ * "id" is the POSIX thread ID. We use the
+ * files pointer for this..
*/
-void fput_close_sync(struct file *file)
+static int filp_flush(struct file *filp, fl_owner_t id)
{
- if (likely(file_ref_put_close(&file->f_ref)))
- __fput(file);
+ int retval = 0;
+
+ if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,
+ "VFS: Close: file count is 0 (f_op=%ps)",
+ filp->f_op)) {
+ return 0;
+ }
+
+ if (filp->f_op->flush)
+ retval = filp->f_op->flush(filp, id);
+
+ if (likely(!(filp->f_mode & FMODE_PATH))) {
+ dnotify_flush(filp, id);
+ locks_remove_posix(filp, id);
+ }
+ return retval;
+}
+
+int filp_close(struct file *filp, fl_owner_t id)
+{
+ int retval;
+
+ retval = filp_flush(filp, id);
+ fput_close(filp);
+
+ return retval;
+}
+EXPORT_SYMBOL(filp_close);
+
+/*
+ * Like filp_close() but without deferring the release of the last
+ * reference, see file_ref_put_close() for details.
+ */
+int filp_close_sync(struct file *filp, fl_owner_t id)
+{
+ int retval;
+
+ retval = filp_flush(filp, id);
+ if (likely(file_ref_put_close(&filp->f_ref)))
+ __fput(filp);
+
+ return retval;
}
/*
diff --git a/fs/internal.h b/fs/internal.h
index 355d93f922086..7ba8cd26a698c 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -127,7 +127,7 @@ static inline void put_file_access(struct file *file)
}
}
-void fput_close_sync(struct file *);
+int filp_close_sync(struct file *filp, fl_owner_t id);
void fput_close(struct file *);
/*
@@ -190,6 +190,7 @@ struct open_flags {
int intent;
int lookup_flags;
};
+
extern struct file *do_file_open(int dfd, struct filename *pathname,
const struct open_flags *op);
extern struct file *do_file_open_root(const struct path *,
diff --git a/fs/open.c b/fs/open.c
index 408925d7bd0b7..d036736d5d435 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1453,41 +1453,6 @@ SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
}
#endif
-/*
- * "id" is the POSIX thread ID. We use the
- * files pointer for this..
- */
-static int filp_flush(struct file *filp, fl_owner_t id)
-{
- int retval = 0;
-
- if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,
- "VFS: Close: file count is 0 (f_op=%ps)",
- filp->f_op)) {
- return 0;
- }
-
- if (filp->f_op->flush)
- retval = filp->f_op->flush(filp, id);
-
- if (likely(!(filp->f_mode & FMODE_PATH))) {
- dnotify_flush(filp, id);
- locks_remove_posix(filp, id);
- }
- return retval;
-}
-
-int filp_close(struct file *filp, fl_owner_t id)
-{
- int retval;
-
- retval = filp_flush(filp, id);
- fput_close(filp);
-
- return retval;
-}
-EXPORT_SYMBOL(filp_close);
-
/*
* Careful here! We test whether the file pointer is NULL before
* releasing the fd. This ensures that one clone task can't release
@@ -1502,13 +1467,11 @@ SYSCALL_DEFINE1(close, unsigned int, fd)
if (!file)
return -EBADF;
- retval = filp_flush(file, current->files);
-
/*
* We're returning to user space. Don't bother
* with any delayed fput() cases.
*/
- fput_close_sync(file);
+ retval = filp_close_sync(file, current->files);
if (likely(retval == 0))
return 0;
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index c45306a9f0072..f0c1003da8ce8 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -99,11 +99,16 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *fil
struct task_struct;
+struct core_thread;
+
void put_files_struct(struct files_struct *fs);
+bool coredump_close_files(void);
+bool coredump_task_close_files(struct core_thread *self);
int unshare_files(void);
struct fd_range {
unsigned int from, to;
};
+
struct files_struct *dup_fd(struct files_struct *, struct fd_range *) __latent_entropy;
void do_close_on_exec(struct files_struct *);
int iterate_fd(struct files_struct *, unsigned,
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 584ae88b435e9..fed02a427de31 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -76,12 +76,15 @@ struct multiprocess_signals {
struct core_thread {
struct task_struct *task;
struct core_thread *next;
+ /* The empty table to switch to, published by the dumping thread. */
+ struct files_struct *files;
};
struct core_state {
atomic_t nr_threads;
struct core_thread dumper;
- struct completion startup;
+ /* Completed by the last thread to park, reused for the table switch. */
+ struct completion done;
};
/*
diff --git a/include/uapi/linux/coredump.h b/include/uapi/linux/coredump.h
index 6d0c53b534ea8..ec09d7ab01314 100644
--- a/include/uapi/linux/coredump.h
+++ b/include/uapi/linux/coredump.h
@@ -19,6 +19,9 @@
* @COREDUMP_MEMORY_TYPES: dump the memory types in
* coredump_ack->memory_types instead of the ones
* the task selected; requires COREDUMP_KERNEL
+ * @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the
+ * coredump is generated; incompatible with
+ * COREDUMP_REJECT
*/
enum {
COREDUMP_KERNEL = (1ULL << 0),
@@ -28,6 +31,7 @@ enum {
COREDUMP_RECORDS = (1ULL << 4),
COREDUMP_SPARSE = (1ULL << 5),
COREDUMP_MEMORY_TYPES = (1ULL << 6),
+ COREDUMP_CLOSE_FILES = (1ULL << 7),
};
/**
@@ -137,6 +141,10 @@ enum {
* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't
* raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of
* at least COREDUMP_ACK_SIZE_VER1 bytes.
+ *
+ * If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file
+ * descriptors of the coredumping task before it generates the coredump.
+ * The task ends up with an empty descriptor table.
*/
struct coredump_ack {
__u32 size;
diff --git a/kernel/exit.c b/kernel/exit.c
index 2c0b1c02920f4..b651679c12e97 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -25,6 +25,7 @@
#include <linux/acct.h>
#include <linux/tsacct_kern.h>
#include <linux/file.h>
+#include <linux/fdtable.h>
#include <linux/freezer.h>
#include <linux/binfmts.h>
#include <linux/nsproxy.h>
@@ -432,6 +433,7 @@ static void coredump_task_exit(struct task_struct *tsk,
struct core_thread self;
self.task = tsk;
+ self.files = NULL;
if (self.task->flags & PF_SIGNALED)
self.next = xchg(&core_state->dumper.next, &self);
else
@@ -441,12 +443,15 @@ static void coredump_task_exit(struct task_struct *tsk,
* to core_state->dumper.
*/
if (atomic_dec_and_test(&core_state->nr_threads))
- complete(&core_state->startup);
+ complete(&core_state->done);
for (;;) {
set_current_state(TASK_IDLE|TASK_FREEZABLE);
if (!self.task) /* see coredump_finish() */
break;
+ /* see coredump_close_files() */
+ if (coredump_task_close_files(&self))
+ continue;
schedule();
}
__set_current_state(TASK_RUNNING);
diff --git a/tools/include/uapi/linux/coredump.h b/tools/include/uapi/linux/coredump.h
index 6d0c53b534ea8..ec09d7ab01314 100644
--- a/tools/include/uapi/linux/coredump.h
+++ b/tools/include/uapi/linux/coredump.h
@@ -19,6 +19,9 @@
* @COREDUMP_MEMORY_TYPES: dump the memory types in
* coredump_ack->memory_types instead of the ones
* the task selected; requires COREDUMP_KERNEL
+ * @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the
+ * coredump is generated; incompatible with
+ * COREDUMP_REJECT
*/
enum {
COREDUMP_KERNEL = (1ULL << 0),
@@ -28,6 +31,7 @@ enum {
COREDUMP_RECORDS = (1ULL << 4),
COREDUMP_SPARSE = (1ULL << 5),
COREDUMP_MEMORY_TYPES = (1ULL << 6),
+ COREDUMP_CLOSE_FILES = (1ULL << 7),
};
/**
@@ -137,6 +141,10 @@ enum {
* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't
* raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of
* at least COREDUMP_ACK_SIZE_VER1 bytes.
+ *
+ * If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file
+ * descriptors of the coredumping task before it generates the coredump.
+ * The task ends up with an empty descriptor table.
*/
struct coredump_ack {
__u32 size;
diff --git a/tools/testing/selftests/coredump/Makefile b/tools/testing/selftests/coredump/Makefile
index dece1a31d561b..dc8489d406184 100644
--- a/tools/testing/selftests/coredump/Makefile
+++ b/tools/testing/selftests/coredump/Makefile
@@ -3,7 +3,8 @@ CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
TEST_GEN_PROGS := stackdump_test \
coredump_socket_test \
- coredump_socket_protocol_test
+ coredump_socket_protocol_test \
+ coredump_close_files_test
TEST_FILES := stackdump
include ../lib.mk
@@ -11,3 +12,4 @@ include ../lib.mk
$(OUTPUT)/stackdump_test: coredump_test_helpers.c
$(OUTPUT)/coredump_socket_test: coredump_test_helpers.c
$(OUTPUT)/coredump_socket_protocol_test: coredump_test_helpers.c
+$(OUTPUT)/coredump_close_files_test: coredump_test_helpers.c
diff --git a/tools/testing/selftests/coredump/coredump_close_files_test.c b/tools/testing/selftests/coredump/coredump_close_files_test.c
new file mode 100644
index 0000000000000..7a41906b46952
--- /dev/null
+++ b/tools/testing/selftests/coredump/coredump_close_files_test.c
@@ -0,0 +1,592 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "coredump_test.h"
+
+#define LOCK_FILE "/tmp/coredump.lock"
+
+/* Idle threads the multi-threaded crashing child spawns. */
+#define NUM_CLOSE_THREADS 4
+
+/* How the crashing child locks LOCK_FILE. */
+enum lock_kind {
+ LOCK_KIND_POSIX,
+ LOCK_KIND_OFD,
+ LOCK_KIND_FLOCK,
+};
+
+/* Who else has a handle on the lock when the child crashes. */
+enum lock_share {
+ LOCK_SHARE_NONE,
+ LOCK_SHARE_DUP, /* a second slot in the child's own table */
+ LOCK_SHARE_FORK, /* a forked process keeps the fd open */
+ LOCK_SHARE_FILES, /* a CLONE_FILES process shares the table */
+};
+
+struct close_test {
+ enum lock_kind kind;
+ enum lock_share share;
+ bool threads;
+ bool close; /* ack with COREDUMP_CLOSE_FILES */
+ bool userspace; /* COREDUMP_USERSPACE instead of COREDUMP_KERNEL */
+ bool released; /* the lock is gone once the kernel is past the close */
+};
+
+FIXTURE_SETUP(coredump)
+{
+ FILE *file;
+ int ret;
+
+ self->pid_coredump_server = -ESRCH;
+ self->fd_tmpfs_detached = -1;
+ file = fopen("/proc/sys/kernel/core_pattern", "r");
+ ASSERT_NE(NULL, file);
+
+ ret = fread(self->original_core_pattern, 1, sizeof(self->original_core_pattern), file);
+ ASSERT_TRUE(ret || feof(file));
+ ASSERT_LT(ret, sizeof(self->original_core_pattern));
+
+ self->original_core_pattern[ret] = '\0';
+ self->fd_tmpfs_detached = create_detached_tmpfs();
+ ASSERT_GE(self->fd_tmpfs_detached, 0);
+
+ ret = fclose(file);
+ ASSERT_EQ(0, ret);
+}
+
+FIXTURE_TEARDOWN(coredump)
+{
+ const char *reason;
+ FILE *file;
+ int ret, status;
+
+ if (self->pid_coredump_server > 0) {
+ kill(self->pid_coredump_server, SIGTERM);
+ waitpid(self->pid_coredump_server, &status, 0);
+ }
+ unlink(LOCK_FILE);
+ unlink("/tmp/coredump.socket");
+
+ file = fopen("/proc/sys/kernel/core_pattern", "w");
+ if (!file) {
+ reason = "Unable to open core_pattern";
+ goto fail;
+ }
+
+ ret = fprintf(file, "%s", self->original_core_pattern);
+ if (ret < 0) {
+ reason = "Unable to write to core_pattern";
+ goto fail;
+ }
+
+ ret = fclose(file);
+ if (ret) {
+ reason = "Unable to close core_pattern";
+ goto fail;
+ }
+
+ if (self->fd_tmpfs_detached >= 0) {
+ ret = close(self->fd_tmpfs_detached);
+ if (ret < 0) {
+ reason = "Unable to close detached tmpfs";
+ goto fail;
+ }
+ self->fd_tmpfs_detached = -1;
+ }
+
+ return;
+fail:
+ /* This should never happen */
+ fprintf(stderr, "Failed to cleanup coredump test: %s\n", reason);
+}
+
+/* Write-lock @fd the way @kind says. */
+static int take_lock(int fd, enum lock_kind kind)
+{
+ struct flock fl = {
+ .l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ };
+
+ switch (kind) {
+ case LOCK_KIND_POSIX:
+ return fcntl(fd, F_SETLK, &fl);
+ case LOCK_KIND_OFD:
+ return fcntl(fd, F_OFD_SETLK, &fl);
+ case LOCK_KIND_FLOCK:
+ return flock(fd, LOCK_EX);
+ }
+
+ return -1;
+}
+
+/* Does anyone else hold a write lock on @fd? 1 if so, 0 if not, -1 on error. */
+static int lock_held(int fd, enum lock_kind kind)
+{
+ struct flock fl = {
+ .l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ };
+
+ if (kind == LOCK_KIND_FLOCK) {
+ if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
+ flock(fd, LOCK_UN);
+ return 0;
+ }
+ return errno == EWOULDBLOCK ? 1 : -1;
+ }
+
+ /* F_GETLK reports conflicting OFD locks too. */
+ if (fcntl(fd, F_GETLK, &fl) < 0)
+ return -1;
+ return fl.l_type != F_UNLCK;
+}
+
+/* Number of entries in /proc/@pid/fd, the lowest one in @first. */
+static int count_fds(pid_t pid, int *first)
+{
+ char path[64];
+ struct dirent *de;
+ DIR *dir;
+ int nr = 0;
+
+ snprintf(path, sizeof(path), "/proc/%d/fd", pid);
+ dir = opendir(path);
+ if (!dir)
+ return -1;
+
+ *first = -1;
+ while ((de = readdir(dir))) {
+ int fd;
+
+ if (de->d_name[0] == '.')
+ continue;
+ fd = atoi(de->d_name);
+ if (*first < 0 || fd < *first)
+ *first = fd;
+ nr++;
+ }
+ closedir(dir);
+ return nr;
+}
+
+/*
+ * Block until the test hangs up @fd_release, keeping every inherited fd
+ * open, then report through @fd_result whether @fd is still open.
+ */
+static void hold_until_released(int fd, int fd_release, int fd_result)
+{
+ char c;
+
+ read_nointr(fd_release, &c, 1);
+ c = fcntl(fd, F_GETFD) < 0 ? 'C' : 'O';
+ write_nointr(fd_result, &c, 1);
+ _exit(EXIT_SUCCESS);
+}
+
+/* Lock LOCK_FILE, share it as requested, then crash. */
+static void crashing_child_locked(const struct close_test *t, int fd_release,
+ int fd_result)
+{
+ pthread_t thread;
+ int fd, pidfd, i;
+ pid_t pid;
+
+ fd = open(LOCK_FILE, O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ _exit(EXIT_FAILURE);
+
+ if (take_lock(fd, t->kind))
+ _exit(EXIT_FAILURE);
+
+ switch (t->share) {
+ case LOCK_SHARE_NONE:
+ break;
+ case LOCK_SHARE_DUP:
+ if (dup(fd) < 0)
+ _exit(EXIT_FAILURE);
+ break;
+ case LOCK_SHARE_FORK:
+ pid = fork();
+ if (pid < 0)
+ _exit(EXIT_FAILURE);
+ if (pid == 0)
+ hold_until_released(fd, fd_release, fd_result);
+ break;
+ case LOCK_SHARE_FILES:
+ pid = create_child(&pidfd, CLONE_FILES);
+ if (pid < 0)
+ _exit(EXIT_FAILURE);
+ if (pid == 0)
+ hold_until_released(fd, fd_release, fd_result);
+ break;
+ }
+
+ if (t->threads)
+ for (i = 0; i < NUM_CLOSE_THREADS; i++)
+ pthread_create(&thread, NULL, do_nothing, NULL);
+
+ /* crash on purpose */
+ *(volatile int *)NULL = 0;
+}
+
+/*
+ * Serve one coredump and look at the task on the way. Before the ack the
+ * lock is held and the descriptors are there. Once the kernel is past the
+ * point where it closes them, which is before the first byte of the dump
+ * or before the hangup in userspace mode, they are gone if we asked for
+ * it and the lock is in the expected state.
+ */
+static int close_server(const struct close_test *t, int fd_ipc)
+{
+ struct coredump_req req = {};
+ struct pidfd_info info = {};
+ int fd_server = -1, fd_coredump = -1, fd_peer_pidfd = -1, fd_lock = -1;
+ int exit_code = EXIT_FAILURE;
+ int fd, first_fd, nr_fds;
+ __u64 mask;
+ ssize_t bytes;
+ char c;
+
+ fd_lock = open(LOCK_FILE, O_RDWR | O_CLOEXEC);
+ if (fd_lock < 0) {
+ fprintf(stderr, "%s: open lock file failed: %m\n", __func__);
+ goto out;
+ }
+
+ fd_server = create_and_listen_unix_socket("/tmp/coredump.socket");
+ if (fd_server < 0) {
+ fprintf(stderr, "%s: create_and_listen_unix_socket failed: %m\n", __func__);
+ goto out;
+ }
+
+ if (write_nointr(fd_ipc, "1", 1) < 0) {
+ fprintf(stderr, "%s: write_nointr to ipc socket failed: %m\n", __func__);
+ goto out;
+ }
+ close(fd_ipc);
+
+ fd_coredump = accept4(fd_server, NULL, NULL, SOCK_CLOEXEC);
+ if (fd_coredump < 0) {
+ fprintf(stderr, "%s: accept4 failed: %m\n", __func__);
+ goto out;
+ }
+
+ fd_peer_pidfd = get_peer_pidfd(fd_coredump);
+ if (fd_peer_pidfd < 0) {
+ fprintf(stderr, "%s: get_peer_pidfd failed\n", __func__);
+ goto out;
+ }
+
+ if (!get_pidfd_info(fd_peer_pidfd, &info)) {
+ fprintf(stderr, "%s: get_pidfd_info failed\n", __func__);
+ goto out;
+ }
+
+ if (!read_coredump_req(fd_coredump, &req)) {
+ fprintf(stderr, "%s: read_coredump_req failed\n", __func__);
+ goto out;
+ }
+
+ if (!check_coredump_req(&req)) {
+ fprintf(stderr, "%s: check_coredump_req failed\n", __func__);
+ goto out;
+ }
+
+ /* The task waits for our answer with everything still in place. */
+ if (lock_held(fd_lock, t->kind) != 1) {
+ fprintf(stderr, "%s: lock not held during the handshake\n", __func__);
+ goto out;
+ }
+
+ nr_fds = count_fds(info.pid, &first_fd);
+ if (nr_fds <= 0) {
+ fprintf(stderr, "%s: no descriptors during the handshake\n", __func__);
+ goto out;
+ }
+
+ fd = sys_pidfd_getfd(fd_peer_pidfd, first_fd, 0);
+ if (fd < 0) {
+ fprintf(stderr, "%s: pidfd_getfd during the handshake failed: %m\n", __func__);
+ goto out;
+ }
+ close(fd);
+
+ mask = COREDUMP_WAIT;
+ mask |= t->userspace ? COREDUMP_USERSPACE : COREDUMP_KERNEL;
+ if (t->close)
+ mask |= COREDUMP_CLOSE_FILES;
+
+ if (!send_coredump_ack(fd_coredump, &req, mask, 0)) {
+ fprintf(stderr, "%s: send_coredump_ack failed\n", __func__);
+ goto out;
+ }
+
+ if (!read_marker(fd_coredump, COREDUMP_MARK_REQACK)) {
+ fprintf(stderr, "%s: read_marker COREDUMP_MARK_REQACK failed\n", __func__);
+ goto out;
+ }
+
+ bytes = read_nointr(fd_coredump, &c, 1);
+ if (bytes != (t->userspace ? 0 : 1)) {
+ fprintf(stderr, "%s: read after the ack returned %zd: %m\n", __func__, bytes);
+ goto out;
+ }
+
+ if (lock_held(fd_lock, t->kind) != !t->released) {
+ fprintf(stderr, "%s: lock %s while the coredump is generated\n",
+ __func__, t->released ? "still held" : "released");
+ goto out;
+ }
+
+ nr_fds = count_fds(info.pid, &first_fd);
+ if (nr_fds < 0 || !nr_fds != t->close) {
+ fprintf(stderr, "%s: %d descriptors while the coredump is generated\n",
+ __func__, nr_fds);
+ goto out;
+ }
+
+ fd = sys_pidfd_getfd(fd_peer_pidfd, first_fd, 0);
+ if (t->close) {
+ if (fd >= 0 || errno != EBADF) {
+ fprintf(stderr, "%s: pidfd_getfd after the close returned %d: %m\n",
+ __func__, fd);
+ goto out;
+ }
+ } else {
+ if (fd < 0) {
+ fprintf(stderr, "%s: pidfd_getfd during the coredump failed: %m\n",
+ __func__);
+ goto out;
+ }
+ close(fd);
+ }
+
+ /* COREDUMP_WAIT keeps the task around until we hang up. */
+ if (!get_pidfd_info(fd_peer_pidfd, &info)) {
+ fprintf(stderr, "%s: get_pidfd_info failed\n", __func__);
+ goto out;
+ }
+
+ if (info.mask & PIDFD_INFO_EXIT) {
+ fprintf(stderr, "%s: task exited before the coredump finished\n", __func__);
+ goto out;
+ }
+
+ for (;;) {
+ char buffer[4096];
+
+ bytes = read_nointr(fd_coredump, buffer, sizeof(buffer));
+ if (bytes < 0) {
+ fprintf(stderr, "%s: read from coredump socket failed: %m\n", __func__);
+ goto out;
+ }
+
+ if (bytes == 0)
+ break;
+ }
+
+ exit_code = EXIT_SUCCESS;
+out:
+ if (fd_lock >= 0)
+ close(fd_lock);
+ if (fd_peer_pidfd >= 0)
+ close(fd_peer_pidfd);
+ if (fd_coredump >= 0)
+ close(fd_coredump);
+ if (fd_server >= 0)
+ close(fd_server);
+ return exit_code;
+}
+
+static void run_close_test(struct __test_metadata *const _metadata,
+ FIXTURE_DATA(coredump) *self,
+ const struct close_test *t)
+{
+ int fd, status, ipc_sockets[2], release_pipe[2], result_pipe[2];
+ pid_t pid, pid_coredump_server;
+ char c;
+
+ ASSERT_TRUE(set_core_pattern("@@/tmp/coredump.socket"));
+
+ fd = open(LOCK_FILE, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
+ ASSERT_GE(fd, 0);
+ EXPECT_EQ(close(fd), 0);
+
+ ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets), 0);
+
+ pid_coredump_server = fork();
+ ASSERT_GE(pid_coredump_server, 0);
+ if (pid_coredump_server == 0) {
+ close(ipc_sockets[0]);
+ _exit(close_server(t, ipc_sockets[1]));
+ }
+ self->pid_coredump_server = pid_coredump_server;
+
+ EXPECT_EQ(close(ipc_sockets[1]), 0);
+ ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
+ EXPECT_EQ(close(ipc_sockets[0]), 0);
+
+ /* Only the crashing child and what it spawns see these pipes. */
+ ASSERT_EQ(pipe2(release_pipe, O_CLOEXEC), 0);
+ ASSERT_EQ(pipe2(result_pipe, O_CLOEXEC), 0);
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+ if (pid == 0) {
+ close(release_pipe[1]);
+ close(result_pipe[0]);
+ crashing_child_locked(t, release_pipe[0], result_pipe[1]);
+ }
+ EXPECT_EQ(close(release_pipe[0]), 0);
+ EXPECT_EQ(close(result_pipe[1]), 0);
+
+ waitpid(pid, &status, 0);
+ ASSERT_TRUE(WIFSIGNALED(status));
+ ASSERT_TRUE(WCOREDUMP(status));
+
+ wait_and_check_coredump_server(pid_coredump_server, _metadata, self);
+
+ /* Let the process holding the shared lock go. */
+ EXPECT_EQ(close(release_pipe[1]), 0);
+
+ /* The crashing child is gone but what it shared with is untouched. */
+ if (t->share == LOCK_SHARE_FORK || t->share == LOCK_SHARE_FILES) {
+ ASSERT_EQ(read_nointr(result_pipe[0], &c, 1), 1);
+ ASSERT_EQ(c, 'O');
+ }
+ EXPECT_EQ(close(result_pipe[0]), 0);
+}
+
+TEST_F(coredump, close_files_posix)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_POSIX,
+ .share = LOCK_SHARE_NONE,
+ .threads = true,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_ofd)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_OFD,
+ .share = LOCK_SHARE_NONE,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_flock)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_NONE,
+ .threads = true,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_flock_dup)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_DUP,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_posix_fork)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_POSIX,
+ .share = LOCK_SHARE_FORK,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_flock_fork)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_FORK,
+ .close = true,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_ofd_fork)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_OFD,
+ .share = LOCK_SHARE_FORK,
+ .close = true,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_posix_shared_table)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_POSIX,
+ .share = LOCK_SHARE_FILES,
+ .close = true,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_userspace)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_NONE,
+ .close = true,
+ .userspace = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_not_requested)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_NONE,
+ .threads = true,
+ .close = false,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/coredump/coredump_socket_protocol_test.c b/tools/testing/selftests/coredump/coredump_socket_protocol_test.c
index f5c9bad875463..beb167698cfdb 100644
--- a/tools/testing/selftests/coredump/coredump_socket_protocol_test.c
+++ b/tools/testing/selftests/coredump/coredump_socket_protocol_test.c
@@ -2377,6 +2377,12 @@ TEST_F(coredump, socket_request_memory_types_without_kernel)
check_conflicting_ack(_metadata, self, COREDUMP_USERSPACE | COREDUMP_MEMORY_TYPES);
}
+/* A rejected task closes everything on its way out anyway. */
+TEST_F(coredump, socket_request_close_files_reject)
+{
+ check_conflicting_ack(_metadata, self, COREDUMP_REJECT | COREDUMP_CLOSE_FILES);
+}
+
/*
* A server built with the first structs reads the request it knows,
* discards the rest and acks with the ack it knows. It raises nothing
diff --git a/tools/testing/selftests/coredump/coredump_test_helpers.c b/tools/testing/selftests/coredump/coredump_test_helpers.c
index 4e36e3e4fb78f..65132a0decedd 100644
--- a/tools/testing/selftests/coredump/coredump_test_helpers.c
+++ b/tools/testing/selftests/coredump/coredump_test_helpers.c
@@ -1612,7 +1612,8 @@ bool send_coredump_ack(int fd, const struct coredump_req *req,
#define TEST_REQ_MASK_ALL \
(COREDUMP_KERNEL | COREDUMP_USERSPACE | \
COREDUMP_REJECT | COREDUMP_WAIT | \
- COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES)
+ COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES | \
+ COREDUMP_CLOSE_FILES)
bool check_coredump_req(const struct coredump_req *req)
{
|
| 3/2 | 2026/08/24 17:57 | llm | patch-evaluator |
5mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 15493 output: 18 thoughts: 362 Thoughts: **My Analysis of this Coredump Patch** Okay, so this patch is digging into the Linux kernel's coredump subsystem, and it looks interesting from a security perspective. It's adding a new flag, `COREDUMP_CLOSE_FILES`, to the coredump protocol. The crucial bit is that when a userspace handler sends back an ACK with this flag set, the kernel will close *all* file descriptors of the crashing task before generating the core dump. Right away, I'm thinking: reachable? Fuzzable? This looks promising. The coredump functionality itself is definitely reachable, no problem there. And this new `COREDUMP_CLOSE_FILES` flag is part of the UAPI, which means a userspace coredump handler can trigger it. That makes it ripe for exploration. I see changes sprinkled across the filesystem code (`fs/coredump.c`, `fs/file.c`, `fs/file_table.c`, `fs/internal.h`, `fs/open.c`, `include/linux/fdtable.h`, `include/linux/sched/signal.h`, `include/uapi/linux/coredump.h`) and even the kernel's exit logic (`kernel/exit.c`). This tells me the core kernel is involved. Closing file descriptors in a controlled manner, or not, is always a sensitive operation. My next step is to pinpoint the specific functions I'll want to focus on for fuzzing. Looks like `coredump_close_files`, `coredump_task_close_files`, and `filp_close_sync` are the most relevant in the filesystem area. `coredump_sock_request` and `do_coredump` need further analysis. These are all worth a close look and fuzzing. Finally, I need to check the Kconfig options. Coredumps are enabled via `CONFIG_COREDUMP`. I need to verify that this is set, otherwise, all this work is for nothing. |
| 4/2 | 2026/08/24 18:02 | tool | grepper |
0mArgs:{
"Expression": "config COREDUMP"
}Results: {
"Output": "fs/Kconfig.binfmt=145=config BINFMT_MISC\n--\nfs/Kconfig.binfmt-170-\nfs/Kconfig.binfmt:171:config COREDUMP\nfs/Kconfig.binfmt-172-\tbool \"Enable core dump support\" if EXPERT\n"
} |
| 5/2 | 2026/08/24 18:02 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 16389 output: 31 thoughts: 12 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Analyzing the `COREDUMP` Dependencies** Okay, so I need to understand the dependencies related to `COREDUMP`. My initial instinct is to delve into the various layers involved in generating, handling, and interpreting core dumps. I'm thinking about the kernel-level components that trigger the dump – signals like SIGSEGV, SIGABRT, SIGILL, etc. – and how those are configured, potentially through `/proc/sys/kernel/core_pattern` or similar sysctl settings. Then, I'm considering the userspace utilities: `gdb`, `lldb`, `coredumpctl`, and the whole range of debuggers that can actually *use* the core file. What are the key libraries these tools rely on for parsing the core dump format? ELF, DWARF, the debugging interface (DBI)... I need to map out the connections. Am I dealing with a specific architecture, which may influence the core file format? I'm also considering the implications of the core dump settings themselves: size limits (ulimit -c), the core file naming convention (potentially injecting process IDs, timestamps, etc.), and whether the dump is being captured in a file at all, or potentially piped to a debugger directly. I should also factor in the possibility of external dependencies – tools that might pre-process or post-process the core dumps. Are there any custom scripts or tools within this system that are involved in core dump analysis or manipulation? Understanding any custom tools or modifications in place is crucial. Ultimately, I need a holistic view: what triggers a core dump, how is it generated, what are the restrictions on the core dump, what tools interact with the core dump, and what tools may analyze the core dump? This is the core dependency graph I need to understand. |
| 6/2 | 2026/08/24 18:02 | tool | grepper |
0mArgs:{
"Expression": "config COREDUMP",
"PathPrefix": "fs/Kconfig.binfmt"
}Results: {
"Output": "fs/Kconfig.binfmt=145=config BINFMT_MISC\n--\nfs/Kconfig.binfmt-170-\nfs/Kconfig.binfmt:171:config COREDUMP\nfs/Kconfig.binfmt-172-\tbool \"Enable core dump support\" if EXPERT\n"
} |
| 7/2 | 2026/08/24 18:02 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 16910 output: 35 thoughts: 20 Thoughts: Here's my summary, as though I'm reading the provided text and thinking through the implications: **Analyzing the Kernel's Binary Format Configuration** Okay, let's dive into the kernel configuration file, specifically the `fs/Kconfig.binfmt` section. My experience tells me that this is where the kernel defines its handling of various binary formats, like executables. The line number provided, around 171, usually points to a significant block of code, often dealing with the low-level details of how the kernel identifies and executes different program types. This is critical for understanding the flexibility and security of the kernel, as it dictates what can be run on the system and how. I need to see what's being configured, what default values are set, and any conditional logic that might be in place. I'm going to look for any clues about what new binary format support has been added, and if any security options are in play. |
| 8/2 | 2026/08/24 18:02 | tool | read-file |
0mArgs:{
"File": "fs/Kconfig.binfmt",
"FirstLine": 165,
"LineCount": 20
}Results: {
"Contents": " 165:\t\t\tmount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc\n 166:\t\n 167:\t\t You may say M here for module support and later load the module when\n 168:\t\t you have use for it; the module is called binfmt_misc. If you\n 169:\t\t don't know what to answer at this point, say Y.\n 170:\t\n 171:\tconfig COREDUMP\n 172:\t\tbool \"Enable core dump support\" if EXPERT\n 173:\t\tdefault y\n 174:\t\thelp\n 175:\t\t This option enables support for performing core dumps. You almost\n 176:\t\t certainly want to say Y here. Not necessary on systems that never\n 177:\t\t need debugging or only ever run flawless code.\n 178:\t\n 179:\tconfig EXEC_KUNIT_TEST\n 180:\t\tbool \"Build execve tests\" if !KUNIT_ALL_TESTS\n 181:\t\tdepends on KUNIT=y\n 182:\t\tdefault KUNIT_ALL_TESTS\n 183:\t\thelp\n 184:\t\t This builds the exec KUnit tests, which tests boundary conditions\n"
} |
| 9/2 | 2026/08/24 18:02 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 17496 output: 30 thoughts: 341 Thoughts: **My Initial Assessment of Core Dump Configuration and Implementation** Okay, so `COREDUMP` seems to be enabled by default. I need to figure out the scope of its behavior, so I need to check for any related configurations. It looks like the main code is spread across `fs/coredump.c`, `fs/file.c`, and `kernel/exit.c`. Let's see if there's anything specific to control the closing of files during a core dump; is there something like `COREDUMP_CLOSE_FILES`? Nope, it's just a simple flag. Now, I should examine the relevant functions to understand how this all works. I'm focusing on `coredump_close_files`, `coredump_task_close_files`, `filp_close_sync`, `coredump_sock_request`, and `do_coredump`. I'll quickly verify the visibility of these functions. Checking `fs/file.c`, `coredump_close_files` and `coredump_task_close_files` are non-static. Then, looking at `fs/file_table.c`, `filp_close_sync` is also non-static. Aha, in `fs/coredump.c`, I see both `coredump_sock_request` and `do_coredump`. Wait a second… is `do_coredump` static? I need to double-check that last one. This might be important. |
| 10/2 | 2026/08/24 18:03 | tool | grepper |
0mArgs:{
"Expression": "do_coredump",
"PathPrefix": "fs/coredump.c"
}Results: {
"Output": "fs/coredump.c=1189=static inline bool coredump_skip(const struct coredump_params *cprm,\n--\nfs/coredump.c-1200-\nfs/coredump.c:1201:static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\nfs/coredump.c-1202-\t\t\tsize_t **argv, int *argc, const struct linux_binfmt *binfmt)\n--\nfs/coredump.c=1279=void vfs_coredump(const kernel_siginfo_t *siginfo)\n--\nfs/coredump.c-1317-\tscoped_with_creds(cred)\nfs/coredump.c:1318:\t\tdo_coredump(\u0026cn, \u0026cprm, \u0026argv, \u0026argc, binfmt);\nfs/coredump.c-1319-\tcoredump_cleanup(\u0026cn, \u0026cprm);\n"
} |
| 11/2 | 2026/08/24 18:03 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18432 output: 31 thoughts: 32 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into the Core Dump Logic** Okay, so I'm looking at how core dumps are generated in this system. The function `do_coredump` is declared as `static`, which immediately tells me it's likely an internal utility function, probably not something directly exposed. The real entry point for the core dump functionality is `vfs_coredump`. That's where the action *starts*. I need to drill into `vfs_coredump` to understand the flow and figure out what it's doing. |
| 12/2 | 2026/08/24 18:03 | tool | grepper |
0mArgs:{
"Expression": "vfs_coredump",
"PathPrefix": "fs/coredump.c"
}Results: {
"Output": "fs/coredump.c=1201=static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\n--\nfs/coredump.c-1278-\nfs/coredump.c:1279:void vfs_coredump(const kernel_siginfo_t *siginfo)\nfs/coredump.c-1280-{\n"
} |
| 13/2 | 2026/08/24 18:03 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18732 output: 32 thoughts: 43 Thoughts: Okay, here's my take on those thoughts, framed as my own: **Analyzing `vfs_coredump` and `coredump_sock_request`** Right, let me see... so `vfs_coredump` is *not* a static function, which immediately raises a flag. We need to understand its potential scope and impact. Now, let's contrast that with `coredump_sock_request` which *is* static. That's good; it suggests a more limited scope, contained within a specific module or file. The obvious question now becomes: Is there any non-static function that directly calls `coredump_sock_request`? That would be the crucial link to identify, wouldn't it? A non-static caller of a static function... hmm... need to trace that potential path. That could be where things get interesting, wouldn't it? |
| 14/2 | 2026/08/24 18:03 | tool | grepper |
0mArgs:{
"Expression": "coredump_sock_request",
"PathPrefix": "fs/coredump.c"
}Results: {
"Output": "fs/coredump.c=821=static inline void coredump_sock_shutdown(struct file *file)\n--\nfs/coredump.c-832-\nfs/coredump.c:833:static bool coredump_sock_request(struct core_name *cn, struct coredump_params *cprm)\nfs/coredump.c-834-{\n--\nfs/coredump.c=954=static bool coredump_socket(struct core_name *cn, struct coredump_params *cprm)\n--\nfs/coredump.c-958-\nfs/coredump.c:959:\treturn coredump_sock_request(cn, cprm);\nfs/coredump.c-960-}\n"
} |
| 15/2 | 2026/08/24 18:03 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 19193 output: 30 thoughts: 20 Thoughts: Here's my thought process, as it stands: **Static Variables and Callers** Okay, so `coredump_socket` is also declared as `static`. That immediately piques my interest. Static variables have specific scoping and lifetime characteristics, so understanding their usage is crucial, especially within a critical part of the system like a coredump mechanism. My next immediate thought is: What *exactly* calls `coredump_socket`? I need to trace back the usage to understand its purpose and how it's being managed. Who is setting up this socket? What is sending data to it? I need to understand the control flow. |
| 16/2 | 2026/08/24 18:03 | tool | grepper |
0mArgs:{
"Expression": "coredump_socket",
"PathPrefix": "fs/coredump.c"
}Results: {
"Output": "fs/coredump.c=833=static bool coredump_sock_request(struct core_name *cn, struct coredump_params *cprm)\n--\nfs/coredump.c-953-\nfs/coredump.c:954:static bool coredump_socket(struct core_name *cn, struct coredump_params *cprm)\nfs/coredump.c-955-{\n--\nfs/coredump.c=963=static inline void coredump_sock_shutdown(struct file *file) { }\nfs/coredump.c:964:static inline bool coredump_socket(struct core_name *cn, struct coredump_params *cprm) { return false; }\nfs/coredump.c-965-#endif\n--\nfs/coredump.c=1201=static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\n--\nfs/coredump.c-1222-\tcase COREDUMP_SOCK:\nfs/coredump.c:1223:\t\tif (!coredump_socket(cn, cprm))\nfs/coredump.c-1224-\t\t\treturn;\n--\nfs/coredump.c=1649=void validate_coredump_safety(void)\n--\nfs/coredump.c-1659-\nfs/coredump.c:1660:static inline bool check_coredump_socket(void)\nfs/coredump.c-1661-{\n--\nfs/coredump.c=1699=static int proc_dostring_coredump(const struct ctl_table *table, int write,\n--\nfs/coredump.c-1714-\nfs/coredump.c:1715:\tif (!check_coredump_socket()) {\nfs/coredump.c-1716-\t\tstrscpy(core_pattern, old_core_pattern, retval + 1);\n"
} |
| 17/2 | 2026/08/24 18:03 | llm | patch-evaluator |
1mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 19871 output: 20 thoughts: 68 Thoughts: Here's my summary, as I see it: **Digging into the Core Dump Flow** Okay, so I'm trying to trace the core dump process. Right now, I'm thinking about how `do_coredump` is connected to `coredump_socket`, which in turn calls `coredump_sock_request`. If `do_coredump` is called by `vfs_coredump`, then that suggests `vfs_coredump` is probably a good place to start digging deeper and focusing my investigation. I'll need to double check that function. My next logical step is to check `filp_close_sync` because of my experiences with resource cleanup in this area. |
| 18/2 | 2026/08/24 18:04 | tool | grepper |
0mArgs:{
"Expression": "filp_close_sync"
}Results: {
"Output": "fs/file.c=501=static struct fdtable *close_files(struct files_struct * files)\n--\nfs/file.c-526-\t\t\t\t\tif (sync)\nfs/file.c:527:\t\t\t\t\t\tfilp_close_sync(file, files);\nfs/file.c-528-\t\t\t\t\telse\n--\nfs/file_table.c=642=EXPORT_SYMBOL(filp_close);\n--\nfs/file_table.c-647- */\nfs/file_table.c:648:int filp_close_sync(struct file *filp, fl_owner_t id)\nfs/file_table.c-649-{\n--\nfs/internal.h=121=static inline void put_file_access(struct file *file)\n--\nfs/internal.h-129-\nfs/internal.h:130:int filp_close_sync(struct file *filp, fl_owner_t id);\nfs/internal.h-131-void fput_close(struct file *);\n--\nfs/open.c=1461=SYSCALL_DEFINE1(close, unsigned int, fd)\n--\nfs/open.c-1473-\t */\nfs/open.c:1474:\tretval = filp_close_sync(file, current-\u003efiles);\nfs/open.c-1475-\n"
} |
| 19/2 | 2026/08/24 18:04 | llm | patch-evaluator |
1mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 20465 output: 20 thoughts: 52 Thoughts: Okay, here's my take on those thoughts, framed as my own: **Analyzing `filp_close_sync` as a Key Focus** Alright, based on those relationships, it looks like `filp_close_sync` is definitely a core function to zero in on. The fact that it's called by both `close_files` and `sys_close` makes it a high-value symbol for understanding the overall file closing process. Knowing that, I need to check how it works with `coredump_close_files`. It probably deals with some of the finer details but I need to understand that relationship. |
| 20/2 | 2026/08/24 18:05 | tool | grepper |
0mArgs:{
"Expression": "coredump_close_files"
}Results: {
"Output": "fs/coredump.c=1201=static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\n--\nfs/coredump.c-1234-\nfs/coredump.c:1235:\tif ((cprm-\u003emask \u0026 COREDUMP_CLOSE_FILES) \u0026\u0026 !coredump_close_files()) {\nfs/coredump.c-1236-\t\tcoredump_report_failure(\"Failed to close files, aborting core\");\n--\nfs/file.c=571=void exit_files(struct task_struct *tsk)\n--\nfs/file.c-581- */\nfs/file.c:582:bool coredump_close_files(void)\nfs/file.c-583-{\n--\nfs/file.c=628=bool coredump_task_close_files(struct core_thread *self)\n--\nfs/file.c-632-\nfs/file.c:633:\t/* Pairs with the release in coredump_close_files(). */\nfs/file.c-634-\tfiles = smp_load_acquire(\u0026self-\u003efiles);\n--\ninclude/linux/fdtable.h=104=void put_files_struct(struct files_struct *fs);\ninclude/linux/fdtable.h:105:bool coredump_close_files(void);\ninclude/linux/fdtable.h-106-bool coredump_task_close_files(struct core_thread *self);\n--\nkernel/exit.c=430=static void coredump_task_exit(struct task_struct *tsk,\n--\nkernel/exit.c-451-\t\t\tbreak;\nkernel/exit.c:452:\t\t/* see coredump_close_files() */\nkernel/exit.c-453-\t\tif (coredump_task_close_files(\u0026self))\n--\ntools/testing/selftests/coredump/Makefile=4=TEST_GEN_PROGS := stackdump_test \\\n--\ntools/testing/selftests/coredump/Makefile-6-\t\t coredump_socket_protocol_test \\\ntools/testing/selftests/coredump/Makefile:7:\t\t coredump_close_files_test\ntools/testing/selftests/coredump/Makefile-8-TEST_FILES := stackdump\n--\ntools/testing/selftests/coredump/Makefile=14=$(OUTPUT)/coredump_socket_protocol_test: coredump_test_helpers.c\ntools/testing/selftests/coredump/Makefile:15:$(OUTPUT)/coredump_close_files_test: coredump_test_helpers.c\n"
} |
| 21/2 | 2026/08/24 18:05 | llm | patch-evaluator |
2mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 21336 output: 17 thoughts: 42 Thoughts: Here's my summary, as if I were thinking these thoughts: **Analyzing Core Dump File Handling** Okay, so I'm honing in on the crucial areas for file handling within the core dump process. `coredump_close_files` and `coredump_task_close_files` are definitely key focus points. Those function names scream "file cleanup," which is exactly what I need to understand better. Now, I should shift my attention to the allocation side of things. I need to understand how the file structures are created and initialized. Let's dig into `alloc_files_struct` and `alloc_files`. Understanding the allocation process will tell me a lot about how these file handles are managed from the very start. This should give me a solid picture of the entire lifecycle. |
| 22/2 | 2026/08/24 18:08 | tool | grepper |
0mArgs:{
"Expression": "alloc_files"
}Results: {
"Output": "fs/file.c=364=static unsigned int sane_fdtable_size(struct fdtable *fdt, struct fd_range *punch_hole)\n--\nfs/file.c-378-/* A table with one reference and the embedded fdtable, nothing copied yet. */\nfs/file.c:379:static struct files_struct *alloc_files(gfp_t gfp)\nfs/file.c-380-{\n--\nfs/file.c-404-/* An empty descriptor table with one reference. */\nfs/file.c:405:static struct files_struct *alloc_files_struct(void)\nfs/file.c-406-{\n--\nfs/file.c-408-\nfs/file.c:409:\tnewf = alloc_files(GFP_KERNEL | __GFP_ZERO);\nfs/file.c-410-\tif (!newf)\n--\nfs/file.c=422=struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)\n--\nfs/file.c-428-\nfs/file.c:429:\tnewf = alloc_files(GFP_KERNEL);\nfs/file.c-430-\tif (!newf)\n--\nfs/file.c=582=bool coredump_close_files(void)\n--\nfs/file.c-588-\nfs/file.c:589:\tfiles = alloc_files_struct();\nfs/file.c-590-\tif (!files)\n--\nfs/xfs/libxfs/xfs_bmap.c=3532=static int\nfs/xfs/libxfs/xfs_bmap.c:3533:xfs_bmap_btalloc_filestreams(\nfs/xfs/libxfs/xfs_bmap.c-3534-\tstruct xfs_bmalloca\t*ap,\n--\nfs/xfs/libxfs/xfs_bmap.c=3625=xfs_bmap_btalloc(\n--\nfs/xfs/libxfs/xfs_bmap.c-3658-\t\t\txfs_inode_is_filestream(ap-\u003eip))\nfs/xfs/libxfs/xfs_bmap.c:3659:\t\terror = xfs_bmap_btalloc_filestreams(ap, \u0026args, stripe_align);\nfs/xfs/libxfs/xfs_bmap.c-3660-\telse\n"
} |
| 23/2 | 2026/08/24 18:08 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 22206 output: 19 thoughts: 28 Thoughts: Here's my take on the thought process, as if it were my own: **Analyzing File Allocation Structures** Okay, so I'm looking at these file allocation functions. I see `alloc_files` and `alloc_files_struct` defined as `static`. Right, that means they're only visible within this compilation unit. That's good to keep in mind, it limits their scope which is usually a positive sign for modularity. Now, let me shift my focus to `switch_files_struct`. I need to understand how it interacts with these other structures and functions. It's likely that it has a role in managing or manipulating the file structure in some way, and its relationship to `alloc_files` is critical to understand. Time to delve into the code associated with `switch_files_struct`. |
| 24/2 | 2026/08/24 18:08 | tool | grepper |
0mArgs:{
"Expression": "switch_files_struct"
}Results: {
"Output": "fs/file.c=541=void put_files_struct(struct files_struct *files)\n--\nfs/file.c-553-/* Hand @tsk the table @files, or none, and drop the one it had. */\nfs/file.c:554:static void switch_files_struct(struct task_struct *tsk,\nfs/file.c-555-\t\t\t\tstruct files_struct *files)\n--\nfs/file.c=571=void exit_files(struct task_struct *tsk)\n--\nfs/file.c-574-\t\treturn;\nfs/file.c:575:\tswitch_files_struct(tsk, NULL);\nfs/file.c-576-}\n--\nfs/file.c=582=bool coredump_close_files(void)\n--\nfs/file.c-609-\tscoped_with_creds(current_real_cred())\nfs/file.c:610:\t\tswitch_files_struct(current, files);\nfs/file.c-611-\n--\nfs/file.c=628=bool coredump_task_close_files(struct core_thread *self)\n--\nfs/file.c-638-\t__set_current_state(TASK_RUNNING);\nfs/file.c:639:\tswitch_files_struct(current, files);\nfs/file.c-640-\tcore_state = current-\u003esignal-\u003ecore_state;\n"
} |
| 25/2 | 2026/08/24 18:08 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 22803 output: 19 thoughts: 315 Thoughts: **Focus Symbol Refinement** Okay, let's nail down these focus symbols. I've got `switch_files_struct` locked in as static, so that's off the table. Now, the non-static functions are the ones I need to examine: `coredump_close_files`, `coredump_task_close_files`, `filp_close_sync`, and `vfs_coredump`. Right, let's revisit `filp_close_sync`. Hang on... it's called by `sys_close`? That's a *very* hot path. I need to be careful; that's probably not ideal as a focus symbol. The kernel sees a ton of `sys_close` calls. Yep, `retval = filp_close_sync(file, current->files);` in `sys_close` confirms it. So, scrap `filp_close_sync` from the list; it's too generic and high-frequency. Now, what about `coredump_close_files` and `coredump_task_close_files`? Those are specific to coredump functionality, right? Not a standard path. That looks good. And `vfs_coredump` is similarly niche. That's good too. Are there any other outliers? `filp_close` is exported and non-static, but it looks like a hot path (or at least generic). And `dup_fd` is modified but it's used in fork, not a focus. Okay, I think we've got it. The focus symbols, given the analysis, should be: * `coredump_close_files` * `coredump_task_close_files` * `vfs_coredump` I'll double-check `vfs_coredump` one more time, just to be sure. It does seem unique to the coredump use case. Excellent. |
| 26/2 | 2026/08/24 18:08 | tool | grepper |
0mArgs:{
"Expression": "vfs_coredump"
}Results: {
"Output": "Documentation/security/credentials.rst=555=different set of credentials. This is done in the following places:\n--\nDocumentation/security/credentials.rst-557- * ``sys_faccessat()``.\nDocumentation/security/credentials.rst:558: * ``vfs_coredump()``.\nDocumentation/security/credentials.rst-559- * nfs4recover.c.\n--\nDocumentation/translations/zh_CN/security/credentials.rst=254=const指针上操作,因此不需要进行类型转换,但需要临时放弃const限定,以便能够修改\n--\nDocumentation/translations/zh_CN/security/credentials.rst-477- * ``sys_faccessat()``.\nDocumentation/translations/zh_CN/security/credentials.rst:478: * ``vfs_coredump()``.\nDocumentation/translations/zh_CN/security/credentials.rst-479- * nfs4recover.c.\n--\nfs/coredump.c=1201=static void do_coredump(struct core_name *cn, struct coredump_params *cprm,\n--\nfs/coredump.c-1278-\nfs/coredump.c:1279:void vfs_coredump(const kernel_siginfo_t *siginfo)\nfs/coredump.c-1280-{\n--\ninclude/linux/coredump.h=67=bool dump_user_range(struct coredump_params *cprm, unsigned long start,\ninclude/linux/coredump.h-68-\t\t unsigned long len);\ninclude/linux/coredump.h:69:void vfs_coredump(const kernel_siginfo_t *siginfo);\ninclude/linux/coredump.h-70-\n--\ninclude/linux/coredump.h-88-#else\ninclude/linux/coredump.h:89:static inline void vfs_coredump(const kernel_siginfo_t *siginfo) {}\ninclude/linux/coredump.h-90-\n--\nkernel/signal.c=2810=bool get_signal(struct ksignal *ksig)\n--\nkernel/signal.c-3030-\t\t\t */\nkernel/signal.c:3031:\t\t\tvfs_coredump(\u0026ksig-\u003einfo);\nkernel/signal.c-3032-\t\t}\n"
} |
| 27/2 | 2026/08/24 18:08 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 24135 output: 180 thoughts: 588 Thoughts: **My Assessment of the Coredump Feature**
Okay, so `vfs_coredump` – that's definitely a good starting point for fuzzing, it's called when a process crashes, so we have a clear trigger. Let's see if there are any other configs related to this area. It looks like `COREDUMP` is the only one. Hmm, it's enabled by default, which is convenient, and this is the config that enables the feature, so yes, we should include it.
Now, where else are the changes? The changes are mainly around file descriptor management. I'm seeing additions and modifications in several files: `fs/file_table.c` (with `filp_close` and `filp_close_sync`), `fs/open.c` (with `sys_close` using `filp_close_sync`), and `fs/file.c` (with a bunch of new functions like `alloc_files`, `alloc_files_struct`, `dup_fd`, `close_files`, `switch_files_struct`, `exit_files`, and specifically, `coredump_close_files`, `coredump_task_close_files`). `fs/coredump.c` also has new functions like `coredump_wait`, `coredump_sock_request`, and `do_coredump`. Finally, `kernel/exit.c` has `coredump_task_exit`.
Yes, this is functionally significant. The coredump feature is definitely worth fuzzing. It's reachable when a process crashes, specifically if the coredump pattern is set to a pipe or socket. Then the userspace handler would reply with `COREDUMP_CLOSE_FILES`. This should be easily reproducible in virtualized environments.
So, here's the summary:
* **WorthFuzzing**: True
* **FocusSymbols**:
* `coredump_close_files`
* `coredump_task_close_files`
* `vfs_coredump`
* **EnableConfigs**:
* `COREDUMP`
Let's check `COREDUMP` for dependencies. It's a boolean option controlled by `EXPERT`. But it doesn't *depend* on expert mode, it just *shows* the prompt in expert mode. So `COREDUMP` is fine as-is.
Looking back at the reasoning... the patch essentially adds a new `COREDUMP_CLOSE_FILES` flag to the coredump protocol. This allows userspace coredump handlers to instruct the kernel to close all the file descriptors of a crashing task *before* dumping the core. This introduces new logic for file descriptor table manipulation and closing in `fs/file.c` and `fs/coredump.c`. We can trigger this by crashing a process and having a userspace handler return the flag.
Okay, I'm satisfied. Time to call `set-results`.
|
| 28/2 | 2026/08/24 18:08 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"COREDUMP"
],
"FocusSymbols": [
"coredump_close_files",
"coredump_task_close_files",
"vfs_coredump"
],
"Reasoning": "The patch introduces a new `COREDUMP_CLOSE_FILES` flag to the coredump protocol. When a userspace coredump handler requests this flag, the kernel closes all file descriptors of the coredumping task before generating the coredump. This involves new core kernel logic in `fs/file.c` and `fs/coredump.c` to handle file descriptor table switching and closing. The feature is reachable by crashing a process and having a userspace coredump handler reply with the new flag, making it functionally impactful and worth fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"COREDUMP"
],
"FocusSymbols": [
"coredump_close_files",
"coredump_task_close_files",
"vfs_coredump"
],
"Reasoning": "The patch introduces a new `COREDUMP_CLOSE_FILES` flag to the coredump protocol. When a userspace coredump handler requests this flag, the kernel closes all file descriptors of the coredumping task before generating the coredump. This involves new core kernel logic in `fs/file.c` and `fs/coredump.c` to handle file descriptor table switching and closing. The feature is reachable by crashing a process and having a userspace coredump handler reply with the new flag, making it functionally impactful and worth fuzzing.",
"WorthFuzzing": true
} |
| 29/1 | 2026/08/24 18:08 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 30/2 | 2026/08/24 18:08 | agent | kmsan-evaluator |
0mModel:best-expensive Args: null Results: {
"KMSANReasoning": "The patch introduces a new feature to close file descriptors before generating a coredump (COREDUMP_CLOSE_FILES). It modifies file descriptor table management, adds a new empty file descriptor table allocation (which is explicitly zeroed using __GFP_ZERO), and alters the coredump synchronization logic. No new data structures are copied to userspace, and no complex structures are allocated without initialization. The changes primarily involve pointer switching, reference counting, and file closing, which might risk use-after-free, NULL pointer dereferences, or locking issues. These types of bugs are effectively caught by KASAN, LOCKDEP, and standard bug detectors. There is no risk of uninitialized memory usage or info-leaks that would require KMSAN.",
"NeedsKMSAN": false
}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: For your convenience, here is the diff of the changes:
commit 0957d000b45ea469cf234e7cad4c57051ec54716
Author: syz-cluster <triage@syzkaller.com>
Date: Mon Aug 24 17:57:04 2026 +0000
syz-cluster: applied patch under review
diff --git a/fs/coredump.c b/fs/coredump.c
index ec9359e382ea5..a6e7da0bc16a7 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -523,7 +523,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
struct task_struct *tsk = current;
int core_waiters = -EBUSY;
- init_completion(&core_state->startup);
+ init_completion(&core_state->done);
core_state->dumper.task = tsk;
core_state->dumper.next = NULL;
@@ -531,7 +531,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
if (core_waiters > 0) {
struct core_thread *ptr;
- wait_for_completion_state(&core_state->startup,
+ wait_for_completion_state(&core_state->done,
TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
/*
* Wait for all the threads to become inactive, so that
@@ -837,7 +837,8 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *
.mask = COREDUMP_KERNEL | COREDUMP_USERSPACE |
COREDUMP_REJECT | COREDUMP_WAIT |
COREDUMP_RECORDS | COREDUMP_SPARSE |
- COREDUMP_MEMORY_TYPES,
+ COREDUMP_MEMORY_TYPES |
+ COREDUMP_CLOSE_FILES,
.size_ack = sizeof(struct coredump_ack),
.memory_types = cprm->memory_types,
.memory_types_mask = COREDUMP_MEMORY_ALL,
@@ -905,6 +906,12 @@ static bool coredump_sock_request(struct core_name *cn, struct coredump_params *
return false;
}
+ /* A rejected task exits right away and closes everything anyway. */
+ if ((ack.mask & COREDUMP_CLOSE_FILES) && (ack.mask & COREDUMP_REJECT)) {
+ coredump_sock_mark(cprm->file, COREDUMP_MARK_CONFLICTING);
+ return false;
+ }
+
if (ack.mask & COREDUMP_MEMORY_TYPES) {
/* The memory types need the whole field. */
if (usize < COREDUMP_ACK_SIZE_VER1) {
@@ -1225,6 +1232,11 @@ static void do_coredump(struct core_name *cn, struct coredump_params *cprm,
if (cprm->mask & COREDUMP_REJECT)
return;
+ if ((cprm->mask & COREDUMP_CLOSE_FILES) && !coredump_close_files()) {
+ coredump_report_failure("Failed to close files, aborting core");
+ return;
+ }
+
/* get us an unshared descriptor table; almost always a no-op */
/* The cell spufs coredump code reads the file descriptor tables */
if (unshare_files())
diff --git a/fs/file.c b/fs/file.c
index 628ca07dc4b17..3b8fce6e36951 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -375,21 +375,15 @@ static unsigned int sane_fdtable_size(struct fdtable *fdt, struct fd_range *punc
return ALIGN(last + 1, BITS_PER_LONG);
}
-/*
- * Allocate a new descriptor table and copy contents from the passed in
- * instance. Returns a pointer to cloned table on success, ERR_PTR()
- * on failure. For 'punch_hole' see sane_fdtable_size().
- */
-struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)
+/* A table with one reference and the embedded fdtable, nothing copied yet. */
+static struct files_struct *alloc_files(gfp_t gfp)
{
struct files_struct *newf;
- struct file **old_fds, **new_fds;
- unsigned int open_files, i;
- struct fdtable *old_fdt, *new_fdt;
+ struct fdtable *new_fdt;
- newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
+ newf = kmem_cache_alloc(files_cachep, gfp);
if (!newf)
- return ERR_PTR(-ENOMEM);
+ return NULL;
atomic_set(&newf->count, 1);
@@ -404,6 +398,39 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_ho
new_fdt->full_fds_bits = newf->full_fds_bits_init;
new_fdt->fd = &newf->fd_array[0];
+ return newf;
+}
+
+/* An empty descriptor table with one reference. */
+static struct files_struct *alloc_files_struct(void)
+{
+ struct files_struct *newf;
+
+ newf = alloc_files(GFP_KERNEL | __GFP_ZERO);
+ if (!newf)
+ return NULL;
+
+ rcu_assign_pointer(newf->fdt, &newf->fdtab);
+ return newf;
+}
+
+/*
+ * Allocate a new descriptor table and copy contents from the passed in
+ * instance. Returns a pointer to cloned table on success, ERR_PTR()
+ * on failure. For 'punch_hole' see sane_fdtable_size().
+ */
+struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole)
+{
+ struct files_struct *newf;
+ struct file **old_fds, **new_fds;
+ unsigned int open_files, i;
+ struct fdtable *old_fdt, *new_fdt;
+
+ newf = alloc_files(GFP_KERNEL);
+ if (!newf)
+ return ERR_PTR(-ENOMEM);
+ new_fdt = &newf->fdtab;
+
spin_lock(&oldf->file_lock);
old_fdt = files_fdtable(oldf);
open_files = sane_fdtable_size(old_fdt, punch_hole);
@@ -480,6 +507,11 @@ static struct fdtable *close_files(struct files_struct * files)
*/
struct fdtable *fdt = rcu_dereference_raw(files->fdt);
unsigned int i, j = 0;
+ /*
+ * A kernel thread that might be needed to make progress on some
+ * umount must not run __fput() itself, see __fput_sync().
+ */
+ bool sync = !(current->flags & PF_KTHREAD);
for (;;) {
unsigned long set;
@@ -491,7 +523,10 @@ static struct fdtable *close_files(struct files_struct * files)
if (set & 1) {
struct file *file = fdt->fd[i];
if (file) {
- filp_close(file, files);
+ if (sync)
+ filp_close_sync(file, files);
+ else
+ filp_close(file, files);
cond_resched();
}
}
@@ -515,16 +550,97 @@ void put_files_struct(struct files_struct *files)
}
}
+/* Hand @tsk the table @files, or none, and drop the one it had. */
+static void switch_files_struct(struct task_struct *tsk,
+ struct files_struct *files)
+{
+ struct files_struct *old;
+
+ task_lock(tsk);
+ old = tsk->files;
+ if (old) {
+ if (files)
+ atomic_inc(&files->count);
+ tsk->files = files;
+ }
+ task_unlock(tsk);
+ if (old)
+ put_files_struct(old);
+}
+
void exit_files(struct task_struct *tsk)
{
- struct files_struct * files = tsk->files;
+ if (!tsk->files)
+ return;
+ switch_files_struct(tsk, NULL);
+}
+
+/*
+ * The thread-group is waiting in coredump_task_exit(). Wake them and
+ * tell them to get rid of their files.
+ */
+bool coredump_close_files(void)
+{
+ struct core_state *core_state = current->signal->core_state;
+ struct files_struct *files;
+ struct core_thread *ct;
+ int nr = 0;
- if (files) {
- task_lock(tsk);
- tsk->files = NULL;
- task_unlock(tsk);
- put_files_struct(files);
+ files = alloc_files_struct();
+ if (!files)
+ return false;
+
+ /* Tasks without a table, vhost workers say, sit the switch out. */
+ for (ct = core_state->dumper.next; ct; ct = ct->next)
+ if (ct->task->files)
+ nr++;
+ atomic_set(&core_state->nr_threads, nr);
+ reinit_completion(&core_state->done);
+
+ for (ct = core_state->dumper.next; ct; ct = ct->next) {
+ if (!ct->task->files)
+ continue;
+ /* Pairs with the acquire in coredump_task_close_files(). */
+ smp_store_release(&ct->files, files);
+ wake_up_process(ct->task);
}
+
+ /* The dumper's subjective creds are the dump's, not its own. */
+ scoped_with_creds(current_real_cred())
+ switch_files_struct(current, files);
+
+ if (nr) {
+ wait_for_completion_state(&core_state->done,
+ TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
+ /* Back asleep before the notes read their registers. */
+ for (ct = core_state->dumper.next; ct; ct = ct->next)
+ wait_task_inactive(ct->task, TASK_ANY);
+ }
+
+ put_files_struct(files);
+ return true;
+}
+
+/*
+ * Get rid of the files and switch to the empty file descriptor table
+ * the dumping thread published in this thread's core_thread entry.
+ */
+bool coredump_task_close_files(struct core_thread *self)
+{
+ struct core_state *core_state;
+ struct files_struct *files;
+
+ /* Pairs with the release in coredump_close_files(). */
+ files = smp_load_acquire(&self->files);
+ if (!files || !current->files || current->files == files)
+ return false;
+
+ __set_current_state(TASK_RUNNING);
+ switch_files_struct(current, files);
+ core_state = current->signal->core_state;
+ if (atomic_dec_and_test(&core_state->nr_threads))
+ complete(&core_state->done);
+ return true;
}
struct files_struct init_files = {
diff --git a/fs/file_table.c b/fs/file_table.c
index c68b8c0a4097a..3c705917646b5 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -13,6 +13,7 @@
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/filelock.h>
+#include <linux/dnotify.h>
#include <linux/security.h>
#include <linux/cred.h>
#include <linux/eventpoll.h>
@@ -606,15 +607,53 @@ void __fput_sync(struct file *file)
EXPORT_SYMBOL(__fput_sync);
/*
- * Equivalent to __fput_sync(), but optimized for being called with the last
- * reference.
- *
- * See file_ref_put_close() for details.
+ * "id" is the POSIX thread ID. We use the
+ * files pointer for this..
*/
-void fput_close_sync(struct file *file)
+static int filp_flush(struct file *filp, fl_owner_t id)
{
- if (likely(file_ref_put_close(&file->f_ref)))
- __fput(file);
+ int retval = 0;
+
+ if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,
+ "VFS: Close: file count is 0 (f_op=%ps)",
+ filp->f_op)) {
+ return 0;
+ }
+
+ if (filp->f_op->flush)
+ retval = filp->f_op->flush(filp, id);
+
+ if (likely(!(filp->f_mode & FMODE_PATH))) {
+ dnotify_flush(filp, id);
+ locks_remove_posix(filp, id);
+ }
+ return retval;
+}
+
+int filp_close(struct file *filp, fl_owner_t id)
+{
+ int retval;
+
+ retval = filp_flush(filp, id);
+ fput_close(filp);
+
+ return retval;
+}
+EXPORT_SYMBOL(filp_close);
+
+/*
+ * Like filp_close() but without deferring the release of the last
+ * reference, see file_ref_put_close() for details.
+ */
+int filp_close_sync(struct file *filp, fl_owner_t id)
+{
+ int retval;
+
+ retval = filp_flush(filp, id);
+ if (likely(file_ref_put_close(&filp->f_ref)))
+ __fput(filp);
+
+ return retval;
}
/*
diff --git a/fs/internal.h b/fs/internal.h
index 355d93f922086..7ba8cd26a698c 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -127,7 +127,7 @@ static inline void put_file_access(struct file *file)
}
}
-void fput_close_sync(struct file *);
+int filp_close_sync(struct file *filp, fl_owner_t id);
void fput_close(struct file *);
/*
@@ -190,6 +190,7 @@ struct open_flags {
int intent;
int lookup_flags;
};
+
extern struct file *do_file_open(int dfd, struct filename *pathname,
const struct open_flags *op);
extern struct file *do_file_open_root(const struct path *,
diff --git a/fs/open.c b/fs/open.c
index 408925d7bd0b7..d036736d5d435 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1453,41 +1453,6 @@ SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
}
#endif
-/*
- * "id" is the POSIX thread ID. We use the
- * files pointer for this..
- */
-static int filp_flush(struct file *filp, fl_owner_t id)
-{
- int retval = 0;
-
- if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,
- "VFS: Close: file count is 0 (f_op=%ps)",
- filp->f_op)) {
- return 0;
- }
-
- if (filp->f_op->flush)
- retval = filp->f_op->flush(filp, id);
-
- if (likely(!(filp->f_mode & FMODE_PATH))) {
- dnotify_flush(filp, id);
- locks_remove_posix(filp, id);
- }
- return retval;
-}
-
-int filp_close(struct file *filp, fl_owner_t id)
-{
- int retval;
-
- retval = filp_flush(filp, id);
- fput_close(filp);
-
- return retval;
-}
-EXPORT_SYMBOL(filp_close);
-
/*
* Careful here! We test whether the file pointer is NULL before
* releasing the fd. This ensures that one clone task can't release
@@ -1502,13 +1467,11 @@ SYSCALL_DEFINE1(close, unsigned int, fd)
if (!file)
return -EBADF;
- retval = filp_flush(file, current->files);
-
/*
* We're returning to user space. Don't bother
* with any delayed fput() cases.
*/
- fput_close_sync(file);
+ retval = filp_close_sync(file, current->files);
if (likely(retval == 0))
return 0;
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index c45306a9f0072..f0c1003da8ce8 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -99,11 +99,16 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *fil
struct task_struct;
+struct core_thread;
+
void put_files_struct(struct files_struct *fs);
+bool coredump_close_files(void);
+bool coredump_task_close_files(struct core_thread *self);
int unshare_files(void);
struct fd_range {
unsigned int from, to;
};
+
struct files_struct *dup_fd(struct files_struct *, struct fd_range *) __latent_entropy;
void do_close_on_exec(struct files_struct *);
int iterate_fd(struct files_struct *, unsigned,
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 584ae88b435e9..fed02a427de31 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -76,12 +76,15 @@ struct multiprocess_signals {
struct core_thread {
struct task_struct *task;
struct core_thread *next;
+ /* The empty table to switch to, published by the dumping thread. */
+ struct files_struct *files;
};
struct core_state {
atomic_t nr_threads;
struct core_thread dumper;
- struct completion startup;
+ /* Completed by the last thread to park, reused for the table switch. */
+ struct completion done;
};
/*
diff --git a/include/uapi/linux/coredump.h b/include/uapi/linux/coredump.h
index 6d0c53b534ea8..ec09d7ab01314 100644
--- a/include/uapi/linux/coredump.h
+++ b/include/uapi/linux/coredump.h
@@ -19,6 +19,9 @@
* @COREDUMP_MEMORY_TYPES: dump the memory types in
* coredump_ack->memory_types instead of the ones
* the task selected; requires COREDUMP_KERNEL
+ * @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the
+ * coredump is generated; incompatible with
+ * COREDUMP_REJECT
*/
enum {
COREDUMP_KERNEL = (1ULL << 0),
@@ -28,6 +31,7 @@ enum {
COREDUMP_RECORDS = (1ULL << 4),
COREDUMP_SPARSE = (1ULL << 5),
COREDUMP_MEMORY_TYPES = (1ULL << 6),
+ COREDUMP_CLOSE_FILES = (1ULL << 7),
};
/**
@@ -137,6 +141,10 @@ enum {
* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't
* raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of
* at least COREDUMP_ACK_SIZE_VER1 bytes.
+ *
+ * If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file
+ * descriptors of the coredumping task before it generates the coredump.
+ * The task ends up with an empty descriptor table.
*/
struct coredump_ack {
__u32 size;
diff --git a/kernel/exit.c b/kernel/exit.c
index 2c0b1c02920f4..b651679c12e97 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -25,6 +25,7 @@
#include <linux/acct.h>
#include <linux/tsacct_kern.h>
#include <linux/file.h>
+#include <linux/fdtable.h>
#include <linux/freezer.h>
#include <linux/binfmts.h>
#include <linux/nsproxy.h>
@@ -432,6 +433,7 @@ static void coredump_task_exit(struct task_struct *tsk,
struct core_thread self;
self.task = tsk;
+ self.files = NULL;
if (self.task->flags & PF_SIGNALED)
self.next = xchg(&core_state->dumper.next, &self);
else
@@ -441,12 +443,15 @@ static void coredump_task_exit(struct task_struct *tsk,
* to core_state->dumper.
*/
if (atomic_dec_and_test(&core_state->nr_threads))
- complete(&core_state->startup);
+ complete(&core_state->done);
for (;;) {
set_current_state(TASK_IDLE|TASK_FREEZABLE);
if (!self.task) /* see coredump_finish() */
break;
+ /* see coredump_close_files() */
+ if (coredump_task_close_files(&self))
+ continue;
schedule();
}
__set_current_state(TASK_RUNNING);
diff --git a/tools/include/uapi/linux/coredump.h b/tools/include/uapi/linux/coredump.h
index 6d0c53b534ea8..ec09d7ab01314 100644
--- a/tools/include/uapi/linux/coredump.h
+++ b/tools/include/uapi/linux/coredump.h
@@ -19,6 +19,9 @@
* @COREDUMP_MEMORY_TYPES: dump the memory types in
* coredump_ack->memory_types instead of the ones
* the task selected; requires COREDUMP_KERNEL
+ * @COREDUMP_CLOSE_FILES: close all file descriptors of the task before the
+ * coredump is generated; incompatible with
+ * COREDUMP_REJECT
*/
enum {
COREDUMP_KERNEL = (1ULL << 0),
@@ -28,6 +31,7 @@ enum {
COREDUMP_RECORDS = (1ULL << 4),
COREDUMP_SPARSE = (1ULL << 5),
COREDUMP_MEMORY_TYPES = (1ULL << 6),
+ COREDUMP_CLOSE_FILES = (1ULL << 7),
};
/**
@@ -137,6 +141,10 @@ enum {
* Note that @memory_types must be zero if COREDUMP_MEMORY_TYPES isn't
* raised. COREDUMP_MEMORY_TYPES requires COREDUMP_KERNEL and an ack of
* at least COREDUMP_ACK_SIZE_VER1 bytes.
+ *
+ * If COREDUMP_CLOSE_FILES is raised in @mask the kernel closes the file
+ * descriptors of the coredumping task before it generates the coredump.
+ * The task ends up with an empty descriptor table.
*/
struct coredump_ack {
__u32 size;
diff --git a/tools/testing/selftests/coredump/Makefile b/tools/testing/selftests/coredump/Makefile
index dece1a31d561b..dc8489d406184 100644
--- a/tools/testing/selftests/coredump/Makefile
+++ b/tools/testing/selftests/coredump/Makefile
@@ -3,7 +3,8 @@ CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
TEST_GEN_PROGS := stackdump_test \
coredump_socket_test \
- coredump_socket_protocol_test
+ coredump_socket_protocol_test \
+ coredump_close_files_test
TEST_FILES := stackdump
include ../lib.mk
@@ -11,3 +12,4 @@ include ../lib.mk
$(OUTPUT)/stackdump_test: coredump_test_helpers.c
$(OUTPUT)/coredump_socket_test: coredump_test_helpers.c
$(OUTPUT)/coredump_socket_protocol_test: coredump_test_helpers.c
+$(OUTPUT)/coredump_close_files_test: coredump_test_helpers.c
diff --git a/tools/testing/selftests/coredump/coredump_close_files_test.c b/tools/testing/selftests/coredump/coredump_close_files_test.c
new file mode 100644
index 0000000000000..7a41906b46952
--- /dev/null
+++ b/tools/testing/selftests/coredump/coredump_close_files_test.c
@@ -0,0 +1,592 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "coredump_test.h"
+
+#define LOCK_FILE "/tmp/coredump.lock"
+
+/* Idle threads the multi-threaded crashing child spawns. */
+#define NUM_CLOSE_THREADS 4
+
+/* How the crashing child locks LOCK_FILE. */
+enum lock_kind {
+ LOCK_KIND_POSIX,
+ LOCK_KIND_OFD,
+ LOCK_KIND_FLOCK,
+};
+
+/* Who else has a handle on the lock when the child crashes. */
+enum lock_share {
+ LOCK_SHARE_NONE,
+ LOCK_SHARE_DUP, /* a second slot in the child's own table */
+ LOCK_SHARE_FORK, /* a forked process keeps the fd open */
+ LOCK_SHARE_FILES, /* a CLONE_FILES process shares the table */
+};
+
+struct close_test {
+ enum lock_kind kind;
+ enum lock_share share;
+ bool threads;
+ bool close; /* ack with COREDUMP_CLOSE_FILES */
+ bool userspace; /* COREDUMP_USERSPACE instead of COREDUMP_KERNEL */
+ bool released; /* the lock is gone once the kernel is past the close */
+};
+
+FIXTURE_SETUP(coredump)
+{
+ FILE *file;
+ int ret;
+
+ self->pid_coredump_server = -ESRCH;
+ self->fd_tmpfs_detached = -1;
+ file = fopen("/proc/sys/kernel/core_pattern", "r");
+ ASSERT_NE(NULL, file);
+
+ ret = fread(self->original_core_pattern, 1, sizeof(self->original_core_pattern), file);
+ ASSERT_TRUE(ret || feof(file));
+ ASSERT_LT(ret, sizeof(self->original_core_pattern));
+
+ self->original_core_pattern[ret] = '\0';
+ self->fd_tmpfs_detached = create_detached_tmpfs();
+ ASSERT_GE(self->fd_tmpfs_detached, 0);
+
+ ret = fclose(file);
+ ASSERT_EQ(0, ret);
+}
+
+FIXTURE_TEARDOWN(coredump)
+{
+ const char *reason;
+ FILE *file;
+ int ret, status;
+
+ if (self->pid_coredump_server > 0) {
+ kill(self->pid_coredump_server, SIGTERM);
+ waitpid(self->pid_coredump_server, &status, 0);
+ }
+ unlink(LOCK_FILE);
+ unlink("/tmp/coredump.socket");
+
+ file = fopen("/proc/sys/kernel/core_pattern", "w");
+ if (!file) {
+ reason = "Unable to open core_pattern";
+ goto fail;
+ }
+
+ ret = fprintf(file, "%s", self->original_core_pattern);
+ if (ret < 0) {
+ reason = "Unable to write to core_pattern";
+ goto fail;
+ }
+
+ ret = fclose(file);
+ if (ret) {
+ reason = "Unable to close core_pattern";
+ goto fail;
+ }
+
+ if (self->fd_tmpfs_detached >= 0) {
+ ret = close(self->fd_tmpfs_detached);
+ if (ret < 0) {
+ reason = "Unable to close detached tmpfs";
+ goto fail;
+ }
+ self->fd_tmpfs_detached = -1;
+ }
+
+ return;
+fail:
+ /* This should never happen */
+ fprintf(stderr, "Failed to cleanup coredump test: %s\n", reason);
+}
+
+/* Write-lock @fd the way @kind says. */
+static int take_lock(int fd, enum lock_kind kind)
+{
+ struct flock fl = {
+ .l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ };
+
+ switch (kind) {
+ case LOCK_KIND_POSIX:
+ return fcntl(fd, F_SETLK, &fl);
+ case LOCK_KIND_OFD:
+ return fcntl(fd, F_OFD_SETLK, &fl);
+ case LOCK_KIND_FLOCK:
+ return flock(fd, LOCK_EX);
+ }
+
+ return -1;
+}
+
+/* Does anyone else hold a write lock on @fd? 1 if so, 0 if not, -1 on error. */
+static int lock_held(int fd, enum lock_kind kind)
+{
+ struct flock fl = {
+ .l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ };
+
+ if (kind == LOCK_KIND_FLOCK) {
+ if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
+ flock(fd, LOCK_UN);
+ return 0;
+ }
+ return errno == EWOULDBLOCK ? 1 : -1;
+ }
+
+ /* F_GETLK reports conflicting OFD locks too. */
+ if (fcntl(fd, F_GETLK, &fl) < 0)
+ return -1;
+ return fl.l_type != F_UNLCK;
+}
+
+/* Number of entries in /proc/@pid/fd, the lowest one in @first. */
+static int count_fds(pid_t pid, int *first)
+{
+ char path[64];
+ struct dirent *de;
+ DIR *dir;
+ int nr = 0;
+
+ snprintf(path, sizeof(path), "/proc/%d/fd", pid);
+ dir = opendir(path);
+ if (!dir)
+ return -1;
+
+ *first = -1;
+ while ((de = readdir(dir))) {
+ int fd;
+
+ if (de->d_name[0] == '.')
+ continue;
+ fd = atoi(de->d_name);
+ if (*first < 0 || fd < *first)
+ *first = fd;
+ nr++;
+ }
+ closedir(dir);
+ return nr;
+}
+
+/*
+ * Block until the test hangs up @fd_release, keeping every inherited fd
+ * open, then report through @fd_result whether @fd is still open.
+ */
+static void hold_until_released(int fd, int fd_release, int fd_result)
+{
+ char c;
+
+ read_nointr(fd_release, &c, 1);
+ c = fcntl(fd, F_GETFD) < 0 ? 'C' : 'O';
+ write_nointr(fd_result, &c, 1);
+ _exit(EXIT_SUCCESS);
+}
+
+/* Lock LOCK_FILE, share it as requested, then crash. */
+static void crashing_child_locked(const struct close_test *t, int fd_release,
+ int fd_result)
+{
+ pthread_t thread;
+ int fd, pidfd, i;
+ pid_t pid;
+
+ fd = open(LOCK_FILE, O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ _exit(EXIT_FAILURE);
+
+ if (take_lock(fd, t->kind))
+ _exit(EXIT_FAILURE);
+
+ switch (t->share) {
+ case LOCK_SHARE_NONE:
+ break;
+ case LOCK_SHARE_DUP:
+ if (dup(fd) < 0)
+ _exit(EXIT_FAILURE);
+ break;
+ case LOCK_SHARE_FORK:
+ pid = fork();
+ if (pid < 0)
+ _exit(EXIT_FAILURE);
+ if (pid == 0)
+ hold_until_released(fd, fd_release, fd_result);
+ break;
+ case LOCK_SHARE_FILES:
+ pid = create_child(&pidfd, CLONE_FILES);
+ if (pid < 0)
+ _exit(EXIT_FAILURE);
+ if (pid == 0)
+ hold_until_released(fd, fd_release, fd_result);
+ break;
+ }
+
+ if (t->threads)
+ for (i = 0; i < NUM_CLOSE_THREADS; i++)
+ pthread_create(&thread, NULL, do_nothing, NULL);
+
+ /* crash on purpose */
+ *(volatile int *)NULL = 0;
+}
+
+/*
+ * Serve one coredump and look at the task on the way. Before the ack the
+ * lock is held and the descriptors are there. Once the kernel is past the
+ * point where it closes them, which is before the first byte of the dump
+ * or before the hangup in userspace mode, they are gone if we asked for
+ * it and the lock is in the expected state.
+ */
+static int close_server(const struct close_test *t, int fd_ipc)
+{
+ struct coredump_req req = {};
+ struct pidfd_info info = {};
+ int fd_server = -1, fd_coredump = -1, fd_peer_pidfd = -1, fd_lock = -1;
+ int exit_code = EXIT_FAILURE;
+ int fd, first_fd, nr_fds;
+ __u64 mask;
+ ssize_t bytes;
+ char c;
+
+ fd_lock = open(LOCK_FILE, O_RDWR | O_CLOEXEC);
+ if (fd_lock < 0) {
+ fprintf(stderr, "%s: open lock file failed: %m\n", __func__);
+ goto out;
+ }
+
+ fd_server = create_and_listen_unix_socket("/tmp/coredump.socket");
+ if (fd_server < 0) {
+ fprintf(stderr, "%s: create_and_listen_unix_socket failed: %m\n", __func__);
+ goto out;
+ }
+
+ if (write_nointr(fd_ipc, "1", 1) < 0) {
+ fprintf(stderr, "%s: write_nointr to ipc socket failed: %m\n", __func__);
+ goto out;
+ }
+ close(fd_ipc);
+
+ fd_coredump = accept4(fd_server, NULL, NULL, SOCK_CLOEXEC);
+ if (fd_coredump < 0) {
+ fprintf(stderr, "%s: accept4 failed: %m\n", __func__);
+ goto out;
+ }
+
+ fd_peer_pidfd = get_peer_pidfd(fd_coredump);
+ if (fd_peer_pidfd < 0) {
+ fprintf(stderr, "%s: get_peer_pidfd failed\n", __func__);
+ goto out;
+ }
+
+ if (!get_pidfd_info(fd_peer_pidfd, &info)) {
+ fprintf(stderr, "%s: get_pidfd_info failed\n", __func__);
+ goto out;
+ }
+
+ if (!read_coredump_req(fd_coredump, &req)) {
+ fprintf(stderr, "%s: read_coredump_req failed\n", __func__);
+ goto out;
+ }
+
+ if (!check_coredump_req(&req)) {
+ fprintf(stderr, "%s: check_coredump_req failed\n", __func__);
+ goto out;
+ }
+
+ /* The task waits for our answer with everything still in place. */
+ if (lock_held(fd_lock, t->kind) != 1) {
+ fprintf(stderr, "%s: lock not held during the handshake\n", __func__);
+ goto out;
+ }
+
+ nr_fds = count_fds(info.pid, &first_fd);
+ if (nr_fds <= 0) {
+ fprintf(stderr, "%s: no descriptors during the handshake\n", __func__);
+ goto out;
+ }
+
+ fd = sys_pidfd_getfd(fd_peer_pidfd, first_fd, 0);
+ if (fd < 0) {
+ fprintf(stderr, "%s: pidfd_getfd during the handshake failed: %m\n", __func__);
+ goto out;
+ }
+ close(fd);
+
+ mask = COREDUMP_WAIT;
+ mask |= t->userspace ? COREDUMP_USERSPACE : COREDUMP_KERNEL;
+ if (t->close)
+ mask |= COREDUMP_CLOSE_FILES;
+
+ if (!send_coredump_ack(fd_coredump, &req, mask, 0)) {
+ fprintf(stderr, "%s: send_coredump_ack failed\n", __func__);
+ goto out;
+ }
+
+ if (!read_marker(fd_coredump, COREDUMP_MARK_REQACK)) {
+ fprintf(stderr, "%s: read_marker COREDUMP_MARK_REQACK failed\n", __func__);
+ goto out;
+ }
+
+ bytes = read_nointr(fd_coredump, &c, 1);
+ if (bytes != (t->userspace ? 0 : 1)) {
+ fprintf(stderr, "%s: read after the ack returned %zd: %m\n", __func__, bytes);
+ goto out;
+ }
+
+ if (lock_held(fd_lock, t->kind) != !t->released) {
+ fprintf(stderr, "%s: lock %s while the coredump is generated\n",
+ __func__, t->released ? "still held" : "released");
+ goto out;
+ }
+
+ nr_fds = count_fds(info.pid, &first_fd);
+ if (nr_fds < 0 || !nr_fds != t->close) {
+ fprintf(stderr, "%s: %d descriptors while the coredump is generated\n",
+ __func__, nr_fds);
+ goto out;
+ }
+
+ fd = sys_pidfd_getfd(fd_peer_pidfd, first_fd, 0);
+ if (t->close) {
+ if (fd >= 0 || errno != EBADF) {
+ fprintf(stderr, "%s: pidfd_getfd after the close returned %d: %m\n",
+ __func__, fd);
+ goto out;
+ }
+ } else {
+ if (fd < 0) {
+ fprintf(stderr, "%s: pidfd_getfd during the coredump failed: %m\n",
+ __func__);
+ goto out;
+ }
+ close(fd);
+ }
+
+ /* COREDUMP_WAIT keeps the task around until we hang up. */
+ if (!get_pidfd_info(fd_peer_pidfd, &info)) {
+ fprintf(stderr, "%s: get_pidfd_info failed\n", __func__);
+ goto out;
+ }
+
+ if (info.mask & PIDFD_INFO_EXIT) {
+ fprintf(stderr, "%s: task exited before the coredump finished\n", __func__);
+ goto out;
+ }
+
+ for (;;) {
+ char buffer[4096];
+
+ bytes = read_nointr(fd_coredump, buffer, sizeof(buffer));
+ if (bytes < 0) {
+ fprintf(stderr, "%s: read from coredump socket failed: %m\n", __func__);
+ goto out;
+ }
+
+ if (bytes == 0)
+ break;
+ }
+
+ exit_code = EXIT_SUCCESS;
+out:
+ if (fd_lock >= 0)
+ close(fd_lock);
+ if (fd_peer_pidfd >= 0)
+ close(fd_peer_pidfd);
+ if (fd_coredump >= 0)
+ close(fd_coredump);
+ if (fd_server >= 0)
+ close(fd_server);
+ return exit_code;
+}
+
+static void run_close_test(struct __test_metadata *const _metadata,
+ FIXTURE_DATA(coredump) *self,
+ const struct close_test *t)
+{
+ int fd, status, ipc_sockets[2], release_pipe[2], result_pipe[2];
+ pid_t pid, pid_coredump_server;
+ char c;
+
+ ASSERT_TRUE(set_core_pattern("@@/tmp/coredump.socket"));
+
+ fd = open(LOCK_FILE, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
+ ASSERT_GE(fd, 0);
+ EXPECT_EQ(close(fd), 0);
+
+ ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets), 0);
+
+ pid_coredump_server = fork();
+ ASSERT_GE(pid_coredump_server, 0);
+ if (pid_coredump_server == 0) {
+ close(ipc_sockets[0]);
+ _exit(close_server(t, ipc_sockets[1]));
+ }
+ self->pid_coredump_server = pid_coredump_server;
+
+ EXPECT_EQ(close(ipc_sockets[1]), 0);
+ ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
+ EXPECT_EQ(close(ipc_sockets[0]), 0);
+
+ /* Only the crashing child and what it spawns see these pipes. */
+ ASSERT_EQ(pipe2(release_pipe, O_CLOEXEC), 0);
+ ASSERT_EQ(pipe2(result_pipe, O_CLOEXEC), 0);
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+ if (pid == 0) {
+ close(release_pipe[1]);
+ close(result_pipe[0]);
+ crashing_child_locked(t, release_pipe[0], result_pipe[1]);
+ }
+ EXPECT_EQ(close(release_pipe[0]), 0);
+ EXPECT_EQ(close(result_pipe[1]), 0);
+
+ waitpid(pid, &status, 0);
+ ASSERT_TRUE(WIFSIGNALED(status));
+ ASSERT_TRUE(WCOREDUMP(status));
+
+ wait_and_check_coredump_server(pid_coredump_server, _metadata, self);
+
+ /* Let the process holding the shared lock go. */
+ EXPECT_EQ(close(release_pipe[1]), 0);
+
+ /* The crashing child is gone but what it shared with is untouched. */
+ if (t->share == LOCK_SHARE_FORK || t->share == LOCK_SHARE_FILES) {
+ ASSERT_EQ(read_nointr(result_pipe[0], &c, 1), 1);
+ ASSERT_EQ(c, 'O');
+ }
+ EXPECT_EQ(close(result_pipe[0]), 0);
+}
+
+TEST_F(coredump, close_files_posix)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_POSIX,
+ .share = LOCK_SHARE_NONE,
+ .threads = true,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_ofd)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_OFD,
+ .share = LOCK_SHARE_NONE,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_flock)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_NONE,
+ .threads = true,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_flock_dup)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_DUP,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_posix_fork)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_POSIX,
+ .share = LOCK_SHARE_FORK,
+ .close = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_flock_fork)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_FORK,
+ .close = true,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_ofd_fork)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_OFD,
+ .share = LOCK_SHARE_FORK,
+ .close = true,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_posix_shared_table)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_POSIX,
+ .share = LOCK_SHARE_FILES,
+ .close = true,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_userspace)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_NONE,
+ .close = true,
+ .userspace = true,
+ .released = true,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_F(coredump, close_files_not_requested)
+{
+ const struct close_test t = {
+ .kind = LOCK_KIND_FLOCK,
+ .share = LOCK_SHARE_NONE,
+ .threads = true,
+ .close = false,
+ .released = false,
+ };
+
+ run_close_test(_metadata, self, &t);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/coredump/coredump_socket_protocol_test.c b/tools/testing/selftests/coredump/coredump_socket_protocol_test.c
index f5c9bad875463..beb167698cfdb 100644
--- a/tools/testing/selftests/coredump/coredump_socket_protocol_test.c
+++ b/tools/testing/selftests/coredump/coredump_socket_protocol_test.c
@@ -2377,6 +2377,12 @@ TEST_F(coredump, socket_request_memory_types_without_kernel)
check_conflicting_ack(_metadata, self, COREDUMP_USERSPACE | COREDUMP_MEMORY_TYPES);
}
+/* A rejected task closes everything on its way out anyway. */
+TEST_F(coredump, socket_request_close_files_reject)
+{
+ check_conflicting_ack(_metadata, self, COREDUMP_REJECT | COREDUMP_CLOSE_FILES);
+}
+
/*
* A server built with the first structs reads the request it knows,
* discards the rest and acks with the ack it knows. It raises nothing
diff --git a/tools/testing/selftests/coredump/coredump_test_helpers.c b/tools/testing/selftests/coredump/coredump_test_helpers.c
index 4e36e3e4fb78f..65132a0decedd 100644
--- a/tools/testing/selftests/coredump/coredump_test_helpers.c
+++ b/tools/testing/selftests/coredump/coredump_test_helpers.c
@@ -1612,7 +1612,8 @@ bool send_coredump_ack(int fd, const struct coredump_req *req,
#define TEST_REQ_MASK_ALL \
(COREDUMP_KERNEL | COREDUMP_USERSPACE | \
COREDUMP_REJECT | COREDUMP_WAIT | \
- COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES)
+ COREDUMP_RECORDS | COREDUMP_SPARSE | COREDUMP_MEMORY_TYPES | \
+ COREDUMP_CLOSE_FILES)
bool check_coredump_req(const struct coredump_req *req)
{
|
| 31/3 | 2026/08/24 18:08 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 15490 output: 178 thoughts: 557 Thoughts: **My Assessment of the Coredump File Descriptor Closure Patch** Okay, so this patch introduces a new feature to the coredump mechanism: `COREDUMP_CLOSE_FILES`. When set in the acknowledgement mask, the kernel will close the file descriptors of the crashing task *before* dumping the core. Smart. Essentially, it's swapping out the existing file descriptor table for an empty one. This is interesting. Let's break down the changes. I see it's touching several files. In `fs/coredump.c`, the `coredump_sock_request` function now checks for the new flag, and `do_coredump` calls a new function, `coredump_close_files()`. `fs/file.c` has seen the bulk of the changes. We have `alloc_files_struct` allocating the new empty table, `close_files` being modified to be more aggressive with `filp_close_sync` for non-kernel threads, a `switch_files_struct` to do the actual swap, and the key `coredump_close_files` which iterates through all threads in the coredump state and orchestrates the closure, along with a `coredump_task_close_files` that handles cleanup during thread exit. They've added `filp_close_sync` in `fs/file_table.c`, which is the immediate, synchronous version of `filp_close`. That makes sense; we don't want to defer file release in this scenario. Finally, `kernel/exit.c` now calls `coredump_task_close_files` during the exit process, waiting for the coredump to complete. Now, the important question: are there any potential uninitialized memory risks here? Let's see... the `alloc_files_struct` uses `GFP_KERNEL | __GFP_ZERO`, so it's initialized to zero. The `coredump_ack` struct in `uapi/linux/coredump.h` hasn't changed size, only the enum for the flags. So, no new user-space exposure of uninitialized memory. From the code, it's all about file descriptor table pointers, switching, and closing file descriptors. Nothing complicated is allocated that I can see, and nothing new is copied to user space. It seems well isolated. Based on this, I don't see any immediate reason to suspect that KMSAN is needed here. This mostly deals with pointers and file descriptors which are the bread and butter of KASAN's analysis. Standard bug detectors like KASAN and others should be sufficient to catch any issues like use-after-free, null pointer dereferences, or locking bugs that might sneak in during the file descriptor manipulations. This looks pretty solid. |
| 32/3 | 2026/08/24 18:08 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch introduces a new feature to close file descriptors before generating a coredump (COREDUMP_CLOSE_FILES). It modifies file descriptor table management, adds a new empty file descriptor table allocation (which is explicitly zeroed using __GFP_ZERO), and alters the coredump synchronization logic. No new data structures are copied to userspace, and no complex structures are allocated without initialization. The changes primarily involve pointer switching, reference counting, and file closing, which might risk use-after-free, NULL pointer dereferences, or locking issues. These types of bugs are effectively caught by KASAN, LOCKDEP, and standard bug detectors. There is no risk of uninitialized memory usage or info-leaks that would require KMSAN.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch introduces a new feature to close file descriptors before generating a coredump (COREDUMP_CLOSE_FILES). It modifies file descriptor table management, adds a new empty file descriptor table allocation (which is explicitly zeroed using __GFP_ZERO), and alters the coredump synchronization logic. No new data structures are copied to userspace, and no complex structures are allocated without initialization. The changes primarily involve pointer switching, reference counting, and file closing, which might risk use-after-free, NULL pointer dereferences, or locking issues. These types of bugs are effectively caught by KASAN, LOCKDEP, and standard bug detectors. There is no risk of uninitialized memory usage or info-leaks that would require KMSAN.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|