Skip to contentSkip to conversation

coredump testing suite

PATCH Superseded Andrea Cervesato 4 participants 7 messages Sep 8 – Sep 9 Library code

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover coredump testing suite

Add a simple coredump01 test that simulate a coredump and verify that it actually works. This is a small coverage that is missing inside LTP testing suite.

Add a coredump02 test that verifies ELF data after a coredump.

---
Changes in v8:
- check PT_LOAD data
- Link to v7: https://lore.kernel.org/[email protected]

Changes in v7:
- define a common header for the testing suite
- update commit message for coredump02
- Link to v6: https://lore.kernel.org/[email protected]

Changes in v6:
- coredump01 check Elf
- add coredump02 test for Elf validation
- Link to v5: https://lore.kernel.org/[email protected]

Changes in v5:
- resolve path from root for helper
- Link to v4: https://lore.kernel.org/[email protected]

Changes in v4:
- read back core_pattern after writing and abort with TBROK on mismatch
- read CONFIG_STATIC_USERMODEHELPER once in setup() and cache the result
- remove namespace visibility scaffolding and skip logic from verify_pipe_pattern()
- resolve helper path with tst_get_path() instead of /proc/self/exe readlink
- Link to v3: https://lore.kernel.org/[email protected]

Changes in v3:
- check for CONFIG_COREDUMP=y
- Link to v2: https://lore.kernel.org/[email protected]

Changes in v2:
- fix CI agent spotted errors
- Link to v1: https://lore.kernel.org/[email protected]

To: Linux Test Project <[email protected]>

---
Andrea Cervesato (2):
coredump01: New core_pattern specifiers test
coredump02: Verify ELF structure and notes

 runtest/kernel_misc                           |   2 +
 testcases/kernel/Makefile                     |   1 +
 testcases/kernel/coredump/.gitignore          |   3 +
 testcases/kernel/coredump/Makefile            |   7 +
 testcases/kernel/coredump/coredump01.c        | 158 ++++++++++++++++++++
 testcases/kernel/coredump/coredump01_helper.c |  73 ++++++++++
 testcases/kernel/coredump/coredump02.c        | 199 ++++++++++++++++++++++++++
 testcases/kernel/coredump/coredump_common.h   |  69 +++++++++
 8 files changed, 512 insertions(+)

---
base-commit: 043bdc49af410f55ece6f61abcdaba7d9580f86a
change-id: 20260820-coredump-22fc731bde08

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v8 1/2 coredump01: New core_pattern specifiers test

From: Andrea Cervesato <[email protected]>

LTP has no coverage for the core_pattern specifiers expansion, neither for the pipe flavor of core_pattern, where the kernel spawns a user space helper and writes the core dump into its standard input.

The new test verifies that %e, %p and %s are expanded both in the core file name and in the arguments of a piped helper, checking also that the helper really receives an ELF core dump.

Root is required to rewrite the system wide core_pattern, which is restored by the test library on all exit paths.

---
 runtest/kernel_misc                           |   1 +
 testcases/kernel/Makefile                     |   1 +
 testcases/kernel/coredump/.gitignore          |   2 +
 testcases/kernel/coredump/Makefile            |   7 ++
 testcases/kernel/coredump/coredump01.c        | 158 ++++++++++++++++++++++++++
 testcases/kernel/coredump/coredump01_helper.c |  73 ++++++++++++
 testcases/kernel/coredump/coredump_common.h   |  69 +++++++++++
 7 files changed, 311 insertions(+)
runtest/kernel_misc +1 -0
index cc3562cb7..ecf9ee2a2 100644
@@ -17,3 +17,4 @@ zram02 zram02.sh
 zram03 zram03
 umip_basic_test umip_basic_test
 aslr01 aslr01
+coredump01 coredump01
testcases/kernel/Makefile +1 -0
index ac816e4e8..6b303c14b 100644
@@ -19,6 +19,7 @@ SUBDIRS			+= lib
 SUBDIRS			+= connectors \
 			   containers \
 			   controllers \
+			   coredump \
 			   crypto \
 			   device-drivers \
 			   firmware \
testcases/kernel/coredump/.gitignore new file +2 -0
new file mode 100644
index 000000000..cd0b51700
@@ -0,0 +1,2 @@
+/coredump01
+/coredump01_helper
testcases/kernel/coredump/Makefile new file +7 -0
new file mode 100644
index 000000000..951a3e976
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2026 Linux Test Project
+
+top_srcdir		?= ../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/coredump/coredump01.c new file +158 -0

158 lines not on this page · raw message

testcases/kernel/coredump/coredump01_helper.c new file +73 -0

73 lines not on this page · raw message

testcases/kernel/coredump/coredump_common.h new file +69 -0

69 lines not on this page · raw message

Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] coredump01: New core_pattern specifiers test

Hi Andrea,

Quoted 1 line · Andrea Cervesato

coredump01: New core_pattern specifiers test

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v8 1/2
#3 lore raw

Re: [LTP] [PATCH v8 1/2] coredump01: New core_pattern specifiers test

In reply to Andrea Cervesato

Hi!

