Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/coredump: split out common helpers

into separate files.

Link: https://patch.msgid.link/20251028-work-coredump-signal-v1-12-ca449b7b7aa0@kernel.org
Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>

+399
+59
tools/testing/selftests/coredump/coredump_test.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + #ifndef __COREDUMP_TEST_H 4 + #define __COREDUMP_TEST_H 5 + 6 + #include <stdbool.h> 7 + #include <sys/types.h> 8 + #include <linux/coredump.h> 9 + 10 + #include "../kselftest_harness.h" 11 + #include "../pidfd/pidfd.h" 12 + 13 + #ifndef PAGE_SIZE 14 + #define PAGE_SIZE 4096 15 + #endif 16 + 17 + #define NUM_THREAD_SPAWN 128 18 + 19 + /* Coredump fixture */ 20 + FIXTURE(coredump) 21 + { 22 + char original_core_pattern[256]; 23 + pid_t pid_coredump_server; 24 + int fd_tmpfs_detached; 25 + }; 26 + 27 + /* Shared helper function declarations */ 28 + void *do_nothing(void *arg); 29 + void crashing_child(void); 30 + int create_detached_tmpfs(void); 31 + int create_and_listen_unix_socket(const char *path); 32 + bool set_core_pattern(const char *pattern); 33 + int get_peer_pidfd(int fd); 34 + bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info); 35 + 36 + /* Inline helper that uses harness types */ 37 + static inline void wait_and_check_coredump_server(pid_t pid_coredump_server, 38 + struct __test_metadata *const _metadata, 39 + FIXTURE_DATA(coredump) *self) 40 + { 41 + int status; 42 + waitpid(pid_coredump_server, &status, 0); 43 + self->pid_coredump_server = -ESRCH; 44 + ASSERT_TRUE(WIFEXITED(status)); 45 + ASSERT_EQ(WEXITSTATUS(status), 0); 46 + } 47 + 48 + /* Protocol helper function declarations */ 49 + ssize_t recv_marker(int fd); 50 + bool read_marker(int fd, enum coredump_mark mark); 51 + bool read_coredump_req(int fd, struct coredump_req *req); 52 + bool send_coredump_ack(int fd, const struct coredump_req *req, 53 + __u64 mask, size_t size_ack); 54 + bool check_coredump_req(const struct coredump_req *req, size_t min_size, 55 + __u64 required_mask); 56 + int open_coredump_tmpfile(int fd_tmpfs_detached); 57 + void process_coredump_worker(int fd_coredump, int fd_peer_pidfd, int fd_core_file); 58 + 59 + #endif /* __COREDUMP_TEST_H */
+340
tools/testing/selftests/coredump/coredump_test_helpers.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include <assert.h> 4 + #include <errno.h> 5 + #include <fcntl.h> 6 + #include <limits.h> 7 + #include <linux/coredump.h> 8 + #include <linux/fs.h> 9 + #include <pthread.h> 10 + #include <stdbool.h> 11 + #include <stdio.h> 12 + #include <stdlib.h> 13 + #include <string.h> 14 + #include <sys/epoll.h> 15 + #include <sys/ioctl.h> 16 + #include <sys/socket.h> 17 + #include <sys/types.h> 18 + #include <sys/un.h> 19 + #include <sys/wait.h> 20 + #include <unistd.h> 21 + 22 + #include "../filesystems/wrappers.h" 23 + #include "../pidfd/pidfd.h" 24 + 25 + /* Forward declarations to avoid including harness header */ 26 + struct __test_metadata; 27 + 28 + /* Match the fixture definition from coredump_test.h */ 29 + struct _fixture_coredump_data { 30 + char original_core_pattern[256]; 31 + pid_t pid_coredump_server; 32 + int fd_tmpfs_detached; 33 + }; 34 + 35 + #ifndef PAGE_SIZE 36 + #define PAGE_SIZE 4096 37 + #endif 38 + 39 + #define NUM_THREAD_SPAWN 128 40 + 41 + void *do_nothing(void *arg) 42 + { 43 + (void)arg; 44 + while (1) 45 + pause(); 46 + 47 + return NULL; 48 + } 49 + 50 + void crashing_child(void) 51 + { 52 + pthread_t thread; 53 + int i; 54 + 55 + for (i = 0; i < NUM_THREAD_SPAWN; ++i) 56 + pthread_create(&thread, NULL, do_nothing, NULL); 57 + 58 + /* crash on purpose */ 59 + i = *(int *)NULL; 60 + } 61 + 62 + int create_detached_tmpfs(void) 63 + { 64 + int fd_context, fd_tmpfs; 65 + 66 + fd_context = sys_fsopen("tmpfs", 0); 67 + if (fd_context < 0) 68 + return -1; 69 + 70 + if (sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) 71 + return -1; 72 + 73 + fd_tmpfs = sys_fsmount(fd_context, 0, 0); 74 + close(fd_context); 75 + return fd_tmpfs; 76 + } 77 + 78 + int create_and_listen_unix_socket(const char *path) 79 + { 80 + struct sockaddr_un addr = { 81 + .sun_family = AF_UNIX, 82 + }; 83 + assert(strlen(path) < sizeof(addr.sun_path) - 1); 84 + strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); 85 + size_t addr_len = 86 + offsetof(struct sockaddr_un, sun_path) + strlen(path) + 1; 87 + int fd, ret; 88 + 89 + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); 90 + if (fd < 0) 91 + goto out; 92 + 93 + ret = bind(fd, (const struct sockaddr *)&addr, addr_len); 94 + if (ret < 0) 95 + goto out; 96 + 97 + ret = listen(fd, 128); 98 + if (ret < 0) 99 + goto out; 100 + 101 + return fd; 102 + 103 + out: 104 + if (fd >= 0) 105 + close(fd); 106 + return -1; 107 + } 108 + 109 + bool set_core_pattern(const char *pattern) 110 + { 111 + int fd; 112 + ssize_t ret; 113 + 114 + fd = open("/proc/sys/kernel/core_pattern", O_WRONLY | O_CLOEXEC); 115 + if (fd < 0) 116 + return false; 117 + 118 + ret = write(fd, pattern, strlen(pattern)); 119 + close(fd); 120 + if (ret < 0) 121 + return false; 122 + 123 + fprintf(stderr, "Set core_pattern to '%s' | %zu == %zu\n", pattern, ret, strlen(pattern)); 124 + return ret == strlen(pattern); 125 + } 126 + 127 + int get_peer_pidfd(int fd) 128 + { 129 + int fd_peer_pidfd; 130 + socklen_t fd_peer_pidfd_len = sizeof(fd_peer_pidfd); 131 + int ret = getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &fd_peer_pidfd, 132 + &fd_peer_pidfd_len); 133 + if (ret < 0) { 134 + fprintf(stderr, "%m - Failed to retrieve peer pidfd for coredump socket connection\n"); 135 + return -1; 136 + } 137 + return fd_peer_pidfd; 138 + } 139 + 140 + bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info) 141 + { 142 + memset(info, 0, sizeof(*info)); 143 + info->mask = PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL; 144 + return ioctl(fd_peer_pidfd, PIDFD_GET_INFO, info) == 0; 145 + } 146 + 147 + /* Protocol helper functions */ 148 + 149 + ssize_t recv_marker(int fd) 150 + { 151 + enum coredump_mark mark = COREDUMP_MARK_REQACK; 152 + ssize_t ret; 153 + 154 + ret = recv(fd, &mark, sizeof(mark), MSG_WAITALL); 155 + if (ret != sizeof(mark)) 156 + return -1; 157 + 158 + switch (mark) { 159 + case COREDUMP_MARK_REQACK: 160 + fprintf(stderr, "Received marker: ReqAck\n"); 161 + return COREDUMP_MARK_REQACK; 162 + case COREDUMP_MARK_MINSIZE: 163 + fprintf(stderr, "Received marker: MinSize\n"); 164 + return COREDUMP_MARK_MINSIZE; 165 + case COREDUMP_MARK_MAXSIZE: 166 + fprintf(stderr, "Received marker: MaxSize\n"); 167 + return COREDUMP_MARK_MAXSIZE; 168 + case COREDUMP_MARK_UNSUPPORTED: 169 + fprintf(stderr, "Received marker: Unsupported\n"); 170 + return COREDUMP_MARK_UNSUPPORTED; 171 + case COREDUMP_MARK_CONFLICTING: 172 + fprintf(stderr, "Received marker: Conflicting\n"); 173 + return COREDUMP_MARK_CONFLICTING; 174 + default: 175 + fprintf(stderr, "Received unknown marker: %u\n", mark); 176 + break; 177 + } 178 + return -1; 179 + } 180 + 181 + bool read_marker(int fd, enum coredump_mark mark) 182 + { 183 + ssize_t ret; 184 + 185 + ret = recv_marker(fd); 186 + if (ret < 0) 187 + return false; 188 + return ret == mark; 189 + } 190 + 191 + bool read_coredump_req(int fd, struct coredump_req *req) 192 + { 193 + ssize_t ret; 194 + size_t field_size, user_size, ack_size, kernel_size, remaining_size; 195 + 196 + memset(req, 0, sizeof(*req)); 197 + field_size = sizeof(req->size); 198 + 199 + /* Peek the size of the coredump request. */ 200 + ret = recv(fd, req, field_size, MSG_PEEK | MSG_WAITALL); 201 + if (ret != field_size) 202 + return false; 203 + kernel_size = req->size; 204 + 205 + if (kernel_size < COREDUMP_ACK_SIZE_VER0) 206 + return false; 207 + if (kernel_size >= PAGE_SIZE) 208 + return false; 209 + 210 + /* Use the minimum of user and kernel size to read the full request. */ 211 + user_size = sizeof(struct coredump_req); 212 + ack_size = user_size < kernel_size ? user_size : kernel_size; 213 + ret = recv(fd, req, ack_size, MSG_WAITALL); 214 + if (ret != ack_size) 215 + return false; 216 + 217 + fprintf(stderr, "Read coredump request with size %u and mask 0x%llx\n", 218 + req->size, (unsigned long long)req->mask); 219 + 220 + if (user_size > kernel_size) 221 + remaining_size = user_size - kernel_size; 222 + else 223 + remaining_size = kernel_size - user_size; 224 + 225 + if (PAGE_SIZE <= remaining_size) 226 + return false; 227 + 228 + /* 229 + * Discard any additional data if the kernel's request was larger than 230 + * what we knew about or cared about. 231 + */ 232 + if (remaining_size) { 233 + char buffer[PAGE_SIZE]; 234 + 235 + ret = recv(fd, buffer, sizeof(buffer), MSG_WAITALL); 236 + if (ret != remaining_size) 237 + return false; 238 + fprintf(stderr, "Discarded %zu bytes of data after coredump request\n", remaining_size); 239 + } 240 + 241 + return true; 242 + } 243 + 244 + bool send_coredump_ack(int fd, const struct coredump_req *req, 245 + __u64 mask, size_t size_ack) 246 + { 247 + ssize_t ret; 248 + /* 249 + * Wrap struct coredump_ack in a larger struct so we can 250 + * simulate sending to much data to the kernel. 251 + */ 252 + struct large_ack_for_size_testing { 253 + struct coredump_ack ack; 254 + char buffer[PAGE_SIZE]; 255 + } large_ack = {}; 256 + 257 + if (!size_ack) 258 + size_ack = sizeof(struct coredump_ack) < req->size_ack ? 259 + sizeof(struct coredump_ack) : 260 + req->size_ack; 261 + large_ack.ack.mask = mask; 262 + large_ack.ack.size = size_ack; 263 + ret = send(fd, &large_ack, size_ack, MSG_NOSIGNAL); 264 + if (ret != size_ack) 265 + return false; 266 + 267 + fprintf(stderr, "Sent coredump ack with size %zu and mask 0x%llx\n", 268 + size_ack, (unsigned long long)mask); 269 + return true; 270 + } 271 + 272 + bool check_coredump_req(const struct coredump_req *req, size_t min_size, 273 + __u64 required_mask) 274 + { 275 + if (req->size < min_size) 276 + return false; 277 + if ((req->mask & required_mask) != required_mask) 278 + return false; 279 + if (req->mask & ~required_mask) 280 + return false; 281 + return true; 282 + } 283 + 284 + int open_coredump_tmpfile(int fd_tmpfs_detached) 285 + { 286 + return openat(fd_tmpfs_detached, ".", O_TMPFILE | O_RDWR | O_EXCL, 0600); 287 + } 288 + 289 + void process_coredump_worker(int fd_coredump, int fd_peer_pidfd, int fd_core_file) 290 + { 291 + int epfd = -1; 292 + int exit_code = EXIT_FAILURE; 293 + struct epoll_event ev; 294 + 295 + epfd = epoll_create1(0); 296 + if (epfd < 0) 297 + goto out; 298 + 299 + ev.events = EPOLLIN | EPOLLRDHUP | EPOLLET; 300 + ev.data.fd = fd_coredump; 301 + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd_coredump, &ev) < 0) 302 + goto out; 303 + 304 + for (;;) { 305 + struct epoll_event events[1]; 306 + int n = epoll_wait(epfd, events, 1, -1); 307 + if (n < 0) 308 + break; 309 + 310 + if (events[0].events & (EPOLLIN | EPOLLRDHUP)) { 311 + for (;;) { 312 + char buffer[4096]; 313 + ssize_t bytes_read = read(fd_coredump, buffer, sizeof(buffer)); 314 + if (bytes_read < 0) { 315 + if (errno == EAGAIN || errno == EWOULDBLOCK) 316 + break; 317 + goto out; 318 + } 319 + if (bytes_read == 0) 320 + goto done; 321 + ssize_t bytes_write = write(fd_core_file, buffer, bytes_read); 322 + if (bytes_write != bytes_read) 323 + goto out; 324 + } 325 + } 326 + } 327 + 328 + done: 329 + exit_code = EXIT_SUCCESS; 330 + out: 331 + if (epfd >= 0) 332 + close(epfd); 333 + if (fd_core_file >= 0) 334 + close(fd_core_file); 335 + if (fd_peer_pidfd >= 0) 336 + close(fd_peer_pidfd); 337 + if (fd_coredump >= 0) 338 + close(fd_coredump); 339 + _exit(exit_code); 340 + }