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