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