File: shadow-newpid.patch

package info (click to toggle)
kpatch 0.9.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,716 kB
  • sloc: ansic: 9,716; sh: 2,592; makefile: 260; asm: 35
file content (75 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
diff -Nupr src.orig/fs/proc/array.c src/fs/proc/array.c
--- src.orig/fs/proc/array.c	2023-01-12 11:20:07.182710551 -0500
+++ src/fs/proc/array.c	2023-01-12 11:21:10.821087869 -0500
@@ -397,12 +397,19 @@ static inline void task_seccomp(struct s
 	seq_putc(m, '\n');
 }
 
+#include <linux/livepatch.h>
 static inline void task_context_switch_counts(struct seq_file *m,
 						struct task_struct *p)
 {
+	int *newpid;
+
 	seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
 	seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
 	seq_putc(m, '\n');
+
+	newpid = klp_shadow_get(p, 0);
+	if (newpid)
+		seq_printf(m, "newpid:\t%d\n", *newpid);
 }
 
 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
diff -Nupr src.orig/kernel/exit.c src/kernel/exit.c
--- src.orig/kernel/exit.c	2023-01-12 11:20:05.408700033 -0500
+++ src/kernel/exit.c	2023-01-12 11:21:10.822087875 -0500
@@ -802,6 +802,7 @@ static void synchronize_group_exit(struc
 	spin_unlock_irq(&sighand->siglock);
 }
 
+#include <linux/livepatch.h>
 void __noreturn do_exit(long code)
 {
 	struct task_struct *tsk = current;
@@ -867,6 +868,8 @@ void __noreturn do_exit(long code)
 	exit_task_work(tsk);
 	exit_thread(tsk);
 
+	klp_shadow_free(tsk, 0, NULL);
+
 	/*
 	 * Flush inherited counters to the parent - before the parent
 	 * gets woken up by child-exit notifications.
diff -Nupr src.orig/kernel/fork.c src/kernel/fork.c
--- src.orig/kernel/fork.c	2023-01-12 11:20:05.408700033 -0500
+++ src/kernel/fork.c	2023-01-12 11:21:10.824087886 -0500
@@ -2637,6 +2637,7 @@ struct task_struct *create_io_thread(int
  *
  * args->exit_signal is expected to be checked for sanity by the caller.
  */
+#include <linux/livepatch.h>
 pid_t kernel_clone(struct kernel_clone_args *args)
 {
 	u64 clone_flags = args->flags;
@@ -2645,6 +2646,8 @@ pid_t kernel_clone(struct kernel_clone_a
 	struct task_struct *p;
 	int trace = 0;
 	pid_t nr;
+	int *newpid;
+	static int ctr = 0;
 
 	/*
 	 * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument
@@ -2684,6 +2687,11 @@ pid_t kernel_clone(struct kernel_clone_a
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
+	newpid = klp_shadow_get_or_alloc(p, 0, sizeof(*newpid), GFP_KERNEL,
+					 NULL, NULL);
+	if (newpid)
+		*newpid = ctr++;
+
 	/*
 	 * Do this prior waking up the new thread - the thread pointer
 	 * might get invalid after that point, if the thread exits quickly.