coredump: allow for flexible coredump handling
Christian Brauner
maintainercover coredump: allow for flexible coredump handling
In addition to the extensive selftests I've already written a (non-production ready) simple Rust coredump server for this in userspace:
https://github.com/brauner/dumdum.git
Extend the coredump socket to allow the coredump server to tell the kernel how to process individual coredumps. This allows for fine-grained coredump management allowing choose how to process individual coredump requests. Userspace can decide to just let the kernel write out the coredump, or generate the coredump itself, or just reject it.
When the crashing task connects to the coredump socket the kernel will send a struct coredump_req to the coredump server. The kernel will set the size member of struct coredump_req allowing the coredump server how much data can be read.
The coredump server uses MSG_PEEK to peek the size of struct coredump_req. If the kernel uses a newer struct coredump_req the coredump server just reads the size it knows and discard any remaining bytes in the buffer. If the kernel uses an older struct coredump_req the coredump server just reads the size the kernel knows.
The returned struct coredump_req will inform the coredump server what features the kernel supports. The coredump_req->mask member is set to the currently know features.
The coredump server may only use features whose bits were raised by the kernel in coredump_req->mask.
In response to a coredump_req from the kernel the coredump server sends a struct coredump_ack to the kernel. The kernel informs the coredump server what version of struct coredump_ack it supports by setting struct coredump_req->size_ack to the size it knows about. The coredump server may only send as many bytes as coredump_req->size_ack indicates (a smaller size is fine of course). The coredump server must set coredump_ack->size accordingly.
The coredump server sets the features it wants to use in struct coredump_ack->mask. Only bits returned in struct coredump_req->mask may be used.
In case an invalid struct coredump_ack is sent to the kernel an out-of-band byte will be sent by the kernel indicating the reason why the coredump_ack was rejected.
If the coredump server sent a valid struct coredump_ack the kernel will place an out-of-band byte indicating that the request was successful. If the kernel is generating the coredump and sending the coredump data on the socket the success out-of-band marker can be used as an indicator when coredump data starts.
The out-of-band markers allow advanced userspace to infer failure. They are optional and can be ignored by not listening for POLLPRI events and aren't necessary for the coredump server to function correctly.
In the initial version the following features are supported in coredump_{req,ack}->mask:
* COREDUMP_KERNEL
The kernel will write the coredump data to the socket after the req-ack sequence has concluded successfully.
* COREDUMP_USERSPACE
The kernel will not write coredump data but will indicate to the parent that a coredump has been generated. This is used when userspace generates its own coredumps.
* COREDUMP_REJECT
The kernel will skip generating a coredump for this task.
* COREDUMP_WAIT
The kernel will prevent the task from exiting until the coredump server has shutdown the socket connection.
---
Christian Brauner (5):
coredump: allow for flexible coredump handling selftests/coredump: fix build selftests/coredump: cleanup coredump tests
tools: add coredump.h header selftests/coredump: add coredump server selftests
fs/coredump.c | 151 +- include/uapi/linux/coredump.h | 104 ++ tools/include/uapi/linux/coredump.h | 104 ++ tools/testing/selftests/coredump/Makefile | 2 +- tools/testing/selftests/coredump/config | 4 + tools/testing/selftests/coredump/stackdump_test.c | 1524 +++++++++++++++++---- 6 files changed, 1639 insertions(+), 250 deletions(-)
---
base-commit: 3e406741b19890c3d8a2ed126aa7c23b106ca9e1
change-id: 20250520-work-coredump-socket-protocol-6980d1f54c2f
Christian Brauner
maintainerPATCH 1/5 coredump: allow for flexible coredump handling
Extend the coredump socket to allow the coredump server to tell the kernel how to process individual coredumps.
When the crashing task connects to the coredump socket the kernel will send a struct coredump_req to the coredump server. The kernel will set the size member of struct coredump_req allowing the coredump server how much data can be read.
The coredump server uses MSG_PEEK to peek the size of struct coredump_req. If the kernel uses a newer struct coredump_req the coredump server just reads the size it knows and discard any remaining bytes in the buffer. If the kernel uses an older struct coredump_req the coredump server just reads the size the kernel knows.
The returned struct coredump_req will inform the coredump server what features the kernel supports. The coredump_req->mask member is set to the currently know features.
The coredump server may only use features whose bits were raised by the kernel in coredump_req->mask.
In response to a coredump_req from the kernel the coredump server sends a struct coredump_ack to the kernel. The kernel informs the coredump server what version of struct coredump_ack it supports by setting struct coredump_req->size_ack to the size it knows about. The coredump server may only send as many bytes as coredump_req->size_ack indicates (a smaller size is fine of course). The coredump server must set coredump_ack->size accordingly.
The coredump server sets the features it wants to use in struct coredump_ack->mask. Only bits returned in struct coredump_req->mask may be used.
In case an invalid struct coredump_ack is sent to the kernel an out-of-band byte will be sent by the kernel indicating the reason why the coredump_ack was rejected.
If the coredump server sent a valid struct coredump_ack the kernel will place an out-of-band byte indicating that the request was successful. If the kernel is generating the coredump and sending the coredump data on the socket the success out-of-band marker can be used as an indicator when coredump data starts.
The out-of-band markers allow advanced userspace to infer failure. They are optional and can be ignored by not listening for POLLPRI events and aren't necessary for the coredump server to function correctly.
In the initial version the following features are supported in coredump_{req,ack}->mask:
* COREDUMP_KERNEL
The kernel will write the coredump data to the socket after the req-ack sequence has concluded successfully.
* COREDUMP_USERSPACE
The kernel will not write coredump data but will indicate to the parent that a coredump has been generated. This is used when userspace generates its own coredumps.
* COREDUMP_REJECT
The kernel will skip generating a coredump for this task.
* COREDUMP_WAIT
The kernel will prevent the task from exiting until the coredump server has shutdown the socket connection.
--- fs/coredump.c | 151 ++++++++++++++++++++++++++++++++++++++++-- include/uapi/linux/coredump.h | 104 +++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+), 7 deletions(-)
fs/coredump.c +144 -7
@@ -51,6 +51,7 @@#include <net/sock.h> #include <uapi/linux/pidfd.h> #include <uapi/linux/un.h> +#include <uapi/linux/coredump.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -83,15 +84,17 @@ static int core_name_size = CORENAME_MAX_SIZE;unsigned int core_file_note_size_limit =CORE_FILE_NOTE_SIZE_DEFAULT ;enum coredump_type_t { -COREDUMP_FILE =1 , -COREDUMP_PIPE =2 , -COREDUMP_SOCK =3 , +COREDUMP_FILE =1 , +COREDUMP_PIPE =2 , +COREDUMP_SOCK =3 , +COREDUMP_SOCK_REQ =4 , };struct core_name {char *corename;int used, size;enum coredump_type_t core_type; +u64 mask; };static int expand_corename (struct core_name *cn,int size)
@@ -235,6 +238,9 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,int pid_in_pattern =0 ;int err =0 ; + cn->mask =COREDUMP_KERNEL ; +if (core_pipe_limit) + cn->mask |=COREDUMP_WAIT ; cn->used =0 ; cn->corename =NULL ;if (*pat_ptr =='|' )
@@ -264,6 +270,13 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm, pat_ptr++;if (!(*pat_ptr))return -ENOMEM ; +if (*pat_ptr =='@' ) { + pat_ptr++; +if (!(*pat_ptr)) +return -ENOMEM ; + + cn->core_type =COREDUMP_SOCK_REQ ; + } err =cn_printf (cn,"%s" , pat_ptr);if (err)
@@ -632,6 +645,110 @@ static int umh_coredump_setup(struct subprocess_info *info, struct cred *new)return 0 ; } +#ifdef CONFIG_UNIX +static inline int coredump_sock_recv (struct file *file,struct coredump_ack *ack,size_t size,int flags) +{ +struct msghdr msg = {}; +struct kvec iov = { .iov_base = ack, .iov_len = size }; +ssize_t ret; + +memset (ack,0 , size); + ret =kernel_recvmsg (sock_from_file (file), &msg, &iov,1 , size, flags); +if (ret <0 ) +return ret; +if (ret != size) +return -EINVAL ; +return 0 ; +} + +static inline int coredump_sock_send (struct file *file,struct coredump_req *req) +{ +struct msghdr msg = { .msg_flags =MSG_NOSIGNAL }; +struct kvec iov = { .iov_base = req, .iov_len =sizeof (*req) }; +ssize_t ret; + + ret =kernel_sendmsg (sock_from_file (file), &msg, &iov,1 ,sizeof (*req)); +if (ret <0 ) +return ret; +if (ret !=sizeof (*req)) +return -EINVAL ; +return 0 ; +} + +static inline int coredump_sock_oob (struct file *file,uint8_t oob) +{ +#ifdef CONFIG_AF_UNIX_OOB +struct msghdr msg = { .msg_flags =MSG_NOSIGNAL |MSG_OOB }; +struct kvec iov = { .iov_base = &oob, .iov_len =sizeof (oob) }; + +kernel_sendmsg (sock_from_file (file), &msg, &iov,1 ,sizeof (oob)); +#endif +return -EINVAL ; +} + +static int coredump_request (struct core_name *cn,struct coredump_params *cprm) +{ +struct coredump_req req = { + .size =sizeof (struct coredump_req ), + .mask =COREDUMP_KERNEL |COREDUMP_USERSPACE | +COREDUMP_REJECT |COREDUMP_WAIT , + .size_ack =sizeof (struct coredump_ack ), + }; +struct coredump_ack ack = {}; +ssize_t ret, usize; + +if (cn->core_type !=COREDUMP_SOCK_REQ ) +return 0 ; + +/* Let userspace know what we support. */ + ret =coredump_sock_send (cprm->file, &req); +if (ret) +return ret; + +/* +* Peek the size of the coredump_ack struct that userspace +* wants to send us and then retrieve it all in one go. +*/ + ret =coredump_sock_recv (cprm->file, &ack,sizeof (ack.size), +MSG_PEEK |MSG_WAITALL ); +if (ret) +return ret; + +/* +* We told userspace what size we know about. +* So refuse anything too small or larger than we know. +*/ + usize = ack.size; +if (usize <COREDUMP_ACK_SIZE_VER0 || usize >sizeof (ack)) +return coredump_sock_oob (cprm->file,COREDUMP_OOB_INVALIDSIZE ); + +/* Now retrieve the coredump_ack. */ + ret =coredump_sock_recv (cprm->file, &ack, usize,MSG_WAITALL ); +if (ret) +return ret; +if (ack.size != usize) +return -EINVAL ; + +/* +* We told userspace what flags we support. +* So refuse any flags we don't know. +*/ +if (ack.mask & ~req.mask) +return coredump_sock_oob (cprm->file,COREDUMP_OOB_UNSUPPORTED ); + +/* Handle mutually exclusive options. */ +if (hweight64 (ack.mask & (COREDUMP_USERSPACE |COREDUMP_KERNEL | +COREDUMP_REJECT )) !=1 ) +return coredump_sock_oob (cprm->file,COREDUMP_OOB_CONFLICTING ); + +if (ack.spare) +return coredump_sock_oob (cprm->file,COREDUMP_OOB_UNSUPPORTED ); + + cn->mask = ack.mask; +return 0 ; +} +#endif +void do_coredump (const kernel_siginfo_t *siginfo) {struct core_state core_state;
@@ -850,6 +967,8 @@ void do_coredump(const kernel_siginfo_t *siginfo) }break ; } +case COREDUMP_SOCK_REQ : + fallthrough;case COREDUMP_SOCK : {#ifdef CONFIG_UNIX struct file *file __free(fput) = NULL;
@@ -918,6 +1037,10 @@ void do_coredump(const kernel_siginfo_t *siginfo) cprm.limit =RLIM_INFINITY ; cprm.file =no_free_ptr (file); + + retval =coredump_request (&cn, &cprm); +if (retval) + goto close_fail;#else coredump_report_failure ("Core dump socket support %s disabled" , cn.corename); goto close_fail;
@@ -929,12 +1052,20 @@ void do_coredump(const kernel_siginfo_t *siginfo) goto close_fail; } +/* Don't even generate the coredump. */ +if (cn.mask &COREDUMP_REJECT ) + goto close_fail; +/* get us an unshared descriptor table; almost always a no-op */ /* The cell spufs coredump code reads the file descriptor tables */ retval =unshare_files ();if (retval) goto close_fail; -if (!dump_interrupted ()) { + +if (dump_interrupted ()) + goto close_fail; + +if (cn.mask &COREDUMP_KERNEL ) {/* * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would * have this set to NULL.
@@ -968,17 +1099,23 @@ void do_coredump(const kernel_siginfo_t *siginfo)kernel_sock_shutdown (sock_from_file (cprm.file),SHUT_WR );#endif +/* Let the parent know that a coredump was generated. */ +if (cn.mask &COREDUMP_USERSPACE ) + core_dumped = true; +/* * When core_pipe_limit is set we wait for the coredump server * or usermodehelper to finish before exiting so it can e.g., * inspect /proc/<pid>. */ -if (core_pipe_limit) { +if (cn.mask &COREDUMP_WAIT ) {switch (cn.core_type) {case COREDUMP_PIPE :wait_for_dump_helpers (cprm.file);break ;#ifdef CONFIG_UNIX +case COREDUMP_SOCK_REQ : + fallthrough;case COREDUMP_SOCK : {ssize_t n;
@@ -1249,8 +1386,8 @@ static inline bool check_coredump_socket(void)if (current->nsproxy->mnt_ns != init_task.nsproxy->mnt_ns)return false; -/* Must be an absolute path. */ -if (*(core_pattern +1 ) !='/' ) +/* Must be an absolute path or the socket request. */ +if (*(core_pattern +1 ) !='/' && *(core_pattern +1 ) !='@' )return false;return true;
include/uapi/linux/coredump.h new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH 2/5 selftests/coredump: fix build
In reply to Christian BraunerFix various warnings in the selftest build.
--- tools/testing/selftests/coredump/Makefile | 2 +- tools/testing/selftests/coredump/stackdump_test.c | 17 +++++------------ 2 files changed, 6 insertions(+), 13 deletions(-)
tools/testing/selftests/coredump/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -CFLAGS = $(KHDR_INCLUDES) +CFLAGS = -Wall -O0 $(KHDR_INCLUDES) TEST_GEN_PROGS := stackdump_test TEST_FILES := stackdump
tools/testing/selftests/coredump/stackdump_test.c +5 -12
@@ -24,6 +24,8 @@ static void *do_nothing(void *) {while (1 )pause (); + +return NULL ; }static void crashing_child (void )
@@ -46,9 +48,7 @@ FIXTURE(coredump)FIXTURE_SETUP (coredump) { -char buf[PATH_MAX ];FILE *file; -char *dir;int ret; self->pid_coredump_server = -ESRCH ;
@@ -106,7 +106,6 @@ FIXTURE_TEARDOWN(coredump) TEST_F_TIMEOUT(coredump, stackdump, 120) { -struct sigaction action = {};unsigned long long stack;char *test_dir, *line;size_t line_length;
@@ -171,11 +170,10 @@ TEST_F_TIMEOUT(coredump, stackdump, 120) TEST_F(coredump, socket) { -int fd, pidfd, ret, status; +int pidfd, ret, status;FILE *file;pid_t pid, pid_coredump_server;struct stat st; -char core_file[PATH_MAX ];struct pidfd_info info = {};int ipc_sockets[2 ];char c;
@@ -356,11 +354,10 @@ TEST_F(coredump, socket) TEST_F(coredump, socket_detect_userspace_client) { -int fd, pidfd, ret, status; +int pidfd, ret, status;FILE *file;pid_t pid, pid_coredump_server;struct stat st; -char core_file[PATH_MAX ];struct pidfd_info info = {};int ipc_sockets[2 ];char c;
@@ -384,7 +381,7 @@ TEST_F(coredump, socket_detect_userspace_client) pid_coredump_server =fork ();ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) { -int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; +int fd_server, fd_coredump, fd_peer_pidfd;socklen_t fd_peer_pidfd_len;close (ipc_sockets[0 ]);
@@ -464,7 +461,6 @@ TEST_F(coredump, socket_detect_userspace_client)close (fd_coredump);close (fd_server);close (fd_peer_pidfd); -close (fd_core_file);_exit (EXIT_SUCCESS ); } self->pid_coredump_server = pid_coredump_server;
@@ -488,7 +484,6 @@ TEST_F(coredump, socket_detect_userspace_client)if (ret <0 )_exit (EXIT_FAILURE ); - (void *)write (fd_socket, &(char ){0 },1 );close (fd_socket);_exit (EXIT_SUCCESS ); }
@@ -519,7 +514,6 @@ TEST_F(coredump, socket_enoent)int pidfd, ret, status;FILE *file;pid_t pid; -char core_file[PATH_MAX ]; file =fopen ("/proc/sys/kernel/core_pattern" ,"w" );ASSERT_NE (file,NULL );
@@ -569,7 +563,6 @@ TEST_F(coredump, socket_no_listener)ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) {int fd_server; -socklen_t fd_peer_pidfd_len;close (ipc_sockets[0 ]);
Signature · 1 line
2.47.2
Re: [PATCH 2/5] selftests/coredump: fix build
Quoted 110 lines · Christian Brauner
Fix various warnings in the selftest build.
Signed-off-by Christian Brauner <[email protected]>--- tools/testing/selftests/coredump/Makefile | 2 +- tools/testing/selftests/coredump/stackdump_test.c | 17 +++++------------ 2 files changed, 6 insertions(+), 13 deletions(-)tools/testing/selftests/coredump/Makefile +1
-1@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -CFLAGS = $(KHDR_INCLUDES) +CFLAGS = -Wall -O0 $(KHDR_INCLUDES) TEST_GEN_PROGS := stackdump_test TEST_FILES := stackdumptools/testing/selftests/coredump/stackdump_test.c +5
-11@@ -24,6 +24,8 @@ static void *do_nothing(void *) {while (1 )pause (); + +return NULL ; }static void crashing_child (void )@@ -46,9 +48,7 @@ FIXTURE(coredump)FIXTURE_SETUP (coredump) { -char buf[PATH_MAX ];FILE *file; -char *dir;int ret; self->pid_coredump_server = -ESRCH ;@@ -106,7 +106,6 @@ FIXTURE_TEARDOWN(coredump) TEST_F_TIMEOUT(coredump, stackdump, 120) { -struct sigaction action = {};unsigned long long stack;char *test_dir, *line;size_t line_length;@@ -171,11 +170,10 @@ TEST_F_TIMEOUT(coredump, stackdump, 120) TEST_F(coredump, socket) { -int fd, pidfd, ret, status; +int pidfd, ret, status;FILE *file;pid_t pid, pid_coredump_server;struct stat st; -char core_file[PATH_MAX ];struct pidfd_info info = {};int ipc_sockets[2 ];char c;@@ -356,11 +354,10 @@ TEST_F(coredump, socket) TEST_F(coredump, socket_detect_userspace_client) { -int fd, pidfd, ret, status; +int pidfd, ret, status;FILE *file;pid_t pid, pid_coredump_server;struct stat st; -char core_file[PATH_MAX ];struct pidfd_info info = {};int ipc_sockets[2 ];char c;@@ -384,7 +381,7 @@ TEST_F(coredump, socket_detect_userspace_client) pid_coredump_server =fork ();ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) { -int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; +int fd_server, fd_coredump, fd_peer_pidfd;socklen_t fd_peer_pidfd_len;close (ipc_sockets[0 ]);@@ -464,7 +461,6 @@ TEST_F(coredump, socket_detect_userspace_client)close (fd_coredump);close (fd_server);close (fd_peer_pidfd); -close (fd_core_file);_exit (EXIT_SUCCESS ); } self->pid_coredump_server = pid_coredump_server;@@ -488,7 +484,6 @@ TEST_F(coredump, socket_detect_userspace_client)if (ret <0 )_exit (EXIT_FAILURE ); - (void *)write (fd_socket, &(char ){0 },1 );close (fd_socket);_exit (EXIT_SUCCESS ); }@@ -519,7 +514,6 @@ TEST_F(coredump, socket_enoent)int pidfd, ret, status;FILE *file;pid_t pid; -char core_file[PATH_MAX ]; file =fopen ("/proc/sys/kernel/core_pattern" ,"w" );ASSERT_NE (file,NULL );
tools/testing/selftests/coredump/stackdump_test.c TEST_F(coredump, socket_no_listener) · line 568
Quoted 7 lines
tools/testing/selftests/coredump/stackdump_test.c +0
-1@@ -569,7 +563,6 @@ TEST_F(coredump, socket_no_listener)ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) {int fd_server; -socklen_t fd_peer_pidfd_len;close (ipc_sockets[0 ]);
Christian Brauner
maintainerPATCH 3/5 selftests/coredump: cleanup coredump tests
In reply to Christian BraunerMake the selftests we added this cycle easier to read.
--- tools/testing/selftests/coredump/stackdump_test.c | 409 +++++++++------------- 1 file changed, 174 insertions(+), 235 deletions(-)
tools/testing/selftests/coredump/stackdump_test.c +174 -235
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +#include <assert.h> #include <fcntl.h> #include <inttypes.h> #include <libgen.h>
@@ -20,6 +21,10 @@#define STACKDUMP_SCRIPT "stackdump"#define NUM_THREAD_SPAWN 128 +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif +static void *do_nothing (void *) {while (1 )
@@ -109,7 +114,7 @@ TEST_F_TIMEOUT(coredump, stackdump, 120)unsigned long long stack;char *test_dir, *line;size_t line_length; -char buf[PATH_MAX ]; +char buf[PAGE_SIZE ];int ret, i, status;FILE *file;pid_t pid;
@@ -168,152 +173,163 @@ TEST_F_TIMEOUT(coredump, stackdump, 120)fclose (file); } +static int create_and_listen_unix_socket (const char *path) +{ +struct sockaddr_un addr = { + .sun_family =AF_UNIX , + }; +assert (strlen (path) <sizeof (addr.sun_path) -1 ); +strncpy (addr.sun_path, path,sizeof (addr.sun_path) -1 ); +size_t addr_len = + offsetof(struct sockaddr_un , sun_path) +strlen (path) +1 ; +int fd, ret; + + fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +if (fd <0 ) + goto out; + + ret =bind (fd, (const struct sockaddr *)&addr, addr_len); +if (ret <0 ) + goto out; + + ret =listen (fd,1 ); +if (ret <0 ) + goto out; + +return fd; + +out: +if (fd >=0 ) +close (fd); +return -1 ; +} + +static bool set_core_pattern (const char *pattern) +{ +FILE *file; +int ret; + + file =fopen ("/proc/sys/kernel/core_pattern" ,"w" ); +if (!file) +return false; + + ret =fprintf (file,"%s" , pattern); +fclose (file); + +return ret ==strlen (pattern); +} + +static int get_peer_pidfd (int fd) +{ +int fd_peer_pidfd; +socklen_t fd_peer_pidfd_len =sizeof (fd_peer_pidfd); +int ret =getsockopt (fd,SOL_SOCKET ,SO_PEERPIDFD , &fd_peer_pidfd, + &fd_peer_pidfd_len); +if (ret <0 ) { +fprintf (stderr,"%m - Failed to retrieve peer pidfd for coredump socket connection\n" ); +return -1 ; + } +return fd_peer_pidfd; +} + +static bool get_pidfd_info (int fd_peer_pidfd,struct pidfd_info *info) +{ +memset (info,0 ,sizeof (*info)); + info->mask =PIDFD_INFO_EXIT |PIDFD_INFO_COREDUMP ; +return ioctl (fd_peer_pidfd,PIDFD_GET_INFO , info) ==0 ; +} + +static void +wait_and_check_coredump_server (pid_t pid_coredump_server, +struct __test_metadata *const _metadata, +FIXTURE_DATA (coredump)* self) +{ +int status; +waitpid (pid_coredump_server, &status,0 ); + self->pid_coredump_server = -ESRCH ; +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +} + TEST_F(coredump, socket) {int pidfd, ret, status; -FILE *file;pid_t pid, pid_coredump_server;struct stat st;struct pidfd_info info = {};int ipc_sockets[2 ];char c; -const struct sockaddr_un coredump_sk = { - .sun_family =AF_UNIX , - .sun_path ="/tmp/coredump.socket" , - }; -size_t coredump_sk_len = offsetof(struct sockaddr_un , sun_path) + -sizeof ("/tmp/coredump.socket" ); + +ASSERT_TRUE (set_core_pattern ("@/tmp/coredump.socket" )); ret =socketpair (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 , ipc_sockets);ASSERT_EQ (ret,0 ); - file =fopen ("/proc/sys/kernel/core_pattern" ,"w" ); -ASSERT_NE (file,NULL ); - - ret =fprintf (file,"@/tmp/coredump.socket" ); -ASSERT_EQ (ret,strlen ("@/tmp/coredump.socket" )); -ASSERT_EQ (fclose (file),0 ); - pid_coredump_server =fork ();ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) { -int fd_server, fd_coredump, fd_peer_pidfd, fd_core_file; -socklen_t fd_peer_pidfd_len; +int fd_server =-1 , fd_coredump =-1 , fd_peer_pidfd =-1 , fd_core_file =-1 ; +int exit_code =EXIT_FAILURE ;close (ipc_sockets[0 ]); - fd_server =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); + fd_server =create_and_listen_unix_socket ("/tmp/coredump.socket" );if (fd_server <0 ) -_exit (EXIT_FAILURE ); - - ret =bind (fd_server, (const struct sockaddr *)&coredump_sk, coredump_sk_len); -if (ret <0 ) { -fprintf (stderr,"Failed to bind coredump socket\n" ); -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } - - ret =listen (fd_server,1 ); -if (ret <0 ) { -fprintf (stderr,"Failed to listen on coredump socket\n" ); -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } + goto out; -if (write_nointr (ipc_sockets[1 ],"1" ,1 ) <0 ) { -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } +if (write_nointr (ipc_sockets[1 ],"1" ,1 ) <0 ) + goto out;close (ipc_sockets[1 ]); fd_coredump =accept4 (fd_server,NULL ,NULL ,SOCK_CLOEXEC ); -if (fd_coredump <0 ) { -fprintf (stderr,"Failed to accept coredump socket connection\n" ); -close (fd_server); -_exit (EXIT_FAILURE ); - } +if (fd_coredump <0 ) + goto out; - fd_peer_pidfd_len =sizeof (fd_peer_pidfd); - ret =getsockopt (fd_coredump,SOL_SOCKET ,SO_PEERPIDFD , - &fd_peer_pidfd, &fd_peer_pidfd_len); -if (ret <0 ) { -fprintf (stderr,"%m - Failed to retrieve peer pidfd for coredump socket connection\n" ); -close (fd_coredump); -close (fd_server); -_exit (EXIT_FAILURE ); - } + fd_peer_pidfd =get_peer_pidfd (fd_coredump); +if (fd_peer_pidfd <0 ) + goto out; -memset (&info,0 ,sizeof (info)); - info.mask =PIDFD_INFO_EXIT |PIDFD_INFO_COREDUMP ; - ret =ioctl (fd_peer_pidfd,PIDFD_GET_INFO , &info); -if (ret <0 ) { -fprintf (stderr,"Failed to retrieve pidfd info from peer pidfd for coredump socket connection\n" ); -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } +if (!get_pidfd_info (fd_peer_pidfd, &info)) + goto out; -if (!(info.mask &PIDFD_INFO_COREDUMP )) { -fprintf (stderr,"Missing coredump information from coredumping task\n" ); -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } +if (!(info.mask &PIDFD_INFO_COREDUMP )) + goto out; -if (!(info.coredump_mask &PIDFD_COREDUMPED )) { -fprintf (stderr,"Received connection from non-coredumping task\n" ); -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } +if (!(info.coredump_mask &PIDFD_COREDUMPED )) + goto out; fd_core_file =creat ("/tmp/coredump.file" ,0644 ); -if (fd_core_file <0 ) { -fprintf (stderr,"Failed to create coredump file\n" ); -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } +if (fd_core_file <0 ) + goto out;for (;;) {char buffer[4096 ];ssize_t bytes_read, bytes_write; bytes_read =read (fd_coredump, buffer,sizeof (buffer)); -if (bytes_read <0 ) { -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -close (fd_core_file); -_exit (EXIT_FAILURE ); - } +if (bytes_read <0 ) + goto out;if (bytes_read ==0 )break ; bytes_write =write (fd_core_file, buffer, bytes_read); -if (bytes_read != bytes_write) { -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -close (fd_core_file); -_exit (EXIT_FAILURE ); - } +if (bytes_read != bytes_write) + goto out; } -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -close (fd_core_file); -_exit (EXIT_SUCCESS ); + exit_code =EXIT_SUCCESS ; +out: +if (fd_core_file >=0 ) +close (fd_core_file); +if (fd_peer_pidfd >=0 ) +close (fd_peer_pidfd); +if (fd_coredump >=0 ) +close (fd_coredump); +if (fd_server >=0 ) +close (fd_server); +_exit (exit_code); } self->pid_coredump_server = pid_coredump_server;
@@ -333,47 +349,27 @@ TEST_F(coredump, socket)ASSERT_TRUE (WIFSIGNALED (status));ASSERT_TRUE (WCOREDUMP (status)); - info.mask =PIDFD_INFO_EXIT |PIDFD_INFO_COREDUMP ; -ASSERT_EQ (ioctl (pidfd,PIDFD_GET_INFO , &info),0 ); +ASSERT_TRUE (get_pidfd_info (pidfd, &info));ASSERT_GT ((info.mask &PIDFD_INFO_COREDUMP ),0 );ASSERT_GT ((info.coredump_mask &PIDFD_COREDUMPED ),0 ); -waitpid (pid_coredump_server, &status,0 ); - self->pid_coredump_server = -ESRCH ; -ASSERT_TRUE (WIFEXITED (status)); -ASSERT_EQ (WEXITSTATUS (status),0 ); +wait_and_check_coredump_server (pid_coredump_server, _metadata, self);ASSERT_EQ (stat ("/tmp/coredump.file" , &st),0 );ASSERT_GT (st.st_size,0 ); -/* -* We should somehow validate the produced core file. -* For now just allow for visual inspection -*/ system ("file /tmp/coredump.file" ); } TEST_F(coredump, socket_detect_userspace_client) {int pidfd, ret, status; -FILE *file;pid_t pid, pid_coredump_server;struct stat st;struct pidfd_info info = {};int ipc_sockets[2 ];char c; -const struct sockaddr_un coredump_sk = { - .sun_family =AF_UNIX , - .sun_path ="/tmp/coredump.socket" , - }; -size_t coredump_sk_len = offsetof(struct sockaddr_un , sun_path) + -sizeof ("/tmp/coredump.socket" ); - file =fopen ("/proc/sys/kernel/core_pattern" ,"w" ); -ASSERT_NE (file,NULL ); - - ret =fprintf (file,"@/tmp/coredump.socket" ); -ASSERT_EQ (ret,strlen ("@/tmp/coredump.socket" )); -ASSERT_EQ (fclose (file),0 ); +ASSERT_TRUE (set_core_pattern ("@/tmp/coredump.socket" )); ret =socketpair (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 , ipc_sockets);ASSERT_EQ (ret,0 );
@@ -381,87 +377,46 @@ TEST_F(coredump, socket_detect_userspace_client) pid_coredump_server =fork ();ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) { -int fd_server, fd_coredump, fd_peer_pidfd; -socklen_t fd_peer_pidfd_len; +int fd_server =-1 , fd_coredump =-1 , fd_peer_pidfd =-1 ; +int exit_code =EXIT_FAILURE ;close (ipc_sockets[0 ]); - fd_server =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); + fd_server =create_and_listen_unix_socket ("/tmp/coredump.socket" );if (fd_server <0 ) -_exit (EXIT_FAILURE ); + goto out; - ret =bind (fd_server, (const struct sockaddr *)&coredump_sk, coredump_sk_len); -if (ret <0 ) { -fprintf (stderr,"Failed to bind coredump socket\n" ); -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } - - ret =listen (fd_server,1 ); -if (ret <0 ) { -fprintf (stderr,"Failed to listen on coredump socket\n" ); -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } - -if (write_nointr (ipc_sockets[1 ],"1" ,1 ) <0 ) { -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } +if (write_nointr (ipc_sockets[1 ],"1" ,1 ) <0 ) + goto out;close (ipc_sockets[1 ]); fd_coredump =accept4 (fd_server,NULL ,NULL ,SOCK_CLOEXEC ); -if (fd_coredump <0 ) { -fprintf (stderr,"Failed to accept coredump socket connection\n" ); -close (fd_server); -_exit (EXIT_FAILURE ); - } +if (fd_coredump <0 ) + goto out; - fd_peer_pidfd_len =sizeof (fd_peer_pidfd); - ret =getsockopt (fd_coredump,SOL_SOCKET ,SO_PEERPIDFD , - &fd_peer_pidfd, &fd_peer_pidfd_len); -if (ret <0 ) { -fprintf (stderr,"%m - Failed to retrieve peer pidfd for coredump socket connection\n" ); -close (fd_coredump); -close (fd_server); -_exit (EXIT_FAILURE ); - } + fd_peer_pidfd =get_peer_pidfd (fd_coredump); +if (fd_peer_pidfd <0 ) + goto out; -memset (&info,0 ,sizeof (info)); - info.mask =PIDFD_INFO_EXIT |PIDFD_INFO_COREDUMP ; - ret =ioctl (fd_peer_pidfd,PIDFD_GET_INFO , &info); -if (ret <0 ) { -fprintf (stderr,"Failed to retrieve pidfd info from peer pidfd for coredump socket connection\n" ); -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } +if (!get_pidfd_info (fd_peer_pidfd, &info)) + goto out; -if (!(info.mask &PIDFD_INFO_COREDUMP )) { -fprintf (stderr,"Missing coredump information from coredumping task\n" ); -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } +if (!(info.mask &PIDFD_INFO_COREDUMP )) + goto out; -if (info.coredump_mask &PIDFD_COREDUMPED ) { -fprintf (stderr,"Received unexpected connection from coredumping task\n" ); +if (info.coredump_mask &PIDFD_COREDUMPED ) + goto out; + + exit_code =EXIT_SUCCESS ; +out: +if (fd_peer_pidfd >=0 ) +close (fd_peer_pidfd); +if (fd_coredump >=0 )close (fd_coredump); +if (fd_server >=0 )close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_FAILURE ); - } - -close (fd_coredump); -close (fd_server); -close (fd_peer_pidfd); -_exit (EXIT_SUCCESS ); +_exit (exit_code); } self->pid_coredump_server = pid_coredump_server;
@@ -474,12 +429,18 @@ TEST_F(coredump, socket_detect_userspace_client)if (pid ==0 ) {int fd_socket;ssize_t ret; +const struct sockaddr_un coredump_sk = { + .sun_family =AF_UNIX , + .sun_path ="/tmp/coredump.socket" , + }; +size_t coredump_sk_len = + offsetof(struct sockaddr_un , sun_path) + +sizeof ("/tmp/coredump.socket" ); fd_socket =socket (AF_UNIX ,SOCK_STREAM ,0 );if (fd_socket <0 )_exit (EXIT_FAILURE ); - ret =connect (fd_socket, (const struct sockaddr *)&coredump_sk, coredump_sk_len);if (ret <0 )_exit (EXIT_FAILURE );
@@ -495,15 +456,11 @@ TEST_F(coredump, socket_detect_userspace_client)ASSERT_TRUE (WIFEXITED (status));ASSERT_EQ (WEXITSTATUS (status),0 ); - info.mask =PIDFD_INFO_EXIT |PIDFD_INFO_COREDUMP ; -ASSERT_EQ (ioctl (pidfd,PIDFD_GET_INFO , &info),0 ); +ASSERT_TRUE (get_pidfd_info (pidfd, &info));ASSERT_GT ((info.mask &PIDFD_INFO_COREDUMP ),0 );ASSERT_EQ ((info.coredump_mask &PIDFD_COREDUMPED ),0 ); -waitpid (pid_coredump_server, &status,0 ); - self->pid_coredump_server = -ESRCH ; -ASSERT_TRUE (WIFEXITED (status)); -ASSERT_EQ (WEXITSTATUS (status),0 ); +wait_and_check_coredump_server (pid_coredump_server, _metadata, self);ASSERT_NE (stat ("/tmp/coredump.file" , &st),0 );ASSERT_EQ (errno,ENOENT );
@@ -511,16 +468,10 @@ TEST_F(coredump, socket_detect_userspace_client) TEST_F(coredump, socket_enoent) { -int pidfd, ret, status; -FILE *file; +int pidfd, status;pid_t pid; - file =fopen ("/proc/sys/kernel/core_pattern" ,"w" ); -ASSERT_NE (file,NULL ); - - ret =fprintf (file,"@/tmp/coredump.socket" ); -ASSERT_EQ (ret,strlen ("@/tmp/coredump.socket" )); -ASSERT_EQ (fclose (file),0 ); +ASSERT_TRUE (set_core_pattern ("@/tmp/coredump.socket" )); pid =fork ();ASSERT_GE (pid,0 );
@@ -538,7 +489,6 @@ TEST_F(coredump, socket_enoent) TEST_F(coredump, socket_no_listener) {int pidfd, ret, status; -FILE *file;pid_t pid, pid_coredump_server;int ipc_sockets[2 ];char c;
@@ -549,44 +499,36 @@ TEST_F(coredump, socket_no_listener)size_t coredump_sk_len = offsetof(struct sockaddr_un , sun_path) +sizeof ("/tmp/coredump.socket" ); +ASSERT_TRUE (set_core_pattern ("@/tmp/coredump.socket" )); + ret =socketpair (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 , ipc_sockets);ASSERT_EQ (ret,0 ); - file =fopen ("/proc/sys/kernel/core_pattern" ,"w" ); -ASSERT_NE (file,NULL ); - - ret =fprintf (file,"@/tmp/coredump.socket" ); -ASSERT_EQ (ret,strlen ("@/tmp/coredump.socket" )); -ASSERT_EQ (fclose (file),0 ); - pid_coredump_server =fork ();ASSERT_GE (pid_coredump_server,0 );if (pid_coredump_server ==0 ) { -int fd_server; +int fd_server =-1 ; +int exit_code =EXIT_FAILURE ;close (ipc_sockets[0 ]); fd_server =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 );if (fd_server <0 ) -_exit (EXIT_FAILURE ); + goto out; ret =bind (fd_server, (const struct sockaddr *)&coredump_sk, coredump_sk_len); -if (ret <0 ) { -fprintf (stderr,"Failed to bind coredump socket\n" ); -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } +if (ret <0 ) + goto out; -if (write_nointr (ipc_sockets[1 ],"1" ,1 ) <0 ) { -close (fd_server); -close (ipc_sockets[1 ]); -_exit (EXIT_FAILURE ); - } +if (write_nointr (ipc_sockets[1 ],"1" ,1 ) <0 ) + goto out; -close (fd_server); + exit_code =EXIT_SUCCESS ; +out: +if (fd_server >=0 ) +close (fd_server);close (ipc_sockets[1 ]); -_exit (EXIT_SUCCESS ); +_exit (exit_code); } self->pid_coredump_server = pid_coredump_server;
@@ -606,10 +548,7 @@ TEST_F(coredump, socket_no_listener)ASSERT_TRUE (WIFSIGNALED (status));ASSERT_FALSE (WCOREDUMP (status)); -waitpid (pid_coredump_server, &status,0 ); - self->pid_coredump_server = -ESRCH ; -ASSERT_TRUE (WIFEXITED (status)); -ASSERT_EQ (WEXITSTATUS (status),0 ); +wait_and_check_coredump_server (pid_coredump_server, _metadata, self); }TEST_HARNESS_MAIN
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH 4/5 tools: add coredump.h header
In reply to Christian BraunerCopy the coredump header so we can rely on it in the selftests.
--- tools/include/uapi/linux/coredump.h | 104 ++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+)
tools/include/uapi/linux/coredump.h new file +104 -0
@@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ + +#ifndef _UAPI_LINUX_COREDUMP_H +#define _UAPI_LINUX_COREDUMP_H + +#include <linux/types.h> + +/** +* coredump_{req,ack} flags +* @COREDUMP_KERNEL: kernel writes coredump +* @COREDUMP_USERSPACE: userspace writes coredump +* @COREDUMP_REJECT: don't generate coredump +* @COREDUMP_WAIT: wait for coredump server +*/ +enum { +COREDUMP_KERNEL = (1ULL <<0 ), +COREDUMP_USERSPACE = (1ULL <<1 ), +COREDUMP_REJECT = (1ULL <<2 ), +COREDUMP_WAIT = (1ULL <<3 ), +}; + +/** +* struct coredump_req - message kernel sends to userspace +* @size: size of struct coredump_req +* @size_ack: known size of struct coredump_ack on this kernel +* @mask: supported features +* +* When a coredump happens the kernel will connect to the coredump +* socket and send a coredump request to the coredump server. The @size +* member is set to the size of struct coredump_req and provides a hint +* to userspace how much data can be read. Userspace may use MSG_PEEK to +* peek the size of struct coredump_req and then choose to consume it in +* one go. Userspace may also simply read a COREDUMP_ACK_SIZE_VER0 +* request. If the size the kernel sends is larger userspace simply +* discards any remaining data. +* +* The coredump_req->mask member is set to the currently know features. +* Userspace may only set coredump_ack->mask to the bits raised by the +* kernel in coredump_req->mask. +* +* The coredump_req->size_ack member is set by the kernel to the size of +* struct coredump_ack the kernel knows. Userspace may only send up to +* coredump_req->size_ack bytes to the kernel and must set +* coredump_ack->size accordingly. +*/ +struct coredump_req { +__u32 size; +__u32 size_ack; +__u64 mask; +}; + +enum { +COREDUMP_REQ_SIZE_VER0 =16 ,/* size of first published struct */ +}; + +/** +* struct coredump_ack - message userspace sends to kernel +* @size: size of the struct +* @spare: unused +* @mask: features kernel is supposed to use +* +* The @size member must be set to the size of struct coredump_ack. It +* may never exceed what the kernel returned in coredump_req->size_ack +* but it may of course be smaller (>= COREDUMP_ACK_SIZE_VER0 and <= +* coredump_req->size_ack). +* +* The @mask member must be set to the features the coredump server +* wants the kernel to use. Only bits the kernel returned in +* coredump_req->mask may be set. +*/ +struct coredump_ack { +__u32 size; +__u32 spare; +__u64 mask; +}; + +enum { +COREDUMP_ACK_SIZE_VER0 =16 ,/* size of first published struct */ +}; + +/** +* enum coredump_oob - Out-of-band markers for the coredump socket +* +* The kernel will place a single coredump_oob marker on the coredump +* socket. An interested coredump server can listen for POLLPRI and +* figure out why the provided coredump_ack was invalid. +* +* The out-of-band markers allow advanced userspace to infer more details +* about a coredump ack. They are optional and can be ignored. They +* aren't necessary for the coredump server to function correctly. +* +* @COREDUMP_OOB_INVALIDSIZE: the provided coredump_ack size was invalid +* @COREDUMP_OOB_UNSUPPORTED: the provided coredump_ack mask was invalid +* @COREDUMP_OOB_CONFLICTING: the provided coredump_ack mask has conflicting options +* @__COREDUMP_OOB_MAX: the maximum value for coredump_oob +*/ +enum coredump_oob { +COREDUMP_OOB_INVALIDSIZE =1U , +COREDUMP_OOB_UNSUPPORTED =2U , +COREDUMP_OOB_CONFLICTING =3U , + __COREDUMP_OOB_MAX =255U , +}; + +#endif /* _UAPI_LINUX_COREDUMP_H */
Signature · 1 line
2.47.2
Christian Brauner
maintainerPATCH 5/5 selftests/coredump: add coredump server selftests
In reply to Christian BraunerThis adds extensive tests for the coredump server.
--- tools/testing/selftests/coredump/Makefile | 2 +- tools/testing/selftests/coredump/config | 4 + tools/testing/selftests/coredump/stackdump_test.c | 1110 ++++++++++++++++++++- 3 files changed, 1114 insertions(+), 2 deletions(-)
tools/testing/selftests/coredump/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -CFLAGS = -Wall -O0 $(KHDR_INCLUDES) +CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) TEST_GEN_PROGS := stackdump_test TEST_FILES := stackdump
tools/testing/selftests/coredump/config new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_AF_UNIX_OOB=y +CONFIG_COREDUMP=y +CONFIG_NET=y +CONFIG_UNIX=y
tools/testing/selftests/coredump/stackdump_test.c +1109 -1
1,162 lines not on this page · raw message
Signature · 1 line
2.47.2
Quoted 91 lines · Christian Brauner
In addition to the extensive selftests I've already written a (non-production ready) simple Rust coredump server for this in userspace:
https://github.com/brauner/dumdum.git
Extend the coredump socket to allow the coredump server to tell the kernel how to process individual coredumps. This allows for fine-grained coredump management allowing choose how to process individual coredump requests. Userspace can decide to just let the kernel write out the coredump, or generate the coredump itself, or just reject it.
When the crashing task connects to the coredump socket the kernel will send a struct
coredump_reqto the coredump server. The kernel will set the size member of structcoredump_reqallowing the coredump server how much data can be read.The coredump server uses
MSG_PEEKto peek the size of structcoredump_req. If the kernel uses a newer structcoredump_reqthe coredump server just reads the size it knows and discard any remaining bytes in the buffer. If the kernel uses an older structcoredump_reqthe coredump server just reads the size the kernel knows.The returned struct
coredump_reqwill inform the coredump server what features the kernel supports. Thecoredump_req->maskmember is set to the currently know features.The coredump server may only use features whose bits were raised by the kernel in
coredump_req->mask.In response to a
coredump_reqfrom the kernel the coredump server sends a structcoredump_ackto the kernel. The kernel informs the coredump server what version of structcoredump_ackit supports by setting structcoredump_req->size_ackto the size it knows about. The coredump server may only send as many bytes ascoredump_req->size_ackindicates (a smaller size is fine of course). The coredump server must setcoredump_ack->sizeaccordingly.The coredump server sets the features it wants to use in struct
coredump_ack->mask. Only bits returned in structcoredump_req->maskmay be used.In case an invalid struct
coredump_ackis sent to the kernel an out-of-band byte will be sent by the kernel indicating the reason why thecoredump_ackwas rejected.If the coredump server sent a valid struct
coredump_ackthe kernel will place an out-of-band byte indicating that the request was successful. If the kernel is generating the coredump and sending the coredump data on the socket the success out-of-band marker can be used as an indicator when coredump data starts.The out-of-band markers allow advanced userspace to infer failure. They are optional and can be ignored by not listening for POLLPRI events and aren't necessary for the coredump server to function correctly.
In the initial version the following features are supported in coredump_{req,ack}->mask:
*
COREDUMP_KERNEL
The kernel will write the coredump data to the socket after the req-ack sequence has concluded successfully.*
COREDUMP_USERSPACE
The kernel will not write coredump data but will indicate to the parent that a coredump has been generated. This is used when userspace generates its own coredumps.*
COREDUMP_REJECT
The kernel will skip generating a coredump for this task.*
COREDUMP_WAIT
The kernel will prevent the task from exiting until the coredump server has shutdown the socket connection.Signed-off-by Christian Brauner <[email protected]>---
Christian Brauner (5):
coredump: allow for flexible coredump handling selftests/coredump: fix build selftests/coredump: cleanup coredump tests
tools: addcoredump.hheader selftests/coredump: add coredump server selftestsfs/coredump.c | 151 +- include/uapi/linux/coredump.h | 104 ++ tools/include/uapi/linux/coredump.h | 104 ++ tools/testing/selftests/coredump/Makefile | 2 +- tools/testing/selftests/coredump/config | 4 + tools/testing/selftests/coredump/stackdump_test.c | 1524 +++++++++++++++++---- 6 files changed, 1639 insertions(+), 250 deletions(-)
Quoted 3 lines
---
base-commit:3e406741b19890c3d8a2ed126aa7c23b106ca9e1
change-id: 20250520-work-coredump-socket-protocol-6980d1f54c2f
Has anyone discussed adding this support to abrt? I imagine they'd like to plumb this support in.
In any case, this all looks good. You can add this to all 5: