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