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