git: aa90eb737dcf - stable/13 - Make core dump writes interruptible with SIGKILL

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sun, 10 Oct 2021 09:24:28 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=aa90eb737dcf80ba1d4342250ab738861d57676e

commit aa90eb737dcf80ba1d4342250ab738861d57676e
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-10-05 05:11:32 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-10-10 09:22:58 +0000

    Make core dump writes interruptible with SIGKILL
    
    (cherry picked from commit b5cadc643e853fa4cb23e5315e6f40bf9979a9c0)
---
 share/man/man5/core.5 | 16 +++++++++++++++-
 sys/kern/imgact_elf.c |  4 ++++
 sys/kern/kern_exec.c  |  5 +++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/share/man/man5/core.5 b/share/man/man5/core.5
index d176548d1570..0af1b2b7e8bf 100644
--- a/share/man/man5/core.5
+++ b/share/man/man5/core.5
@@ -28,7 +28,7 @@
 .\"     @(#)core.5	8.3 (Berkeley) 12/11/93
 .\" $FreeBSD$
 .\"
-.Dd August 2, 2020
+.Dd October 5, 2021
 .Dt CORE 5
 .Os
 .Sh NAME
@@ -57,6 +57,20 @@ The maximum size of a core file is limited by the
 limit.
 Files which would be larger than the limit are not created.
 .Pp
+With a large limit, a process that had mapped a very large,
+and perhaps sparsely populated, virtual memory region, could take
+a very long time to create core dumps.
+The system ignores all signals sent to a process writing a core file, except
+.Dv SIGKILL
+which terminates the writing and causes immediate exit of the process.
+The behavior of
+.Dv SIGKILL
+can be disabled by setting tunable
+.Xr sysctl 8
+variable
+.Va kern.core_dump_can_intr
+to zero.
+.Pp
 The name of the file is controlled via the
 .Xr sysctl 8
 variable
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index 057c583324bb..e1b6e3fc6ba7 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -1547,6 +1547,8 @@ core_write(struct coredump_params *p, const void *base, size_t len,
 	    p->active_cred, p->file_cred, resid, p->td));
 }
 
+extern int core_dump_can_intr;
+
 static int
 core_output(char *base, size_t len, off_t offset, struct coredump_params *p,
     void *tmpbuf)
@@ -1572,6 +1574,8 @@ core_output(char *base, size_t len, off_t offset, struct coredump_params *p,
 		 * anonymous memory or truncated files, for example.
 		 */
 		for (runlen = 0; runlen < len; runlen += PAGE_SIZE) {
+			if (core_dump_can_intr && curproc_sigkilled())
+				return (EINTR);
 			error = vm_fault(map, (uintptr_t)base + runlen,
 			    VM_PROT_READ, VM_FAULT_NOFILL, NULL);
 			if (runlen == 0)
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index 0611eedcec73..2ea0efc4a2cb 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -150,6 +150,11 @@ static int map_at_zero = 0;
 SYSCTL_INT(_security_bsd, OID_AUTO, map_at_zero, CTLFLAG_RWTUN, &map_at_zero, 0,
     "Permit processes to map an object at virtual address 0.");
 
+int core_dump_can_intr = 1;
+SYSCTL_INT(_kern, OID_AUTO, core_dump_can_intr, CTLFLAG_RWTUN,
+    &core_dump_can_intr, 0,
+    "Core dumping interruptible with SIGKILL");
+
 static int
 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
 {