4343#include <linux/timekeeping.h>
4444#include <linux/sysctl.h>
4545#include <linux/elf.h>
46+ #include <linux/pidfs.h>
47+ #include <uapi/linux/pidfd.h>
4648
4749#include <linux/uaccess.h>
4850#include <asm/mmu_context.h>
5658
5759static bool dump_vma_snapshot (struct coredump_params * cprm );
5860static void free_vma_snapshot (struct coredump_params * cprm );
61+ /*
62+ * File descriptor number for the pidfd for the thread-group leader of
63+ * the coredumping task installed into the usermode helper's file
64+ * descriptor table.
65+ */
66+ #define COREDUMP_PIDFD_NUMBER 3
5967
6068static int core_uses_pid ;
6169static unsigned int core_pipe_limit ;
@@ -329,6 +337,31 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
329337 err = cn_printf (cn , "%lu" ,
330338 rlimit (RLIMIT_CORE ));
331339 break ;
340+ /* CPU the task ran on */
341+ case 'C' :
342+ err = cn_printf (cn , "%d" , cprm -> cpu );
343+ break ;
344+ /* pidfd number */
345+ case 'F' : {
346+ /*
347+ * Installing a pidfd only makes sense if
348+ * we actually spawn a usermode helper.
349+ */
350+ if (!ispipe )
351+ break ;
352+
353+ /*
354+ * Note that we'll install a pidfd for the
355+ * thread-group leader. We know that task
356+ * linkage hasn't been removed yet and even if
357+ * this @current isn't the actual thread-group
358+ * leader we know that the thread-group leader
359+ * cannot be reaped until @current has exited.
360+ */
361+ cprm -> pid = task_tgid (current );
362+ err = cn_printf (cn , "%d" , COREDUMP_PIDFD_NUMBER );
363+ break ;
364+ }
332365 default :
333366 break ;
334367 }
@@ -358,7 +391,7 @@ static int zap_process(struct task_struct *start, int exit_code)
358391 struct task_struct * t ;
359392 int nr = 0 ;
360393
361- /* ignore all signals except SIGKILL, see prepare_signal() */
394+ /* Allow SIGKILL, see prepare_signal() */
362395 start -> signal -> flags = SIGNAL_GROUP_EXIT ;
363396 start -> signal -> group_exit_code = exit_code ;
364397 start -> signal -> group_stop_count = 0 ;
@@ -483,7 +516,7 @@ static void wait_for_dump_helpers(struct file *file)
483516}
484517
485518/*
486- * umh_pipe_setup
519+ * umh_coredump_setup
487520 * helper function to customize the process used
488521 * to collect the core in userspace. Specifically
489522 * it sets up a pipe and installs it as fd 0 (stdin)
@@ -493,22 +526,46 @@ static void wait_for_dump_helpers(struct file *file)
493526 * is a special value that we use to trap recursive
494527 * core dumps
495528 */
496- static int umh_pipe_setup (struct subprocess_info * info , struct cred * new )
529+ static int umh_coredump_setup (struct subprocess_info * info , struct cred * new )
497530{
498531 struct file * files [2 ];
499532 struct coredump_params * cp = (struct coredump_params * )info -> data ;
500- int err = create_pipe_files (files , 0 );
533+ int err ;
534+
535+ if (cp -> pid ) {
536+ struct file * pidfs_file __free (fput ) = NULL ;
537+
538+ pidfs_file = pidfs_alloc_file (cp -> pid , 0 );
539+ if (IS_ERR (pidfs_file ))
540+ return PTR_ERR (pidfs_file );
541+
542+ /*
543+ * Usermode helpers are childen of either
544+ * system_unbound_wq or of kthreadd. So we know that
545+ * we're starting off with a clean file descriptor
546+ * table. So we should always be able to use
547+ * COREDUMP_PIDFD_NUMBER as our file descriptor value.
548+ */
549+ err = replace_fd (COREDUMP_PIDFD_NUMBER , pidfs_file , 0 );
550+ if (err < 0 )
551+ return err ;
552+ }
553+
554+ err = create_pipe_files (files , 0 );
501555 if (err )
502556 return err ;
503557
504558 cp -> file = files [1 ];
505559
506560 err = replace_fd (0 , files [0 ], 0 );
507561 fput (files [0 ]);
562+ if (err < 0 )
563+ return err ;
564+
508565 /* and disallow core files too */
509566 current -> signal -> rlim [RLIMIT_CORE ] = (struct rlimit ){1 , 1 };
510567
511- return err ;
568+ return 0 ;
512569}
513570
514571void do_coredump (const kernel_siginfo_t * siginfo )
@@ -538,6 +595,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
538595 */
539596 .mm_flags = mm -> flags ,
540597 .vma_meta = NULL ,
598+ .cpu = raw_smp_processor_id (),
541599 };
542600
543601 audit_core_dumps (siginfo -> si_signo );
@@ -584,7 +642,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
584642 }
585643
586644 if (cprm .limit == 1 ) {
587- /* See umh_pipe_setup () which sets RLIMIT_CORE = 1.
645+ /* See umh_coredump_setup () which sets RLIMIT_CORE = 1.
588646 *
589647 * Normally core limits are irrelevant to pipes, since
590648 * we're not writing to the file system, but we use
@@ -629,7 +687,7 @@ void do_coredump(const kernel_siginfo_t *siginfo)
629687 retval = - ENOMEM ;
630688 sub_info = call_usermodehelper_setup (helper_argv [0 ],
631689 helper_argv , NULL , GFP_KERNEL ,
632- umh_pipe_setup , NULL , & cprm );
690+ umh_coredump_setup , NULL , & cprm );
633691 if (sub_info )
634692 retval = call_usermodehelper_exec (sub_info ,
635693 UMH_WAIT_EXEC );
0 commit comments