-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpindown.c
More file actions
358 lines (322 loc) · 9.21 KB
/
pindown.c
File metadata and controls
358 lines (322 loc) · 9.21 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
/**
* PinDOWN implementation
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/security.h>
#include <linux/xattr.h>
#include <linux/capability.h>
#include <linux/unistd.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/smp_lock.h>
#include <linux/spinlock.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/ext2_fs.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/kd.h>
#include <linux/stat.h>
#include <asm/uaccess.h>
#include <asm/ioctls.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/quota.h>
#include <linux/parser.h>
#include <linux/nfs_mount.h>
#include <linux/hugetlb.h>
#include <linux/personality.h>
#include <linux/sysctl.h>
#include <linux/audit.h>
#include <linux/string.h>
#define MAX_PATHLEN 128
typedef struct pindown_security_t {
char bprm_pathname[MAX_PATHLEN];
u32 pathlen;
} pindown_security_t;
MODULE_LICENSE("GPL");
#define INITCONTEXTLEN 100
#define XATTR_SAMPLE_SUFFIX "pindown"
#define XATTR_NAME_SAMPLE XATTR_SECURITY_PREFIX XATTR_SAMPLE_SUFFIX
extern struct security_operations *security_ops;
/* Function: get_inode_policy(@inode, @name)
* Description:
* - Utility function for getting the pathname policy from an inode
* - returns pointer to allocated string (needs deallocation)
* Input:
* @inode : the inode
* @name : xattr name to lookup
* Output:
* - returns allocated string of pathname
* - NULL indicates error or not found
* - return value needs to be kfree()'d after this call if not NULL
*/
char *get_inode_policy(struct inode *inode, const char *name)
{
int rc = -1;
char *pathname = NULL;
struct dentry *dentry;
int len = 0;
printk(KERN_INFO "PinDOWN: get_inode_policy.\n");
/* Make sure this inode supports the functions we need */
if (!inode || !inode->i_op || !inode->i_op->getxattr) {
goto out;
}
/* getxattr requires a dentry */
dentry = d_find_alias(inode);
if (!dentry) {
goto out;
}
/* Try default length */
len = INITCONTEXTLEN;
pathname = (char*)kmalloc(sizeof(char)*len, GFP_KERNEL);
if (!pathname) {
dput(dentry);
goto out;
}
rc = inode->i_op->getxattr(dentry, name, pathname, len*sizeof(char));
if (rc == -ERANGE) {
/* Need a larger buffer. Query for the right size */
rc = inode->i_op->getxattr(dentry, name, NULL, 0);
if (rc < 0) { /* could not get size */
dput(dentry);
kfree(pathname);
pathname = NULL;
goto out;
}
/* start over with correct size */
kfree(pathname);
len = rc / sizeof(char);
pathname = (char*)kmalloc(sizeof(char)*len, GFP_KERNEL);
if (!pathname) {
rc = -ENOMEM;
dput(dentry);
goto out;
}
rc = inode->i_op->getxattr(dentry, name, pathname, len*sizeof(char));
}
dput(dentry);
if (rc < 0) {
kfree(pathname);
pathname = NULL;
goto out;
}
out:
return pathname;
}
/* Function: pindown_inode_permission(@inode, @mask, @nd)
* Description:
* - LSM Hook .inode_permission()
* - Performs the main access control check on files
* Input:
* @inode : pointer to the inode (object) of the lookup
* @mask : permission mask of the lookup (not used at all)
* @nd : ?? (not used at all)
* Output:
* - returns 0 for access granted, -EACCES for permission denied
*/
int pindown_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
{
//Initial default allow. Change to default deny once implemented.
int rc = 0;
pindown_security_t *sec = NULL;
char *inode_policy = NULL;
int i;
/* Don't check this if it is a directory */
if ((inode->i_mode & S_IFMT) == S_IFDIR) {
rc = 0;
//printk(KERN_INFO "PinDOWN: pindown_inode_permission: Don't check this if it is a directory.\n");
goto out;
}
/* Get the inode policy */
inode_policy = get_inode_policy(inode, "security.pindown");
if (!inode_policy) {
rc = 0;
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: No policy, allowing access.\n");
goto out;
}
/* Get the process security info */
sec = current->security;
if (!sec || sec->bprm_pathname[0] == '\0') {
// Not allowing a program to access a file if the program has not set the security field
rc = -EACCES;
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: Not allowing a program to access a file if the program has not set the security field.\n");
goto out;
}
/* Compare process security info to inode policy */
rc = 0;
i=0;
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: inode_policy:%s.\n", inode_policy);
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: sec->bprm_pathname:%s.\n", sec->bprm_pathname);
while ((i < MAX_PATHLEN) && (inode_policy[i] != '\0') && (sec->bprm_pathname[i] != '\0')) {
if (inode_policy[i] != sec->bprm_pathname[i]) {
rc = -EACCES;
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: pathname and filename mismatch.\n");
goto out;
}
i++;
}
if ((i <= MAX_PATHLEN) && (inode_policy[i] == sec->bprm_pathname[i]) && (inode_policy[i] == '\0')) {
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: allowed.\n");
rc = 0;
}
else {
// printk(KERN_INFO "PinDOWN: pindown_inode_permission: denied.\n");
rc = -EACCES;
}
out:
if (inode_policy) {
kfree(inode_policy);
}
return rc;
}
/* Function: pindown_task_alloc_security(@p)
* Description:
* - LSM Hook .task_alloc_security()
* - Allocates @p->security to store the path
* Input:
* @p : pointer to the child task_struct
* Output:
* - @p->security is allocated
* - returns 0 if successful
*/
int pindown_task_alloc_security(struct task_struct * p)
{
int err = 0;
int i;
pindown_security_t *sec = NULL;
pindown_security_t *parent_sec = NULL; // Parent
printk(KERN_INFO "PinDOWN: pindown_task_alloc_security.\n");
sec = (pindown_security_t *)kmalloc(sizeof(pindown_security_t), GFP_KERNEL);
if (sec == NULL) {
err = -ENOMEM;
goto out;
}
/* When we fork, we are still the same application as our
* parent, therefore, it is appropriate to copy the
* parent's digest. On exec(), the digest will be set to
* the new application binary with pindown_bprm_set_security()
*/
parent_sec = current->security;
if (!parent_sec) {
sec->bprm_pathname[0] = '\0';
sec->pathlen = 0;
}
else {
//printk(KERN_INFO "PinDOWN: PARENT %s.\n", parent_sec->bprm_pathname);
i = 0;
while ((i < MAX_PATHLEN) && (parent_sec->bprm_pathname[i] != '\0')) {
sec->bprm_pathname[i] = parent_sec->bprm_pathname[i];
i++;
}
if (i <= MAX_PATHLEN) {
sec->pathlen = i;
}
else {
err = -ENOMEM;
// printk(KERN_INFO "PinDOWN: PATHLEN is too big to store\n");
goto out;
}
}
p->security = sec;
out:
return err;
}
/* Function: pindown_task_free_security(@p)
* Description:
* - LSM Hook .task_free_security()
* - Deallocates @p->security
* Input:
* @p : pointer to the child task_struct
* Output:
* - @p->security is deallocated
*/
void pindown_task_free_security(struct task_struct * p)
{
pindown_security_t *sec;
printk(KERN_INFO "PinDOWN: pindown_task_free_security.\n");
if (!p->security) {
return;
}
sec = p->security;
kfree(sec);
p->security = NULL;
return;
}
/* Function: pindown_bprm_set_security(@bprm)
* Description:
* - LSM Hook .bprm_set_security()
* - Sets @current->security to the path of the binary
* Input:
* @bprm : pointer to a binary being loaded by the kernel
* Output:
* - @current->security is set to the path of the binary
* - return 0 if the hook is successful and permission is granted
*/
int pindown_bprm_set_security(struct linux_binprm * bprm)
{
int rc = 0;
int i;
pindown_security_t *sec = current->security;
printk(KERN_INFO "PinDOWN: pindown_bprm_set_security.\n");
if (sec == NULL) {
rc = pindown_task_alloc_security(current);
}
/* Set the pathname from the exec()'d binary filename */
if (!rc) {
if (!bprm || !bprm->filename) {
sec->bprm_pathname[0] = '\0';
sec->pathlen = 0;
}
else {
// printk(KERN_INFO "PinDOWN: BINARY %s.\n", bprm->filename);
i = 0;
while ((i < MAX_PATHLEN) && (bprm->filename[i] != '\0')) {
sec->bprm_pathname[i] = bprm->filename[i];
i++;
}
while (i < MAX_PATHLEN) {
sec->bprm_pathname[i] = '\0';
i++;
}
sec->pathlen = i;
// printk(KERN_INFO "PinDOWN: SAVED %d %s.\n",sec->pathlen ,sec->bprm_pathname);
}
}
else {
//printk(KERN_INFO "PinDOWN: Should I do something if error was returned by pindown_task_alloc_security() ??");
}
return rc;
}
static struct security_operations pindown_ops = {
.task_alloc_security = pindown_task_alloc_security,
.task_free_security = pindown_task_free_security,
.bprm_set_security = pindown_bprm_set_security,
.inode_permission = pindown_inode_permission,
};
static __init int pindown_init(void)
{
if (register_security (&pindown_ops)) {
printk("PinDOWN: Unable to register with kernel.\n");
return 0;
}
printk(KERN_INFO "PinDOWN: Initializing.\n");
return 0;
}
static __exit void pindown_exit(void)
{
printk(KERN_INFO "PinDOWN: Exiting.\n");
unregister_security(&pindown_ops);
}
module_init(pindown_init);
module_exit(pindown_exit);