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