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