-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathbaseband_guard.c
More file actions
459 lines (380 loc) · 11.4 KB
/
baseband_guard.c
File metadata and controls
459 lines (380 loc) · 11.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <linux/module.h>
#include <linux/init.h>
#include <linux/security.h>
#include <linux/fs.h>
#include <linux/binfmts.h>
#include <linux/namei.h>
#include <linux/blk_types.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/version.h>
#include <linux/cred.h>
#include <linux/dcache.h>
#include <linux/hashtable.h>
#include "kernel_compat.h"
#include "baseband_guard.h"
#include "tracing/tracing.h"
extern char *saved_command_line;
static const char *slot_suffix_from_cmdline(void)
{
const char *p = saved_command_line;
if (!p) return NULL;
p = strstr(p, "androidboot.slot_suffix=");
if (!p) return NULL;
p += strlen("androidboot.slot_suffix=");
if (p[0] == '_' && (p[1] == 'a' || p[1] == 'b')) return (p[1] == 'a') ? "_a" : "_b";
return NULL;
}
static bool inline resolve_byname_dev(const char *name, dev_t *out)
{
char *path;
dev_t dev;
int ret;
if (!name || !out) return false;
path = kasprintf(GFP_KERNEL, "%s/%s", BB_BYNAME_DIR, name);
if (!path) return false;
ret = lookup_bdev_compat(path, &dev);
kfree(path);
if (ret) return false;
*out = dev;
return true;
}
struct device_hash_node { dev_t dev; struct hlist_node h; };
DEFINE_HASHTABLE(allowed_devs, 7);
DEFINE_HASHTABLE(blocked_devs, 7);
static bool allow_has(dev_t dev)
{
struct device_hash_node *p;
hash_for_each_possible(blocked_devs, p, h, (u64)dev) // process blocklist
if (p->dev == dev) return false;
hash_for_each_possible(allowed_devs, p, h, (u64)dev)
if (p->dev == dev) return true;
return false;
}
static void allow_add(dev_t dev)
{
struct device_hash_node *n;
if (!dev || allow_has(dev)) return;
n = kmalloc(sizeof(*n), GFP_ATOMIC);
if (!n) return;
n->dev = dev;
hash_add(allowed_devs, &n->h, (u64)dev);
bb_pr("allow-cache dev %u:%u\n", MAJOR(dev), MINOR(dev));
}
static void block_add(dev_t dev)
{
struct device_hash_node *n;
if (!dev || allow_has(dev)) return;
n = kmalloc(sizeof(*n), GFP_ATOMIC);
if (!n) return;
n->dev = dev;
hash_add(blocked_devs, &n->h, (u64)dev);
bb_pr("block-cache dev %u:%u\n", MAJOR(dev), MINOR(dev));
}
static inline bool is_allowed_partition_dev_resolve(dev_t cur)
{
size_t i;
dev_t dev;
const char *suf = slot_suffix_from_cmdline();
for (i = 0; i < allowlist_cnt; i++) {
const char *n = allowlist_names[i];
bool ok = false;
char *nm, *na, *nb;
if (resolve_byname_dev(n, &dev) && dev == cur) return true;
if (suf) {
nm = kasprintf(GFP_ATOMIC, "%s%s", n, suf);
if (nm) {
ok = resolve_byname_dev(nm, &dev);
kfree(nm);
if (ok && dev == cur) return true;
}
}
if (!ok) {
na = kasprintf(GFP_ATOMIC, "%s_a", n);
if (na) {
ok = resolve_byname_dev(na, &dev);
kfree(na);
if (ok && dev == cur) return true;
}
nb = kasprintf(GFP_ATOMIC, "%s_b", n);
if (nb) {
ok = resolve_byname_dev(nb, &dev);
kfree(nb);
if (ok && dev == cur) return true;
}
}
}
return false;
}
static bool is_zram_device(dev_t dev)
{
bool is_zram = bbg_is_named_device(dev, "zram");
if (is_zram) {
bb_pr("zram dev %u:%u identified, whitelisting\n",
MAJOR(dev), MINOR(dev));
}
return is_zram;
}
static bool reverse_allow_match_and_cache(dev_t cur)
{
if (!cur) return false;
if (is_zram_device(cur)) {
allow_add(cur);
return true;
}
if (is_allowed_partition_dev_resolve(cur)) {
allow_add(cur);
return true;
}
return false;
}
static const char *bbg_file_path(struct file *file, char *buf, int buflen)
{
char *p;
if (!file || !buf || buflen <= 0) return NULL;
buf[0] = '\0';
p = d_path(&file->f_path, buf, buflen);
return IS_ERR(p) ? NULL : p;
}
static int bbg_get_cmdline(char *buf, int buflen)
{
int n, i;
if (!buf || buflen <= 0) return 0;
n = get_cmdline(current, buf, buflen);
if (n <= 0) return 0;
for (i = 0; i < n - 1; i++) if (buf[i] == '\0') buf[i] = ' ';
if (n < buflen) buf[n] = '\0';
else buf[buflen - 1] = '\0';
return n;
}
static void bbg_log_deny_detail(const char *why, struct file *file, struct inode *inode, unsigned int cmd_opt)
{
const int PATH_BUFLEN = 256;
const int CMD_BUFLEN = 256;
char *pathbuf = kmalloc(PATH_BUFLEN, GFP_ATOMIC);
char *cmdbuf = kmalloc(CMD_BUFLEN, GFP_ATOMIC);
const char *path = pathbuf ? bbg_file_path(file, pathbuf, PATH_BUFLEN) : NULL;
dev_t dev = inode ? inode->i_rdev : 0;
if (cmdbuf)
bbg_get_cmdline(cmdbuf, CMD_BUFLEN);
if (cmd_opt) {
pr_info(
"baseband_guard: deny %s cmd=0x%x dev=%u:%u path=%s pid=%d comm=%s argv=\"%s\"\n",
why, cmd_opt, MAJOR(dev), MINOR(dev),
path ? path : "?", current->pid, current->comm,
cmdbuf ? cmdbuf : "?");
} else {
pr_info(
"baseband_guard: deny %s dev=%u:%u path=%s pid=%d comm=%s argv=\"%s\"\n",
why, MAJOR(dev), MINOR(dev),
path ? path : "?", current->pid, current->comm,
cmdbuf ? cmdbuf : "?");
}
kfree(cmdbuf);
kfree(pathbuf);
}
static int deny(const char *why, struct file *file, struct inode *inode, unsigned int cmd_opt)
{
bbg_log_deny_detail(why, file, inode, cmd_opt);
bb_pr_rl("deny %s pid=%d comm=%s\n", why, current->pid, current->comm);
if (!BB_ENFORCING) return 0;
return -EPERM;
}
static int bb_file_permission(struct file *file, int mask)
{
struct inode *inode;
if (!(mask & MAY_WRITE)) return 0;
if (!file) return 0;
inode = file_inode(file);
if (likely(!S_ISBLK(inode->i_mode))) return 0;
if (likely(current_process_trusted()))
return 0;
if (allow_has(inode->i_rdev) || reverse_allow_match_and_cache(inode->i_rdev))
return 0;
return deny("write to protected partition", file, inode, 0);
}
static inline int is_protected_blkdev(struct dentry *dentry)
{
struct inode *inode;
if (IS_ERR_OR_NULL(dentry))
return 0;
inode = d_backing_inode(dentry);
if (!inode)
return 0;
if (unlikely(S_ISBLK(inode->i_mode))) { // just add blkdevs into blocklist for now, to avoid rename to zramxxx
if (allow_has(inode->i_rdev) || reverse_allow_match_and_cache(inode->i_rdev))
return 0;
// mean we are processing protect devices, add them to blocklist!!!
block_add(inode->i_rdev);
return 0;
}
// there will handle all symlink, to avoid create an symlink -> /dev/block/by-name and modify
if (unlikely(S_ISLNK(inode->i_mode) && inode->i_op->get_link)) { // fix /dev/block/by-name/xxx rename bypass
DEFINE_DELAYED_CALL(done);
const char* symlink_target_link = vfs_get_link(dentry, &done);
int result = 0;
struct path target_path;
if (IS_ERR_OR_NULL(symlink_target_link)) {
result = 0;
goto out;
}
if (symlink_target_link[0] != '/') {
// because /dev/block/by-name's symlink's target always is absolute path, so we don't care relative path
result = 0;
goto out;
}
if (kern_path(symlink_target_link, LOOKUP_FOLLOW, &target_path) == 0) {
struct inode *target_inode = d_backing_inode(target_path.dentry);
if (target_inode && S_ISBLK(target_inode->i_mode)) {
result = 1;
}
path_put(&target_path);
}
out:
do_delayed_call(&done);
clear_delayed_call(&done);
return result;
}
return 0;
}
static dev_t byname_dev = 0;
static unsigned long byname_ino = 0;
static int is_bb_byname_dir(struct inode *dir)
{
if (unlikely(byname_ino == 0)) {
struct path path;
if (kern_path(BB_BYNAME_DIR, LOOKUP_FOLLOW, &path) == 0) {
struct inode *inode = d_backing_inode(path.dentry);
if (inode) {
byname_dev = inode->i_sb->s_dev;
byname_ino = inode->i_ino;
}
path_put(&path);
} else {
return 0;
}
}
if (dir->i_ino == byname_ino && dir->i_sb->s_dev == byname_dev)
return 1;
return 0;
}
static int bb_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
{
if (likely(current_process_trusted()))
return 0;
if (unlikely(is_bb_byname_dir(dir))) {
return deny("create symlink on BB_BYNAME_DIR", 0, dir, 0);
} // i don't want check allowlist in there, because root process WHY create symlink in /dev/block/by-name?
return 0;
}
static int bb_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
if (IS_ERR_OR_NULL(old_dentry))
return 0;
if (unlikely(is_protected_blkdev(old_dentry)))
return deny("rename on protected block device", 0, d_inode(old_dentry), 0);
if (unlikely(new_dentry && is_protected_blkdev(new_dentry)))
return deny("rename on protected target block device's symlink", 0, d_inode(new_dentry), 0);
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,9,0)
static int bb_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *iattr)
#else
static int bb_inode_setattr(struct dentry *dentry, struct iattr *iattr)
#endif
{
if (current_process_trusted())
return 0;
if (is_protected_blkdev(dentry))
return deny("setattr on protected partition", 0, d_inode(dentry), 0);
return 0;
}
static inline bool is_destructive_ioctl(unsigned int cmd)
{
switch (cmd) {
case BLKDISCARD:
case BLKSECDISCARD:
case BLKZEROOUT:
#ifdef BLKPG
case BLKPG:
#endif
#ifdef BLKTRIM
case BLKTRIM:
#endif
#ifdef BLKRRPART
case BLKRRPART:
#endif
#ifdef BLKSETRO
case BLKSETRO:
#endif
#ifdef BLKSETBADSECTORS
case BLKSETBADSECTORS:
#endif
return true;
default:
return false;
}
}
static int bb_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct inode *inode;
if (!file) return 0;
inode = file_inode(file);
if (likely(!S_ISBLK(inode->i_mode))) return 0;
if (!is_destructive_ioctl(cmd))
return 0;
if (likely(current_process_trusted()))
return 0;
if (allow_has(inode->i_rdev) || reverse_allow_match_and_cache(inode->i_rdev))
return 0;
return deny("destructive ioctl on protected partition", file, inode, cmd);
}
#ifdef BB_HAS_IOCTL_COMPAT
static int bb_file_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
{
return bb_file_ioctl(file, cmd, arg);
}
#endif
extern int bb_bprm_set_creds(struct linux_binprm *bprm);
extern void bb_cred_transfer(struct cred *new, const struct cred *old);
extern int bb_cred_prepare(struct cred *new, const struct cred *old, gfp_t gfp);
static struct security_hook_list bb_hooks[] = {
LSM_HOOK_INIT(file_permission, bb_file_permission),
LSM_HOOK_INIT(file_ioctl, bb_file_ioctl),
LSM_HOOK_INIT(inode_setattr, bb_inode_setattr),
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0)
LSM_HOOK_INIT(bprm_creds_for_exec, bb_bprm_set_creds),
#else
LSM_HOOK_INIT(bprm_set_creds, bb_bprm_set_creds),
#endif
LSM_HOOK_INIT(cred_transfer, bb_cred_transfer),
LSM_HOOK_INIT(cred_prepare, bb_cred_prepare),
#ifdef BB_HAS_IOCTL_COMPAT
LSM_HOOK_INIT(file_ioctl_compat, bb_file_ioctl_compat),
#endif
LSM_HOOK_INIT(inode_rename, bb_inode_rename),
LSM_HOOK_INIT(inode_symlink, bb_inode_symlink),
};
static int __init bbg_init(void)
{
security_add_hooks_compat(bb_hooks, ARRAY_SIZE(bb_hooks)); // init lsm hooks and print version notice
pr_info("baseband_guard power by https://t.me/qdykernel\n");
pr_info("baseband_guard repo: %s", __stringify(BBG_REPO));
pr_info("baseband_guard version: %s", __stringify(BBG_VERSION));
return 0;
}
extern struct lsm_blob_sizes bbg_blob_sizes;
#ifndef BBG_USE_DEFINE_LSM
security_initcall(bbg_init);
#else
DEFINE_LSM(baseband_guard) = {
.name = "baseband_guard",
.init = bbg_init,
.blobs = &bbg_blob_sizes
};
#endif
MODULE_DESCRIPTION("protect All Block & Power by TG@qdykernel");
MODULE_AUTHOR("秋刀鱼 & https://t.me/qdykernel");
MODULE_LICENSE("GPL v2");