Quoted 5 lines
(quoted diff) +5 -0
+static void set_pattern(const char *fmt, ...)
+{
+	char pattern[PATTERN_MAX];
+	char readback[PATTERN_MAX];
+	va_list va;

Review

Quoted 10 lines
(quoted diff) +10 -0
+	int len;
+
+	va_start(va, fmt);
+	len = vsnprintf(pattern, sizeof(pattern), fmt, va);
+	va_end(va);
+
+	if (len >= PATTERN_MAX)
+		tst_brk(TCONF, "core_pattern does not fit into %i bytes", PATTERN_MAX - 1);
+
+	SAFE_FILE_PRINTF(PATH_KERN_CORE_PATTERN, "%s", pattern);

We actually have safe_file_vprintf() it's not just exposed, we can easily expose the API with:

include/tst_safe_file_ops.h +4 -0
index 73ebd2ab8..f3b322072 100644
@@ -65,6 +65,10 @@ void safe_file_read_str(const char *file, const int lineno,
        safe_file_printf(__FILE__, __LINE__, NULL, \
                         (path), (fmt), ## __VA_ARGS__)
 
+#define SAFE_FILE_VPRINTF(path, fmt, va) \
+       safe_file_vprintf(__FILE__, __LINE__, NULL, \
+                        (path), (fmt), va)
+
 /* Same as SAFE_FILE_PRINTF() but returns quietly if the path doesn't exist */
 #define SAFE_TRY_FILE_PRINTF(path, fmt, ...) \
        safe_try_file_printf(__FILE__, __LINE__, NULL, \
lib/safe_file_ops.c +1 -1
index 19da1fd97..1069a017f 100644
@@ -280,7 +280,7 @@ err:
        return 1;
 }
 
-static void safe_file_vprintf(const char *file, const int lineno,
+void safe_file_vprintf(const char *file, const int lineno,
        void (*cleanup_fn)(void), const char *path, const char *fmt,
        va_list va)
 {

And then we can do SAFE_FILE_VPRINTF() here directly instead of the double printf().

The rest looks fine:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v8 2/2 coredump02: Verify ELF structure and notes

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

LTP currently only tests core_pattern specifier expansion and basic ELF magic in coredump01, leaving the internal ELF structure and notes generated by the kernel unverified.

Add a test to verify that the kernel produces a valid ET_CORE ELF file with the expected PT_NOTE and PT_LOAD segments, and that the NT_PRPSINFO and NT_PRSTATUS notes contain the expected process name, PID, and terminating signal.

Root is required to set the system-wide core_pattern into the test's temporary directory.

---
 runtest/kernel_misc                    |   1 +
 testcases/kernel/coredump/.gitignore   |   1 +
 testcases/kernel/coredump/coredump02.c | 199 +++++++++++++++++++++++++++++++++
 3 files changed, 201 insertions(+)
runtest/kernel_misc +1 -0
index ecf9ee2a2..3311e1929 100644
@@ -18,3 +18,4 @@ zram03 zram03
 umip_basic_test umip_basic_test
 aslr01 aslr01
 coredump01 coredump01
+coredump02 coredump02
testcases/kernel/coredump/.gitignore +1 -0
index cd0b51700..e241a112e 100644
@@ -1,2 +1,3 @@
 /coredump01
 /coredump01_helper
+/coredump02
testcases/kernel/coredump/coredump02.c new file +199 -0

199 lines not on this page · raw message

Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v8 2/2] coredump02: Verify ELF structure and notes

Hi!

Quoted 9 lines
testcases/kernel/coredump/coredump02.c new file +9 -0
+static const void *core_at(size_t off, size_t need)
+{
+	if (off > core_len || need > core_len - off)
+		tst_brk(TFAIL, "core file truncated at %zu (need %zu, have %zu)",
+			off, need, core_len);
+
+	return core_buf + off;
+}
+

testcases/kernel/coredump/coredump02.c line 95

Quoted 10 lines
testcases/kernel/coredump/coredump02.c new file +10 -0
+static void handle_note(uint32_t type, const void *desc, size_t descsz, pid_t pid)
+{
+	if (type == NT_PRPSINFO && descsz >= sizeof(struct elf_prpsinfo)) {
+		struct elf_prpsinfo info;
+		char name[sizeof(info.pr_fname) + 1] = {0};
+
+		memcpy(&info, desc, sizeof(info));
+		memcpy(name, info.pr_fname, sizeof(info.pr_fname));
+
+		TST_EXP_EQ_STR(name, "coredump02");

We do have TST_EXP_EQ_STRN() there is no need for the memcpy.

Also as long as the alignment is proper there is no need for the first memcpy() either.

I would have expected that the elf headers are aligned properly and since we loaded the whole file into aligned buffer (malloc() aligns properly), it should be safe to just cast the desc pointer into the strcut elf_prpsinfo *info = desc; and acces info.pr_name directly from the buffer.

testcases/kernel/coredump/coredump02.c line 101

Quoted 5 lines
testcases/kernel/coredump/coredump02.c new file +5 -0
+		prpsinfo_seen = 1;
+	} else if (type == NT_PRSTATUS && descsz >= sizeof(struct elf_prstatus)) {
+		struct elf_prstatus st;
+
+		memcpy(&st, desc, sizeof(st));

Here as well.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH v8 2/2] coredump02: Verify ELF structure and notes

Hi Cyril,

Quoted 5 lines

I would have expected that the elf headers are aligned properly and since we loaded the whole file into aligned buffer (malloc() aligns properly), it should be safe to just cast the desc pointer into the strcut elf_prpsinfo *info = desc; and acces info.pr_name directly from the buffer.

I made an analysis and I found out this is not valid.

It seems like we can't use the cast in x86_64 because it requires a 8 bytes aligned struct.

The kernel writes:

12 bytes: note header
5 bytes: "CORE" and its ending zero
3 bytes: padding
---
20 bytes total

Although malloc() aligns the buffer’s starting address, the descriptor inside it is not 8-byte aligned. For example:

Buffer starts at address:    4096
Notes start at file offset:  624  (64 + 10 × 56)
First descriptor is another: 20 bytes later

Descriptor's memory address: 4740

So memory is not 8 bytes aligned and when we cast to a pointer we might be misaligned when accessing pr_pid.

We still need memcpy(), so we make sure we are accessing the right portion of the memory.

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp