-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.c
More file actions
172 lines (145 loc) · 4.51 KB
/
Copy pathcore.c
File metadata and controls
172 lines (145 loc) · 4.51 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: GPL-2.0
/*
* wrong8007
*
* Copyright (c) 2023, 03C0 (https://03c0.net/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <linux/slab.h>
#include <linux/kmod.h>
#include <linux/atomic.h>
#include <wrong8007.h>
#define REGISTER_TRIGGER(t) &t
#define EXEC_MAX_LEN 4096
MODULE_LICENSE("GPL");
MODULE_AUTHOR("03C0");
MODULE_DESCRIPTION("wrong8007 is an equivalent of a burner phone");
static char *exec;
module_param(exec, charp, 0000);
// Internal storage of module params
static char *exec_buf;
// Deferred work to run userspace helper
static struct work_struct exec_work;
// Execution policy state
static atomic_t exec_armed = ATOMIC_INIT(1);
// Exported trigger list
extern struct wrong8007_trigger keyboard_trigger;
extern struct wrong8007_trigger usb_trigger;
extern struct wrong8007_trigger network_trigger;
/*
* Trigger interface contract:
*
* - Triggers are owned and managed by the core module.
* - init() is called once at module load, in array order, and returns 0
* on success or a negative errno on failure.
* - On partial init failure, exit() is called in reverse order for
* triggers whose init() succeeded.
* - exit() is called once at module unload and must fully undo init().
* - Triggers must not call wrong8007_activate() from init() or exit().
* - Trigger callbacks may call wrong8007_activate() after init().
*
* This contract enforces fail-closed behavior and one-shot execution.
*/
static struct wrong8007_trigger *triggers[] = {
REGISTER_TRIGGER(keyboard_trigger),
REGISTER_TRIGGER(usb_trigger),
REGISTER_TRIGGER(network_trigger),
};
// Minimal environment for shell execution
static char *env[] = {
"HOME=/",
"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
NULL
};
/*
* Deferred work handler to execute usermode command
*/
static void do_exec_work(struct work_struct *w)
{
int ret;
struct subprocess_info *info;
const char *argv[4] = { "/bin/sh", "-c", exec_buf, NULL };
info = call_usermodehelper_setup(argv[0], (char **)argv, env, GFP_KERNEL, NULL, NULL, NULL);
if (!info) {
wb_err("helper setup failed\n");
return;
}
/* Wait for completion to ensure one-shot semantics */
ret = call_usermodehelper_exec(info, UMH_WAIT_PROC);
wb_dbg("exec returned %d\n", ret);
}
/*
* Authorize execution of the configured action by trigger
* backends when their activation condition is satisfied.
*
* Only the first caller while execution is armed will schedule
* the deferred work; all subsequent calls are ignored.
*
*/
void wrong8007_activate(void)
{
if (atomic_cmpxchg(&exec_armed, 1, 0) == 1) {
schedule_work(&exec_work);
}
}
/*
* Module init: duplicate input params, register notifier, init work
*/
static int __init wrong8007_init(void)
{
int i, err;
if (!exec || !*exec) {
wb_err("exec parameter required\n");
return -EINVAL;
}
if (strnlen(exec, EXEC_MAX_LEN + 1) > EXEC_MAX_LEN) {
wb_err("exec parameter too long (max: %d)\n", EXEC_MAX_LEN);
return -EINVAL;
}
exec_buf = kstrdup(exec, GFP_KERNEL);
if (!exec_buf) {
return -ENOMEM;
}
// Explicitly re-arm execution on module load; redundant with static initialization but intentional
atomic_set(&exec_armed, 1);
INIT_WORK(&exec_work, do_exec_work);
for (i = 0; i < ARRAY_SIZE(triggers); i++) {
err = triggers[i]->init();
if (err) {
wb_err("wrong8007: failed to init trigger: %s\n", triggers[i]->name);
goto fail;
}
}
wb_info("loaded\n");
return 0;
fail:
while (--i >= 0)
triggers[i]->exit();
kfree(exec_buf);
return err;
}
/*
* Module exit: cleanup memory, flush workqueue, unregister hook
*/
static void __exit wrong8007_exit(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(triggers); i++)
triggers[i]->exit();
flush_work(&exec_work);
kfree(exec_buf);
wb_info("unloaded\n");
}
module_init( wrong8007_init );
module_exit( wrong8007_exit );