Commit | Line | Data |
---|---|---|
e114e473 CS |
1 | /* |
2 | * Simplified MAC Kernel (smack) security module | |
3 | * | |
4 | * This file contains the smack hook function implementations. | |
5 | * | |
6 | * Author: | |
7 | * Casey Schaufler <casey@schaufler-ca.com> | |
8 | * | |
9 | * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2, | |
13 | * as published by the Free Software Foundation. | |
14 | */ | |
15 | ||
16 | #include <linux/xattr.h> | |
17 | #include <linux/pagemap.h> | |
18 | #include <linux/mount.h> | |
19 | #include <linux/stat.h> | |
20 | #include <linux/ext2_fs.h> | |
21 | #include <linux/kd.h> | |
22 | #include <asm/ioctls.h> | |
23 | #include <linux/tcp.h> | |
24 | #include <linux/udp.h> | |
25 | #include <linux/mutex.h> | |
26 | #include <linux/pipe_fs_i.h> | |
27 | #include <net/netlabel.h> | |
28 | #include <net/cipso_ipv4.h> | |
d20bdda6 | 29 | #include <linux/audit.h> |
e114e473 CS |
30 | |
31 | #include "smack.h" | |
32 | ||
c69e8d9c DH |
33 | #define task_security(task) (task_cred_xxx((task), security)) |
34 | ||
e114e473 CS |
35 | /* |
36 | * I hope these are the hokeyist lines of code in the module. Casey. | |
37 | */ | |
38 | #define DEVPTS_SUPER_MAGIC 0x1cd1 | |
39 | #define SOCKFS_MAGIC 0x534F434B | |
40 | #define TMPFS_MAGIC 0x01021994 | |
41 | ||
42 | /** | |
43 | * smk_fetch - Fetch the smack label from a file. | |
44 | * @ip: a pointer to the inode | |
45 | * @dp: a pointer to the dentry | |
46 | * | |
47 | * Returns a pointer to the master list entry for the Smack label | |
48 | * or NULL if there was no label to fetch. | |
49 | */ | |
50 | static char *smk_fetch(struct inode *ip, struct dentry *dp) | |
51 | { | |
52 | int rc; | |
53 | char in[SMK_LABELLEN]; | |
54 | ||
55 | if (ip->i_op->getxattr == NULL) | |
56 | return NULL; | |
57 | ||
58 | rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN); | |
59 | if (rc < 0) | |
60 | return NULL; | |
61 | ||
62 | return smk_import(in, rc); | |
63 | } | |
64 | ||
65 | /** | |
66 | * new_inode_smack - allocate an inode security blob | |
67 | * @smack: a pointer to the Smack label to use in the blob | |
68 | * | |
69 | * Returns the new blob or NULL if there's no memory available | |
70 | */ | |
71 | struct inode_smack *new_inode_smack(char *smack) | |
72 | { | |
73 | struct inode_smack *isp; | |
74 | ||
75 | isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL); | |
76 | if (isp == NULL) | |
77 | return NULL; | |
78 | ||
79 | isp->smk_inode = smack; | |
80 | isp->smk_flags = 0; | |
81 | mutex_init(&isp->smk_lock); | |
82 | ||
83 | return isp; | |
84 | } | |
85 | ||
86 | /* | |
87 | * LSM hooks. | |
88 | * We he, that is fun! | |
89 | */ | |
90 | ||
91 | /** | |
5cd9c58f | 92 | * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH |
e114e473 CS |
93 | * @ctp: child task pointer |
94 | * | |
95 | * Returns 0 if access is OK, an error code otherwise | |
96 | * | |
97 | * Do the capability checks, and require read and write. | |
98 | */ | |
5cd9c58f | 99 | static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode) |
e114e473 CS |
100 | { |
101 | int rc; | |
102 | ||
5cd9c58f | 103 | rc = cap_ptrace_may_access(ctp, mode); |
e114e473 CS |
104 | if (rc != 0) |
105 | return rc; | |
106 | ||
d84f4f99 | 107 | rc = smk_access(current_security(), task_security(ctp), MAY_READWRITE); |
5cd9c58f | 108 | if (rc != 0 && capable(CAP_MAC_OVERRIDE)) |
e114e473 | 109 | return 0; |
5cd9c58f DH |
110 | return rc; |
111 | } | |
112 | ||
113 | /** | |
114 | * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME | |
115 | * @ptp: parent task pointer | |
116 | * | |
117 | * Returns 0 if access is OK, an error code otherwise | |
118 | * | |
119 | * Do the capability checks, and require read and write. | |
120 | */ | |
121 | static int smack_ptrace_traceme(struct task_struct *ptp) | |
122 | { | |
123 | int rc; | |
124 | ||
125 | rc = cap_ptrace_traceme(ptp); | |
126 | if (rc != 0) | |
127 | return rc; | |
e114e473 | 128 | |
d84f4f99 | 129 | rc = smk_access(task_security(ptp), current_security(), MAY_READWRITE); |
5cd9c58f DH |
130 | if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE)) |
131 | return 0; | |
e114e473 CS |
132 | return rc; |
133 | } | |
134 | ||
135 | /** | |
136 | * smack_syslog - Smack approval on syslog | |
137 | * @type: message type | |
138 | * | |
139 | * Require that the task has the floor label | |
140 | * | |
141 | * Returns 0 on success, error code otherwise. | |
142 | */ | |
143 | static int smack_syslog(int type) | |
144 | { | |
145 | int rc; | |
86a264ab | 146 | char *sp = current_security(); |
e114e473 CS |
147 | |
148 | rc = cap_syslog(type); | |
149 | if (rc != 0) | |
150 | return rc; | |
151 | ||
152 | if (capable(CAP_MAC_OVERRIDE)) | |
153 | return 0; | |
154 | ||
155 | if (sp != smack_known_floor.smk_known) | |
156 | rc = -EACCES; | |
157 | ||
158 | return rc; | |
159 | } | |
160 | ||
161 | ||
162 | /* | |
163 | * Superblock Hooks. | |
164 | */ | |
165 | ||
166 | /** | |
167 | * smack_sb_alloc_security - allocate a superblock blob | |
168 | * @sb: the superblock getting the blob | |
169 | * | |
170 | * Returns 0 on success or -ENOMEM on error. | |
171 | */ | |
172 | static int smack_sb_alloc_security(struct super_block *sb) | |
173 | { | |
174 | struct superblock_smack *sbsp; | |
175 | ||
176 | sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL); | |
177 | ||
178 | if (sbsp == NULL) | |
179 | return -ENOMEM; | |
180 | ||
181 | sbsp->smk_root = smack_known_floor.smk_known; | |
182 | sbsp->smk_default = smack_known_floor.smk_known; | |
183 | sbsp->smk_floor = smack_known_floor.smk_known; | |
184 | sbsp->smk_hat = smack_known_hat.smk_known; | |
185 | sbsp->smk_initialized = 0; | |
186 | spin_lock_init(&sbsp->smk_sblock); | |
187 | ||
188 | sb->s_security = sbsp; | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | /** | |
194 | * smack_sb_free_security - free a superblock blob | |
195 | * @sb: the superblock getting the blob | |
196 | * | |
197 | */ | |
198 | static void smack_sb_free_security(struct super_block *sb) | |
199 | { | |
200 | kfree(sb->s_security); | |
201 | sb->s_security = NULL; | |
202 | } | |
203 | ||
204 | /** | |
205 | * smack_sb_copy_data - copy mount options data for processing | |
206 | * @type: file system type | |
207 | * @orig: where to start | |
208 | * @smackopts | |
209 | * | |
210 | * Returns 0 on success or -ENOMEM on error. | |
211 | * | |
212 | * Copy the Smack specific mount options out of the mount | |
213 | * options list. | |
214 | */ | |
e0007529 | 215 | static int smack_sb_copy_data(char *orig, char *smackopts) |
e114e473 CS |
216 | { |
217 | char *cp, *commap, *otheropts, *dp; | |
218 | ||
e114e473 CS |
219 | otheropts = (char *)get_zeroed_page(GFP_KERNEL); |
220 | if (otheropts == NULL) | |
221 | return -ENOMEM; | |
222 | ||
223 | for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) { | |
224 | if (strstr(cp, SMK_FSDEFAULT) == cp) | |
225 | dp = smackopts; | |
226 | else if (strstr(cp, SMK_FSFLOOR) == cp) | |
227 | dp = smackopts; | |
228 | else if (strstr(cp, SMK_FSHAT) == cp) | |
229 | dp = smackopts; | |
230 | else if (strstr(cp, SMK_FSROOT) == cp) | |
231 | dp = smackopts; | |
232 | else | |
233 | dp = otheropts; | |
234 | ||
235 | commap = strchr(cp, ','); | |
236 | if (commap != NULL) | |
237 | *commap = '\0'; | |
238 | ||
239 | if (*dp != '\0') | |
240 | strcat(dp, ","); | |
241 | strcat(dp, cp); | |
242 | } | |
243 | ||
244 | strcpy(orig, otheropts); | |
245 | free_page((unsigned long)otheropts); | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | /** | |
251 | * smack_sb_kern_mount - Smack specific mount processing | |
252 | * @sb: the file system superblock | |
12204e24 | 253 | * @flags: the mount flags |
e114e473 CS |
254 | * @data: the smack mount options |
255 | * | |
256 | * Returns 0 on success, an error code on failure | |
257 | */ | |
12204e24 | 258 | static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data) |
e114e473 CS |
259 | { |
260 | struct dentry *root = sb->s_root; | |
261 | struct inode *inode = root->d_inode; | |
262 | struct superblock_smack *sp = sb->s_security; | |
263 | struct inode_smack *isp; | |
264 | char *op; | |
265 | char *commap; | |
266 | char *nsp; | |
267 | ||
268 | spin_lock(&sp->smk_sblock); | |
269 | if (sp->smk_initialized != 0) { | |
270 | spin_unlock(&sp->smk_sblock); | |
271 | return 0; | |
272 | } | |
273 | sp->smk_initialized = 1; | |
274 | spin_unlock(&sp->smk_sblock); | |
275 | ||
276 | for (op = data; op != NULL; op = commap) { | |
277 | commap = strchr(op, ','); | |
278 | if (commap != NULL) | |
279 | *commap++ = '\0'; | |
280 | ||
281 | if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) { | |
282 | op += strlen(SMK_FSHAT); | |
283 | nsp = smk_import(op, 0); | |
284 | if (nsp != NULL) | |
285 | sp->smk_hat = nsp; | |
286 | } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) { | |
287 | op += strlen(SMK_FSFLOOR); | |
288 | nsp = smk_import(op, 0); | |
289 | if (nsp != NULL) | |
290 | sp->smk_floor = nsp; | |
291 | } else if (strncmp(op, SMK_FSDEFAULT, | |
292 | strlen(SMK_FSDEFAULT)) == 0) { | |
293 | op += strlen(SMK_FSDEFAULT); | |
294 | nsp = smk_import(op, 0); | |
295 | if (nsp != NULL) | |
296 | sp->smk_default = nsp; | |
297 | } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) { | |
298 | op += strlen(SMK_FSROOT); | |
299 | nsp = smk_import(op, 0); | |
300 | if (nsp != NULL) | |
301 | sp->smk_root = nsp; | |
302 | } | |
303 | } | |
304 | ||
305 | /* | |
306 | * Initialize the root inode. | |
307 | */ | |
308 | isp = inode->i_security; | |
309 | if (isp == NULL) | |
310 | inode->i_security = new_inode_smack(sp->smk_root); | |
311 | else | |
312 | isp->smk_inode = sp->smk_root; | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | /** | |
318 | * smack_sb_statfs - Smack check on statfs | |
319 | * @dentry: identifies the file system in question | |
320 | * | |
321 | * Returns 0 if current can read the floor of the filesystem, | |
322 | * and error code otherwise | |
323 | */ | |
324 | static int smack_sb_statfs(struct dentry *dentry) | |
325 | { | |
326 | struct superblock_smack *sbp = dentry->d_sb->s_security; | |
327 | ||
328 | return smk_curacc(sbp->smk_floor, MAY_READ); | |
329 | } | |
330 | ||
331 | /** | |
332 | * smack_sb_mount - Smack check for mounting | |
333 | * @dev_name: unused | |
334 | * @nd: mount point | |
335 | * @type: unused | |
336 | * @flags: unused | |
337 | * @data: unused | |
338 | * | |
339 | * Returns 0 if current can write the floor of the filesystem | |
340 | * being mounted on, an error code otherwise. | |
341 | */ | |
b5266eb4 | 342 | static int smack_sb_mount(char *dev_name, struct path *path, |
e114e473 CS |
343 | char *type, unsigned long flags, void *data) |
344 | { | |
b5266eb4 | 345 | struct superblock_smack *sbp = path->mnt->mnt_sb->s_security; |
e114e473 CS |
346 | |
347 | return smk_curacc(sbp->smk_floor, MAY_WRITE); | |
348 | } | |
349 | ||
350 | /** | |
351 | * smack_sb_umount - Smack check for unmounting | |
352 | * @mnt: file system to unmount | |
353 | * @flags: unused | |
354 | * | |
355 | * Returns 0 if current can write the floor of the filesystem | |
356 | * being unmounted, an error code otherwise. | |
357 | */ | |
358 | static int smack_sb_umount(struct vfsmount *mnt, int flags) | |
359 | { | |
360 | struct superblock_smack *sbp; | |
361 | ||
362 | sbp = mnt->mnt_sb->s_security; | |
363 | ||
364 | return smk_curacc(sbp->smk_floor, MAY_WRITE); | |
365 | } | |
366 | ||
367 | /* | |
368 | * Inode hooks | |
369 | */ | |
370 | ||
371 | /** | |
372 | * smack_inode_alloc_security - allocate an inode blob | |
373 | * @inode - the inode in need of a blob | |
374 | * | |
375 | * Returns 0 if it gets a blob, -ENOMEM otherwise | |
376 | */ | |
377 | static int smack_inode_alloc_security(struct inode *inode) | |
378 | { | |
86a264ab | 379 | inode->i_security = new_inode_smack(current_security()); |
e114e473 CS |
380 | if (inode->i_security == NULL) |
381 | return -ENOMEM; | |
382 | return 0; | |
383 | } | |
384 | ||
385 | /** | |
386 | * smack_inode_free_security - free an inode blob | |
387 | * @inode - the inode with a blob | |
388 | * | |
389 | * Clears the blob pointer in inode | |
390 | */ | |
391 | static void smack_inode_free_security(struct inode *inode) | |
392 | { | |
393 | kfree(inode->i_security); | |
394 | inode->i_security = NULL; | |
395 | } | |
396 | ||
397 | /** | |
398 | * smack_inode_init_security - copy out the smack from an inode | |
399 | * @inode: the inode | |
400 | * @dir: unused | |
401 | * @name: where to put the attribute name | |
402 | * @value: where to put the attribute value | |
403 | * @len: where to put the length of the attribute | |
404 | * | |
405 | * Returns 0 if it all works out, -ENOMEM if there's no memory | |
406 | */ | |
407 | static int smack_inode_init_security(struct inode *inode, struct inode *dir, | |
408 | char **name, void **value, size_t *len) | |
409 | { | |
410 | char *isp = smk_of_inode(inode); | |
411 | ||
412 | if (name) { | |
413 | *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL); | |
414 | if (*name == NULL) | |
415 | return -ENOMEM; | |
416 | } | |
417 | ||
418 | if (value) { | |
419 | *value = kstrdup(isp, GFP_KERNEL); | |
420 | if (*value == NULL) | |
421 | return -ENOMEM; | |
422 | } | |
423 | ||
424 | if (len) | |
425 | *len = strlen(isp) + 1; | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
430 | /** | |
431 | * smack_inode_link - Smack check on link | |
432 | * @old_dentry: the existing object | |
433 | * @dir: unused | |
434 | * @new_dentry: the new object | |
435 | * | |
436 | * Returns 0 if access is permitted, an error code otherwise | |
437 | */ | |
438 | static int smack_inode_link(struct dentry *old_dentry, struct inode *dir, | |
439 | struct dentry *new_dentry) | |
440 | { | |
441 | int rc; | |
442 | char *isp; | |
443 | ||
444 | isp = smk_of_inode(old_dentry->d_inode); | |
445 | rc = smk_curacc(isp, MAY_WRITE); | |
446 | ||
447 | if (rc == 0 && new_dentry->d_inode != NULL) { | |
448 | isp = smk_of_inode(new_dentry->d_inode); | |
449 | rc = smk_curacc(isp, MAY_WRITE); | |
450 | } | |
451 | ||
452 | return rc; | |
453 | } | |
454 | ||
455 | /** | |
456 | * smack_inode_unlink - Smack check on inode deletion | |
457 | * @dir: containing directory object | |
458 | * @dentry: file to unlink | |
459 | * | |
460 | * Returns 0 if current can write the containing directory | |
461 | * and the object, error code otherwise | |
462 | */ | |
463 | static int smack_inode_unlink(struct inode *dir, struct dentry *dentry) | |
464 | { | |
465 | struct inode *ip = dentry->d_inode; | |
466 | int rc; | |
467 | ||
468 | /* | |
469 | * You need write access to the thing you're unlinking | |
470 | */ | |
471 | rc = smk_curacc(smk_of_inode(ip), MAY_WRITE); | |
472 | if (rc == 0) | |
473 | /* | |
474 | * You also need write access to the containing directory | |
475 | */ | |
476 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE); | |
477 | ||
478 | return rc; | |
479 | } | |
480 | ||
481 | /** | |
482 | * smack_inode_rmdir - Smack check on directory deletion | |
483 | * @dir: containing directory object | |
484 | * @dentry: directory to unlink | |
485 | * | |
486 | * Returns 0 if current can write the containing directory | |
487 | * and the directory, error code otherwise | |
488 | */ | |
489 | static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
490 | { | |
491 | int rc; | |
492 | ||
493 | /* | |
494 | * You need write access to the thing you're removing | |
495 | */ | |
496 | rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); | |
497 | if (rc == 0) | |
498 | /* | |
499 | * You also need write access to the containing directory | |
500 | */ | |
501 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE); | |
502 | ||
503 | return rc; | |
504 | } | |
505 | ||
506 | /** | |
507 | * smack_inode_rename - Smack check on rename | |
508 | * @old_inode: the old directory | |
509 | * @old_dentry: unused | |
510 | * @new_inode: the new directory | |
511 | * @new_dentry: unused | |
512 | * | |
513 | * Read and write access is required on both the old and | |
514 | * new directories. | |
515 | * | |
516 | * Returns 0 if access is permitted, an error code otherwise | |
517 | */ | |
518 | static int smack_inode_rename(struct inode *old_inode, | |
519 | struct dentry *old_dentry, | |
520 | struct inode *new_inode, | |
521 | struct dentry *new_dentry) | |
522 | { | |
523 | int rc; | |
524 | char *isp; | |
525 | ||
526 | isp = smk_of_inode(old_dentry->d_inode); | |
527 | rc = smk_curacc(isp, MAY_READWRITE); | |
528 | ||
529 | if (rc == 0 && new_dentry->d_inode != NULL) { | |
530 | isp = smk_of_inode(new_dentry->d_inode); | |
531 | rc = smk_curacc(isp, MAY_READWRITE); | |
532 | } | |
533 | ||
534 | return rc; | |
535 | } | |
536 | ||
537 | /** | |
538 | * smack_inode_permission - Smack version of permission() | |
539 | * @inode: the inode in question | |
540 | * @mask: the access requested | |
541 | * @nd: unused | |
542 | * | |
543 | * This is the important Smack hook. | |
544 | * | |
545 | * Returns 0 if access is permitted, -EACCES otherwise | |
546 | */ | |
b77b0646 | 547 | static int smack_inode_permission(struct inode *inode, int mask) |
e114e473 CS |
548 | { |
549 | /* | |
550 | * No permission to check. Existence test. Yup, it's there. | |
551 | */ | |
552 | if (mask == 0) | |
553 | return 0; | |
554 | ||
555 | return smk_curacc(smk_of_inode(inode), mask); | |
556 | } | |
557 | ||
558 | /** | |
559 | * smack_inode_setattr - Smack check for setting attributes | |
560 | * @dentry: the object | |
561 | * @iattr: for the force flag | |
562 | * | |
563 | * Returns 0 if access is permitted, an error code otherwise | |
564 | */ | |
565 | static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr) | |
566 | { | |
567 | /* | |
568 | * Need to allow for clearing the setuid bit. | |
569 | */ | |
570 | if (iattr->ia_valid & ATTR_FORCE) | |
571 | return 0; | |
572 | ||
573 | return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); | |
574 | } | |
575 | ||
576 | /** | |
577 | * smack_inode_getattr - Smack check for getting attributes | |
578 | * @mnt: unused | |
579 | * @dentry: the object | |
580 | * | |
581 | * Returns 0 if access is permitted, an error code otherwise | |
582 | */ | |
583 | static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
584 | { | |
585 | return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ); | |
586 | } | |
587 | ||
588 | /** | |
589 | * smack_inode_setxattr - Smack check for setting xattrs | |
590 | * @dentry: the object | |
591 | * @name: name of the attribute | |
592 | * @value: unused | |
593 | * @size: unused | |
594 | * @flags: unused | |
595 | * | |
596 | * This protects the Smack attribute explicitly. | |
597 | * | |
598 | * Returns 0 if access is permitted, an error code otherwise | |
599 | */ | |
8f0cfa52 DH |
600 | static int smack_inode_setxattr(struct dentry *dentry, const char *name, |
601 | const void *value, size_t size, int flags) | |
e114e473 | 602 | { |
bcdca225 | 603 | int rc = 0; |
e114e473 | 604 | |
bcdca225 CS |
605 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
606 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || | |
607 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { | |
608 | if (!capable(CAP_MAC_ADMIN)) | |
609 | rc = -EPERM; | |
610 | } else | |
611 | rc = cap_inode_setxattr(dentry, name, value, size, flags); | |
612 | ||
613 | if (rc == 0) | |
614 | rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); | |
615 | ||
616 | return rc; | |
e114e473 CS |
617 | } |
618 | ||
619 | /** | |
620 | * smack_inode_post_setxattr - Apply the Smack update approved above | |
621 | * @dentry: object | |
622 | * @name: attribute name | |
623 | * @value: attribute value | |
624 | * @size: attribute size | |
625 | * @flags: unused | |
626 | * | |
627 | * Set the pointer in the inode blob to the entry found | |
628 | * in the master label list. | |
629 | */ | |
8f0cfa52 DH |
630 | static void smack_inode_post_setxattr(struct dentry *dentry, const char *name, |
631 | const void *value, size_t size, int flags) | |
e114e473 CS |
632 | { |
633 | struct inode_smack *isp; | |
634 | char *nsp; | |
635 | ||
636 | /* | |
637 | * Not SMACK | |
638 | */ | |
639 | if (strcmp(name, XATTR_NAME_SMACK)) | |
640 | return; | |
641 | ||
642 | if (size >= SMK_LABELLEN) | |
643 | return; | |
644 | ||
645 | isp = dentry->d_inode->i_security; | |
646 | ||
647 | /* | |
648 | * No locking is done here. This is a pointer | |
649 | * assignment. | |
650 | */ | |
651 | nsp = smk_import(value, size); | |
652 | if (nsp != NULL) | |
653 | isp->smk_inode = nsp; | |
654 | else | |
655 | isp->smk_inode = smack_known_invalid.smk_known; | |
656 | ||
657 | return; | |
658 | } | |
659 | ||
660 | /* | |
661 | * smack_inode_getxattr - Smack check on getxattr | |
662 | * @dentry: the object | |
663 | * @name: unused | |
664 | * | |
665 | * Returns 0 if access is permitted, an error code otherwise | |
666 | */ | |
8f0cfa52 | 667 | static int smack_inode_getxattr(struct dentry *dentry, const char *name) |
e114e473 CS |
668 | { |
669 | return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ); | |
670 | } | |
671 | ||
672 | /* | |
673 | * smack_inode_removexattr - Smack check on removexattr | |
674 | * @dentry: the object | |
675 | * @name: name of the attribute | |
676 | * | |
677 | * Removing the Smack attribute requires CAP_MAC_ADMIN | |
678 | * | |
679 | * Returns 0 if access is permitted, an error code otherwise | |
680 | */ | |
8f0cfa52 | 681 | static int smack_inode_removexattr(struct dentry *dentry, const char *name) |
e114e473 | 682 | { |
bcdca225 | 683 | int rc = 0; |
e114e473 | 684 | |
bcdca225 CS |
685 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
686 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || | |
687 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { | |
688 | if (!capable(CAP_MAC_ADMIN)) | |
689 | rc = -EPERM; | |
690 | } else | |
691 | rc = cap_inode_removexattr(dentry, name); | |
692 | ||
693 | if (rc == 0) | |
694 | rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE); | |
695 | ||
696 | return rc; | |
e114e473 CS |
697 | } |
698 | ||
699 | /** | |
700 | * smack_inode_getsecurity - get smack xattrs | |
701 | * @inode: the object | |
702 | * @name: attribute name | |
703 | * @buffer: where to put the result | |
704 | * @size: size of the buffer | |
705 | * @err: unused | |
706 | * | |
707 | * Returns the size of the attribute or an error code | |
708 | */ | |
709 | static int smack_inode_getsecurity(const struct inode *inode, | |
710 | const char *name, void **buffer, | |
711 | bool alloc) | |
712 | { | |
713 | struct socket_smack *ssp; | |
714 | struct socket *sock; | |
715 | struct super_block *sbp; | |
716 | struct inode *ip = (struct inode *)inode; | |
717 | char *isp; | |
718 | int ilen; | |
719 | int rc = 0; | |
720 | ||
721 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { | |
722 | isp = smk_of_inode(inode); | |
723 | ilen = strlen(isp) + 1; | |
724 | *buffer = isp; | |
725 | return ilen; | |
726 | } | |
727 | ||
728 | /* | |
729 | * The rest of the Smack xattrs are only on sockets. | |
730 | */ | |
731 | sbp = ip->i_sb; | |
732 | if (sbp->s_magic != SOCKFS_MAGIC) | |
733 | return -EOPNOTSUPP; | |
734 | ||
735 | sock = SOCKET_I(ip); | |
2e1d146a | 736 | if (sock == NULL || sock->sk == NULL) |
e114e473 CS |
737 | return -EOPNOTSUPP; |
738 | ||
739 | ssp = sock->sk->sk_security; | |
740 | ||
741 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) | |
742 | isp = ssp->smk_in; | |
743 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) | |
744 | isp = ssp->smk_out; | |
745 | else | |
746 | return -EOPNOTSUPP; | |
747 | ||
748 | ilen = strlen(isp) + 1; | |
749 | if (rc == 0) { | |
750 | *buffer = isp; | |
751 | rc = ilen; | |
752 | } | |
753 | ||
754 | return rc; | |
755 | } | |
756 | ||
757 | ||
758 | /** | |
759 | * smack_inode_listsecurity - list the Smack attributes | |
760 | * @inode: the object | |
761 | * @buffer: where they go | |
762 | * @buffer_size: size of buffer | |
763 | * | |
764 | * Returns 0 on success, -EINVAL otherwise | |
765 | */ | |
766 | static int smack_inode_listsecurity(struct inode *inode, char *buffer, | |
767 | size_t buffer_size) | |
768 | { | |
769 | int len = strlen(XATTR_NAME_SMACK); | |
770 | ||
771 | if (buffer != NULL && len <= buffer_size) { | |
772 | memcpy(buffer, XATTR_NAME_SMACK, len); | |
773 | return len; | |
774 | } | |
775 | return -EINVAL; | |
776 | } | |
777 | ||
d20bdda6 AD |
778 | /** |
779 | * smack_inode_getsecid - Extract inode's security id | |
780 | * @inode: inode to extract the info from | |
781 | * @secid: where result will be saved | |
782 | */ | |
783 | static void smack_inode_getsecid(const struct inode *inode, u32 *secid) | |
784 | { | |
785 | struct inode_smack *isp = inode->i_security; | |
786 | ||
787 | *secid = smack_to_secid(isp->smk_inode); | |
788 | } | |
789 | ||
e114e473 CS |
790 | /* |
791 | * File Hooks | |
792 | */ | |
793 | ||
794 | /** | |
795 | * smack_file_permission - Smack check on file operations | |
796 | * @file: unused | |
797 | * @mask: unused | |
798 | * | |
799 | * Returns 0 | |
800 | * | |
801 | * Should access checks be done on each read or write? | |
802 | * UNICOS and SELinux say yes. | |
803 | * Trusted Solaris, Trusted Irix, and just about everyone else says no. | |
804 | * | |
805 | * I'll say no for now. Smack does not do the frequent | |
806 | * label changing that SELinux does. | |
807 | */ | |
808 | static int smack_file_permission(struct file *file, int mask) | |
809 | { | |
810 | return 0; | |
811 | } | |
812 | ||
813 | /** | |
814 | * smack_file_alloc_security - assign a file security blob | |
815 | * @file: the object | |
816 | * | |
817 | * The security blob for a file is a pointer to the master | |
818 | * label list, so no allocation is done. | |
819 | * | |
820 | * Returns 0 | |
821 | */ | |
822 | static int smack_file_alloc_security(struct file *file) | |
823 | { | |
86a264ab | 824 | file->f_security = current_security(); |
e114e473 CS |
825 | return 0; |
826 | } | |
827 | ||
828 | /** | |
829 | * smack_file_free_security - clear a file security blob | |
830 | * @file: the object | |
831 | * | |
832 | * The security blob for a file is a pointer to the master | |
833 | * label list, so no memory is freed. | |
834 | */ | |
835 | static void smack_file_free_security(struct file *file) | |
836 | { | |
837 | file->f_security = NULL; | |
838 | } | |
839 | ||
840 | /** | |
841 | * smack_file_ioctl - Smack check on ioctls | |
842 | * @file: the object | |
843 | * @cmd: what to do | |
844 | * @arg: unused | |
845 | * | |
846 | * Relies heavily on the correct use of the ioctl command conventions. | |
847 | * | |
848 | * Returns 0 if allowed, error code otherwise | |
849 | */ | |
850 | static int smack_file_ioctl(struct file *file, unsigned int cmd, | |
851 | unsigned long arg) | |
852 | { | |
853 | int rc = 0; | |
854 | ||
855 | if (_IOC_DIR(cmd) & _IOC_WRITE) | |
856 | rc = smk_curacc(file->f_security, MAY_WRITE); | |
857 | ||
858 | if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) | |
859 | rc = smk_curacc(file->f_security, MAY_READ); | |
860 | ||
861 | return rc; | |
862 | } | |
863 | ||
864 | /** | |
865 | * smack_file_lock - Smack check on file locking | |
866 | * @file: the object | |
867 | * @cmd unused | |
868 | * | |
869 | * Returns 0 if current has write access, error code otherwise | |
870 | */ | |
871 | static int smack_file_lock(struct file *file, unsigned int cmd) | |
872 | { | |
873 | return smk_curacc(file->f_security, MAY_WRITE); | |
874 | } | |
875 | ||
876 | /** | |
877 | * smack_file_fcntl - Smack check on fcntl | |
878 | * @file: the object | |
879 | * @cmd: what action to check | |
880 | * @arg: unused | |
881 | * | |
882 | * Returns 0 if current has access, error code otherwise | |
883 | */ | |
884 | static int smack_file_fcntl(struct file *file, unsigned int cmd, | |
885 | unsigned long arg) | |
886 | { | |
887 | int rc; | |
888 | ||
889 | switch (cmd) { | |
890 | case F_DUPFD: | |
891 | case F_GETFD: | |
892 | case F_GETFL: | |
893 | case F_GETLK: | |
894 | case F_GETOWN: | |
895 | case F_GETSIG: | |
896 | rc = smk_curacc(file->f_security, MAY_READ); | |
897 | break; | |
898 | case F_SETFD: | |
899 | case F_SETFL: | |
900 | case F_SETLK: | |
901 | case F_SETLKW: | |
902 | case F_SETOWN: | |
903 | case F_SETSIG: | |
904 | rc = smk_curacc(file->f_security, MAY_WRITE); | |
905 | break; | |
906 | default: | |
907 | rc = smk_curacc(file->f_security, MAY_READWRITE); | |
908 | } | |
909 | ||
910 | return rc; | |
911 | } | |
912 | ||
913 | /** | |
914 | * smack_file_set_fowner - set the file security blob value | |
915 | * @file: object in question | |
916 | * | |
917 | * Returns 0 | |
918 | * Further research may be required on this one. | |
919 | */ | |
920 | static int smack_file_set_fowner(struct file *file) | |
921 | { | |
86a264ab | 922 | file->f_security = current_security(); |
e114e473 CS |
923 | return 0; |
924 | } | |
925 | ||
926 | /** | |
927 | * smack_file_send_sigiotask - Smack on sigio | |
928 | * @tsk: The target task | |
929 | * @fown: the object the signal come from | |
930 | * @signum: unused | |
931 | * | |
932 | * Allow a privileged task to get signals even if it shouldn't | |
933 | * | |
934 | * Returns 0 if a subject with the object's smack could | |
935 | * write to the task, an error code otherwise. | |
936 | */ | |
937 | static int smack_file_send_sigiotask(struct task_struct *tsk, | |
938 | struct fown_struct *fown, int signum) | |
939 | { | |
940 | struct file *file; | |
941 | int rc; | |
942 | ||
943 | /* | |
944 | * struct fown_struct is never outside the context of a struct file | |
945 | */ | |
946 | file = container_of(fown, struct file, f_owner); | |
b6dff3ec | 947 | rc = smk_access(file->f_security, tsk->cred->security, MAY_WRITE); |
5cd9c58f | 948 | if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE)) |
e114e473 CS |
949 | return 0; |
950 | return rc; | |
951 | } | |
952 | ||
953 | /** | |
954 | * smack_file_receive - Smack file receive check | |
955 | * @file: the object | |
956 | * | |
957 | * Returns 0 if current has access, error code otherwise | |
958 | */ | |
959 | static int smack_file_receive(struct file *file) | |
960 | { | |
961 | int may = 0; | |
962 | ||
963 | /* | |
964 | * This code relies on bitmasks. | |
965 | */ | |
966 | if (file->f_mode & FMODE_READ) | |
967 | may = MAY_READ; | |
968 | if (file->f_mode & FMODE_WRITE) | |
969 | may |= MAY_WRITE; | |
970 | ||
971 | return smk_curacc(file->f_security, may); | |
972 | } | |
973 | ||
974 | /* | |
975 | * Task hooks | |
976 | */ | |
977 | ||
e114e473 | 978 | /** |
f1752eec DH |
979 | * smack_cred_free - "free" task-level security credentials |
980 | * @cred: the credentials in question | |
e114e473 CS |
981 | * |
982 | * Smack isn't using copies of blobs. Everyone | |
983 | * points to an immutable list. The blobs never go away. | |
984 | * There is no leak here. | |
985 | */ | |
f1752eec | 986 | static void smack_cred_free(struct cred *cred) |
e114e473 | 987 | { |
f1752eec | 988 | cred->security = NULL; |
e114e473 CS |
989 | } |
990 | ||
d84f4f99 DH |
991 | /** |
992 | * smack_cred_prepare - prepare new set of credentials for modification | |
993 | * @new: the new credentials | |
994 | * @old: the original credentials | |
995 | * @gfp: the atomicity of any memory allocations | |
996 | * | |
997 | * Prepare a new set of credentials for modification. | |
998 | */ | |
999 | static int smack_cred_prepare(struct cred *new, const struct cred *old, | |
1000 | gfp_t gfp) | |
1001 | { | |
1002 | new->security = old->security; | |
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * commit new credentials | |
1008 | * @new: the new credentials | |
1009 | * @old: the original credentials | |
1010 | */ | |
1011 | static void smack_cred_commit(struct cred *new, const struct cred *old) | |
1012 | { | |
1013 | } | |
1014 | ||
3a3b7ce9 DH |
1015 | /** |
1016 | * smack_kernel_act_as - Set the subjective context in a set of credentials | |
1017 | * @new points to the set of credentials to be modified. | |
1018 | * @secid specifies the security ID to be set | |
1019 | * | |
1020 | * Set the security data for a kernel service. | |
1021 | */ | |
1022 | static int smack_kernel_act_as(struct cred *new, u32 secid) | |
1023 | { | |
1024 | char *smack = smack_from_secid(secid); | |
1025 | ||
1026 | if (smack == NULL) | |
1027 | return -EINVAL; | |
1028 | ||
1029 | new->security = smack; | |
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | /** | |
1034 | * smack_kernel_create_files_as - Set the file creation label in a set of creds | |
1035 | * @new points to the set of credentials to be modified | |
1036 | * @inode points to the inode to use as a reference | |
1037 | * | |
1038 | * Set the file creation context in a set of credentials to the same | |
1039 | * as the objective context of the specified inode | |
1040 | */ | |
1041 | static int smack_kernel_create_files_as(struct cred *new, | |
1042 | struct inode *inode) | |
1043 | { | |
1044 | struct inode_smack *isp = inode->i_security; | |
1045 | ||
1046 | new->security = isp->smk_inode; | |
1047 | return 0; | |
1048 | } | |
1049 | ||
e114e473 CS |
1050 | /** |
1051 | * smack_task_setpgid - Smack check on setting pgid | |
1052 | * @p: the task object | |
1053 | * @pgid: unused | |
1054 | * | |
1055 | * Return 0 if write access is permitted | |
1056 | */ | |
1057 | static int smack_task_setpgid(struct task_struct *p, pid_t pgid) | |
1058 | { | |
c69e8d9c | 1059 | return smk_curacc(task_security(p), MAY_WRITE); |
e114e473 CS |
1060 | } |
1061 | ||
1062 | /** | |
1063 | * smack_task_getpgid - Smack access check for getpgid | |
1064 | * @p: the object task | |
1065 | * | |
1066 | * Returns 0 if current can read the object task, error code otherwise | |
1067 | */ | |
1068 | static int smack_task_getpgid(struct task_struct *p) | |
1069 | { | |
c69e8d9c | 1070 | return smk_curacc(task_security(p), MAY_READ); |
e114e473 CS |
1071 | } |
1072 | ||
1073 | /** | |
1074 | * smack_task_getsid - Smack access check for getsid | |
1075 | * @p: the object task | |
1076 | * | |
1077 | * Returns 0 if current can read the object task, error code otherwise | |
1078 | */ | |
1079 | static int smack_task_getsid(struct task_struct *p) | |
1080 | { | |
c69e8d9c | 1081 | return smk_curacc(task_security(p), MAY_READ); |
e114e473 CS |
1082 | } |
1083 | ||
1084 | /** | |
1085 | * smack_task_getsecid - get the secid of the task | |
1086 | * @p: the object task | |
1087 | * @secid: where to put the result | |
1088 | * | |
1089 | * Sets the secid to contain a u32 version of the smack label. | |
1090 | */ | |
1091 | static void smack_task_getsecid(struct task_struct *p, u32 *secid) | |
1092 | { | |
c69e8d9c | 1093 | *secid = smack_to_secid(task_security(p)); |
e114e473 CS |
1094 | } |
1095 | ||
1096 | /** | |
1097 | * smack_task_setnice - Smack check on setting nice | |
1098 | * @p: the task object | |
1099 | * @nice: unused | |
1100 | * | |
1101 | * Return 0 if write access is permitted | |
1102 | */ | |
1103 | static int smack_task_setnice(struct task_struct *p, int nice) | |
1104 | { | |
bcdca225 CS |
1105 | int rc; |
1106 | ||
1107 | rc = cap_task_setnice(p, nice); | |
1108 | if (rc == 0) | |
c69e8d9c | 1109 | rc = smk_curacc(task_security(p), MAY_WRITE); |
bcdca225 | 1110 | return rc; |
e114e473 CS |
1111 | } |
1112 | ||
1113 | /** | |
1114 | * smack_task_setioprio - Smack check on setting ioprio | |
1115 | * @p: the task object | |
1116 | * @ioprio: unused | |
1117 | * | |
1118 | * Return 0 if write access is permitted | |
1119 | */ | |
1120 | static int smack_task_setioprio(struct task_struct *p, int ioprio) | |
1121 | { | |
bcdca225 CS |
1122 | int rc; |
1123 | ||
1124 | rc = cap_task_setioprio(p, ioprio); | |
1125 | if (rc == 0) | |
c69e8d9c | 1126 | rc = smk_curacc(task_security(p), MAY_WRITE); |
bcdca225 | 1127 | return rc; |
e114e473 CS |
1128 | } |
1129 | ||
1130 | /** | |
1131 | * smack_task_getioprio - Smack check on reading ioprio | |
1132 | * @p: the task object | |
1133 | * | |
1134 | * Return 0 if read access is permitted | |
1135 | */ | |
1136 | static int smack_task_getioprio(struct task_struct *p) | |
1137 | { | |
c69e8d9c | 1138 | return smk_curacc(task_security(p), MAY_READ); |
e114e473 CS |
1139 | } |
1140 | ||
1141 | /** | |
1142 | * smack_task_setscheduler - Smack check on setting scheduler | |
1143 | * @p: the task object | |
1144 | * @policy: unused | |
1145 | * @lp: unused | |
1146 | * | |
1147 | * Return 0 if read access is permitted | |
1148 | */ | |
1149 | static int smack_task_setscheduler(struct task_struct *p, int policy, | |
1150 | struct sched_param *lp) | |
1151 | { | |
bcdca225 CS |
1152 | int rc; |
1153 | ||
1154 | rc = cap_task_setscheduler(p, policy, lp); | |
1155 | if (rc == 0) | |
c69e8d9c | 1156 | rc = smk_curacc(task_security(p), MAY_WRITE); |
bcdca225 | 1157 | return rc; |
e114e473 CS |
1158 | } |
1159 | ||
1160 | /** | |
1161 | * smack_task_getscheduler - Smack check on reading scheduler | |
1162 | * @p: the task object | |
1163 | * | |
1164 | * Return 0 if read access is permitted | |
1165 | */ | |
1166 | static int smack_task_getscheduler(struct task_struct *p) | |
1167 | { | |
c69e8d9c | 1168 | return smk_curacc(task_security(p), MAY_READ); |
e114e473 CS |
1169 | } |
1170 | ||
1171 | /** | |
1172 | * smack_task_movememory - Smack check on moving memory | |
1173 | * @p: the task object | |
1174 | * | |
1175 | * Return 0 if write access is permitted | |
1176 | */ | |
1177 | static int smack_task_movememory(struct task_struct *p) | |
1178 | { | |
c69e8d9c | 1179 | return smk_curacc(task_security(p), MAY_WRITE); |
e114e473 CS |
1180 | } |
1181 | ||
1182 | /** | |
1183 | * smack_task_kill - Smack check on signal delivery | |
1184 | * @p: the task object | |
1185 | * @info: unused | |
1186 | * @sig: unused | |
1187 | * @secid: identifies the smack to use in lieu of current's | |
1188 | * | |
1189 | * Return 0 if write access is permitted | |
1190 | * | |
1191 | * The secid behavior is an artifact of an SELinux hack | |
1192 | * in the USB code. Someday it may go away. | |
1193 | */ | |
1194 | static int smack_task_kill(struct task_struct *p, struct siginfo *info, | |
1195 | int sig, u32 secid) | |
1196 | { | |
e114e473 CS |
1197 | /* |
1198 | * Sending a signal requires that the sender | |
1199 | * can write the receiver. | |
1200 | */ | |
1201 | if (secid == 0) | |
c69e8d9c | 1202 | return smk_curacc(task_security(p), MAY_WRITE); |
e114e473 CS |
1203 | /* |
1204 | * If the secid isn't 0 we're dealing with some USB IO | |
1205 | * specific behavior. This is not clean. For one thing | |
1206 | * we can't take privilege into account. | |
1207 | */ | |
c69e8d9c | 1208 | return smk_access(smack_from_secid(secid), task_security(p), MAY_WRITE); |
e114e473 CS |
1209 | } |
1210 | ||
1211 | /** | |
1212 | * smack_task_wait - Smack access check for waiting | |
1213 | * @p: task to wait for | |
1214 | * | |
1215 | * Returns 0 if current can wait for p, error code otherwise | |
1216 | */ | |
1217 | static int smack_task_wait(struct task_struct *p) | |
1218 | { | |
1219 | int rc; | |
1220 | ||
c69e8d9c | 1221 | rc = smk_access(current_security(), task_security(p), MAY_WRITE); |
e114e473 CS |
1222 | if (rc == 0) |
1223 | return 0; | |
1224 | ||
1225 | /* | |
1226 | * Allow the operation to succeed if either task | |
1227 | * has privilege to perform operations that might | |
1228 | * account for the smack labels having gotten to | |
1229 | * be different in the first place. | |
1230 | * | |
5cd9c58f | 1231 | * This breaks the strict subject/object access |
e114e473 CS |
1232 | * control ideal, taking the object's privilege |
1233 | * state into account in the decision as well as | |
1234 | * the smack value. | |
1235 | */ | |
5cd9c58f | 1236 | if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE)) |
e114e473 CS |
1237 | return 0; |
1238 | ||
1239 | return rc; | |
1240 | } | |
1241 | ||
1242 | /** | |
1243 | * smack_task_to_inode - copy task smack into the inode blob | |
1244 | * @p: task to copy from | |
1245 | * inode: inode to copy to | |
1246 | * | |
1247 | * Sets the smack pointer in the inode security blob | |
1248 | */ | |
1249 | static void smack_task_to_inode(struct task_struct *p, struct inode *inode) | |
1250 | { | |
1251 | struct inode_smack *isp = inode->i_security; | |
c69e8d9c | 1252 | isp->smk_inode = task_security(p); |
e114e473 CS |
1253 | } |
1254 | ||
1255 | /* | |
1256 | * Socket hooks. | |
1257 | */ | |
1258 | ||
1259 | /** | |
1260 | * smack_sk_alloc_security - Allocate a socket blob | |
1261 | * @sk: the socket | |
1262 | * @family: unused | |
1263 | * @priority: memory allocation priority | |
1264 | * | |
1265 | * Assign Smack pointers to current | |
1266 | * | |
1267 | * Returns 0 on success, -ENOMEM is there's no memory | |
1268 | */ | |
1269 | static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags) | |
1270 | { | |
86a264ab | 1271 | char *csp = current_security(); |
e114e473 CS |
1272 | struct socket_smack *ssp; |
1273 | ||
1274 | ssp = kzalloc(sizeof(struct socket_smack), gfp_flags); | |
1275 | if (ssp == NULL) | |
1276 | return -ENOMEM; | |
1277 | ||
1278 | ssp->smk_in = csp; | |
1279 | ssp->smk_out = csp; | |
1280 | ssp->smk_packet[0] = '\0'; | |
1281 | ||
1282 | sk->sk_security = ssp; | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | /** | |
1288 | * smack_sk_free_security - Free a socket blob | |
1289 | * @sk: the socket | |
1290 | * | |
1291 | * Clears the blob pointer | |
1292 | */ | |
1293 | static void smack_sk_free_security(struct sock *sk) | |
1294 | { | |
1295 | kfree(sk->sk_security); | |
1296 | } | |
1297 | ||
1298 | /** | |
1299 | * smack_set_catset - convert a capset to netlabel mls categories | |
1300 | * @catset: the Smack categories | |
1301 | * @sap: where to put the netlabel categories | |
1302 | * | |
1303 | * Allocates and fills attr.mls.cat | |
1304 | */ | |
1305 | static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap) | |
1306 | { | |
1307 | unsigned char *cp; | |
1308 | unsigned char m; | |
1309 | int cat; | |
1310 | int rc; | |
1311 | int byte; | |
1312 | ||
c60264c4 | 1313 | if (!catset) |
e114e473 CS |
1314 | return; |
1315 | ||
1316 | sap->flags |= NETLBL_SECATTR_MLS_CAT; | |
1317 | sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC); | |
1318 | sap->attr.mls.cat->startbit = 0; | |
1319 | ||
1320 | for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++) | |
1321 | for (m = 0x80; m != 0; m >>= 1, cat++) { | |
1322 | if ((m & *cp) == 0) | |
1323 | continue; | |
1324 | rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat, | |
1325 | cat, GFP_ATOMIC); | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | /** | |
1330 | * smack_to_secattr - fill a secattr from a smack value | |
1331 | * @smack: the smack value | |
1332 | * @nlsp: where the result goes | |
1333 | * | |
1334 | * Casey says that CIPSO is good enough for now. | |
1335 | * It can be used to effect. | |
1336 | * It can also be abused to effect when necessary. | |
1337 | * Appologies to the TSIG group in general and GW in particular. | |
1338 | */ | |
1339 | static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp) | |
1340 | { | |
1341 | struct smack_cipso cipso; | |
1342 | int rc; | |
1343 | ||
1344 | switch (smack_net_nltype) { | |
1345 | case NETLBL_NLTYPE_CIPSOV4: | |
00447872 | 1346 | nlsp->domain = smack; |
4bc87e62 | 1347 | nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL; |
e114e473 CS |
1348 | |
1349 | rc = smack_to_cipso(smack, &cipso); | |
1350 | if (rc == 0) { | |
1351 | nlsp->attr.mls.lvl = cipso.smk_level; | |
1352 | smack_set_catset(cipso.smk_catset, nlsp); | |
1353 | } else { | |
1354 | nlsp->attr.mls.lvl = smack_cipso_direct; | |
1355 | smack_set_catset(smack, nlsp); | |
1356 | } | |
1357 | break; | |
1358 | default: | |
1359 | break; | |
1360 | } | |
1361 | } | |
1362 | ||
1363 | /** | |
1364 | * smack_netlabel - Set the secattr on a socket | |
1365 | * @sk: the socket | |
1366 | * | |
1367 | * Convert the outbound smack value (smk_out) to a | |
1368 | * secattr and attach it to the socket. | |
1369 | * | |
1370 | * Returns 0 on success or an error code | |
1371 | */ | |
1372 | static int smack_netlabel(struct sock *sk) | |
1373 | { | |
2e1d146a | 1374 | struct socket_smack *ssp; |
e114e473 | 1375 | struct netlbl_lsm_secattr secattr; |
4bc87e62 | 1376 | int rc; |
e114e473 | 1377 | |
2e1d146a | 1378 | ssp = sk->sk_security; |
e114e473 CS |
1379 | netlbl_secattr_init(&secattr); |
1380 | smack_to_secattr(ssp->smk_out, &secattr); | |
4bc87e62 | 1381 | rc = netlbl_sock_setattr(sk, &secattr); |
e114e473 | 1382 | netlbl_secattr_destroy(&secattr); |
4bc87e62 | 1383 | |
e114e473 CS |
1384 | return rc; |
1385 | } | |
1386 | ||
1387 | /** | |
1388 | * smack_inode_setsecurity - set smack xattrs | |
1389 | * @inode: the object | |
1390 | * @name: attribute name | |
1391 | * @value: attribute value | |
1392 | * @size: size of the attribute | |
1393 | * @flags: unused | |
1394 | * | |
1395 | * Sets the named attribute in the appropriate blob | |
1396 | * | |
1397 | * Returns 0 on success, or an error code | |
1398 | */ | |
1399 | static int smack_inode_setsecurity(struct inode *inode, const char *name, | |
1400 | const void *value, size_t size, int flags) | |
1401 | { | |
1402 | char *sp; | |
1403 | struct inode_smack *nsp = inode->i_security; | |
1404 | struct socket_smack *ssp; | |
1405 | struct socket *sock; | |
4bc87e62 | 1406 | int rc = 0; |
e114e473 CS |
1407 | |
1408 | if (value == NULL || size > SMK_LABELLEN) | |
1409 | return -EACCES; | |
1410 | ||
1411 | sp = smk_import(value, size); | |
1412 | if (sp == NULL) | |
1413 | return -EINVAL; | |
1414 | ||
1415 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { | |
1416 | nsp->smk_inode = sp; | |
1417 | return 0; | |
1418 | } | |
1419 | /* | |
1420 | * The rest of the Smack xattrs are only on sockets. | |
1421 | */ | |
1422 | if (inode->i_sb->s_magic != SOCKFS_MAGIC) | |
1423 | return -EOPNOTSUPP; | |
1424 | ||
1425 | sock = SOCKET_I(inode); | |
2e1d146a | 1426 | if (sock == NULL || sock->sk == NULL) |
e114e473 CS |
1427 | return -EOPNOTSUPP; |
1428 | ||
1429 | ssp = sock->sk->sk_security; | |
1430 | ||
1431 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) | |
1432 | ssp->smk_in = sp; | |
1433 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) { | |
1434 | ssp->smk_out = sp; | |
4bc87e62 CS |
1435 | rc = smack_netlabel(sock->sk); |
1436 | if (rc != 0) | |
1437 | printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n", | |
1438 | __func__, -rc); | |
e114e473 CS |
1439 | } else |
1440 | return -EOPNOTSUPP; | |
1441 | ||
1442 | return 0; | |
1443 | } | |
1444 | ||
1445 | /** | |
1446 | * smack_socket_post_create - finish socket setup | |
1447 | * @sock: the socket | |
1448 | * @family: protocol family | |
1449 | * @type: unused | |
1450 | * @protocol: unused | |
1451 | * @kern: unused | |
1452 | * | |
1453 | * Sets the netlabel information on the socket | |
1454 | * | |
1455 | * Returns 0 on success, and error code otherwise | |
1456 | */ | |
1457 | static int smack_socket_post_create(struct socket *sock, int family, | |
1458 | int type, int protocol, int kern) | |
1459 | { | |
2e1d146a | 1460 | if (family != PF_INET || sock->sk == NULL) |
e114e473 CS |
1461 | return 0; |
1462 | /* | |
1463 | * Set the outbound netlbl. | |
1464 | */ | |
1465 | return smack_netlabel(sock->sk); | |
1466 | } | |
1467 | ||
1468 | /** | |
1469 | * smack_flags_to_may - convert S_ to MAY_ values | |
1470 | * @flags: the S_ value | |
1471 | * | |
1472 | * Returns the equivalent MAY_ value | |
1473 | */ | |
1474 | static int smack_flags_to_may(int flags) | |
1475 | { | |
1476 | int may = 0; | |
1477 | ||
1478 | if (flags & S_IRUGO) | |
1479 | may |= MAY_READ; | |
1480 | if (flags & S_IWUGO) | |
1481 | may |= MAY_WRITE; | |
1482 | if (flags & S_IXUGO) | |
1483 | may |= MAY_EXEC; | |
1484 | ||
1485 | return may; | |
1486 | } | |
1487 | ||
1488 | /** | |
1489 | * smack_msg_msg_alloc_security - Set the security blob for msg_msg | |
1490 | * @msg: the object | |
1491 | * | |
1492 | * Returns 0 | |
1493 | */ | |
1494 | static int smack_msg_msg_alloc_security(struct msg_msg *msg) | |
1495 | { | |
86a264ab | 1496 | msg->security = current_security(); |
e114e473 CS |
1497 | return 0; |
1498 | } | |
1499 | ||
1500 | /** | |
1501 | * smack_msg_msg_free_security - Clear the security blob for msg_msg | |
1502 | * @msg: the object | |
1503 | * | |
1504 | * Clears the blob pointer | |
1505 | */ | |
1506 | static void smack_msg_msg_free_security(struct msg_msg *msg) | |
1507 | { | |
1508 | msg->security = NULL; | |
1509 | } | |
1510 | ||
1511 | /** | |
1512 | * smack_of_shm - the smack pointer for the shm | |
1513 | * @shp: the object | |
1514 | * | |
1515 | * Returns a pointer to the smack value | |
1516 | */ | |
1517 | static char *smack_of_shm(struct shmid_kernel *shp) | |
1518 | { | |
1519 | return (char *)shp->shm_perm.security; | |
1520 | } | |
1521 | ||
1522 | /** | |
1523 | * smack_shm_alloc_security - Set the security blob for shm | |
1524 | * @shp: the object | |
1525 | * | |
1526 | * Returns 0 | |
1527 | */ | |
1528 | static int smack_shm_alloc_security(struct shmid_kernel *shp) | |
1529 | { | |
1530 | struct kern_ipc_perm *isp = &shp->shm_perm; | |
1531 | ||
86a264ab | 1532 | isp->security = current_security(); |
e114e473 CS |
1533 | return 0; |
1534 | } | |
1535 | ||
1536 | /** | |
1537 | * smack_shm_free_security - Clear the security blob for shm | |
1538 | * @shp: the object | |
1539 | * | |
1540 | * Clears the blob pointer | |
1541 | */ | |
1542 | static void smack_shm_free_security(struct shmid_kernel *shp) | |
1543 | { | |
1544 | struct kern_ipc_perm *isp = &shp->shm_perm; | |
1545 | ||
1546 | isp->security = NULL; | |
1547 | } | |
1548 | ||
1549 | /** | |
1550 | * smack_shm_associate - Smack access check for shm | |
1551 | * @shp: the object | |
1552 | * @shmflg: access requested | |
1553 | * | |
1554 | * Returns 0 if current has the requested access, error code otherwise | |
1555 | */ | |
1556 | static int smack_shm_associate(struct shmid_kernel *shp, int shmflg) | |
1557 | { | |
1558 | char *ssp = smack_of_shm(shp); | |
1559 | int may; | |
1560 | ||
1561 | may = smack_flags_to_may(shmflg); | |
1562 | return smk_curacc(ssp, may); | |
1563 | } | |
1564 | ||
1565 | /** | |
1566 | * smack_shm_shmctl - Smack access check for shm | |
1567 | * @shp: the object | |
1568 | * @cmd: what it wants to do | |
1569 | * | |
1570 | * Returns 0 if current has the requested access, error code otherwise | |
1571 | */ | |
1572 | static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd) | |
1573 | { | |
1d252fb8 | 1574 | char *ssp; |
e114e473 CS |
1575 | int may; |
1576 | ||
1577 | switch (cmd) { | |
1578 | case IPC_STAT: | |
1579 | case SHM_STAT: | |
1580 | may = MAY_READ; | |
1581 | break; | |
1582 | case IPC_SET: | |
1583 | case SHM_LOCK: | |
1584 | case SHM_UNLOCK: | |
1585 | case IPC_RMID: | |
1586 | may = MAY_READWRITE; | |
1587 | break; | |
1588 | case IPC_INFO: | |
1589 | case SHM_INFO: | |
1590 | /* | |
1591 | * System level information. | |
1592 | */ | |
1593 | return 0; | |
1594 | default: | |
1595 | return -EINVAL; | |
1596 | } | |
1597 | ||
1d252fb8 | 1598 | ssp = smack_of_shm(shp); |
e114e473 CS |
1599 | return smk_curacc(ssp, may); |
1600 | } | |
1601 | ||
1602 | /** | |
1603 | * smack_shm_shmat - Smack access for shmat | |
1604 | * @shp: the object | |
1605 | * @shmaddr: unused | |
1606 | * @shmflg: access requested | |
1607 | * | |
1608 | * Returns 0 if current has the requested access, error code otherwise | |
1609 | */ | |
1610 | static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, | |
1611 | int shmflg) | |
1612 | { | |
1613 | char *ssp = smack_of_shm(shp); | |
1614 | int may; | |
1615 | ||
1616 | may = smack_flags_to_may(shmflg); | |
1617 | return smk_curacc(ssp, may); | |
1618 | } | |
1619 | ||
1620 | /** | |
1621 | * smack_of_sem - the smack pointer for the sem | |
1622 | * @sma: the object | |
1623 | * | |
1624 | * Returns a pointer to the smack value | |
1625 | */ | |
1626 | static char *smack_of_sem(struct sem_array *sma) | |
1627 | { | |
1628 | return (char *)sma->sem_perm.security; | |
1629 | } | |
1630 | ||
1631 | /** | |
1632 | * smack_sem_alloc_security - Set the security blob for sem | |
1633 | * @sma: the object | |
1634 | * | |
1635 | * Returns 0 | |
1636 | */ | |
1637 | static int smack_sem_alloc_security(struct sem_array *sma) | |
1638 | { | |
1639 | struct kern_ipc_perm *isp = &sma->sem_perm; | |
1640 | ||
86a264ab | 1641 | isp->security = current_security(); |
e114e473 CS |
1642 | return 0; |
1643 | } | |
1644 | ||
1645 | /** | |
1646 | * smack_sem_free_security - Clear the security blob for sem | |
1647 | * @sma: the object | |
1648 | * | |
1649 | * Clears the blob pointer | |
1650 | */ | |
1651 | static void smack_sem_free_security(struct sem_array *sma) | |
1652 | { | |
1653 | struct kern_ipc_perm *isp = &sma->sem_perm; | |
1654 | ||
1655 | isp->security = NULL; | |
1656 | } | |
1657 | ||
1658 | /** | |
1659 | * smack_sem_associate - Smack access check for sem | |
1660 | * @sma: the object | |
1661 | * @semflg: access requested | |
1662 | * | |
1663 | * Returns 0 if current has the requested access, error code otherwise | |
1664 | */ | |
1665 | static int smack_sem_associate(struct sem_array *sma, int semflg) | |
1666 | { | |
1667 | char *ssp = smack_of_sem(sma); | |
1668 | int may; | |
1669 | ||
1670 | may = smack_flags_to_may(semflg); | |
1671 | return smk_curacc(ssp, may); | |
1672 | } | |
1673 | ||
1674 | /** | |
1675 | * smack_sem_shmctl - Smack access check for sem | |
1676 | * @sma: the object | |
1677 | * @cmd: what it wants to do | |
1678 | * | |
1679 | * Returns 0 if current has the requested access, error code otherwise | |
1680 | */ | |
1681 | static int smack_sem_semctl(struct sem_array *sma, int cmd) | |
1682 | { | |
1d252fb8 | 1683 | char *ssp; |
e114e473 CS |
1684 | int may; |
1685 | ||
1686 | switch (cmd) { | |
1687 | case GETPID: | |
1688 | case GETNCNT: | |
1689 | case GETZCNT: | |
1690 | case GETVAL: | |
1691 | case GETALL: | |
1692 | case IPC_STAT: | |
1693 | case SEM_STAT: | |
1694 | may = MAY_READ; | |
1695 | break; | |
1696 | case SETVAL: | |
1697 | case SETALL: | |
1698 | case IPC_RMID: | |
1699 | case IPC_SET: | |
1700 | may = MAY_READWRITE; | |
1701 | break; | |
1702 | case IPC_INFO: | |
1703 | case SEM_INFO: | |
1704 | /* | |
1705 | * System level information | |
1706 | */ | |
1707 | return 0; | |
1708 | default: | |
1709 | return -EINVAL; | |
1710 | } | |
1711 | ||
1d252fb8 | 1712 | ssp = smack_of_sem(sma); |
e114e473 CS |
1713 | return smk_curacc(ssp, may); |
1714 | } | |
1715 | ||
1716 | /** | |
1717 | * smack_sem_semop - Smack checks of semaphore operations | |
1718 | * @sma: the object | |
1719 | * @sops: unused | |
1720 | * @nsops: unused | |
1721 | * @alter: unused | |
1722 | * | |
1723 | * Treated as read and write in all cases. | |
1724 | * | |
1725 | * Returns 0 if access is allowed, error code otherwise | |
1726 | */ | |
1727 | static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops, | |
1728 | unsigned nsops, int alter) | |
1729 | { | |
1730 | char *ssp = smack_of_sem(sma); | |
1731 | ||
1732 | return smk_curacc(ssp, MAY_READWRITE); | |
1733 | } | |
1734 | ||
1735 | /** | |
1736 | * smack_msg_alloc_security - Set the security blob for msg | |
1737 | * @msq: the object | |
1738 | * | |
1739 | * Returns 0 | |
1740 | */ | |
1741 | static int smack_msg_queue_alloc_security(struct msg_queue *msq) | |
1742 | { | |
1743 | struct kern_ipc_perm *kisp = &msq->q_perm; | |
1744 | ||
86a264ab | 1745 | kisp->security = current_security(); |
e114e473 CS |
1746 | return 0; |
1747 | } | |
1748 | ||
1749 | /** | |
1750 | * smack_msg_free_security - Clear the security blob for msg | |
1751 | * @msq: the object | |
1752 | * | |
1753 | * Clears the blob pointer | |
1754 | */ | |
1755 | static void smack_msg_queue_free_security(struct msg_queue *msq) | |
1756 | { | |
1757 | struct kern_ipc_perm *kisp = &msq->q_perm; | |
1758 | ||
1759 | kisp->security = NULL; | |
1760 | } | |
1761 | ||
1762 | /** | |
1763 | * smack_of_msq - the smack pointer for the msq | |
1764 | * @msq: the object | |
1765 | * | |
1766 | * Returns a pointer to the smack value | |
1767 | */ | |
1768 | static char *smack_of_msq(struct msg_queue *msq) | |
1769 | { | |
1770 | return (char *)msq->q_perm.security; | |
1771 | } | |
1772 | ||
1773 | /** | |
1774 | * smack_msg_queue_associate - Smack access check for msg_queue | |
1775 | * @msq: the object | |
1776 | * @msqflg: access requested | |
1777 | * | |
1778 | * Returns 0 if current has the requested access, error code otherwise | |
1779 | */ | |
1780 | static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg) | |
1781 | { | |
1782 | char *msp = smack_of_msq(msq); | |
1783 | int may; | |
1784 | ||
1785 | may = smack_flags_to_may(msqflg); | |
1786 | return smk_curacc(msp, may); | |
1787 | } | |
1788 | ||
1789 | /** | |
1790 | * smack_msg_queue_msgctl - Smack access check for msg_queue | |
1791 | * @msq: the object | |
1792 | * @cmd: what it wants to do | |
1793 | * | |
1794 | * Returns 0 if current has the requested access, error code otherwise | |
1795 | */ | |
1796 | static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd) | |
1797 | { | |
1d252fb8 | 1798 | char *msp; |
e114e473 CS |
1799 | int may; |
1800 | ||
1801 | switch (cmd) { | |
1802 | case IPC_STAT: | |
1803 | case MSG_STAT: | |
1804 | may = MAY_READ; | |
1805 | break; | |
1806 | case IPC_SET: | |
1807 | case IPC_RMID: | |
1808 | may = MAY_READWRITE; | |
1809 | break; | |
1810 | case IPC_INFO: | |
1811 | case MSG_INFO: | |
1812 | /* | |
1813 | * System level information | |
1814 | */ | |
1815 | return 0; | |
1816 | default: | |
1817 | return -EINVAL; | |
1818 | } | |
1819 | ||
1d252fb8 | 1820 | msp = smack_of_msq(msq); |
e114e473 CS |
1821 | return smk_curacc(msp, may); |
1822 | } | |
1823 | ||
1824 | /** | |
1825 | * smack_msg_queue_msgsnd - Smack access check for msg_queue | |
1826 | * @msq: the object | |
1827 | * @msg: unused | |
1828 | * @msqflg: access requested | |
1829 | * | |
1830 | * Returns 0 if current has the requested access, error code otherwise | |
1831 | */ | |
1832 | static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, | |
1833 | int msqflg) | |
1834 | { | |
1835 | char *msp = smack_of_msq(msq); | |
1836 | int rc; | |
1837 | ||
1838 | rc = smack_flags_to_may(msqflg); | |
1839 | return smk_curacc(msp, rc); | |
1840 | } | |
1841 | ||
1842 | /** | |
1843 | * smack_msg_queue_msgsnd - Smack access check for msg_queue | |
1844 | * @msq: the object | |
1845 | * @msg: unused | |
1846 | * @target: unused | |
1847 | * @type: unused | |
1848 | * @mode: unused | |
1849 | * | |
1850 | * Returns 0 if current has read and write access, error code otherwise | |
1851 | */ | |
1852 | static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, | |
1853 | struct task_struct *target, long type, int mode) | |
1854 | { | |
1855 | char *msp = smack_of_msq(msq); | |
1856 | ||
1857 | return smk_curacc(msp, MAY_READWRITE); | |
1858 | } | |
1859 | ||
1860 | /** | |
1861 | * smack_ipc_permission - Smack access for ipc_permission() | |
1862 | * @ipp: the object permissions | |
1863 | * @flag: access requested | |
1864 | * | |
1865 | * Returns 0 if current has read and write access, error code otherwise | |
1866 | */ | |
1867 | static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) | |
1868 | { | |
1869 | char *isp = ipp->security; | |
1870 | int may; | |
1871 | ||
1872 | may = smack_flags_to_may(flag); | |
1873 | return smk_curacc(isp, may); | |
1874 | } | |
1875 | ||
d20bdda6 AD |
1876 | /** |
1877 | * smack_ipc_getsecid - Extract smack security id | |
1878 | * @ipcp: the object permissions | |
1879 | * @secid: where result will be saved | |
1880 | */ | |
1881 | static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid) | |
1882 | { | |
1883 | char *smack = ipp->security; | |
1884 | ||
1885 | *secid = smack_to_secid(smack); | |
1886 | } | |
1887 | ||
e114e473 CS |
1888 | /** |
1889 | * smack_d_instantiate - Make sure the blob is correct on an inode | |
1890 | * @opt_dentry: unused | |
1891 | * @inode: the object | |
1892 | * | |
1893 | * Set the inode's security blob if it hasn't been done already. | |
1894 | */ | |
1895 | static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) | |
1896 | { | |
1897 | struct super_block *sbp; | |
1898 | struct superblock_smack *sbsp; | |
1899 | struct inode_smack *isp; | |
86a264ab | 1900 | char *csp = current_security(); |
e114e473 CS |
1901 | char *fetched; |
1902 | char *final; | |
1903 | struct dentry *dp; | |
1904 | ||
1905 | if (inode == NULL) | |
1906 | return; | |
1907 | ||
1908 | isp = inode->i_security; | |
1909 | ||
1910 | mutex_lock(&isp->smk_lock); | |
1911 | /* | |
1912 | * If the inode is already instantiated | |
1913 | * take the quick way out | |
1914 | */ | |
1915 | if (isp->smk_flags & SMK_INODE_INSTANT) | |
1916 | goto unlockandout; | |
1917 | ||
1918 | sbp = inode->i_sb; | |
1919 | sbsp = sbp->s_security; | |
1920 | /* | |
1921 | * We're going to use the superblock default label | |
1922 | * if there's no label on the file. | |
1923 | */ | |
1924 | final = sbsp->smk_default; | |
1925 | ||
e97dcb0e CS |
1926 | /* |
1927 | * If this is the root inode the superblock | |
1928 | * may be in the process of initialization. | |
1929 | * If that is the case use the root value out | |
1930 | * of the superblock. | |
1931 | */ | |
1932 | if (opt_dentry->d_parent == opt_dentry) { | |
1933 | isp->smk_inode = sbsp->smk_root; | |
1934 | isp->smk_flags |= SMK_INODE_INSTANT; | |
1935 | goto unlockandout; | |
1936 | } | |
1937 | ||
e114e473 CS |
1938 | /* |
1939 | * This is pretty hackish. | |
1940 | * Casey says that we shouldn't have to do | |
1941 | * file system specific code, but it does help | |
1942 | * with keeping it simple. | |
1943 | */ | |
1944 | switch (sbp->s_magic) { | |
1945 | case SMACK_MAGIC: | |
1946 | /* | |
1947 | * Casey says that it's a little embarassing | |
1948 | * that the smack file system doesn't do | |
1949 | * extended attributes. | |
1950 | */ | |
1951 | final = smack_known_star.smk_known; | |
1952 | break; | |
1953 | case PIPEFS_MAGIC: | |
1954 | /* | |
1955 | * Casey says pipes are easy (?) | |
1956 | */ | |
1957 | final = smack_known_star.smk_known; | |
1958 | break; | |
1959 | case DEVPTS_SUPER_MAGIC: | |
1960 | /* | |
1961 | * devpts seems content with the label of the task. | |
1962 | * Programs that change smack have to treat the | |
1963 | * pty with respect. | |
1964 | */ | |
1965 | final = csp; | |
1966 | break; | |
1967 | case SOCKFS_MAGIC: | |
1968 | /* | |
1969 | * Casey says sockets get the smack of the task. | |
1970 | */ | |
1971 | final = csp; | |
1972 | break; | |
1973 | case PROC_SUPER_MAGIC: | |
1974 | /* | |
1975 | * Casey says procfs appears not to care. | |
1976 | * The superblock default suffices. | |
1977 | */ | |
1978 | break; | |
1979 | case TMPFS_MAGIC: | |
1980 | /* | |
1981 | * Device labels should come from the filesystem, | |
1982 | * but watch out, because they're volitile, | |
1983 | * getting recreated on every reboot. | |
1984 | */ | |
1985 | final = smack_known_star.smk_known; | |
1986 | /* | |
1987 | * No break. | |
1988 | * | |
1989 | * If a smack value has been set we want to use it, | |
1990 | * but since tmpfs isn't giving us the opportunity | |
1991 | * to set mount options simulate setting the | |
1992 | * superblock default. | |
1993 | */ | |
1994 | default: | |
1995 | /* | |
1996 | * This isn't an understood special case. | |
1997 | * Get the value from the xattr. | |
1998 | * | |
1999 | * No xattr support means, alas, no SMACK label. | |
2000 | * Use the aforeapplied default. | |
2001 | * It would be curious if the label of the task | |
2002 | * does not match that assigned. | |
2003 | */ | |
2004 | if (inode->i_op->getxattr == NULL) | |
2005 | break; | |
2006 | /* | |
2007 | * Get the dentry for xattr. | |
2008 | */ | |
2009 | if (opt_dentry == NULL) { | |
2010 | dp = d_find_alias(inode); | |
2011 | if (dp == NULL) | |
2012 | break; | |
2013 | } else { | |
2014 | dp = dget(opt_dentry); | |
2015 | if (dp == NULL) | |
2016 | break; | |
2017 | } | |
2018 | ||
2019 | fetched = smk_fetch(inode, dp); | |
2020 | if (fetched != NULL) | |
2021 | final = fetched; | |
2022 | ||
2023 | dput(dp); | |
2024 | break; | |
2025 | } | |
2026 | ||
2027 | if (final == NULL) | |
2028 | isp->smk_inode = csp; | |
2029 | else | |
2030 | isp->smk_inode = final; | |
2031 | ||
2032 | isp->smk_flags |= SMK_INODE_INSTANT; | |
2033 | ||
2034 | unlockandout: | |
2035 | mutex_unlock(&isp->smk_lock); | |
2036 | return; | |
2037 | } | |
2038 | ||
2039 | /** | |
2040 | * smack_getprocattr - Smack process attribute access | |
2041 | * @p: the object task | |
2042 | * @name: the name of the attribute in /proc/.../attr | |
2043 | * @value: where to put the result | |
2044 | * | |
2045 | * Places a copy of the task Smack into value | |
2046 | * | |
2047 | * Returns the length of the smack label or an error code | |
2048 | */ | |
2049 | static int smack_getprocattr(struct task_struct *p, char *name, char **value) | |
2050 | { | |
2051 | char *cp; | |
2052 | int slen; | |
2053 | ||
2054 | if (strcmp(name, "current") != 0) | |
2055 | return -EINVAL; | |
2056 | ||
c69e8d9c | 2057 | cp = kstrdup(task_security(p), GFP_KERNEL); |
e114e473 CS |
2058 | if (cp == NULL) |
2059 | return -ENOMEM; | |
2060 | ||
2061 | slen = strlen(cp); | |
2062 | *value = cp; | |
2063 | return slen; | |
2064 | } | |
2065 | ||
2066 | /** | |
2067 | * smack_setprocattr - Smack process attribute setting | |
2068 | * @p: the object task | |
2069 | * @name: the name of the attribute in /proc/.../attr | |
2070 | * @value: the value to set | |
2071 | * @size: the size of the value | |
2072 | * | |
2073 | * Sets the Smack value of the task. Only setting self | |
2074 | * is permitted and only with privilege | |
2075 | * | |
2076 | * Returns the length of the smack label or an error code | |
2077 | */ | |
2078 | static int smack_setprocattr(struct task_struct *p, char *name, | |
2079 | void *value, size_t size) | |
2080 | { | |
d84f4f99 | 2081 | struct cred *new; |
e114e473 CS |
2082 | char *newsmack; |
2083 | ||
e114e473 CS |
2084 | /* |
2085 | * Changing another process' Smack value is too dangerous | |
2086 | * and supports no sane use case. | |
2087 | */ | |
2088 | if (p != current) | |
2089 | return -EPERM; | |
2090 | ||
5cd9c58f DH |
2091 | if (!capable(CAP_MAC_ADMIN)) |
2092 | return -EPERM; | |
2093 | ||
e114e473 CS |
2094 | if (value == NULL || size == 0 || size >= SMK_LABELLEN) |
2095 | return -EINVAL; | |
2096 | ||
2097 | if (strcmp(name, "current") != 0) | |
2098 | return -EINVAL; | |
2099 | ||
2100 | newsmack = smk_import(value, size); | |
2101 | if (newsmack == NULL) | |
2102 | return -EINVAL; | |
2103 | ||
d84f4f99 DH |
2104 | new = prepare_creds(); |
2105 | if (!new) | |
2106 | return -ENOMEM; | |
2107 | new->security = newsmack; | |
2108 | commit_creds(new); | |
e114e473 CS |
2109 | return size; |
2110 | } | |
2111 | ||
2112 | /** | |
2113 | * smack_unix_stream_connect - Smack access on UDS | |
2114 | * @sock: one socket | |
2115 | * @other: the other socket | |
2116 | * @newsk: unused | |
2117 | * | |
2118 | * Return 0 if a subject with the smack of sock could access | |
2119 | * an object with the smack of other, otherwise an error code | |
2120 | */ | |
2121 | static int smack_unix_stream_connect(struct socket *sock, | |
2122 | struct socket *other, struct sock *newsk) | |
2123 | { | |
2124 | struct inode *sp = SOCK_INODE(sock); | |
2125 | struct inode *op = SOCK_INODE(other); | |
2126 | ||
2127 | return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_READWRITE); | |
2128 | } | |
2129 | ||
2130 | /** | |
2131 | * smack_unix_may_send - Smack access on UDS | |
2132 | * @sock: one socket | |
2133 | * @other: the other socket | |
2134 | * | |
2135 | * Return 0 if a subject with the smack of sock could access | |
2136 | * an object with the smack of other, otherwise an error code | |
2137 | */ | |
2138 | static int smack_unix_may_send(struct socket *sock, struct socket *other) | |
2139 | { | |
2140 | struct inode *sp = SOCK_INODE(sock); | |
2141 | struct inode *op = SOCK_INODE(other); | |
2142 | ||
2143 | return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE); | |
2144 | } | |
2145 | ||
2146 | /** | |
2147 | * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat | |
2148 | * pair to smack | |
2149 | * @sap: netlabel secattr | |
2150 | * @sip: where to put the result | |
2151 | * | |
2152 | * Copies a smack label into sip | |
2153 | */ | |
2154 | static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip) | |
2155 | { | |
2156 | char smack[SMK_LABELLEN]; | |
2157 | int pcat; | |
2158 | ||
2159 | if ((sap->flags & NETLBL_SECATTR_MLS_LVL) == 0) { | |
2160 | /* | |
2161 | * If there are flags but no level netlabel isn't | |
2162 | * behaving the way we expect it to. | |
2163 | * | |
2164 | * Without guidance regarding the smack value | |
2165 | * for the packet fall back on the network | |
2166 | * ambient value. | |
2167 | */ | |
2168 | strncpy(sip, smack_net_ambient, SMK_MAXLEN); | |
2169 | return; | |
2170 | } | |
2171 | /* | |
2172 | * Get the categories, if any | |
2173 | */ | |
2174 | memset(smack, '\0', SMK_LABELLEN); | |
2175 | if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0) | |
2176 | for (pcat = -1;;) { | |
2177 | pcat = netlbl_secattr_catmap_walk(sap->attr.mls.cat, | |
2178 | pcat + 1); | |
2179 | if (pcat < 0) | |
2180 | break; | |
2181 | smack_catset_bit(pcat, smack); | |
2182 | } | |
2183 | /* | |
2184 | * If it is CIPSO using smack direct mapping | |
2185 | * we are already done. WeeHee. | |
2186 | */ | |
2187 | if (sap->attr.mls.lvl == smack_cipso_direct) { | |
2188 | memcpy(sip, smack, SMK_MAXLEN); | |
2189 | return; | |
2190 | } | |
2191 | /* | |
2192 | * Look it up in the supplied table if it is not a direct mapping. | |
2193 | */ | |
2194 | smack_from_cipso(sap->attr.mls.lvl, smack, sip); | |
2195 | return; | |
2196 | } | |
2197 | ||
2198 | /** | |
2199 | * smack_socket_sock_rcv_skb - Smack packet delivery access check | |
2200 | * @sk: socket | |
2201 | * @skb: packet | |
2202 | * | |
2203 | * Returns 0 if the packet should be delivered, an error code otherwise | |
2204 | */ | |
2205 | static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
2206 | { | |
2207 | struct netlbl_lsm_secattr secattr; | |
2208 | struct socket_smack *ssp = sk->sk_security; | |
2209 | char smack[SMK_LABELLEN]; | |
2210 | int rc; | |
2211 | ||
2212 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) | |
2213 | return 0; | |
2214 | ||
2215 | /* | |
2216 | * Translate what netlabel gave us. | |
2217 | */ | |
2218 | memset(smack, '\0', SMK_LABELLEN); | |
2219 | netlbl_secattr_init(&secattr); | |
2220 | rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr); | |
2221 | if (rc == 0) | |
2222 | smack_from_secattr(&secattr, smack); | |
2223 | else | |
2224 | strncpy(smack, smack_net_ambient, SMK_MAXLEN); | |
2225 | netlbl_secattr_destroy(&secattr); | |
2226 | /* | |
2227 | * Receiving a packet requires that the other end | |
2228 | * be able to write here. Read access is not required. | |
2229 | * This is the simplist possible security model | |
2230 | * for networking. | |
2231 | */ | |
a8134296 PM |
2232 | rc = smk_access(smack, ssp->smk_in, MAY_WRITE); |
2233 | if (rc != 0) | |
2234 | netlbl_skbuff_err(skb, rc, 0); | |
2235 | return rc; | |
e114e473 CS |
2236 | } |
2237 | ||
2238 | /** | |
2239 | * smack_socket_getpeersec_stream - pull in packet label | |
2240 | * @sock: the socket | |
2241 | * @optval: user's destination | |
2242 | * @optlen: size thereof | |
2243 | * @len: max thereoe | |
2244 | * | |
2245 | * returns zero on success, an error code otherwise | |
2246 | */ | |
2247 | static int smack_socket_getpeersec_stream(struct socket *sock, | |
2248 | char __user *optval, | |
2249 | int __user *optlen, unsigned len) | |
2250 | { | |
2251 | struct socket_smack *ssp; | |
2252 | int slen; | |
2253 | int rc = 0; | |
2254 | ||
2255 | ssp = sock->sk->sk_security; | |
2256 | slen = strlen(ssp->smk_packet) + 1; | |
2257 | ||
2258 | if (slen > len) | |
2259 | rc = -ERANGE; | |
2260 | else if (copy_to_user(optval, ssp->smk_packet, slen) != 0) | |
2261 | rc = -EFAULT; | |
2262 | ||
2263 | if (put_user(slen, optlen) != 0) | |
2264 | rc = -EFAULT; | |
2265 | ||
2266 | return rc; | |
2267 | } | |
2268 | ||
2269 | ||
2270 | /** | |
2271 | * smack_socket_getpeersec_dgram - pull in packet label | |
2272 | * @sock: the socket | |
2273 | * @skb: packet data | |
2274 | * @secid: pointer to where to put the secid of the packet | |
2275 | * | |
2276 | * Sets the netlabel socket state on sk from parent | |
2277 | */ | |
2278 | static int smack_socket_getpeersec_dgram(struct socket *sock, | |
2279 | struct sk_buff *skb, u32 *secid) | |
2280 | ||
2281 | { | |
2282 | struct netlbl_lsm_secattr secattr; | |
2283 | struct sock *sk; | |
2284 | char smack[SMK_LABELLEN]; | |
2285 | int family = PF_INET; | |
2286 | u32 s; | |
2287 | int rc; | |
2288 | ||
2289 | /* | |
2290 | * Only works for families with packets. | |
2291 | */ | |
2292 | if (sock != NULL) { | |
2293 | sk = sock->sk; | |
2294 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) | |
2295 | return 0; | |
2296 | family = sk->sk_family; | |
2297 | } | |
2298 | /* | |
2299 | * Translate what netlabel gave us. | |
2300 | */ | |
2301 | memset(smack, '\0', SMK_LABELLEN); | |
2302 | netlbl_secattr_init(&secattr); | |
2303 | rc = netlbl_skbuff_getattr(skb, family, &secattr); | |
2304 | if (rc == 0) | |
2305 | smack_from_secattr(&secattr, smack); | |
2306 | netlbl_secattr_destroy(&secattr); | |
2307 | ||
2308 | /* | |
2309 | * Give up if we couldn't get anything | |
2310 | */ | |
2311 | if (rc != 0) | |
2312 | return rc; | |
2313 | ||
2314 | s = smack_to_secid(smack); | |
2315 | if (s == 0) | |
2316 | return -EINVAL; | |
2317 | ||
2318 | *secid = s; | |
2319 | return 0; | |
2320 | } | |
2321 | ||
2322 | /** | |
2323 | * smack_sock_graft - graft access state between two sockets | |
2324 | * @sk: fresh sock | |
2325 | * @parent: donor socket | |
2326 | * | |
2327 | * Sets the netlabel socket state on sk from parent | |
2328 | */ | |
2329 | static void smack_sock_graft(struct sock *sk, struct socket *parent) | |
2330 | { | |
2331 | struct socket_smack *ssp; | |
2332 | int rc; | |
2333 | ||
2334 | if (sk == NULL) | |
2335 | return; | |
2336 | ||
2337 | if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6) | |
2338 | return; | |
2339 | ||
2340 | ssp = sk->sk_security; | |
86a264ab | 2341 | ssp->smk_in = ssp->smk_out = current_security(); |
e114e473 CS |
2342 | ssp->smk_packet[0] = '\0'; |
2343 | ||
2344 | rc = smack_netlabel(sk); | |
4bc87e62 CS |
2345 | if (rc != 0) |
2346 | printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n", | |
2347 | __func__, -rc); | |
e114e473 CS |
2348 | } |
2349 | ||
2350 | /** | |
2351 | * smack_inet_conn_request - Smack access check on connect | |
2352 | * @sk: socket involved | |
2353 | * @skb: packet | |
2354 | * @req: unused | |
2355 | * | |
2356 | * Returns 0 if a task with the packet label could write to | |
2357 | * the socket, otherwise an error code | |
2358 | */ | |
2359 | static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb, | |
2360 | struct request_sock *req) | |
2361 | { | |
2362 | struct netlbl_lsm_secattr skb_secattr; | |
2363 | struct socket_smack *ssp = sk->sk_security; | |
2364 | char smack[SMK_LABELLEN]; | |
2365 | int rc; | |
2366 | ||
2367 | if (skb == NULL) | |
2368 | return -EACCES; | |
2369 | ||
2370 | memset(smack, '\0', SMK_LABELLEN); | |
2371 | netlbl_secattr_init(&skb_secattr); | |
2372 | rc = netlbl_skbuff_getattr(skb, sk->sk_family, &skb_secattr); | |
2373 | if (rc == 0) | |
2374 | smack_from_secattr(&skb_secattr, smack); | |
2375 | else | |
2376 | strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN); | |
2377 | netlbl_secattr_destroy(&skb_secattr); | |
2378 | /* | |
2379 | * Receiving a packet requires that the other end | |
2380 | * be able to write here. Read access is not required. | |
2381 | * | |
2382 | * If the request is successful save the peer's label | |
2383 | * so that SO_PEERCRED can report it. | |
2384 | */ | |
2385 | rc = smk_access(smack, ssp->smk_in, MAY_WRITE); | |
2386 | if (rc == 0) | |
2387 | strncpy(ssp->smk_packet, smack, SMK_MAXLEN); | |
2388 | ||
2389 | return rc; | |
2390 | } | |
2391 | ||
2392 | /* | |
2393 | * Key management security hooks | |
2394 | * | |
2395 | * Casey has not tested key support very heavily. | |
2396 | * The permission check is most likely too restrictive. | |
2397 | * If you care about keys please have a look. | |
2398 | */ | |
2399 | #ifdef CONFIG_KEYS | |
2400 | ||
2401 | /** | |
2402 | * smack_key_alloc - Set the key security blob | |
2403 | * @key: object | |
d84f4f99 | 2404 | * @cred: the credentials to use |
e114e473 CS |
2405 | * @flags: unused |
2406 | * | |
2407 | * No allocation required | |
2408 | * | |
2409 | * Returns 0 | |
2410 | */ | |
d84f4f99 | 2411 | static int smack_key_alloc(struct key *key, const struct cred *cred, |
e114e473 CS |
2412 | unsigned long flags) |
2413 | { | |
d84f4f99 | 2414 | key->security = cred->security; |
e114e473 CS |
2415 | return 0; |
2416 | } | |
2417 | ||
2418 | /** | |
2419 | * smack_key_free - Clear the key security blob | |
2420 | * @key: the object | |
2421 | * | |
2422 | * Clear the blob pointer | |
2423 | */ | |
2424 | static void smack_key_free(struct key *key) | |
2425 | { | |
2426 | key->security = NULL; | |
2427 | } | |
2428 | ||
2429 | /* | |
2430 | * smack_key_permission - Smack access on a key | |
2431 | * @key_ref: gets to the object | |
d84f4f99 | 2432 | * @cred: the credentials to use |
e114e473 CS |
2433 | * @perm: unused |
2434 | * | |
2435 | * Return 0 if the task has read and write to the object, | |
2436 | * an error code otherwise | |
2437 | */ | |
2438 | static int smack_key_permission(key_ref_t key_ref, | |
d84f4f99 | 2439 | const struct cred *cred, key_perm_t perm) |
e114e473 CS |
2440 | { |
2441 | struct key *keyp; | |
2442 | ||
2443 | keyp = key_ref_to_ptr(key_ref); | |
2444 | if (keyp == NULL) | |
2445 | return -EINVAL; | |
2446 | /* | |
2447 | * If the key hasn't been initialized give it access so that | |
2448 | * it may do so. | |
2449 | */ | |
2450 | if (keyp->security == NULL) | |
2451 | return 0; | |
2452 | /* | |
2453 | * This should not occur | |
2454 | */ | |
d84f4f99 | 2455 | if (cred->security == NULL) |
e114e473 CS |
2456 | return -EACCES; |
2457 | ||
d84f4f99 | 2458 | return smk_access(cred->security, keyp->security, MAY_READWRITE); |
e114e473 CS |
2459 | } |
2460 | #endif /* CONFIG_KEYS */ | |
2461 | ||
d20bdda6 AD |
2462 | /* |
2463 | * Smack Audit hooks | |
2464 | * | |
2465 | * Audit requires a unique representation of each Smack specific | |
2466 | * rule. This unique representation is used to distinguish the | |
2467 | * object to be audited from remaining kernel objects and also | |
2468 | * works as a glue between the audit hooks. | |
2469 | * | |
2470 | * Since repository entries are added but never deleted, we'll use | |
2471 | * the smack_known label address related to the given audit rule as | |
2472 | * the needed unique representation. This also better fits the smack | |
2473 | * model where nearly everything is a label. | |
2474 | */ | |
2475 | #ifdef CONFIG_AUDIT | |
2476 | ||
2477 | /** | |
2478 | * smack_audit_rule_init - Initialize a smack audit rule | |
2479 | * @field: audit rule fields given from user-space (audit.h) | |
2480 | * @op: required testing operator (=, !=, >, <, ...) | |
2481 | * @rulestr: smack label to be audited | |
2482 | * @vrule: pointer to save our own audit rule representation | |
2483 | * | |
2484 | * Prepare to audit cases where (@field @op @rulestr) is true. | |
2485 | * The label to be audited is created if necessay. | |
2486 | */ | |
2487 | static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) | |
2488 | { | |
2489 | char **rule = (char **)vrule; | |
2490 | *rule = NULL; | |
2491 | ||
2492 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) | |
2493 | return -EINVAL; | |
2494 | ||
2495 | if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL) | |
2496 | return -EINVAL; | |
2497 | ||
2498 | *rule = smk_import(rulestr, 0); | |
2499 | ||
2500 | return 0; | |
2501 | } | |
2502 | ||
2503 | /** | |
2504 | * smack_audit_rule_known - Distinguish Smack audit rules | |
2505 | * @krule: rule of interest, in Audit kernel representation format | |
2506 | * | |
2507 | * This is used to filter Smack rules from remaining Audit ones. | |
2508 | * If it's proved that this rule belongs to us, the | |
2509 | * audit_rule_match hook will be called to do the final judgement. | |
2510 | */ | |
2511 | static int smack_audit_rule_known(struct audit_krule *krule) | |
2512 | { | |
2513 | struct audit_field *f; | |
2514 | int i; | |
2515 | ||
2516 | for (i = 0; i < krule->field_count; i++) { | |
2517 | f = &krule->fields[i]; | |
2518 | ||
2519 | if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER) | |
2520 | return 1; | |
2521 | } | |
2522 | ||
2523 | return 0; | |
2524 | } | |
2525 | ||
2526 | /** | |
2527 | * smack_audit_rule_match - Audit given object ? | |
2528 | * @secid: security id for identifying the object to test | |
2529 | * @field: audit rule flags given from user-space | |
2530 | * @op: required testing operator | |
2531 | * @vrule: smack internal rule presentation | |
2532 | * @actx: audit context associated with the check | |
2533 | * | |
2534 | * The core Audit hook. It's used to take the decision of | |
2535 | * whether to audit or not to audit a given object. | |
2536 | */ | |
2537 | static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule, | |
2538 | struct audit_context *actx) | |
2539 | { | |
2540 | char *smack; | |
2541 | char *rule = vrule; | |
2542 | ||
2543 | if (!rule) { | |
2544 | audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR, | |
2545 | "Smack: missing rule\n"); | |
2546 | return -ENOENT; | |
2547 | } | |
2548 | ||
2549 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) | |
2550 | return 0; | |
2551 | ||
2552 | smack = smack_from_secid(secid); | |
2553 | ||
2554 | /* | |
2555 | * No need to do string comparisons. If a match occurs, | |
2556 | * both pointers will point to the same smack_known | |
2557 | * label. | |
2558 | */ | |
2559 | if (op == AUDIT_EQUAL) | |
2560 | return (rule == smack); | |
2561 | if (op == AUDIT_NOT_EQUAL) | |
2562 | return (rule != smack); | |
2563 | ||
2564 | return 0; | |
2565 | } | |
2566 | ||
2567 | /** | |
2568 | * smack_audit_rule_free - free smack rule representation | |
2569 | * @vrule: rule to be freed. | |
2570 | * | |
2571 | * No memory was allocated. | |
2572 | */ | |
2573 | static void smack_audit_rule_free(void *vrule) | |
2574 | { | |
2575 | /* No-op */ | |
2576 | } | |
2577 | ||
2578 | #endif /* CONFIG_AUDIT */ | |
2579 | ||
e114e473 CS |
2580 | /* |
2581 | * smack_secid_to_secctx - return the smack label for a secid | |
2582 | * @secid: incoming integer | |
2583 | * @secdata: destination | |
2584 | * @seclen: how long it is | |
2585 | * | |
2586 | * Exists for networking code. | |
2587 | */ | |
2588 | static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) | |
2589 | { | |
2590 | char *sp = smack_from_secid(secid); | |
2591 | ||
2592 | *secdata = sp; | |
2593 | *seclen = strlen(sp); | |
2594 | return 0; | |
2595 | } | |
2596 | ||
4bc87e62 CS |
2597 | /* |
2598 | * smack_secctx_to_secid - return the secid for a smack label | |
2599 | * @secdata: smack label | |
2600 | * @seclen: how long result is | |
2601 | * @secid: outgoing integer | |
2602 | * | |
2603 | * Exists for audit and networking code. | |
2604 | */ | |
e52c1764 | 2605 | static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) |
4bc87e62 CS |
2606 | { |
2607 | *secid = smack_to_secid(secdata); | |
2608 | return 0; | |
2609 | } | |
2610 | ||
e114e473 CS |
2611 | /* |
2612 | * smack_release_secctx - don't do anything. | |
2613 | * @key_ref: unused | |
2614 | * @context: unused | |
2615 | * @perm: unused | |
2616 | * | |
2617 | * Exists to make sure nothing gets done, and properly | |
2618 | */ | |
2619 | static void smack_release_secctx(char *secdata, u32 seclen) | |
2620 | { | |
2621 | } | |
2622 | ||
076c54c5 AD |
2623 | struct security_operations smack_ops = { |
2624 | .name = "smack", | |
2625 | ||
5cd9c58f DH |
2626 | .ptrace_may_access = smack_ptrace_may_access, |
2627 | .ptrace_traceme = smack_ptrace_traceme, | |
e114e473 | 2628 | .capget = cap_capget, |
d84f4f99 | 2629 | .capset = cap_capset, |
e114e473 CS |
2630 | .capable = cap_capable, |
2631 | .syslog = smack_syslog, | |
2632 | .settime = cap_settime, | |
2633 | .vm_enough_memory = cap_vm_enough_memory, | |
2634 | ||
a6f76f23 | 2635 | .bprm_set_creds = cap_bprm_set_creds, |
e114e473 CS |
2636 | .bprm_secureexec = cap_bprm_secureexec, |
2637 | ||
2638 | .sb_alloc_security = smack_sb_alloc_security, | |
2639 | .sb_free_security = smack_sb_free_security, | |
2640 | .sb_copy_data = smack_sb_copy_data, | |
2641 | .sb_kern_mount = smack_sb_kern_mount, | |
2642 | .sb_statfs = smack_sb_statfs, | |
2643 | .sb_mount = smack_sb_mount, | |
2644 | .sb_umount = smack_sb_umount, | |
2645 | ||
2646 | .inode_alloc_security = smack_inode_alloc_security, | |
2647 | .inode_free_security = smack_inode_free_security, | |
2648 | .inode_init_security = smack_inode_init_security, | |
2649 | .inode_link = smack_inode_link, | |
2650 | .inode_unlink = smack_inode_unlink, | |
2651 | .inode_rmdir = smack_inode_rmdir, | |
2652 | .inode_rename = smack_inode_rename, | |
2653 | .inode_permission = smack_inode_permission, | |
2654 | .inode_setattr = smack_inode_setattr, | |
2655 | .inode_getattr = smack_inode_getattr, | |
2656 | .inode_setxattr = smack_inode_setxattr, | |
2657 | .inode_post_setxattr = smack_inode_post_setxattr, | |
2658 | .inode_getxattr = smack_inode_getxattr, | |
2659 | .inode_removexattr = smack_inode_removexattr, | |
bcdca225 CS |
2660 | .inode_need_killpriv = cap_inode_need_killpriv, |
2661 | .inode_killpriv = cap_inode_killpriv, | |
e114e473 CS |
2662 | .inode_getsecurity = smack_inode_getsecurity, |
2663 | .inode_setsecurity = smack_inode_setsecurity, | |
2664 | .inode_listsecurity = smack_inode_listsecurity, | |
d20bdda6 | 2665 | .inode_getsecid = smack_inode_getsecid, |
e114e473 CS |
2666 | |
2667 | .file_permission = smack_file_permission, | |
2668 | .file_alloc_security = smack_file_alloc_security, | |
2669 | .file_free_security = smack_file_free_security, | |
2670 | .file_ioctl = smack_file_ioctl, | |
2671 | .file_lock = smack_file_lock, | |
2672 | .file_fcntl = smack_file_fcntl, | |
2673 | .file_set_fowner = smack_file_set_fowner, | |
2674 | .file_send_sigiotask = smack_file_send_sigiotask, | |
2675 | .file_receive = smack_file_receive, | |
2676 | ||
f1752eec | 2677 | .cred_free = smack_cred_free, |
d84f4f99 DH |
2678 | .cred_prepare = smack_cred_prepare, |
2679 | .cred_commit = smack_cred_commit, | |
3a3b7ce9 DH |
2680 | .kernel_act_as = smack_kernel_act_as, |
2681 | .kernel_create_files_as = smack_kernel_create_files_as, | |
d84f4f99 | 2682 | .task_fix_setuid = cap_task_fix_setuid, |
e114e473 CS |
2683 | .task_setpgid = smack_task_setpgid, |
2684 | .task_getpgid = smack_task_getpgid, | |
2685 | .task_getsid = smack_task_getsid, | |
2686 | .task_getsecid = smack_task_getsecid, | |
2687 | .task_setnice = smack_task_setnice, | |
2688 | .task_setioprio = smack_task_setioprio, | |
2689 | .task_getioprio = smack_task_getioprio, | |
2690 | .task_setscheduler = smack_task_setscheduler, | |
2691 | .task_getscheduler = smack_task_getscheduler, | |
2692 | .task_movememory = smack_task_movememory, | |
2693 | .task_kill = smack_task_kill, | |
2694 | .task_wait = smack_task_wait, | |
e114e473 | 2695 | .task_to_inode = smack_task_to_inode, |
1236cc3c | 2696 | .task_prctl = cap_task_prctl, |
e114e473 CS |
2697 | |
2698 | .ipc_permission = smack_ipc_permission, | |
d20bdda6 | 2699 | .ipc_getsecid = smack_ipc_getsecid, |
e114e473 CS |
2700 | |
2701 | .msg_msg_alloc_security = smack_msg_msg_alloc_security, | |
2702 | .msg_msg_free_security = smack_msg_msg_free_security, | |
2703 | ||
2704 | .msg_queue_alloc_security = smack_msg_queue_alloc_security, | |
2705 | .msg_queue_free_security = smack_msg_queue_free_security, | |
2706 | .msg_queue_associate = smack_msg_queue_associate, | |
2707 | .msg_queue_msgctl = smack_msg_queue_msgctl, | |
2708 | .msg_queue_msgsnd = smack_msg_queue_msgsnd, | |
2709 | .msg_queue_msgrcv = smack_msg_queue_msgrcv, | |
2710 | ||
2711 | .shm_alloc_security = smack_shm_alloc_security, | |
2712 | .shm_free_security = smack_shm_free_security, | |
2713 | .shm_associate = smack_shm_associate, | |
2714 | .shm_shmctl = smack_shm_shmctl, | |
2715 | .shm_shmat = smack_shm_shmat, | |
2716 | ||
2717 | .sem_alloc_security = smack_sem_alloc_security, | |
2718 | .sem_free_security = smack_sem_free_security, | |
2719 | .sem_associate = smack_sem_associate, | |
2720 | .sem_semctl = smack_sem_semctl, | |
2721 | .sem_semop = smack_sem_semop, | |
2722 | ||
2723 | .netlink_send = cap_netlink_send, | |
2724 | .netlink_recv = cap_netlink_recv, | |
2725 | ||
2726 | .d_instantiate = smack_d_instantiate, | |
2727 | ||
2728 | .getprocattr = smack_getprocattr, | |
2729 | .setprocattr = smack_setprocattr, | |
2730 | ||
2731 | .unix_stream_connect = smack_unix_stream_connect, | |
2732 | .unix_may_send = smack_unix_may_send, | |
2733 | ||
2734 | .socket_post_create = smack_socket_post_create, | |
2735 | .socket_sock_rcv_skb = smack_socket_sock_rcv_skb, | |
2736 | .socket_getpeersec_stream = smack_socket_getpeersec_stream, | |
2737 | .socket_getpeersec_dgram = smack_socket_getpeersec_dgram, | |
2738 | .sk_alloc_security = smack_sk_alloc_security, | |
2739 | .sk_free_security = smack_sk_free_security, | |
2740 | .sock_graft = smack_sock_graft, | |
2741 | .inet_conn_request = smack_inet_conn_request, | |
d20bdda6 | 2742 | |
e114e473 CS |
2743 | /* key management security hooks */ |
2744 | #ifdef CONFIG_KEYS | |
2745 | .key_alloc = smack_key_alloc, | |
2746 | .key_free = smack_key_free, | |
2747 | .key_permission = smack_key_permission, | |
2748 | #endif /* CONFIG_KEYS */ | |
d20bdda6 AD |
2749 | |
2750 | /* Audit hooks */ | |
2751 | #ifdef CONFIG_AUDIT | |
2752 | .audit_rule_init = smack_audit_rule_init, | |
2753 | .audit_rule_known = smack_audit_rule_known, | |
2754 | .audit_rule_match = smack_audit_rule_match, | |
2755 | .audit_rule_free = smack_audit_rule_free, | |
2756 | #endif /* CONFIG_AUDIT */ | |
2757 | ||
e114e473 | 2758 | .secid_to_secctx = smack_secid_to_secctx, |
4bc87e62 | 2759 | .secctx_to_secid = smack_secctx_to_secid, |
e114e473 CS |
2760 | .release_secctx = smack_release_secctx, |
2761 | }; | |
2762 | ||
2763 | /** | |
2764 | * smack_init - initialize the smack system | |
2765 | * | |
2766 | * Returns 0 | |
2767 | */ | |
2768 | static __init int smack_init(void) | |
2769 | { | |
d84f4f99 DH |
2770 | struct cred *cred; |
2771 | ||
076c54c5 AD |
2772 | if (!security_module_enable(&smack_ops)) |
2773 | return 0; | |
2774 | ||
e114e473 CS |
2775 | printk(KERN_INFO "Smack: Initializing.\n"); |
2776 | ||
2777 | /* | |
2778 | * Set the security state for the initial task. | |
2779 | */ | |
d84f4f99 DH |
2780 | cred = (struct cred *) current->cred; |
2781 | cred->security = &smack_known_floor.smk_known; | |
e114e473 CS |
2782 | |
2783 | /* | |
2784 | * Initialize locks | |
2785 | */ | |
2786 | spin_lock_init(&smack_known_unset.smk_cipsolock); | |
2787 | spin_lock_init(&smack_known_huh.smk_cipsolock); | |
2788 | spin_lock_init(&smack_known_hat.smk_cipsolock); | |
2789 | spin_lock_init(&smack_known_star.smk_cipsolock); | |
2790 | spin_lock_init(&smack_known_floor.smk_cipsolock); | |
2791 | spin_lock_init(&smack_known_invalid.smk_cipsolock); | |
2792 | ||
2793 | /* | |
2794 | * Register with LSM | |
2795 | */ | |
2796 | if (register_security(&smack_ops)) | |
2797 | panic("smack: Unable to register with kernel.\n"); | |
2798 | ||
2799 | return 0; | |
2800 | } | |
2801 | ||
2802 | /* | |
2803 | * Smack requires early initialization in order to label | |
2804 | * all processes and objects when they are created. | |
2805 | */ | |
2806 | security_initcall(smack_init); |