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