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