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