-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathkernel_compat.h
More file actions
110 lines (88 loc) · 2.54 KB
/
Copy pathkernel_compat.h
File metadata and controls
110 lines (88 loc) · 2.54 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
#include <linux/blkdev.h>
#include <linux/security.h>
#include <linux/lsm_hooks.h>
#include <linux/version.h>
#include "objsec.h"
#include "blk.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,11,0)
static __maybe_unused inline int lookup_bdev_compat(char *path, dev_t *out) {
struct block_device *bdev;
if (!path || !out) {
return 1;
}
bdev = lookup_bdev(path);
if (IS_ERR(bdev))
return 1;
*out = bdev->bd_dev;
bdput(bdev);
return 0;
}
#else
static __maybe_unused inline int lookup_bdev_compat(char *path, dev_t *out) {
dev_t dev;
int ret;
if (!path || !out) {
return 1;
}
ret = lookup_bdev(path, &dev);
if (ret) return ret;
*out = dev;
return 0;
}
#endif
// https://github.com/torvalds/linux/commit/22ae8ce8b89241c94ac00c237752c0ffa37ba5ae
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,11,0)
static __maybe_unused bool bbg_is_named_device(dev_t dev, const char *name_prefix)
{
struct block_device *bdev;
bool match = false;
bdev = blkdev_get_by_dev(dev, FMODE_READ, THIS_MODULE);
if (IS_ERR(bdev))
return false;
if (bdev->bd_disk && name_prefix) {
const char *disk_name = bdev->bd_disk->disk_name;
size_t prefix_len = strlen(name_prefix);
if (strncmp(disk_name, name_prefix, prefix_len) == 0) {
match = true;
}
}
blkdev_put(bdev, FMODE_READ);
return match;
}
#else
static __maybe_unused bool bbg_is_named_device(dev_t dev, const char *name_prefix)
{
struct block_device *bdev;
bool match = false;
// https://github.com/torvalds/linux/commit/5f33b5226c9d92359e58e91ad0bf0c1791da36a1
#if LINUX_VERSION_CODE < KERNEL_VERSION(6,15,0)
bdev = blkdev_get_no_open(dev);
#else
bdev = blkdev_get_no_open(dev, true);
#endif
if (IS_ERR(bdev))
return false;
if (bdev->bd_disk && name_prefix) {
const char *disk_name = bdev->bd_disk->disk_name;
size_t prefix_len = strlen(name_prefix);
match = (strncmp(disk_name, name_prefix, prefix_len) == 0);
}
blkdev_put_no_open(bdev);
return match;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,8,0)
const struct lsm_id bbg_lsmid = {
.name = "baseband_guard",
.id = 995,
};
#endif
static __maybe_unused inline void __init security_add_hooks_compat(struct security_hook_list *hooks, int count) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,8,0)
security_add_hooks(hooks, count, &bbg_lsmid);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)
security_add_hooks(hooks, count, "baseband_guard");
#else
security_add_hooks(hooks, count);
#endif
}