xref: /linux/include/linux/coredump.h (revision a6656aefd328d544420ecec121fc898b7f067827)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_COREDUMP_H
3 #define _LINUX_COREDUMP_H
4 
5 #include <linux/types.h>
6 #include <linux/mm.h>
7 #include <linux/fs.h>
8 #include <linux/pid_types.h>
9 #include <linux/sched/coredump.h>
10 #include <uapi/linux/coredump.h>
11 #include <asm/siginfo.h>
12 
13 #ifdef CONFIG_COREDUMP
14 /**
15  * enum coredump_state - what happened while the coredump was written
16  * @COREDUMP_STATE_STARTED: the dumper committed to writing a coredump
17  * @COREDUMP_STATE_TRUNCATED: the dumper stopped before it had written all of it
18  */
19 enum coredump_state {
20 	COREDUMP_STATE_STARTED		= (1U << 0),
21 	COREDUMP_STATE_TRUNCATED	= (1U << 1),
22 };
23 
24 struct core_vma_metadata {
25 	unsigned long start, end;
26 	vm_flags_t flags;
27 	unsigned long dump_size;
28 	unsigned long pgoff;
29 	struct file   *file;
30 };
31 
32 struct coredump_params {
33 	const kernel_siginfo_t *siginfo;
34 	struct file *file;
35 	unsigned long limit;
36 	/* COREDUMP_MEMORY_* types to dump, the task's or the server's. */
37 	u64 memory_types;
38 	/* Snapshot of dumpable at dump start. */
39 	enum task_dumpable dumpable;
40 	int cpu;
41 	/* COREDUMP_* options negotiated with the coredump server. */
42 	u64 mask;
43 	/* COREDUMP_STATE_* raised while the coredump is written. */
44 	enum coredump_state state;
45 	/* Record header scratch, NULL unless the coredump is a record stream. */
46 	struct coredump_record_header *record_hdr;
47 	/* Bytes handed to the file, record headers included. */
48 	loff_t written;
49 	/* Offset in the coredump, record headers excluded. */
50 	loff_t pos;
51 	loff_t to_skip;
52 	int vma_count;
53 	size_t vma_data_size;
54 	struct core_vma_metadata *vma_meta;
55 	/* Dumping thread and its thread-group leader by pid type. */
56 	DECLARE_PIDS(pid, PIDTYPE_TGID);
57 };
58 
59 extern unsigned int core_file_note_size_limit;
60 
61 /*
62  * These are the only things you should do on a core-file: use only these
63  * functions to write out all the necessary info.
64  */
65 void dump_skip_to(struct coredump_params *cprm, unsigned long to);
66 void dump_skip(struct coredump_params *cprm, size_t nr);
67 bool dump_emit(struct coredump_params *cprm, const void *addr, int nr);
68 bool dump_align(struct coredump_params *cprm, int align);
69 bool dump_user_range(struct coredump_params *cprm, unsigned long start,
70 		     unsigned long len);
71 void vfs_coredump(const kernel_siginfo_t *siginfo);
72 
73 /*
74  * Logging for the coredump code, ratelimited.
75  * The TGID and comm fields are added to the message.
76  */
77 
78 #define __COREDUMP_PRINTK(Level, Format, ...) \
79 	do {	\
80 		char comm[TASK_COMM_LEN];	\
81 		/* This will always be NUL terminated. */ \
82 		memcpy(comm, current->comm, sizeof(comm)); \
83 		printk_ratelimited(Level "coredump: %d(%*pE): " Format "\n",	\
84 			task_tgid_vnr(current), (int)strlen(comm), comm, ##__VA_ARGS__);	\
85 	} while (0)	\
86 
87 #define coredump_report(fmt, ...) __COREDUMP_PRINTK(KERN_INFO, fmt, ##__VA_ARGS__)
88 #define coredump_report_failure(fmt, ...) __COREDUMP_PRINTK(KERN_WARNING, fmt, ##__VA_ARGS__)
89 
90 #else
vfs_coredump(const kernel_siginfo_t * siginfo)91 static inline void vfs_coredump(const kernel_siginfo_t *siginfo) {}
92 
93 #define coredump_report(...)
94 #define coredump_report_failure(...)
95 
96 #endif
97 
98 #if defined(CONFIG_COREDUMP) && defined(CONFIG_SYSCTL)
99 extern void validate_coredump_safety(void);
100 #else
validate_coredump_safety(void)101 static inline void validate_coredump_safety(void) {}
102 #endif
103 
104 #endif /* _LINUX_COREDUMP_H */
105