Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b5e95b48 JJ |
2 | /* |
3 | * AppArmor security module | |
4 | * | |
5 | * This file contains AppArmor LSM hooks. | |
6 | * | |
7 | * Copyright (C) 1998-2008 Novell/SUSE | |
8 | * Copyright 2009-2010 Canonical Ltd. | |
b5e95b48 JJ |
9 | */ |
10 | ||
3c4ed7bd | 11 | #include <linux/lsm_hooks.h> |
b5e95b48 JJ |
12 | #include <linux/moduleparam.h> |
13 | #include <linux/mm.h> | |
14 | #include <linux/mman.h> | |
15 | #include <linux/mount.h> | |
16 | #include <linux/namei.h> | |
17 | #include <linux/ptrace.h> | |
18 | #include <linux/ctype.h> | |
19 | #include <linux/sysctl.h> | |
20 | #include <linux/audit.h> | |
3486740a | 21 | #include <linux/user_namespace.h> |
ab9f2115 MG |
22 | #include <linux/netfilter_ipv4.h> |
23 | #include <linux/netfilter_ipv6.h> | |
f4d6b94b | 24 | #include <linux/zstd.h> |
b5e95b48 | 25 | #include <net/sock.h> |
e262e32d | 26 | #include <uapi/linux/mount.h> |
f3b8788c | 27 | #include <uapi/linux/lsm.h> |
b5e95b48 JJ |
28 | |
29 | #include "include/apparmor.h" | |
30 | #include "include/apparmorfs.h" | |
31 | #include "include/audit.h" | |
32 | #include "include/capability.h" | |
d8889d49 | 33 | #include "include/cred.h" |
b5e95b48 JJ |
34 | #include "include/file.h" |
35 | #include "include/ipc.h" | |
56974a6f | 36 | #include "include/net.h" |
b5e95b48 | 37 | #include "include/path.h" |
637f688d | 38 | #include "include/label.h" |
b5e95b48 | 39 | #include "include/policy.h" |
cff281f6 | 40 | #include "include/policy_ns.h" |
b5e95b48 | 41 | #include "include/procattr.h" |
2ea3ffb7 | 42 | #include "include/mount.h" |
c0929212 | 43 | #include "include/secid.h" |
b5e95b48 JJ |
44 | |
45 | /* Flag indicating whether initialization completed */ | |
545de8fe | 46 | int apparmor_initialized; |
b5e95b48 | 47 | |
df323337 SAS |
48 | union aa_buffer { |
49 | struct list_head list; | |
ba808cb5 | 50 | DECLARE_FLEX_ARRAY(char, buffer); |
df323337 | 51 | }; |
d4669f0b | 52 | |
ea9bae12 JJ |
53 | struct aa_local_cache { |
54 | unsigned int hold; | |
55 | unsigned int count; | |
56 | struct list_head head; | |
57 | }; | |
58 | ||
341c1fda JJ |
59 | #define RESERVE_COUNT 2 |
60 | static int reserve_count = RESERVE_COUNT; | |
61 | static int buffer_count; | |
d4669f0b | 62 | |
df323337 SAS |
63 | static LIST_HEAD(aa_global_buffers); |
64 | static DEFINE_SPINLOCK(aa_buffers_lock); | |
ea9bae12 | 65 | static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers); |
d4669f0b | 66 | |
b5e95b48 JJ |
67 | /* |
68 | * LSM hook functions | |
69 | */ | |
70 | ||
71 | /* | |
d9087c49 | 72 | * put the associated labels |
b5e95b48 JJ |
73 | */ |
74 | static void apparmor_cred_free(struct cred *cred) | |
75 | { | |
d9087c49 | 76 | aa_put_label(cred_label(cred)); |
69b5a44a | 77 | set_cred_label(cred, NULL); |
b5e95b48 JJ |
78 | } |
79 | ||
80 | /* | |
81 | * allocate the apparmor part of blank credentials | |
82 | */ | |
83 | static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) | |
84 | { | |
69b5a44a | 85 | set_cred_label(cred, NULL); |
b5e95b48 JJ |
86 | return 0; |
87 | } | |
88 | ||
89 | /* | |
d9087c49 | 90 | * prepare new cred label for modification by prepare_cred block |
b5e95b48 JJ |
91 | */ |
92 | static int apparmor_cred_prepare(struct cred *new, const struct cred *old, | |
93 | gfp_t gfp) | |
94 | { | |
69b5a44a | 95 | set_cred_label(new, aa_get_newest_label(cred_label(old))); |
b5e95b48 JJ |
96 | return 0; |
97 | } | |
98 | ||
99 | /* | |
100 | * transfer the apparmor data to a blank set of creds | |
101 | */ | |
102 | static void apparmor_cred_transfer(struct cred *new, const struct cred *old) | |
103 | { | |
69b5a44a | 104 | set_cred_label(new, aa_get_newest_label(cred_label(old))); |
b5e95b48 JJ |
105 | } |
106 | ||
3b529a76 JJ |
107 | static void apparmor_task_free(struct task_struct *task) |
108 | { | |
109 | ||
110 | aa_free_task_ctx(task_ctx(task)); | |
3b529a76 JJ |
111 | } |
112 | ||
113 | static int apparmor_task_alloc(struct task_struct *task, | |
114 | unsigned long clone_flags) | |
115 | { | |
f4ad8f2c | 116 | struct aa_task_ctx *new = task_ctx(task); |
3b529a76 | 117 | |
de62de59 | 118 | aa_dup_task_ctx(new, task_ctx(current)); |
b5e95b48 | 119 | |
3b529a76 | 120 | return 0; |
b5e95b48 JJ |
121 | } |
122 | ||
123 | static int apparmor_ptrace_access_check(struct task_struct *child, | |
124 | unsigned int mode) | |
125 | { | |
b2d09ae4 | 126 | struct aa_label *tracer, *tracee; |
90c436a6 | 127 | const struct cred *cred; |
b2d09ae4 JJ |
128 | int error; |
129 | ||
90c436a6 JJ |
130 | cred = get_task_cred(child); |
131 | tracee = cred_label(cred); /* ref count on cred */ | |
1f8266ff | 132 | tracer = __begin_current_label_crit_section(); |
90c436a6 | 133 | error = aa_may_ptrace(current_cred(), tracer, cred, tracee, |
338d0be4 JJ |
134 | (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ |
135 | : AA_PTRACE_TRACE); | |
1f8266ff | 136 | __end_current_label_crit_section(tracer); |
90c436a6 | 137 | put_cred(cred); |
b2d09ae4 JJ |
138 | |
139 | return error; | |
b5e95b48 JJ |
140 | } |
141 | ||
142 | static int apparmor_ptrace_traceme(struct task_struct *parent) | |
143 | { | |
b2d09ae4 | 144 | struct aa_label *tracer, *tracee; |
90c436a6 | 145 | const struct cred *cred; |
b2d09ae4 JJ |
146 | int error; |
147 | ||
ca3fde52 | 148 | tracee = __begin_current_label_crit_section(); |
90c436a6 JJ |
149 | cred = get_task_cred(parent); |
150 | tracer = cred_label(cred); /* ref count on cred */ | |
151 | error = aa_may_ptrace(cred, tracer, current_cred(), tracee, | |
152 | AA_PTRACE_TRACE); | |
153 | put_cred(cred); | |
ca3fde52 | 154 | __end_current_label_crit_section(tracee); |
b2d09ae4 JJ |
155 | |
156 | return error; | |
b5e95b48 JJ |
157 | } |
158 | ||
159 | /* Derived from security/commoncap.c:cap_capget */ | |
6672efbb | 160 | static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective, |
b5e95b48 JJ |
161 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
162 | { | |
637f688d | 163 | struct aa_label *label; |
b5e95b48 JJ |
164 | const struct cred *cred; |
165 | ||
166 | rcu_read_lock(); | |
167 | cred = __task_cred(target); | |
637f688d | 168 | label = aa_get_newest_cred_label(cred); |
c70c86c4 | 169 | |
b1d9e6b0 CS |
170 | /* |
171 | * cap_capget is stacked ahead of this and will | |
172 | * initialize effective and permitted. | |
173 | */ | |
c70c86c4 JJ |
174 | if (!unconfined(label)) { |
175 | struct aa_profile *profile; | |
176 | struct label_it i; | |
177 | ||
178 | label_for_each_confined(i, label, profile) { | |
1ad22fcc | 179 | struct aa_ruleset *rules; |
c70c86c4 JJ |
180 | if (COMPLAIN_MODE(profile)) |
181 | continue; | |
1ad22fcc JJ |
182 | rules = list_first_entry(&profile->rules, |
183 | typeof(*rules), list); | |
c70c86c4 | 184 | *effective = cap_intersect(*effective, |
1ad22fcc | 185 | rules->caps.allow); |
c70c86c4 | 186 | *permitted = cap_intersect(*permitted, |
1ad22fcc | 187 | rules->caps.allow); |
c70c86c4 | 188 | } |
b5e95b48 JJ |
189 | } |
190 | rcu_read_unlock(); | |
637f688d | 191 | aa_put_label(label); |
b5e95b48 JJ |
192 | |
193 | return 0; | |
194 | } | |
195 | ||
6a9de491 | 196 | static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, |
c1a85a00 | 197 | int cap, unsigned int opts) |
b5e95b48 | 198 | { |
637f688d | 199 | struct aa_label *label; |
b1d9e6b0 CS |
200 | int error = 0; |
201 | ||
637f688d JJ |
202 | label = aa_get_newest_cred_label(cred); |
203 | if (!unconfined(label)) | |
90c436a6 | 204 | error = aa_capable(cred, label, cap, opts); |
637f688d | 205 | aa_put_label(label); |
cf797c0e | 206 | |
b5e95b48 JJ |
207 | return error; |
208 | } | |
209 | ||
210 | /** | |
211 | * common_perm - basic common permission check wrapper fn for paths | |
212 | * @op: operation being checked | |
213 | * @path: path to check permission of (NOT NULL) | |
214 | * @mask: requested permissions mask | |
215 | * @cond: conditional info for the permission request (NOT NULL) | |
216 | * | |
217 | * Returns: %0 else error code if error or permission denied | |
218 | */ | |
47f6e5cc | 219 | static int common_perm(const char *op, const struct path *path, u32 mask, |
b5e95b48 JJ |
220 | struct path_cond *cond) |
221 | { | |
637f688d | 222 | struct aa_label *label; |
b5e95b48 JJ |
223 | int error = 0; |
224 | ||
637f688d JJ |
225 | label = __begin_current_label_crit_section(); |
226 | if (!unconfined(label)) | |
90c436a6 JJ |
227 | error = aa_path_perm(op, current_cred(), label, path, 0, mask, |
228 | cond); | |
637f688d | 229 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
230 | |
231 | return error; | |
232 | } | |
233 | ||
234 | /** | |
31f75bfe | 235 | * common_perm_cond - common permission wrapper around inode cond |
b5e95b48 | 236 | * @op: operation being checked |
31f75bfe | 237 | * @path: location to check (NOT NULL) |
b5e95b48 | 238 | * @mask: requested permissions mask |
b5e95b48 JJ |
239 | * |
240 | * Returns: %0 else error code if error or permission denied | |
241 | */ | |
31f75bfe | 242 | static int common_perm_cond(const char *op, const struct path *path, u32 mask) |
b5e95b48 | 243 | { |
e67fe633 | 244 | vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt), |
5e26a01e | 245 | d_backing_inode(path->dentry)); |
3cee6079 | 246 | struct path_cond cond = { |
5e26a01e | 247 | vfsuid_into_kuid(vfsuid), |
3cee6079 | 248 | d_backing_inode(path->dentry)->i_mode |
31f75bfe | 249 | }; |
b5e95b48 | 250 | |
31f75bfe JJ |
251 | if (!path_mediated_fs(path->dentry)) |
252 | return 0; | |
253 | ||
254 | return common_perm(op, path, mask, &cond); | |
b5e95b48 JJ |
255 | } |
256 | ||
257 | /** | |
31f75bfe | 258 | * common_perm_dir_dentry - common permission wrapper when path is dir, dentry |
b5e95b48 | 259 | * @op: operation being checked |
31f75bfe JJ |
260 | * @dir: directory of the dentry (NOT NULL) |
261 | * @dentry: dentry to check (NOT NULL) | |
b5e95b48 | 262 | * @mask: requested permissions mask |
31f75bfe | 263 | * @cond: conditional info for the permission request (NOT NULL) |
b5e95b48 JJ |
264 | * |
265 | * Returns: %0 else error code if error or permission denied | |
266 | */ | |
31f75bfe JJ |
267 | static int common_perm_dir_dentry(const char *op, const struct path *dir, |
268 | struct dentry *dentry, u32 mask, | |
269 | struct path_cond *cond) | |
b5e95b48 | 270 | { |
31f75bfe | 271 | struct path path = { .mnt = dir->mnt, .dentry = dentry }; |
b5e95b48 | 272 | |
31f75bfe | 273 | return common_perm(op, &path, mask, cond); |
b5e95b48 JJ |
274 | } |
275 | ||
276 | /** | |
277 | * common_perm_rm - common permission wrapper for operations doing rm | |
278 | * @op: operation being checked | |
279 | * @dir: directory that the dentry is in (NOT NULL) | |
280 | * @dentry: dentry being rm'd (NOT NULL) | |
281 | * @mask: requested permission mask | |
282 | * | |
283 | * Returns: %0 else error code if error or permission denied | |
284 | */ | |
47f6e5cc | 285 | static int common_perm_rm(const char *op, const struct path *dir, |
b5e95b48 JJ |
286 | struct dentry *dentry, u32 mask) |
287 | { | |
c6f493d6 | 288 | struct inode *inode = d_backing_inode(dentry); |
b5e95b48 | 289 | struct path_cond cond = { }; |
5e26a01e | 290 | vfsuid_t vfsuid; |
b5e95b48 | 291 | |
efeee83a | 292 | if (!inode || !path_mediated_fs(dentry)) |
b5e95b48 JJ |
293 | return 0; |
294 | ||
e67fe633 | 295 | vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode); |
5e26a01e | 296 | cond.uid = vfsuid_into_kuid(vfsuid); |
b5e95b48 JJ |
297 | cond.mode = inode->i_mode; |
298 | ||
299 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); | |
300 | } | |
301 | ||
302 | /** | |
303 | * common_perm_create - common permission wrapper for operations doing create | |
304 | * @op: operation being checked | |
305 | * @dir: directory that dentry will be created in (NOT NULL) | |
306 | * @dentry: dentry to create (NOT NULL) | |
307 | * @mask: request permission mask | |
308 | * @mode: created file mode | |
309 | * | |
310 | * Returns: %0 else error code if error or permission denied | |
311 | */ | |
47f6e5cc | 312 | static int common_perm_create(const char *op, const struct path *dir, |
d6b49f7a | 313 | struct dentry *dentry, u32 mask, umode_t mode) |
b5e95b48 JJ |
314 | { |
315 | struct path_cond cond = { current_fsuid(), mode }; | |
316 | ||
efeee83a | 317 | if (!path_mediated_fs(dir->dentry)) |
b5e95b48 JJ |
318 | return 0; |
319 | ||
320 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); | |
321 | } | |
322 | ||
989f74e0 | 323 | static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) |
b5e95b48 JJ |
324 | { |
325 | return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); | |
326 | } | |
327 | ||
d3607752 | 328 | static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, |
4572befe | 329 | umode_t mode) |
b5e95b48 JJ |
330 | { |
331 | return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, | |
332 | S_IFDIR); | |
333 | } | |
334 | ||
989f74e0 | 335 | static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) |
b5e95b48 JJ |
336 | { |
337 | return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); | |
338 | } | |
339 | ||
d3607752 | 340 | static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, |
04fc66e7 | 341 | umode_t mode, unsigned int dev) |
b5e95b48 JJ |
342 | { |
343 | return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); | |
344 | } | |
345 | ||
81f4c506 | 346 | static int apparmor_path_truncate(const struct path *path) |
b5e95b48 | 347 | { |
e53cfe6c | 348 | return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); |
b5e95b48 JJ |
349 | } |
350 | ||
3350607d GN |
351 | static int apparmor_file_truncate(struct file *file) |
352 | { | |
353 | return apparmor_path_truncate(&file->f_path); | |
354 | } | |
355 | ||
d3607752 | 356 | static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, |
b5e95b48 JJ |
357 | const char *old_name) |
358 | { | |
359 | return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, | |
360 | S_IFLNK); | |
361 | } | |
362 | ||
3ccee46a | 363 | static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, |
b5e95b48 JJ |
364 | struct dentry *new_dentry) |
365 | { | |
637f688d | 366 | struct aa_label *label; |
b5e95b48 JJ |
367 | int error = 0; |
368 | ||
efeee83a | 369 | if (!path_mediated_fs(old_dentry)) |
b5e95b48 JJ |
370 | return 0; |
371 | ||
637f688d JJ |
372 | label = begin_current_label_crit_section(); |
373 | if (!unconfined(label)) | |
90c436a6 JJ |
374 | error = aa_path_link(current_cred(), label, old_dentry, new_dir, |
375 | new_dentry); | |
637f688d | 376 | end_current_label_crit_section(label); |
cf797c0e | 377 | |
b5e95b48 JJ |
378 | return error; |
379 | } | |
380 | ||
3ccee46a | 381 | static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, |
100f59d9 MS |
382 | const struct path *new_dir, struct dentry *new_dentry, |
383 | const unsigned int flags) | |
b5e95b48 | 384 | { |
637f688d | 385 | struct aa_label *label; |
b5e95b48 JJ |
386 | int error = 0; |
387 | ||
efeee83a | 388 | if (!path_mediated_fs(old_dentry)) |
b5e95b48 | 389 | return 0; |
100f59d9 MS |
390 | if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry)) |
391 | return 0; | |
b5e95b48 | 392 | |
637f688d JJ |
393 | label = begin_current_label_crit_section(); |
394 | if (!unconfined(label)) { | |
e67fe633 | 395 | struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt); |
5e26a01e | 396 | vfsuid_t vfsuid; |
8486adf0 KC |
397 | struct path old_path = { .mnt = old_dir->mnt, |
398 | .dentry = old_dentry }; | |
399 | struct path new_path = { .mnt = new_dir->mnt, | |
400 | .dentry = new_dentry }; | |
3cee6079 | 401 | struct path_cond cond = { |
5e26a01e | 402 | .mode = d_backing_inode(old_dentry)->i_mode |
b5e95b48 | 403 | }; |
e67fe633 | 404 | vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry)); |
5e26a01e | 405 | cond.uid = vfsuid_into_kuid(vfsuid); |
b5e95b48 | 406 | |
100f59d9 MS |
407 | if (flags & RENAME_EXCHANGE) { |
408 | struct path_cond cond_exchange = { | |
5e26a01e | 409 | .mode = d_backing_inode(new_dentry)->i_mode, |
100f59d9 | 410 | }; |
e67fe633 | 411 | vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry)); |
5e26a01e | 412 | cond_exchange.uid = vfsuid_into_kuid(vfsuid); |
100f59d9 | 413 | |
90c436a6 JJ |
414 | error = aa_path_perm(OP_RENAME_SRC, current_cred(), |
415 | label, &new_path, 0, | |
100f59d9 MS |
416 | MAY_READ | AA_MAY_GETATTR | MAY_WRITE | |
417 | AA_MAY_SETATTR | AA_MAY_DELETE, | |
418 | &cond_exchange); | |
419 | if (!error) | |
90c436a6 JJ |
420 | error = aa_path_perm(OP_RENAME_DEST, current_cred(), |
421 | label, &old_path, | |
100f59d9 MS |
422 | 0, MAY_WRITE | AA_MAY_SETATTR | |
423 | AA_MAY_CREATE, &cond_exchange); | |
424 | } | |
425 | ||
426 | if (!error) | |
90c436a6 JJ |
427 | error = aa_path_perm(OP_RENAME_SRC, current_cred(), |
428 | label, &old_path, 0, | |
100f59d9 MS |
429 | MAY_READ | AA_MAY_GETATTR | MAY_WRITE | |
430 | AA_MAY_SETATTR | AA_MAY_DELETE, | |
431 | &cond); | |
b5e95b48 | 432 | if (!error) |
90c436a6 JJ |
433 | error = aa_path_perm(OP_RENAME_DEST, current_cred(), |
434 | label, &new_path, | |
e53cfe6c | 435 | 0, MAY_WRITE | AA_MAY_SETATTR | |
b5e95b48 JJ |
436 | AA_MAY_CREATE, &cond); |
437 | ||
438 | } | |
637f688d | 439 | end_current_label_crit_section(label); |
cf797c0e | 440 | |
b5e95b48 JJ |
441 | return error; |
442 | } | |
443 | ||
be01f9f2 | 444 | static int apparmor_path_chmod(const struct path *path, umode_t mode) |
b5e95b48 | 445 | { |
31f75bfe | 446 | return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); |
b5e95b48 JJ |
447 | } |
448 | ||
7fd25dac | 449 | static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
b5e95b48 | 450 | { |
31f75bfe | 451 | return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); |
b5e95b48 JJ |
452 | } |
453 | ||
3f7036a0 | 454 | static int apparmor_inode_getattr(const struct path *path) |
b5e95b48 | 455 | { |
e53cfe6c | 456 | return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); |
b5e95b48 JJ |
457 | } |
458 | ||
94817692 | 459 | static int apparmor_file_open(struct file *file) |
b5e95b48 | 460 | { |
637f688d JJ |
461 | struct aa_file_ctx *fctx = file_ctx(file); |
462 | struct aa_label *label; | |
b5e95b48 | 463 | int error = 0; |
f4fee216 | 464 | bool needput; |
b5e95b48 | 465 | |
efeee83a | 466 | if (!path_mediated_fs(file->f_path.dentry)) |
b5e95b48 JJ |
467 | return 0; |
468 | ||
469 | /* If in exec, permission is handled by bprm hooks. | |
470 | * Cache permissions granted by the previous exec check, with | |
471 | * implicit read and executable mmap which are required to | |
472 | * actually execute the image. | |
4759ff71 KC |
473 | * |
474 | * Illogically, FMODE_EXEC is in f_flags, not f_mode. | |
b5e95b48 | 475 | */ |
4759ff71 | 476 | if (file->f_flags & __FMODE_EXEC) { |
55a26ebf | 477 | fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; |
b5e95b48 JJ |
478 | return 0; |
479 | } | |
480 | ||
f4fee216 | 481 | label = aa_get_newest_cred_label_condref(file->f_cred, &needput); |
637f688d | 482 | if (!unconfined(label)) { |
e67fe633 | 483 | struct mnt_idmap *idmap = file_mnt_idmap(file); |
496ad9aa | 484 | struct inode *inode = file_inode(file); |
5e26a01e | 485 | vfsuid_t vfsuid; |
3cee6079 | 486 | struct path_cond cond = { |
5e26a01e | 487 | .mode = inode->i_mode, |
3cee6079 | 488 | }; |
e67fe633 | 489 | vfsuid = i_uid_into_vfsuid(idmap, inode); |
5e26a01e | 490 | cond.uid = vfsuid_into_kuid(vfsuid); |
b5e95b48 | 491 | |
90c436a6 JJ |
492 | error = aa_path_perm(OP_OPEN, file->f_cred, |
493 | label, &file->f_path, 0, | |
b5e95b48 JJ |
494 | aa_map_file_to_perms(file), &cond); |
495 | /* todo cache full allowed permissions set and state */ | |
55a26ebf | 496 | fctx->allow = aa_map_file_to_perms(file); |
b5e95b48 | 497 | } |
f4fee216 | 498 | aa_put_label_condref(label, needput); |
b5e95b48 JJ |
499 | |
500 | return error; | |
501 | } | |
502 | ||
503 | static int apparmor_file_alloc_security(struct file *file) | |
504 | { | |
33bf60ca | 505 | struct aa_file_ctx *ctx = file_ctx(file); |
637f688d | 506 | struct aa_label *label = begin_current_label_crit_section(); |
b5e95b48 | 507 | |
33bf60ca CS |
508 | spin_lock_init(&ctx->lock); |
509 | rcu_assign_pointer(ctx->label, aa_get_label(label)); | |
510 | end_current_label_crit_section(label); | |
511 | return 0; | |
b5e95b48 JJ |
512 | } |
513 | ||
514 | static void apparmor_file_free_security(struct file *file) | |
515 | { | |
33bf60ca CS |
516 | struct aa_file_ctx *ctx = file_ctx(file); |
517 | ||
518 | if (ctx) | |
519 | aa_put_label(rcu_access_pointer(ctx->label)); | |
b5e95b48 JJ |
520 | } |
521 | ||
341c1fda JJ |
522 | static int common_file_perm(const char *op, struct file *file, u32 mask, |
523 | bool in_atomic) | |
b5e95b48 | 524 | { |
190a9518 | 525 | struct aa_label *label; |
b5e95b48 JJ |
526 | int error = 0; |
527 | ||
192ca6b5 JJ |
528 | /* don't reaudit files closed during inheritance */ |
529 | if (file->f_path.dentry == aa_null.dentry) | |
530 | return -EACCES; | |
531 | ||
637f688d | 532 | label = __begin_current_label_crit_section(); |
90c436a6 | 533 | error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic); |
637f688d | 534 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
535 | |
536 | return error; | |
537 | } | |
538 | ||
064dc947 JJ |
539 | static int apparmor_file_receive(struct file *file) |
540 | { | |
341c1fda JJ |
541 | return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file), |
542 | false); | |
064dc947 JJ |
543 | } |
544 | ||
b5e95b48 JJ |
545 | static int apparmor_file_permission(struct file *file, int mask) |
546 | { | |
341c1fda | 547 | return common_file_perm(OP_FPERM, file, mask, false); |
b5e95b48 JJ |
548 | } |
549 | ||
550 | static int apparmor_file_lock(struct file *file, unsigned int cmd) | |
551 | { | |
552 | u32 mask = AA_MAY_LOCK; | |
553 | ||
554 | if (cmd == F_WRLCK) | |
555 | mask |= MAY_WRITE; | |
556 | ||
341c1fda | 557 | return common_file_perm(OP_FLOCK, file, mask, false); |
b5e95b48 JJ |
558 | } |
559 | ||
47f6e5cc | 560 | static int common_mmap(const char *op, struct file *file, unsigned long prot, |
341c1fda | 561 | unsigned long flags, bool in_atomic) |
b5e95b48 | 562 | { |
b5e95b48 JJ |
563 | int mask = 0; |
564 | ||
637f688d | 565 | if (!file || !file_ctx(file)) |
b5e95b48 JJ |
566 | return 0; |
567 | ||
568 | if (prot & PROT_READ) | |
569 | mask |= MAY_READ; | |
570 | /* | |
571 | * Private mappings don't require write perms since they don't | |
572 | * write back to the files | |
573 | */ | |
574 | if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) | |
575 | mask |= MAY_WRITE; | |
576 | if (prot & PROT_EXEC) | |
577 | mask |= AA_EXEC_MMAP; | |
578 | ||
341c1fda | 579 | return common_file_perm(op, file, mask, in_atomic); |
b5e95b48 JJ |
580 | } |
581 | ||
e5467859 AV |
582 | static int apparmor_mmap_file(struct file *file, unsigned long reqprot, |
583 | unsigned long prot, unsigned long flags) | |
b5e95b48 | 584 | { |
341c1fda | 585 | return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC); |
b5e95b48 JJ |
586 | } |
587 | ||
588 | static int apparmor_file_mprotect(struct vm_area_struct *vma, | |
589 | unsigned long reqprot, unsigned long prot) | |
590 | { | |
591 | return common_mmap(OP_FMPROT, vma->vm_file, prot, | |
341c1fda JJ |
592 | !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0, |
593 | false); | |
b5e95b48 JJ |
594 | } |
595 | ||
c4371d90 GG |
596 | #ifdef CONFIG_IO_URING |
597 | static const char *audit_uring_mask(u32 mask) | |
598 | { | |
599 | if (mask & AA_MAY_CREATE_SQPOLL) | |
600 | return "sqpoll"; | |
601 | if (mask & AA_MAY_OVERRIDE_CRED) | |
602 | return "override_creds"; | |
603 | return ""; | |
604 | } | |
605 | ||
606 | static void audit_uring_cb(struct audit_buffer *ab, void *va) | |
607 | { | |
608 | struct apparmor_audit_data *ad = aad_of_va(va); | |
609 | ||
610 | if (ad->request & AA_URING_PERM_MASK) { | |
611 | audit_log_format(ab, " requested=\"%s\"", | |
612 | audit_uring_mask(ad->request)); | |
613 | if (ad->denied & AA_URING_PERM_MASK) { | |
614 | audit_log_format(ab, " denied=\"%s\"", | |
615 | audit_uring_mask(ad->denied)); | |
616 | } | |
617 | } | |
618 | if (ad->uring.target) { | |
619 | audit_log_format(ab, " tcontext="); | |
620 | aa_label_xaudit(ab, labels_ns(ad->subj_label), | |
621 | ad->uring.target, | |
622 | FLAGS_NONE, GFP_ATOMIC); | |
623 | } | |
624 | } | |
625 | ||
626 | static int profile_uring(struct aa_profile *profile, u32 request, | |
627 | struct aa_label *new, int cap, | |
628 | struct apparmor_audit_data *ad) | |
629 | { | |
630 | unsigned int state; | |
631 | struct aa_ruleset *rules; | |
632 | int error = 0; | |
633 | ||
634 | AA_BUG(!profile); | |
635 | ||
636 | rules = list_first_entry(&profile->rules, typeof(*rules), list); | |
637 | state = RULE_MEDIATES(rules, AA_CLASS_IO_URING); | |
638 | if (state) { | |
639 | struct aa_perms perms = { }; | |
640 | ||
641 | if (new) { | |
642 | aa_label_match(profile, rules, new, state, | |
643 | false, request, &perms); | |
644 | } else { | |
645 | perms = *aa_lookup_perms(rules->policy, state); | |
646 | } | |
647 | aa_apply_modes_to_perms(profile, &perms); | |
648 | error = aa_check_perms(profile, &perms, request, ad, | |
649 | audit_uring_cb); | |
650 | } | |
651 | ||
652 | return error; | |
653 | } | |
654 | ||
655 | /** | |
656 | * apparmor_uring_override_creds - check the requested cred override | |
657 | * @new: the target creds | |
658 | * | |
659 | * Check to see if the current task is allowed to override it's credentials | |
660 | * to service an io_uring operation. | |
661 | */ | |
7060d3cc | 662 | static int apparmor_uring_override_creds(const struct cred *new) |
c4371d90 GG |
663 | { |
664 | struct aa_profile *profile; | |
665 | struct aa_label *label; | |
666 | int error; | |
667 | DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING, | |
668 | OP_URING_OVERRIDE); | |
669 | ||
670 | ad.uring.target = cred_label(new); | |
671 | label = __begin_current_label_crit_section(); | |
672 | error = fn_for_each(label, profile, | |
673 | profile_uring(profile, AA_MAY_OVERRIDE_CRED, | |
674 | cred_label(new), CAP_SYS_ADMIN, &ad)); | |
675 | __end_current_label_crit_section(label); | |
676 | ||
677 | return error; | |
678 | } | |
679 | ||
680 | /** | |
681 | * apparmor_uring_sqpoll - check if a io_uring polling thread can be created | |
682 | * | |
683 | * Check to see if the current task is allowed to create a new io_uring | |
684 | * kernel polling thread. | |
685 | */ | |
7060d3cc | 686 | static int apparmor_uring_sqpoll(void) |
c4371d90 GG |
687 | { |
688 | struct aa_profile *profile; | |
689 | struct aa_label *label; | |
690 | int error; | |
691 | DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING, | |
692 | OP_URING_SQPOLL); | |
693 | ||
694 | label = __begin_current_label_crit_section(); | |
695 | error = fn_for_each(label, profile, | |
696 | profile_uring(profile, AA_MAY_CREATE_SQPOLL, | |
697 | NULL, CAP_SYS_ADMIN, &ad)); | |
698 | __end_current_label_crit_section(label); | |
699 | ||
700 | return error; | |
701 | } | |
702 | #endif /* CONFIG_IO_URING */ | |
703 | ||
2ea3ffb7 JJ |
704 | static int apparmor_sb_mount(const char *dev_name, const struct path *path, |
705 | const char *type, unsigned long flags, void *data) | |
706 | { | |
707 | struct aa_label *label; | |
708 | int error = 0; | |
709 | ||
710 | /* Discard magic */ | |
711 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) | |
712 | flags &= ~MS_MGC_MSK; | |
713 | ||
714 | flags &= ~AA_MS_IGNORE_MASK; | |
715 | ||
716 | label = __begin_current_label_crit_section(); | |
717 | if (!unconfined(label)) { | |
718 | if (flags & MS_REMOUNT) | |
90c436a6 JJ |
719 | error = aa_remount(current_cred(), label, path, flags, |
720 | data); | |
2ea3ffb7 | 721 | else if (flags & MS_BIND) |
90c436a6 JJ |
722 | error = aa_bind_mount(current_cred(), label, path, |
723 | dev_name, flags); | |
2ea3ffb7 JJ |
724 | else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | |
725 | MS_UNBINDABLE)) | |
90c436a6 JJ |
726 | error = aa_mount_change_type(current_cred(), label, |
727 | path, flags); | |
2ea3ffb7 | 728 | else if (flags & MS_MOVE) |
157a3537 JJ |
729 | error = aa_move_mount_old(current_cred(), label, path, |
730 | dev_name); | |
2ea3ffb7 | 731 | else |
90c436a6 JJ |
732 | error = aa_new_mount(current_cred(), label, dev_name, |
733 | path, type, flags, data); | |
2ea3ffb7 JJ |
734 | } |
735 | __end_current_label_crit_section(label); | |
736 | ||
737 | return error; | |
738 | } | |
739 | ||
157a3537 JJ |
740 | static int apparmor_move_mount(const struct path *from_path, |
741 | const struct path *to_path) | |
742 | { | |
743 | struct aa_label *label; | |
744 | int error = 0; | |
745 | ||
746 | label = __begin_current_label_crit_section(); | |
747 | if (!unconfined(label)) | |
748 | error = aa_move_mount(current_cred(), label, from_path, | |
749 | to_path); | |
750 | __end_current_label_crit_section(label); | |
751 | ||
752 | return error; | |
753 | } | |
754 | ||
2ea3ffb7 JJ |
755 | static int apparmor_sb_umount(struct vfsmount *mnt, int flags) |
756 | { | |
757 | struct aa_label *label; | |
758 | int error = 0; | |
759 | ||
760 | label = __begin_current_label_crit_section(); | |
761 | if (!unconfined(label)) | |
90c436a6 | 762 | error = aa_umount(current_cred(), label, mnt, flags); |
2ea3ffb7 JJ |
763 | __end_current_label_crit_section(label); |
764 | ||
765 | return error; | |
766 | } | |
767 | ||
768 | static int apparmor_sb_pivotroot(const struct path *old_path, | |
769 | const struct path *new_path) | |
770 | { | |
771 | struct aa_label *label; | |
772 | int error = 0; | |
773 | ||
774 | label = aa_get_current_label(); | |
775 | if (!unconfined(label)) | |
90c436a6 | 776 | error = aa_pivotroot(current_cred(), label, old_path, new_path); |
2ea3ffb7 JJ |
777 | aa_put_label(label); |
778 | ||
779 | return error; | |
780 | } | |
781 | ||
223981db | 782 | static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx, |
a5a858f6 | 783 | u32 *size, u32 flags) |
223981db CS |
784 | { |
785 | int error = -ENOENT; | |
786 | struct aa_task_ctx *ctx = task_ctx(current); | |
787 | struct aa_label *label = NULL; | |
6d2fb472 | 788 | char *value = NULL; |
223981db CS |
789 | |
790 | switch (attr) { | |
791 | case LSM_ATTR_CURRENT: | |
792 | label = aa_get_newest_label(cred_label(current_cred())); | |
793 | break; | |
794 | case LSM_ATTR_PREV: | |
795 | if (ctx->previous) | |
796 | label = aa_get_newest_label(ctx->previous); | |
797 | break; | |
798 | case LSM_ATTR_EXEC: | |
799 | if (ctx->onexec) | |
800 | label = aa_get_newest_label(ctx->onexec); | |
801 | break; | |
802 | default: | |
803 | error = -EOPNOTSUPP; | |
804 | break; | |
805 | } | |
806 | ||
807 | if (label) { | |
808 | error = aa_getprocattr(label, &value, false); | |
d7cf3412 PM |
809 | if (error > 0) |
810 | error = lsm_fill_user_ctx(lx, size, value, error, | |
811 | LSM_ID_APPARMOR, 0); | |
223981db CS |
812 | kfree(value); |
813 | } | |
814 | ||
815 | aa_put_label(label); | |
816 | ||
223981db CS |
817 | if (error < 0) |
818 | return error; | |
819 | return 1; | |
820 | } | |
821 | ||
c8e477c6 | 822 | static int apparmor_getprocattr(struct task_struct *task, const char *name, |
b5e95b48 JJ |
823 | char **value) |
824 | { | |
825 | int error = -ENOENT; | |
b5e95b48 JJ |
826 | /* released below */ |
827 | const struct cred *cred = get_task_cred(task); | |
de62de59 | 828 | struct aa_task_ctx *ctx = task_ctx(current); |
637f688d | 829 | struct aa_label *label = NULL; |
b5e95b48 JJ |
830 | |
831 | if (strcmp(name, "current") == 0) | |
d9087c49 | 832 | label = aa_get_newest_label(cred_label(cred)); |
55a26ebf | 833 | else if (strcmp(name, "prev") == 0 && ctx->previous) |
637f688d | 834 | label = aa_get_newest_label(ctx->previous); |
55a26ebf | 835 | else if (strcmp(name, "exec") == 0 && ctx->onexec) |
637f688d | 836 | label = aa_get_newest_label(ctx->onexec); |
b5e95b48 JJ |
837 | else |
838 | error = -EINVAL; | |
839 | ||
637f688d | 840 | if (label) |
223981db | 841 | error = aa_getprocattr(label, value, true); |
77b071b3 | 842 | |
637f688d | 843 | aa_put_label(label); |
b5e95b48 JJ |
844 | put_cred(cred); |
845 | ||
846 | return error; | |
847 | } | |
848 | ||
223981db | 849 | static int do_setattr(u64 attr, void *value, size_t size) |
b5e95b48 | 850 | { |
e89b8081 | 851 | char *command, *largs = NULL, *args = value; |
b5e95b48 JJ |
852 | size_t arg_size; |
853 | int error; | |
bd7bd201 | 854 | DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, |
8c4b785a | 855 | OP_SETPROCATTR); |
b5e95b48 JJ |
856 | |
857 | if (size == 0) | |
858 | return -EINVAL; | |
b5e95b48 | 859 | |
e89b8081 VN |
860 | /* AppArmor requires that the buffer must be null terminated atm */ |
861 | if (args[size - 1] != '\0') { | |
862 | /* null terminate */ | |
863 | largs = args = kmalloc(size + 1, GFP_KERNEL); | |
864 | if (!args) | |
865 | return -ENOMEM; | |
866 | memcpy(args, value, size); | |
867 | args[size] = '\0'; | |
868 | } | |
869 | ||
870 | error = -EINVAL; | |
b5e95b48 JJ |
871 | args = strim(args); |
872 | command = strsep(&args, " "); | |
873 | if (!args) | |
e89b8081 | 874 | goto out; |
b5e95b48 JJ |
875 | args = skip_spaces(args); |
876 | if (!*args) | |
e89b8081 | 877 | goto out; |
b5e95b48 | 878 | |
d4d03f74 | 879 | arg_size = size - (args - (largs ? largs : (char *) value)); |
223981db | 880 | if (attr == LSM_ATTR_CURRENT) { |
b5e95b48 JJ |
881 | if (strcmp(command, "changehat") == 0) { |
882 | error = aa_setprocattr_changehat(args, arg_size, | |
df8073c6 | 883 | AA_CHANGE_NOFLAGS); |
b5e95b48 JJ |
884 | } else if (strcmp(command, "permhat") == 0) { |
885 | error = aa_setprocattr_changehat(args, arg_size, | |
df8073c6 | 886 | AA_CHANGE_TEST); |
b5e95b48 | 887 | } else if (strcmp(command, "changeprofile") == 0) { |
df8073c6 | 888 | error = aa_change_profile(args, AA_CHANGE_NOFLAGS); |
b5e95b48 | 889 | } else if (strcmp(command, "permprofile") == 0) { |
df8073c6 | 890 | error = aa_change_profile(args, AA_CHANGE_TEST); |
6c5fc8f1 JJ |
891 | } else if (strcmp(command, "stack") == 0) { |
892 | error = aa_change_profile(args, AA_CHANGE_STACK); | |
3eea57c2 JJ |
893 | } else |
894 | goto fail; | |
223981db | 895 | } else if (attr == LSM_ATTR_EXEC) { |
3eea57c2 | 896 | if (strcmp(command, "exec") == 0) |
df8073c6 | 897 | error = aa_change_profile(args, AA_CHANGE_ONEXEC); |
6c5fc8f1 JJ |
898 | else if (strcmp(command, "stack") == 0) |
899 | error = aa_change_profile(args, (AA_CHANGE_ONEXEC | | |
900 | AA_CHANGE_STACK)); | |
3eea57c2 JJ |
901 | else |
902 | goto fail; | |
903 | } else | |
b5e95b48 | 904 | /* only support the "current" and "exec" process attributes */ |
e89b8081 | 905 | goto fail; |
3eea57c2 | 906 | |
b5e95b48 JJ |
907 | if (!error) |
908 | error = size; | |
e89b8081 VN |
909 | out: |
910 | kfree(largs); | |
b5e95b48 | 911 | return error; |
3eea57c2 JJ |
912 | |
913 | fail: | |
d20f5a1a | 914 | ad.subj_label = begin_current_label_crit_section(); |
223981db CS |
915 | if (attr == LSM_ATTR_CURRENT) |
916 | ad.info = "current"; | |
917 | else if (attr == LSM_ATTR_EXEC) | |
918 | ad.info = "exec"; | |
919 | else | |
920 | ad.info = "invalid"; | |
bd7bd201 JJ |
921 | ad.error = error = -EINVAL; |
922 | aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL); | |
d20f5a1a | 923 | end_current_label_crit_section(ad.subj_label); |
e89b8081 | 924 | goto out; |
b5e95b48 JJ |
925 | } |
926 | ||
223981db | 927 | static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx, |
a5a858f6 | 928 | u32 size, u32 flags) |
223981db CS |
929 | { |
930 | int rc; | |
931 | ||
932 | if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC) | |
933 | return -EOPNOTSUPP; | |
934 | ||
935 | rc = do_setattr(attr, ctx->ctx, ctx->ctx_len); | |
936 | if (rc > 0) | |
937 | return 0; | |
938 | return rc; | |
939 | } | |
940 | ||
941 | static int apparmor_setprocattr(const char *name, void *value, | |
942 | size_t size) | |
943 | { | |
944 | int attr = lsm_name_to_attr(name); | |
945 | ||
946 | if (attr) | |
947 | return do_setattr(attr, value, size); | |
948 | return -EINVAL; | |
949 | } | |
950 | ||
fe864821 JJ |
951 | /** |
952 | * apparmor_bprm_committing_creds - do task cleanup on committing new creds | |
953 | * @bprm: binprm for the exec (NOT NULL) | |
954 | */ | |
64fc9526 | 955 | static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm) |
fe864821 | 956 | { |
637f688d | 957 | struct aa_label *label = aa_current_raw_label(); |
d9087c49 | 958 | struct aa_label *new_label = cred_label(bprm->cred); |
fe864821 JJ |
959 | |
960 | /* bail out if unconfined or not changing profile */ | |
d9087c49 JJ |
961 | if ((new_label->proxy == label->proxy) || |
962 | (unconfined(new_label))) | |
fe864821 JJ |
963 | return; |
964 | ||
192ca6b5 JJ |
965 | aa_inherit_files(bprm->cred, current->files); |
966 | ||
fe864821 JJ |
967 | current->pdeath_signal = 0; |
968 | ||
637f688d | 969 | /* reset soft limits and set hard limits for the new label */ |
d9087c49 | 970 | __aa_transition_rlimits(label, new_label); |
fe864821 JJ |
971 | } |
972 | ||
973 | /** | |
391f1211 | 974 | * apparmor_bprm_committed_creds() - do cleanup after new creds committed |
fe864821 JJ |
975 | * @bprm: binprm for the exec (NOT NULL) |
976 | */ | |
a721f7b8 | 977 | static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm) |
fe864821 | 978 | { |
3b529a76 | 979 | /* clear out temporary/transitional state from the context */ |
de62de59 | 980 | aa_clear_task_ctx_trans(task_ctx(current)); |
3b529a76 | 981 | |
fe864821 JJ |
982 | return; |
983 | } | |
984 | ||
37f670aa | 985 | static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop) |
6326948f | 986 | { |
2516fde1 | 987 | struct aa_label *label = __begin_current_label_crit_section(); |
37f670aa CS |
988 | |
989 | prop->apparmor.label = label; | |
2516fde1 | 990 | __end_current_label_crit_section(label); |
6326948f PM |
991 | } |
992 | ||
37f670aa CS |
993 | static void apparmor_task_getlsmprop_obj(struct task_struct *p, |
994 | struct lsm_prop *prop) | |
a7ae3645 JJ |
995 | { |
996 | struct aa_label *label = aa_get_task_label(p); | |
37f670aa CS |
997 | |
998 | prop->apparmor.label = label; | |
a7ae3645 JJ |
999 | aa_put_label(label); |
1000 | } | |
1001 | ||
7cb4dc9f JS |
1002 | static int apparmor_task_setrlimit(struct task_struct *task, |
1003 | unsigned int resource, struct rlimit *new_rlim) | |
b5e95b48 | 1004 | { |
637f688d | 1005 | struct aa_label *label = __begin_current_label_crit_section(); |
b5e95b48 JJ |
1006 | int error = 0; |
1007 | ||
637f688d | 1008 | if (!unconfined(label)) |
90c436a6 JJ |
1009 | error = aa_task_setrlimit(current_cred(), label, task, |
1010 | resource, new_rlim); | |
637f688d | 1011 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
1012 | |
1013 | return error; | |
1014 | } | |
1015 | ||
ae7795bc | 1016 | static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, |
6b4f3d01 | 1017 | int sig, const struct cred *cred) |
cd1dbf76 | 1018 | { |
90c436a6 | 1019 | const struct cred *tc; |
cd1dbf76 JJ |
1020 | struct aa_label *cl, *tl; |
1021 | int error; | |
1022 | ||
90c436a6 JJ |
1023 | tc = get_task_cred(target); |
1024 | tl = aa_get_newest_cred_label(tc); | |
6b4f3d01 SS |
1025 | if (cred) { |
1026 | /* | |
1027 | * Dealing with USB IO specific behavior | |
cd1dbf76 | 1028 | */ |
6b4f3d01 | 1029 | cl = aa_get_newest_cred_label(cred); |
90c436a6 | 1030 | error = aa_may_signal(cred, cl, tc, tl, sig); |
6b4f3d01 | 1031 | aa_put_label(cl); |
90c436a6 JJ |
1032 | } else { |
1033 | cl = __begin_current_label_crit_section(); | |
1034 | error = aa_may_signal(current_cred(), cl, tc, tl, sig); | |
1035 | __end_current_label_crit_section(cl); | |
6b4f3d01 | 1036 | } |
cd1dbf76 | 1037 | aa_put_label(tl); |
90c436a6 | 1038 | put_cred(tc); |
cd1dbf76 JJ |
1039 | |
1040 | return error; | |
1041 | } | |
1042 | ||
fa9b63ad JJ |
1043 | static int apparmor_userns_create(const struct cred *cred) |
1044 | { | |
1045 | struct aa_label *label; | |
1046 | struct aa_profile *profile; | |
1047 | int error = 0; | |
1048 | DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS, | |
1049 | OP_USERNS_CREATE); | |
1050 | ||
1051 | ad.subj_cred = current_cred(); | |
1052 | ||
1053 | label = begin_current_label_crit_section(); | |
1054 | if (!unconfined(label)) { | |
1055 | error = fn_for_each(label, profile, | |
1056 | aa_profile_ns_perm(profile, &ad, | |
1057 | AA_USERNS_CREATE)); | |
1058 | } | |
1059 | end_current_label_crit_section(label); | |
cd1dbf76 JJ |
1060 | |
1061 | return error; | |
1062 | } | |
1063 | ||
56974a6f JJ |
1064 | static void apparmor_sk_free_security(struct sock *sk) |
1065 | { | |
79ddd4a7 | 1066 | struct aa_sk_ctx *ctx = aa_sock(sk); |
56974a6f | 1067 | |
56974a6f JJ |
1068 | aa_put_label(ctx->label); |
1069 | aa_put_label(ctx->peer); | |
56974a6f JJ |
1070 | } |
1071 | ||
1072 | /** | |
0fc6ab40 | 1073 | * apparmor_sk_clone_security - clone the sk_security field |
1cba2750 JJ |
1074 | * @sk: sock to have security cloned |
1075 | * @newsk: sock getting clone | |
56974a6f JJ |
1076 | */ |
1077 | static void apparmor_sk_clone_security(const struct sock *sk, | |
1078 | struct sock *newsk) | |
1079 | { | |
79ddd4a7 JJ |
1080 | struct aa_sk_ctx *ctx = aa_sock(sk); |
1081 | struct aa_sk_ctx *new = aa_sock(newsk); | |
56974a6f | 1082 | |
3b646abc MFO |
1083 | if (new->label) |
1084 | aa_put_label(new->label); | |
56974a6f | 1085 | new->label = aa_get_label(ctx->label); |
3b646abc MFO |
1086 | |
1087 | if (new->peer) | |
1088 | aa_put_label(new->peer); | |
56974a6f JJ |
1089 | new->peer = aa_get_label(ctx->peer); |
1090 | } | |
1091 | ||
56974a6f JJ |
1092 | static int apparmor_socket_create(int family, int type, int protocol, int kern) |
1093 | { | |
1094 | struct aa_label *label; | |
1095 | int error = 0; | |
1096 | ||
1097 | AA_BUG(in_interrupt()); | |
1098 | ||
1099 | label = begin_current_label_crit_section(); | |
1100 | if (!(kern || unconfined(label))) | |
1101 | error = af_select(family, | |
1102 | create_perm(label, family, type, protocol), | |
90c436a6 JJ |
1103 | aa_af_perm(current_cred(), label, |
1104 | OP_CREATE, AA_MAY_CREATE, | |
56974a6f JJ |
1105 | family, type, protocol)); |
1106 | end_current_label_crit_section(label); | |
1107 | ||
1108 | return error; | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * apparmor_socket_post_create - setup the per-socket security struct | |
1cba2750 JJ |
1113 | * @sock: socket that is being setup |
1114 | * @family: family of socket being created | |
1115 | * @type: type of the socket | |
b2c85814 | 1116 | * @protocol: protocol of the socket |
1cba2750 | 1117 | * @kern: socket is a special kernel socket |
56974a6f JJ |
1118 | * |
1119 | * Note: | |
1cba2750 | 1120 | * - kernel sockets labeled kernel_t used to use unconfined |
56974a6f JJ |
1121 | * - socket may not have sk here if created with sock_create_lite or |
1122 | * sock_alloc. These should be accept cases which will be handled in | |
1123 | * sock_graft. | |
1124 | */ | |
1125 | static int apparmor_socket_post_create(struct socket *sock, int family, | |
1126 | int type, int protocol, int kern) | |
1127 | { | |
1128 | struct aa_label *label; | |
1129 | ||
1130 | if (kern) { | |
95c0581f | 1131 | label = aa_get_label(kernel_t); |
56974a6f JJ |
1132 | } else |
1133 | label = aa_get_current_label(); | |
1134 | ||
1135 | if (sock->sk) { | |
79ddd4a7 | 1136 | struct aa_sk_ctx *ctx = aa_sock(sock->sk); |
56974a6f JJ |
1137 | |
1138 | aa_put_label(ctx->label); | |
1139 | ctx->label = aa_get_label(label); | |
1140 | } | |
1141 | aa_put_label(label); | |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
56974a6f JJ |
1146 | static int apparmor_socket_bind(struct socket *sock, |
1147 | struct sockaddr *address, int addrlen) | |
1148 | { | |
1149 | AA_BUG(!sock); | |
1150 | AA_BUG(!sock->sk); | |
1151 | AA_BUG(!address); | |
1152 | AA_BUG(in_interrupt()); | |
1153 | ||
1154 | return af_select(sock->sk->sk_family, | |
1155 | bind_perm(sock, address, addrlen), | |
1156 | aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); | |
1157 | } | |
1158 | ||
56974a6f JJ |
1159 | static int apparmor_socket_connect(struct socket *sock, |
1160 | struct sockaddr *address, int addrlen) | |
1161 | { | |
1162 | AA_BUG(!sock); | |
1163 | AA_BUG(!sock->sk); | |
1164 | AA_BUG(!address); | |
1165 | AA_BUG(in_interrupt()); | |
1166 | ||
1167 | return af_select(sock->sk->sk_family, | |
1168 | connect_perm(sock, address, addrlen), | |
1169 | aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); | |
1170 | } | |
1171 | ||
56974a6f JJ |
1172 | static int apparmor_socket_listen(struct socket *sock, int backlog) |
1173 | { | |
1174 | AA_BUG(!sock); | |
1175 | AA_BUG(!sock->sk); | |
1176 | AA_BUG(in_interrupt()); | |
1177 | ||
1178 | return af_select(sock->sk->sk_family, | |
1179 | listen_perm(sock, backlog), | |
1180 | aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); | |
1181 | } | |
1182 | ||
1cba2750 | 1183 | /* |
56974a6f JJ |
1184 | * Note: while @newsock is created and has some information, the accept |
1185 | * has not been done. | |
1186 | */ | |
1187 | static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) | |
1188 | { | |
1189 | AA_BUG(!sock); | |
1190 | AA_BUG(!sock->sk); | |
1191 | AA_BUG(!newsock); | |
1192 | AA_BUG(in_interrupt()); | |
1193 | ||
1194 | return af_select(sock->sk->sk_family, | |
1195 | accept_perm(sock, newsock), | |
1196 | aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); | |
1197 | } | |
1198 | ||
1199 | static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, | |
1200 | struct msghdr *msg, int size) | |
1201 | { | |
1202 | AA_BUG(!sock); | |
1203 | AA_BUG(!sock->sk); | |
1204 | AA_BUG(!msg); | |
1205 | AA_BUG(in_interrupt()); | |
1206 | ||
1207 | return af_select(sock->sk->sk_family, | |
1208 | msg_perm(op, request, sock, msg, size), | |
1209 | aa_sk_perm(op, request, sock->sk)); | |
1210 | } | |
1211 | ||
56974a6f JJ |
1212 | static int apparmor_socket_sendmsg(struct socket *sock, |
1213 | struct msghdr *msg, int size) | |
1214 | { | |
1215 | return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); | |
1216 | } | |
1217 | ||
56974a6f JJ |
1218 | static int apparmor_socket_recvmsg(struct socket *sock, |
1219 | struct msghdr *msg, int size, int flags) | |
1220 | { | |
1221 | return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); | |
1222 | } | |
1223 | ||
1224 | /* revaliation, get/set attr, shutdown */ | |
1225 | static int aa_sock_perm(const char *op, u32 request, struct socket *sock) | |
1226 | { | |
1227 | AA_BUG(!sock); | |
1228 | AA_BUG(!sock->sk); | |
1229 | AA_BUG(in_interrupt()); | |
1230 | ||
1231 | return af_select(sock->sk->sk_family, | |
1232 | sock_perm(op, request, sock), | |
1233 | aa_sk_perm(op, request, sock->sk)); | |
1234 | } | |
1235 | ||
56974a6f JJ |
1236 | static int apparmor_socket_getsockname(struct socket *sock) |
1237 | { | |
1238 | return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); | |
1239 | } | |
1240 | ||
56974a6f JJ |
1241 | static int apparmor_socket_getpeername(struct socket *sock) |
1242 | { | |
1243 | return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); | |
1244 | } | |
1245 | ||
1246 | /* revaliation, get/set attr, opt */ | |
1247 | static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, | |
1248 | int level, int optname) | |
1249 | { | |
1250 | AA_BUG(!sock); | |
1251 | AA_BUG(!sock->sk); | |
1252 | AA_BUG(in_interrupt()); | |
1253 | ||
1254 | return af_select(sock->sk->sk_family, | |
1255 | opt_perm(op, request, sock, level, optname), | |
1256 | aa_sk_perm(op, request, sock->sk)); | |
1257 | } | |
1258 | ||
56974a6f JJ |
1259 | static int apparmor_socket_getsockopt(struct socket *sock, int level, |
1260 | int optname) | |
1261 | { | |
1262 | return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, | |
1263 | level, optname); | |
1264 | } | |
1265 | ||
56974a6f JJ |
1266 | static int apparmor_socket_setsockopt(struct socket *sock, int level, |
1267 | int optname) | |
1268 | { | |
1269 | return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, | |
1270 | level, optname); | |
1271 | } | |
1272 | ||
56974a6f JJ |
1273 | static int apparmor_socket_shutdown(struct socket *sock, int how) |
1274 | { | |
1275 | return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); | |
1276 | } | |
1277 | ||
e1af4779 | 1278 | #ifdef CONFIG_NETWORK_SECMARK |
56974a6f | 1279 | /** |
0fc6ab40 | 1280 | * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk |
1cba2750 JJ |
1281 | * @sk: sk to associate @skb with |
1282 | * @skb: skb to check for perms | |
56974a6f JJ |
1283 | * |
1284 | * Note: can not sleep may be called with locks held | |
1285 | * | |
1286 | * dont want protocol specific in __skb_recv_datagram() | |
1287 | * to deny an incoming connection socket_sock_rcv_skb() | |
1288 | */ | |
1289 | static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
1290 | { | |
79ddd4a7 | 1291 | struct aa_sk_ctx *ctx = aa_sock(sk); |
ab9f2115 MG |
1292 | |
1293 | if (!skb->secmark) | |
1294 | return 0; | |
1295 | ||
fce09ea3 XL |
1296 | /* |
1297 | * If reach here before socket_post_create hook is called, in which | |
1298 | * case label is null, drop the packet. | |
1299 | */ | |
1300 | if (!ctx->label) | |
1301 | return -EACCES; | |
1302 | ||
ab9f2115 MG |
1303 | return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, |
1304 | skb->secmark, sk); | |
56974a6f | 1305 | } |
e1af4779 | 1306 | #endif |
56974a6f JJ |
1307 | |
1308 | ||
1309 | static struct aa_label *sk_peer_label(struct sock *sk) | |
1310 | { | |
79ddd4a7 | 1311 | struct aa_sk_ctx *ctx = aa_sock(sk); |
56974a6f JJ |
1312 | |
1313 | if (ctx->peer) | |
1314 | return ctx->peer; | |
1315 | ||
1316 | return ERR_PTR(-ENOPROTOOPT); | |
1317 | } | |
1318 | ||
1319 | /** | |
1320 | * apparmor_socket_getpeersec_stream - get security context of peer | |
1cba2750 JJ |
1321 | * @sock: socket that we are trying to get the peer context of |
1322 | * @optval: output - buffer to copy peer name to | |
1323 | * @optlen: output - size of copied name in @optval | |
1324 | * @len: size of @optval buffer | |
1325 | * Returns: 0 on success, -errno of failure | |
56974a6f JJ |
1326 | * |
1327 | * Note: for tcp only valid if using ipsec or cipso on lan | |
1328 | */ | |
1329 | static int apparmor_socket_getpeersec_stream(struct socket *sock, | |
b10b9c34 | 1330 | sockptr_t optval, sockptr_t optlen, |
56974a6f JJ |
1331 | unsigned int len) |
1332 | { | |
b10b9c34 | 1333 | char *name = NULL; |
56974a6f JJ |
1334 | int slen, error = 0; |
1335 | struct aa_label *label; | |
1336 | struct aa_label *peer; | |
1337 | ||
1338 | label = begin_current_label_crit_section(); | |
1339 | peer = sk_peer_label(sock->sk); | |
1340 | if (IS_ERR(peer)) { | |
1341 | error = PTR_ERR(peer); | |
1342 | goto done; | |
1343 | } | |
1344 | slen = aa_label_asxprint(&name, labels_ns(label), peer, | |
1345 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | | |
1346 | FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); | |
1347 | /* don't include terminating \0 in slen, it breaks some apps */ | |
1348 | if (slen < 0) { | |
1349 | error = -ENOMEM; | |
b10b9c34 PM |
1350 | goto done; |
1351 | } | |
1352 | if (slen > len) { | |
1353 | error = -ERANGE; | |
1354 | goto done_len; | |
56974a6f JJ |
1355 | } |
1356 | ||
b10b9c34 PM |
1357 | if (copy_to_sockptr(optval, name, slen)) |
1358 | error = -EFAULT; | |
1359 | done_len: | |
1360 | if (copy_to_sockptr(optlen, &slen, sizeof(slen))) | |
1361 | error = -EFAULT; | |
56974a6f JJ |
1362 | done: |
1363 | end_current_label_crit_section(label); | |
b10b9c34 | 1364 | kfree(name); |
56974a6f JJ |
1365 | return error; |
1366 | } | |
1367 | ||
1368 | /** | |
1369 | * apparmor_socket_getpeersec_dgram - get security label of packet | |
1370 | * @sock: the peer socket | |
1371 | * @skb: packet data | |
1372 | * @secid: pointer to where to put the secid of the packet | |
1373 | * | |
1374 | * Sets the netlabel socket state on sk from parent | |
1375 | */ | |
1376 | static int apparmor_socket_getpeersec_dgram(struct socket *sock, | |
1377 | struct sk_buff *skb, u32 *secid) | |
1378 | ||
1379 | { | |
1380 | /* TODO: requires secid support */ | |
1381 | return -ENOPROTOOPT; | |
1382 | } | |
1383 | ||
1384 | /** | |
1385 | * apparmor_sock_graft - Initialize newly created socket | |
1386 | * @sk: child sock | |
1387 | * @parent: parent socket | |
1388 | * | |
1389 | * Note: could set off of SOCK_CTX(parent) but need to track inode and we can | |
1390 | * just set sk security information off of current creating process label | |
1391 | * Labeling of sk for accept case - probably should be sock based | |
1392 | * instead of task, because of the case where an implicitly labeled | |
1393 | * socket is shared by different tasks. | |
1394 | */ | |
1395 | static void apparmor_sock_graft(struct sock *sk, struct socket *parent) | |
1396 | { | |
79ddd4a7 | 1397 | struct aa_sk_ctx *ctx = aa_sock(sk); |
56974a6f JJ |
1398 | |
1399 | if (!ctx->label) | |
1400 | ctx->label = aa_get_current_label(); | |
1401 | } | |
1402 | ||
e1af4779 | 1403 | #ifdef CONFIG_NETWORK_SECMARK |
41dd9596 | 1404 | static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb, |
ab9f2115 MG |
1405 | struct request_sock *req) |
1406 | { | |
79ddd4a7 | 1407 | struct aa_sk_ctx *ctx = aa_sock(sk); |
ab9f2115 MG |
1408 | |
1409 | if (!skb->secmark) | |
1410 | return 0; | |
1411 | ||
1412 | return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, | |
1413 | skb->secmark, sk); | |
1414 | } | |
e1af4779 | 1415 | #endif |
ab9f2115 | 1416 | |
bbd3662a | 1417 | /* |
37923d43 | 1418 | * The cred blob is a pointer to, not an instance of, an aa_label. |
bbd3662a | 1419 | */ |
f22f9aaf | 1420 | struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = { |
37923d43 | 1421 | .lbs_cred = sizeof(struct aa_label *), |
33bf60ca | 1422 | .lbs_file = sizeof(struct aa_file_ctx), |
f4ad8f2c | 1423 | .lbs_task = sizeof(struct aa_task_ctx), |
2aff9d20 | 1424 | .lbs_sock = sizeof(struct aa_sk_ctx), |
bbd3662a CS |
1425 | }; |
1426 | ||
b1a867ee | 1427 | static const struct lsm_id apparmor_lsmid = { |
f3b8788c CS |
1428 | .name = "apparmor", |
1429 | .id = LSM_ID_APPARMOR, | |
1430 | }; | |
1431 | ||
f22f9aaf | 1432 | static struct security_hook_list apparmor_hooks[] __ro_after_init = { |
e20b043a CS |
1433 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
1434 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), | |
1435 | LSM_HOOK_INIT(capget, apparmor_capget), | |
1436 | LSM_HOOK_INIT(capable, apparmor_capable), | |
1437 | ||
157a3537 | 1438 | LSM_HOOK_INIT(move_mount, apparmor_move_mount), |
2ea3ffb7 JJ |
1439 | LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), |
1440 | LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), | |
1441 | LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), | |
1442 | ||
e20b043a CS |
1443 | LSM_HOOK_INIT(path_link, apparmor_path_link), |
1444 | LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), | |
1445 | LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), | |
1446 | LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), | |
1447 | LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), | |
1448 | LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), | |
1449 | LSM_HOOK_INIT(path_rename, apparmor_path_rename), | |
1450 | LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), | |
1451 | LSM_HOOK_INIT(path_chown, apparmor_path_chown), | |
1452 | LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), | |
1453 | LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), | |
1454 | ||
1455 | LSM_HOOK_INIT(file_open, apparmor_file_open), | |
064dc947 | 1456 | LSM_HOOK_INIT(file_receive, apparmor_file_receive), |
e20b043a CS |
1457 | LSM_HOOK_INIT(file_permission, apparmor_file_permission), |
1458 | LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), | |
1459 | LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), | |
1460 | LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), | |
e20b043a CS |
1461 | LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), |
1462 | LSM_HOOK_INIT(file_lock, apparmor_file_lock), | |
3350607d | 1463 | LSM_HOOK_INIT(file_truncate, apparmor_file_truncate), |
e20b043a | 1464 | |
223981db CS |
1465 | LSM_HOOK_INIT(getselfattr, apparmor_getselfattr), |
1466 | LSM_HOOK_INIT(setselfattr, apparmor_setselfattr), | |
e20b043a CS |
1467 | LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), |
1468 | LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), | |
1469 | ||
56974a6f JJ |
1470 | LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), |
1471 | LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), | |
1472 | ||
1473 | LSM_HOOK_INIT(socket_create, apparmor_socket_create), | |
1474 | LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), | |
1475 | LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), | |
1476 | LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), | |
1477 | LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), | |
1478 | LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), | |
1479 | LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), | |
1480 | LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), | |
1481 | LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), | |
1482 | LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), | |
1483 | LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), | |
1484 | LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), | |
1485 | LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), | |
e1af4779 | 1486 | #ifdef CONFIG_NETWORK_SECMARK |
56974a6f | 1487 | LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), |
e1af4779 | 1488 | #endif |
56974a6f JJ |
1489 | LSM_HOOK_INIT(socket_getpeersec_stream, |
1490 | apparmor_socket_getpeersec_stream), | |
1491 | LSM_HOOK_INIT(socket_getpeersec_dgram, | |
1492 | apparmor_socket_getpeersec_dgram), | |
1493 | LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), | |
e1af4779 | 1494 | #ifdef CONFIG_NETWORK_SECMARK |
ab9f2115 | 1495 | LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), |
e1af4779 | 1496 | #endif |
56974a6f | 1497 | |
e20b043a CS |
1498 | LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), |
1499 | LSM_HOOK_INIT(cred_free, apparmor_cred_free), | |
1500 | LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), | |
1501 | LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), | |
1502 | ||
b8bff599 | 1503 | LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), |
e20b043a CS |
1504 | LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), |
1505 | LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), | |
e20b043a | 1506 | |
3b529a76 JJ |
1507 | LSM_HOOK_INIT(task_free, apparmor_task_free), |
1508 | LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), | |
37f670aa CS |
1509 | LSM_HOOK_INIT(current_getlsmprop_subj, |
1510 | apparmor_current_getlsmprop_subj), | |
1511 | LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj), | |
e20b043a | 1512 | LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), |
cd1dbf76 | 1513 | LSM_HOOK_INIT(task_kill, apparmor_task_kill), |
fa9b63ad | 1514 | LSM_HOOK_INIT(userns_create, apparmor_userns_create), |
c0929212 | 1515 | |
e79c26d0 MG |
1516 | #ifdef CONFIG_AUDIT |
1517 | LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), | |
1518 | LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), | |
1519 | LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), | |
1520 | LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), | |
1521 | #endif | |
1522 | ||
c0929212 | 1523 | LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), |
6f2f724f | 1524 | LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx), |
c0929212 JJ |
1525 | LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), |
1526 | LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), | |
c4371d90 GG |
1527 | |
1528 | #ifdef CONFIG_IO_URING | |
1529 | LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds), | |
1530 | LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll), | |
1531 | #endif | |
b5e95b48 JJ |
1532 | }; |
1533 | ||
1534 | /* | |
1535 | * AppArmor sysfs module parameters | |
1536 | */ | |
1537 | ||
101d6c82 SR |
1538 | static int param_set_aabool(const char *val, const struct kernel_param *kp); |
1539 | static int param_get_aabool(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1540 | #define param_check_aabool param_check_bool |
9c27847d | 1541 | static const struct kernel_param_ops param_ops_aabool = { |
6a4c2643 | 1542 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
101d6c82 SR |
1543 | .set = param_set_aabool, |
1544 | .get = param_get_aabool | |
1545 | }; | |
b5e95b48 | 1546 | |
101d6c82 SR |
1547 | static int param_set_aauint(const char *val, const struct kernel_param *kp); |
1548 | static int param_get_aauint(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1549 | #define param_check_aauint param_check_uint |
9c27847d | 1550 | static const struct kernel_param_ops param_ops_aauint = { |
101d6c82 SR |
1551 | .set = param_set_aauint, |
1552 | .get = param_get_aauint | |
1553 | }; | |
b5e95b48 | 1554 | |
63c16c3a CC |
1555 | static int param_set_aacompressionlevel(const char *val, |
1556 | const struct kernel_param *kp); | |
1557 | static int param_get_aacompressionlevel(char *buffer, | |
1558 | const struct kernel_param *kp); | |
1559 | #define param_check_aacompressionlevel param_check_int | |
1560 | static const struct kernel_param_ops param_ops_aacompressionlevel = { | |
1561 | .set = param_set_aacompressionlevel, | |
1562 | .get = param_get_aacompressionlevel | |
1563 | }; | |
1564 | ||
101d6c82 SR |
1565 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); |
1566 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1567 | #define param_check_aalockpolicy param_check_bool |
9c27847d | 1568 | static const struct kernel_param_ops param_ops_aalockpolicy = { |
6a4c2643 | 1569 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
101d6c82 SR |
1570 | .set = param_set_aalockpolicy, |
1571 | .get = param_get_aalockpolicy | |
1572 | }; | |
b5e95b48 | 1573 | |
e4dca7b7 KC |
1574 | static int param_set_audit(const char *val, const struct kernel_param *kp); |
1575 | static int param_get_audit(char *buffer, const struct kernel_param *kp); | |
b5e95b48 | 1576 | |
e4dca7b7 KC |
1577 | static int param_set_mode(const char *val, const struct kernel_param *kp); |
1578 | static int param_get_mode(char *buffer, const struct kernel_param *kp); | |
b5e95b48 JJ |
1579 | |
1580 | /* Flag values, also controllable via /sys/module/apparmor/parameters | |
1581 | * We define special types as we want to do additional mediation. | |
1582 | */ | |
1583 | ||
1584 | /* AppArmor global enforcement switch - complain, enforce, kill */ | |
1585 | enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; | |
1586 | module_param_call(mode, param_set_mode, param_get_mode, | |
1587 | &aa_g_profile_mode, S_IRUSR | S_IWUSR); | |
1588 | ||
6059f71f | 1589 | /* whether policy verification hashing is enabled */ |
7616ac70 | 1590 | bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); |
3ccb76c5 | 1591 | #ifdef CONFIG_SECURITY_APPARMOR_HASH |
6059f71f | 1592 | module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); |
7616ac70 | 1593 | #endif |
6059f71f | 1594 | |
d61c57fd JJ |
1595 | /* whether policy exactly as loaded is retained for debug and checkpointing */ |
1596 | bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY); | |
1597 | #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY | |
1598 | module_param_named(export_binary, aa_g_export_binary, aabool, 0600); | |
1599 | #endif | |
1600 | ||
63c16c3a | 1601 | /* policy loaddata compression level */ |
70f24a9f | 1602 | int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL; |
63c16c3a CC |
1603 | module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, |
1604 | aacompressionlevel, 0400); | |
1605 | ||
b5e95b48 | 1606 | /* Debug mode */ |
eea7a05f | 1607 | bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); |
b5e95b48 JJ |
1608 | module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); |
1609 | ||
1610 | /* Audit mode */ | |
1611 | enum audit_mode aa_g_audit; | |
1612 | module_param_call(audit, param_set_audit, param_get_audit, | |
1613 | &aa_g_audit, S_IRUSR | S_IWUSR); | |
1614 | ||
1615 | /* Determines if audit header is included in audited messages. This | |
1616 | * provides more context if the audit daemon is not running | |
1617 | */ | |
954317fe | 1618 | bool aa_g_audit_header = true; |
b5e95b48 JJ |
1619 | module_param_named(audit_header, aa_g_audit_header, aabool, |
1620 | S_IRUSR | S_IWUSR); | |
1621 | ||
1622 | /* lock out loading/removal of policy | |
1623 | * TODO: add in at boot loading of policy, which is the only way to | |
1624 | * load policy, if lock_policy is set | |
1625 | */ | |
90ab5ee9 | 1626 | bool aa_g_lock_policy; |
b5e95b48 JJ |
1627 | module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, |
1628 | S_IRUSR | S_IWUSR); | |
1629 | ||
1630 | /* Syscall logging mode */ | |
90ab5ee9 | 1631 | bool aa_g_logsyscall; |
b5e95b48 JJ |
1632 | module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); |
1633 | ||
1634 | /* Maximum pathname length before accesses will start getting rejected */ | |
1635 | unsigned int aa_g_path_max = 2 * PATH_MAX; | |
622f6e32 | 1636 | module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); |
b5e95b48 JJ |
1637 | |
1638 | /* Determines how paranoid loading of policy is and how much verification | |
1639 | * on the loaded policy is done. | |
abbf8734 JJ |
1640 | * DEPRECATED: read only as strict checking of load is always done now |
1641 | * that none root users (user namespaces) can load policy. | |
b5e95b48 | 1642 | */ |
79eb2711 | 1643 | bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD); |
abbf8734 | 1644 | module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); |
b5e95b48 | 1645 | |
e33c1b99 KC |
1646 | static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); |
1647 | static int param_set_aaintbool(const char *val, const struct kernel_param *kp); | |
1648 | #define param_check_aaintbool param_check_int | |
1649 | static const struct kernel_param_ops param_ops_aaintbool = { | |
1650 | .set = param_set_aaintbool, | |
1651 | .get = param_get_aaintbool | |
1652 | }; | |
b5e95b48 | 1653 | /* Boot time disable flag */ |
f22f9aaf | 1654 | static int apparmor_enabled __ro_after_init = 1; |
e33c1b99 | 1655 | module_param_named(enabled, apparmor_enabled, aaintbool, 0444); |
b5e95b48 JJ |
1656 | |
1657 | static int __init apparmor_enabled_setup(char *str) | |
1658 | { | |
1659 | unsigned long enabled; | |
29707b20 | 1660 | int error = kstrtoul(str, 0, &enabled); |
b5e95b48 JJ |
1661 | if (!error) |
1662 | apparmor_enabled = enabled ? 1 : 0; | |
1663 | return 1; | |
1664 | } | |
1665 | ||
1666 | __setup("apparmor=", apparmor_enabled_setup); | |
1667 | ||
1668 | /* set global flag turning off the ability to load policy */ | |
101d6c82 | 1669 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1670 | { |
545de8fe JJ |
1671 | if (!apparmor_enabled) |
1672 | return -EINVAL; | |
92de220a | 1673 | if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) |
b5e95b48 | 1674 | return -EPERM; |
b5e95b48 JJ |
1675 | return param_set_bool(val, kp); |
1676 | } | |
1677 | ||
101d6c82 | 1678 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1679 | { |
ca4bd5ae JJ |
1680 | if (!apparmor_enabled) |
1681 | return -EINVAL; | |
92de220a | 1682 | if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) |
545de8fe | 1683 | return -EPERM; |
b5e95b48 JJ |
1684 | return param_get_bool(buffer, kp); |
1685 | } | |
1686 | ||
101d6c82 | 1687 | static int param_set_aabool(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1688 | { |
ca4bd5ae JJ |
1689 | if (!apparmor_enabled) |
1690 | return -EINVAL; | |
92de220a | 1691 | if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) |
545de8fe | 1692 | return -EPERM; |
b5e95b48 JJ |
1693 | return param_set_bool(val, kp); |
1694 | } | |
1695 | ||
101d6c82 | 1696 | static int param_get_aabool(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1697 | { |
ca4bd5ae JJ |
1698 | if (!apparmor_enabled) |
1699 | return -EINVAL; | |
92de220a | 1700 | if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) |
545de8fe | 1701 | return -EPERM; |
b5e95b48 JJ |
1702 | return param_get_bool(buffer, kp); |
1703 | } | |
1704 | ||
101d6c82 | 1705 | static int param_set_aauint(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1706 | { |
39d84824 JJ |
1707 | int error; |
1708 | ||
ca4bd5ae JJ |
1709 | if (!apparmor_enabled) |
1710 | return -EINVAL; | |
39d84824 JJ |
1711 | /* file is ro but enforce 2nd line check */ |
1712 | if (apparmor_initialized) | |
545de8fe | 1713 | return -EPERM; |
39d84824 JJ |
1714 | |
1715 | error = param_set_uint(val, kp); | |
df323337 | 1716 | aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); |
39d84824 JJ |
1717 | pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); |
1718 | ||
1719 | return error; | |
b5e95b48 JJ |
1720 | } |
1721 | ||
101d6c82 | 1722 | static int param_get_aauint(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1723 | { |
ca4bd5ae JJ |
1724 | if (!apparmor_enabled) |
1725 | return -EINVAL; | |
92de220a | 1726 | if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) |
545de8fe | 1727 | return -EPERM; |
b5e95b48 JJ |
1728 | return param_get_uint(buffer, kp); |
1729 | } | |
1730 | ||
e33c1b99 KC |
1731 | /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ |
1732 | static int param_set_aaintbool(const char *val, const struct kernel_param *kp) | |
1733 | { | |
1734 | struct kernel_param kp_local; | |
1735 | bool value; | |
1736 | int error; | |
1737 | ||
1738 | if (apparmor_initialized) | |
1739 | return -EPERM; | |
1740 | ||
1741 | /* Create local copy, with arg pointing to bool type. */ | |
1742 | value = !!*((int *)kp->arg); | |
1743 | memcpy(&kp_local, kp, sizeof(kp_local)); | |
1744 | kp_local.arg = &value; | |
1745 | ||
1746 | error = param_set_bool(val, &kp_local); | |
1747 | if (!error) | |
1748 | *((int *)kp->arg) = *((bool *)kp_local.arg); | |
1749 | return error; | |
1750 | } | |
1751 | ||
1752 | /* | |
1753 | * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to | |
1754 | * 1/0, this converts the "int that is actually bool" back to bool for | |
1755 | * display in the /sys filesystem, while keeping it "int" for the LSM | |
1756 | * infrastructure. | |
1757 | */ | |
1758 | static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) | |
1759 | { | |
1760 | struct kernel_param kp_local; | |
1761 | bool value; | |
1762 | ||
1763 | /* Create local copy, with arg pointing to bool type. */ | |
1764 | value = !!*((int *)kp->arg); | |
1765 | memcpy(&kp_local, kp, sizeof(kp_local)); | |
1766 | kp_local.arg = &value; | |
1767 | ||
1768 | return param_get_bool(buffer, &kp_local); | |
1769 | } | |
1770 | ||
63c16c3a CC |
1771 | static int param_set_aacompressionlevel(const char *val, |
1772 | const struct kernel_param *kp) | |
1773 | { | |
1774 | int error; | |
1775 | ||
1776 | if (!apparmor_enabled) | |
1777 | return -EINVAL; | |
1778 | if (apparmor_initialized) | |
1779 | return -EPERM; | |
1780 | ||
1781 | error = param_set_int(val, kp); | |
1782 | ||
1783 | aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, | |
70f24a9f | 1784 | AA_MIN_CLEVEL, AA_MAX_CLEVEL); |
f4d6b94b | 1785 | pr_info("AppArmor: policy rawdata compression level set to %d\n", |
63c16c3a CC |
1786 | aa_g_rawdata_compression_level); |
1787 | ||
1788 | return error; | |
1789 | } | |
1790 | ||
1791 | static int param_get_aacompressionlevel(char *buffer, | |
1792 | const struct kernel_param *kp) | |
1793 | { | |
1794 | if (!apparmor_enabled) | |
1795 | return -EINVAL; | |
92de220a | 1796 | if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) |
63c16c3a CC |
1797 | return -EPERM; |
1798 | return param_get_int(buffer, kp); | |
1799 | } | |
1800 | ||
e4dca7b7 | 1801 | static int param_get_audit(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1802 | { |
b5e95b48 JJ |
1803 | if (!apparmor_enabled) |
1804 | return -EINVAL; | |
92de220a | 1805 | if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) |
545de8fe | 1806 | return -EPERM; |
b5e95b48 JJ |
1807 | return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); |
1808 | } | |
1809 | ||
e4dca7b7 | 1810 | static int param_set_audit(const char *val, const struct kernel_param *kp) |
b5e95b48 JJ |
1811 | { |
1812 | int i; | |
b5e95b48 JJ |
1813 | |
1814 | if (!apparmor_enabled) | |
1815 | return -EINVAL; | |
b5e95b48 JJ |
1816 | if (!val) |
1817 | return -EINVAL; | |
92de220a | 1818 | if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) |
545de8fe | 1819 | return -EPERM; |
b5e95b48 | 1820 | |
5d8779a5 AS |
1821 | i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val); |
1822 | if (i < 0) | |
1823 | return -EINVAL; | |
b5e95b48 | 1824 | |
5d8779a5 AS |
1825 | aa_g_audit = i; |
1826 | return 0; | |
b5e95b48 JJ |
1827 | } |
1828 | ||
e4dca7b7 | 1829 | static int param_get_mode(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1830 | { |
b5e95b48 JJ |
1831 | if (!apparmor_enabled) |
1832 | return -EINVAL; | |
92de220a | 1833 | if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) |
545de8fe | 1834 | return -EPERM; |
b5e95b48 | 1835 | |
0d259f04 | 1836 | return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); |
b5e95b48 JJ |
1837 | } |
1838 | ||
e4dca7b7 | 1839 | static int param_set_mode(const char *val, const struct kernel_param *kp) |
b5e95b48 JJ |
1840 | { |
1841 | int i; | |
b5e95b48 JJ |
1842 | |
1843 | if (!apparmor_enabled) | |
1844 | return -EINVAL; | |
b5e95b48 JJ |
1845 | if (!val) |
1846 | return -EINVAL; | |
92de220a | 1847 | if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) |
545de8fe | 1848 | return -EPERM; |
b5e95b48 | 1849 | |
5d8779a5 AS |
1850 | i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, |
1851 | val); | |
1852 | if (i < 0) | |
1853 | return -EINVAL; | |
b5e95b48 | 1854 | |
5d8779a5 AS |
1855 | aa_g_profile_mode = i; |
1856 | return 0; | |
b5e95b48 JJ |
1857 | } |
1858 | ||
341c1fda | 1859 | char *aa_get_buffer(bool in_atomic) |
df323337 SAS |
1860 | { |
1861 | union aa_buffer *aa_buf; | |
ea9bae12 | 1862 | struct aa_local_cache *cache; |
df323337 | 1863 | bool try_again = true; |
341c1fda | 1864 | gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); |
df323337 | 1865 | |
ea9bae12 JJ |
1866 | /* use per cpu cached buffers first */ |
1867 | cache = get_cpu_ptr(&aa_local_buffers); | |
1868 | if (!list_empty(&cache->head)) { | |
1869 | aa_buf = list_first_entry(&cache->head, union aa_buffer, list); | |
1870 | list_del(&aa_buf->list); | |
1871 | cache->hold--; | |
1872 | cache->count--; | |
1873 | put_cpu_ptr(&aa_local_buffers); | |
1874 | return &aa_buf->buffer[0]; | |
1875 | } | |
1876 | put_cpu_ptr(&aa_local_buffers); | |
1877 | ||
1878 | if (!spin_trylock(&aa_buffers_lock)) { | |
1879 | cache = get_cpu_ptr(&aa_local_buffers); | |
1880 | cache->hold += 1; | |
1881 | put_cpu_ptr(&aa_local_buffers); | |
1882 | spin_lock(&aa_buffers_lock); | |
1883 | } else { | |
1884 | cache = get_cpu_ptr(&aa_local_buffers); | |
1885 | put_cpu_ptr(&aa_local_buffers); | |
1886 | } | |
df323337 | 1887 | retry: |
341c1fda JJ |
1888 | if (buffer_count > reserve_count || |
1889 | (in_atomic && !list_empty(&aa_global_buffers))) { | |
df323337 SAS |
1890 | aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, |
1891 | list); | |
1892 | list_del(&aa_buf->list); | |
341c1fda | 1893 | buffer_count--; |
df323337 | 1894 | spin_unlock(&aa_buffers_lock); |
ba808cb5 | 1895 | return aa_buf->buffer; |
df323337 | 1896 | } |
341c1fda JJ |
1897 | if (in_atomic) { |
1898 | /* | |
1899 | * out of reserve buffers and in atomic context so increase | |
1900 | * how many buffers to keep in reserve | |
1901 | */ | |
1902 | reserve_count++; | |
1903 | flags = GFP_ATOMIC; | |
1904 | } | |
df323337 SAS |
1905 | spin_unlock(&aa_buffers_lock); |
1906 | ||
341c1fda JJ |
1907 | if (!in_atomic) |
1908 | might_sleep(); | |
1909 | aa_buf = kmalloc(aa_g_path_max, flags); | |
df323337 SAS |
1910 | if (!aa_buf) { |
1911 | if (try_again) { | |
1912 | try_again = false; | |
ea9bae12 | 1913 | spin_lock(&aa_buffers_lock); |
df323337 SAS |
1914 | goto retry; |
1915 | } | |
1916 | pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); | |
1917 | return NULL; | |
1918 | } | |
ba808cb5 | 1919 | return aa_buf->buffer; |
df323337 SAS |
1920 | } |
1921 | ||
1922 | void aa_put_buffer(char *buf) | |
1923 | { | |
1924 | union aa_buffer *aa_buf; | |
ea9bae12 | 1925 | struct aa_local_cache *cache; |
df323337 SAS |
1926 | |
1927 | if (!buf) | |
1928 | return; | |
1929 | aa_buf = container_of(buf, union aa_buffer, buffer[0]); | |
1930 | ||
ea9bae12 JJ |
1931 | cache = get_cpu_ptr(&aa_local_buffers); |
1932 | if (!cache->hold) { | |
1933 | put_cpu_ptr(&aa_local_buffers); | |
1934 | ||
1935 | if (spin_trylock(&aa_buffers_lock)) { | |
1936 | /* put back on global list */ | |
1937 | list_add(&aa_buf->list, &aa_global_buffers); | |
1938 | buffer_count++; | |
1939 | spin_unlock(&aa_buffers_lock); | |
1940 | cache = get_cpu_ptr(&aa_local_buffers); | |
1941 | put_cpu_ptr(&aa_local_buffers); | |
1942 | return; | |
1943 | } | |
1944 | /* contention on global list, fallback to percpu */ | |
1945 | cache = get_cpu_ptr(&aa_local_buffers); | |
1946 | cache->hold += 1; | |
1947 | } | |
1948 | ||
1949 | /* cache in percpu list */ | |
1950 | list_add(&aa_buf->list, &cache->head); | |
1951 | cache->count++; | |
1952 | put_cpu_ptr(&aa_local_buffers); | |
df323337 SAS |
1953 | } |
1954 | ||
b5e95b48 JJ |
1955 | /* |
1956 | * AppArmor init functions | |
1957 | */ | |
1958 | ||
1959 | /** | |
55a26ebf | 1960 | * set_init_ctx - set a task context and profile on the first task. |
b5e95b48 JJ |
1961 | * |
1962 | * TODO: allow setting an alternate profile than unconfined | |
1963 | */ | |
55a26ebf | 1964 | static int __init set_init_ctx(void) |
b5e95b48 | 1965 | { |
bf1d2ee7 | 1966 | struct cred *cred = (__force struct cred *)current->real_cred; |
b5e95b48 | 1967 | |
69b5a44a | 1968 | set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); |
b5e95b48 JJ |
1969 | |
1970 | return 0; | |
1971 | } | |
1972 | ||
d4669f0b JJ |
1973 | static void destroy_buffers(void) |
1974 | { | |
df323337 | 1975 | union aa_buffer *aa_buf; |
d4669f0b | 1976 | |
df323337 SAS |
1977 | spin_lock(&aa_buffers_lock); |
1978 | while (!list_empty(&aa_global_buffers)) { | |
1979 | aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, | |
1980 | list); | |
1981 | list_del(&aa_buf->list); | |
1982 | spin_unlock(&aa_buffers_lock); | |
1983 | kfree(aa_buf); | |
1984 | spin_lock(&aa_buffers_lock); | |
d4669f0b | 1985 | } |
df323337 | 1986 | spin_unlock(&aa_buffers_lock); |
d4669f0b JJ |
1987 | } |
1988 | ||
1989 | static int __init alloc_buffers(void) | |
1990 | { | |
df323337 SAS |
1991 | union aa_buffer *aa_buf; |
1992 | int i, num; | |
1993 | ||
ea9bae12 JJ |
1994 | /* |
1995 | * per cpu set of cached allocated buffers used to help reduce | |
1996 | * lock contention | |
1997 | */ | |
1998 | for_each_possible_cpu(i) { | |
1999 | per_cpu(aa_local_buffers, i).hold = 0; | |
2000 | per_cpu(aa_local_buffers, i).count = 0; | |
2001 | INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head); | |
2002 | } | |
df323337 SAS |
2003 | /* |
2004 | * A function may require two buffers at once. Usually the buffers are | |
2005 | * used for a short period of time and are shared. On UP kernel buffers | |
2006 | * two should be enough, with more CPUs it is possible that more | |
2007 | * buffers will be used simultaneously. The preallocated pool may grow. | |
2008 | * This preallocation has also the side-effect that AppArmor will be | |
2009 | * disabled early at boot if aa_g_path_max is extremly high. | |
2010 | */ | |
2011 | if (num_online_cpus() > 1) | |
341c1fda | 2012 | num = 4 + RESERVE_COUNT; |
df323337 | 2013 | else |
341c1fda | 2014 | num = 2 + RESERVE_COUNT; |
df323337 SAS |
2015 | |
2016 | for (i = 0; i < num; i++) { | |
2017 | ||
2018 | aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | | |
2019 | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); | |
2020 | if (!aa_buf) { | |
2021 | destroy_buffers(); | |
2022 | return -ENOMEM; | |
d4669f0b | 2023 | } |
ba808cb5 | 2024 | aa_put_buffer(aa_buf->buffer); |
d4669f0b | 2025 | } |
d4669f0b JJ |
2026 | return 0; |
2027 | } | |
2028 | ||
e3ea1ca5 | 2029 | #ifdef CONFIG_SYSCTL |
78eb4ea2 | 2030 | static int apparmor_dointvec(const struct ctl_table *table, int write, |
32927393 | 2031 | void *buffer, size_t *lenp, loff_t *ppos) |
e3ea1ca5 | 2032 | { |
92de220a | 2033 | if (!aa_current_policy_admin_capable(NULL)) |
e3ea1ca5 TH |
2034 | return -EPERM; |
2035 | if (!apparmor_enabled) | |
2036 | return -EINVAL; | |
2037 | ||
2038 | return proc_dointvec(table, write, buffer, lenp, ppos); | |
2039 | } | |
2040 | ||
1751f872 | 2041 | static const struct ctl_table apparmor_sysctl_table[] = { |
fa9b63ad | 2042 | #ifdef CONFIG_USER_NS |
e3ea1ca5 TH |
2043 | { |
2044 | .procname = "unprivileged_userns_apparmor_policy", | |
2045 | .data = &unprivileged_userns_apparmor_policy, | |
2046 | .maxlen = sizeof(int), | |
2047 | .mode = 0600, | |
2048 | .proc_handler = apparmor_dointvec, | |
2049 | }, | |
fa9b63ad | 2050 | #endif /* CONFIG_USER_NS */ |
524d8e14 JJ |
2051 | { |
2052 | .procname = "apparmor_display_secid_mode", | |
2053 | .data = &apparmor_display_secid_mode, | |
2054 | .maxlen = sizeof(int), | |
2055 | .mode = 0600, | |
2056 | .proc_handler = apparmor_dointvec, | |
2057 | }, | |
2d9da9b1 JJ |
2058 | { |
2059 | .procname = "apparmor_restrict_unprivileged_unconfined", | |
2060 | .data = &aa_unprivileged_unconfined_restricted, | |
2061 | .maxlen = sizeof(int), | |
2062 | .mode = 0600, | |
2063 | .proc_handler = apparmor_dointvec, | |
2064 | }, | |
e3ea1ca5 TH |
2065 | }; |
2066 | ||
2067 | static int __init apparmor_init_sysctl(void) | |
2068 | { | |
96200952 | 2069 | return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM; |
e3ea1ca5 TH |
2070 | } |
2071 | #else | |
2072 | static inline int apparmor_init_sysctl(void) | |
2073 | { | |
2074 | return 0; | |
2075 | } | |
2076 | #endif /* CONFIG_SYSCTL */ | |
2077 | ||
e1af4779 | 2078 | #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) |
ab9f2115 MG |
2079 | static unsigned int apparmor_ip_postroute(void *priv, |
2080 | struct sk_buff *skb, | |
2081 | const struct nf_hook_state *state) | |
2082 | { | |
2083 | struct aa_sk_ctx *ctx; | |
2084 | struct sock *sk; | |
2085 | ||
2086 | if (!skb->secmark) | |
2087 | return NF_ACCEPT; | |
2088 | ||
2089 | sk = skb_to_full_sk(skb); | |
2090 | if (sk == NULL) | |
2091 | return NF_ACCEPT; | |
2092 | ||
79ddd4a7 | 2093 | ctx = aa_sock(sk); |
ab9f2115 MG |
2094 | if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, |
2095 | skb->secmark, sk)) | |
2096 | return NF_ACCEPT; | |
2097 | ||
2098 | return NF_DROP_ERR(-ECONNREFUSED); | |
2099 | ||
2100 | } | |
2101 | ||
ab9f2115 MG |
2102 | static const struct nf_hook_ops apparmor_nf_ops[] = { |
2103 | { | |
7b721124 | 2104 | .hook = apparmor_ip_postroute, |
ab9f2115 MG |
2105 | .pf = NFPROTO_IPV4, |
2106 | .hooknum = NF_INET_POST_ROUTING, | |
2107 | .priority = NF_IP_PRI_SELINUX_FIRST, | |
2108 | }, | |
2109 | #if IS_ENABLED(CONFIG_IPV6) | |
2110 | { | |
7b721124 | 2111 | .hook = apparmor_ip_postroute, |
ab9f2115 MG |
2112 | .pf = NFPROTO_IPV6, |
2113 | .hooknum = NF_INET_POST_ROUTING, | |
2114 | .priority = NF_IP6_PRI_SELINUX_FIRST, | |
2115 | }, | |
2116 | #endif | |
2117 | }; | |
2118 | ||
2119 | static int __net_init apparmor_nf_register(struct net *net) | |
2120 | { | |
84117994 | 2121 | return nf_register_net_hooks(net, apparmor_nf_ops, |
ab9f2115 | 2122 | ARRAY_SIZE(apparmor_nf_ops)); |
ab9f2115 MG |
2123 | } |
2124 | ||
2125 | static void __net_exit apparmor_nf_unregister(struct net *net) | |
2126 | { | |
2127 | nf_unregister_net_hooks(net, apparmor_nf_ops, | |
2128 | ARRAY_SIZE(apparmor_nf_ops)); | |
2129 | } | |
2130 | ||
2131 | static struct pernet_operations apparmor_net_ops = { | |
2132 | .init = apparmor_nf_register, | |
2133 | .exit = apparmor_nf_unregister, | |
2134 | }; | |
2135 | ||
2136 | static int __init apparmor_nf_ip_init(void) | |
2137 | { | |
2138 | int err; | |
2139 | ||
2140 | if (!apparmor_enabled) | |
2141 | return 0; | |
2142 | ||
2143 | err = register_pernet_subsys(&apparmor_net_ops); | |
2144 | if (err) | |
2145 | panic("Apparmor: register_pernet_subsys: error %d\n", err); | |
2146 | ||
2147 | return 0; | |
2148 | } | |
2149 | __initcall(apparmor_nf_ip_init); | |
e1af4779 | 2150 | #endif |
ab9f2115 | 2151 | |
98b824ff JJ |
2152 | static char nulldfa_src[] = { |
2153 | #include "nulldfa.in" | |
2154 | }; | |
735ad5d1 | 2155 | static struct aa_dfa *nulldfa; |
98b824ff JJ |
2156 | |
2157 | static char stacksplitdfa_src[] = { | |
2158 | #include "stacksplitdfa.in" | |
2159 | }; | |
2160 | struct aa_dfa *stacksplitdfa; | |
2161 | struct aa_policydb *nullpdb; | |
2162 | ||
2163 | static int __init aa_setup_dfa_engine(void) | |
2164 | { | |
2165 | int error = -ENOMEM; | |
2166 | ||
2167 | nullpdb = aa_alloc_pdb(GFP_KERNEL); | |
2168 | if (!nullpdb) | |
2169 | return -ENOMEM; | |
2170 | ||
2171 | nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src), | |
2172 | TO_ACCEPT1_FLAG(YYTD_DATA32) | | |
2173 | TO_ACCEPT2_FLAG(YYTD_DATA32)); | |
2174 | if (IS_ERR(nulldfa)) { | |
2175 | error = PTR_ERR(nulldfa); | |
2176 | goto fail; | |
2177 | } | |
2178 | nullpdb->dfa = aa_get_dfa(nulldfa); | |
2179 | nullpdb->perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL); | |
2180 | if (!nullpdb->perms) | |
2181 | goto fail; | |
2182 | nullpdb->size = 2; | |
2183 | ||
2184 | stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src, | |
2185 | sizeof(stacksplitdfa_src), | |
2186 | TO_ACCEPT1_FLAG(YYTD_DATA32) | | |
2187 | TO_ACCEPT2_FLAG(YYTD_DATA32)); | |
2188 | if (IS_ERR(stacksplitdfa)) { | |
2189 | error = PTR_ERR(stacksplitdfa); | |
2190 | goto fail; | |
2191 | } | |
2192 | ||
2193 | return 0; | |
2194 | ||
2195 | fail: | |
2196 | aa_put_pdb(nullpdb); | |
2197 | aa_put_dfa(nulldfa); | |
2198 | nullpdb = NULL; | |
2199 | nulldfa = NULL; | |
2200 | stacksplitdfa = NULL; | |
2201 | ||
2202 | return error; | |
2203 | } | |
2204 | ||
2205 | static void __init aa_teardown_dfa_engine(void) | |
2206 | { | |
2207 | aa_put_dfa(stacksplitdfa); | |
2208 | aa_put_dfa(nulldfa); | |
2209 | aa_put_pdb(nullpdb); | |
2210 | nullpdb = NULL; | |
2211 | stacksplitdfa = NULL; | |
2212 | nulldfa = NULL; | |
2213 | } | |
2214 | ||
b5e95b48 JJ |
2215 | static int __init apparmor_init(void) |
2216 | { | |
2217 | int error; | |
2218 | ||
11c236b8 JJ |
2219 | error = aa_setup_dfa_engine(); |
2220 | if (error) { | |
2221 | AA_ERROR("Unable to setup dfa engine\n"); | |
2222 | goto alloc_out; | |
2223 | } | |
2224 | ||
b5e95b48 JJ |
2225 | error = aa_alloc_root_ns(); |
2226 | if (error) { | |
2227 | AA_ERROR("Unable to allocate default profile namespace\n"); | |
2228 | goto alloc_out; | |
2229 | } | |
2230 | ||
e3ea1ca5 TH |
2231 | error = apparmor_init_sysctl(); |
2232 | if (error) { | |
2233 | AA_ERROR("Unable to register sysctls\n"); | |
2234 | goto alloc_out; | |
2235 | ||
2236 | } | |
2237 | ||
d4669f0b JJ |
2238 | error = alloc_buffers(); |
2239 | if (error) { | |
2240 | AA_ERROR("Unable to allocate work buffers\n"); | |
df323337 | 2241 | goto alloc_out; |
d4669f0b JJ |
2242 | } |
2243 | ||
55a26ebf | 2244 | error = set_init_ctx(); |
b5e95b48 JJ |
2245 | if (error) { |
2246 | AA_ERROR("Failed to set context on init task\n"); | |
b1d9e6b0 | 2247 | aa_free_root_ns(); |
d4669f0b | 2248 | goto buffers_out; |
b5e95b48 | 2249 | } |
d69dece5 | 2250 | security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), |
f3b8788c | 2251 | &apparmor_lsmid); |
b5e95b48 JJ |
2252 | |
2253 | /* Report that AppArmor successfully initialized */ | |
2254 | apparmor_initialized = 1; | |
2255 | if (aa_g_profile_mode == APPARMOR_COMPLAIN) | |
2256 | aa_info_message("AppArmor initialized: complain mode enabled"); | |
2257 | else if (aa_g_profile_mode == APPARMOR_KILL) | |
2258 | aa_info_message("AppArmor initialized: kill mode enabled"); | |
2259 | else | |
2260 | aa_info_message("AppArmor initialized"); | |
2261 | ||
2262 | return error; | |
2263 | ||
d4669f0b JJ |
2264 | buffers_out: |
2265 | destroy_buffers(); | |
b5e95b48 JJ |
2266 | alloc_out: |
2267 | aa_destroy_aafs(); | |
11c236b8 | 2268 | aa_teardown_dfa_engine(); |
b5e95b48 | 2269 | |
954317fe | 2270 | apparmor_enabled = false; |
b5e95b48 | 2271 | return error; |
b5e95b48 JJ |
2272 | } |
2273 | ||
3d6e5f6d | 2274 | DEFINE_LSM(apparmor) = { |
07aed2f2 | 2275 | .name = "apparmor", |
14bd99c8 | 2276 | .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
c5459b82 | 2277 | .enabled = &apparmor_enabled, |
bbd3662a | 2278 | .blobs = &apparmor_blob_sizes, |
3d6e5f6d KC |
2279 | .init = apparmor_init, |
2280 | }; |