blob: 7866161f685bb13ede371629a2e0b0c64e2d25d9 [file] [log] [blame]
John Johansenb5e95b42010-07-29 14:48:07 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor LSM hooks.
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 */
14
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070015#include <linux/lsm_hooks.h>
John Johansenb5e95b42010-07-29 14:48:07 -070016#include <linux/moduleparam.h>
17#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/mount.h>
20#include <linux/namei.h>
21#include <linux/ptrace.h>
22#include <linux/ctype.h>
23#include <linux/sysctl.h>
24#include <linux/audit.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070025#include <linux/user_namespace.h>
John Johansenb5e95b42010-07-29 14:48:07 -070026#include <net/sock.h>
27
28#include "include/apparmor.h"
29#include "include/apparmorfs.h"
30#include "include/audit.h"
31#include "include/capability.h"
John Johansend8889d42017-10-11 01:04:48 -070032#include "include/cred.h"
John Johansenb5e95b42010-07-29 14:48:07 -070033#include "include/file.h"
34#include "include/ipc.h"
John Johansen56974a62017-07-18 23:18:33 -070035#include "include/net.h"
John Johansenb5e95b42010-07-29 14:48:07 -070036#include "include/path.h"
John Johansen637f6882017-06-09 08:14:28 -070037#include "include/label.h"
John Johansenb5e95b42010-07-29 14:48:07 -070038#include "include/policy.h"
John Johansencff281f2017-01-16 00:42:15 -080039#include "include/policy_ns.h"
John Johansenb5e95b42010-07-29 14:48:07 -070040#include "include/procattr.h"
John Johansen2ea3ffb2017-07-18 23:04:47 -070041#include "include/mount.h"
John Johansenc0929212017-07-31 17:36:45 -070042#include "include/secid.h"
John Johansenb5e95b42010-07-29 14:48:07 -070043
44/* Flag indicating whether initialization completed */
John Johansen545de8f2017-04-06 06:55:23 -070045int apparmor_initialized;
John Johansenb5e95b42010-07-29 14:48:07 -070046
John Johansend4669f02017-01-16 00:43:10 -080047DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
48
49
John Johansenb5e95b42010-07-29 14:48:07 -070050/*
51 * LSM hook functions
52 */
53
54/*
John Johansend9087c42017-01-27 03:53:53 -080055 * put the associated labels
John Johansenb5e95b42010-07-29 14:48:07 -070056 */
57static void apparmor_cred_free(struct cred *cred)
58{
John Johansend9087c42017-01-27 03:53:53 -080059 aa_put_label(cred_label(cred));
60 cred_label(cred) = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -070061}
62
63/*
64 * allocate the apparmor part of blank credentials
65 */
66static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
67{
John Johansend9087c42017-01-27 03:53:53 -080068 cred_label(cred) = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -070069 return 0;
70}
71
72/*
John Johansend9087c42017-01-27 03:53:53 -080073 * prepare new cred label for modification by prepare_cred block
John Johansenb5e95b42010-07-29 14:48:07 -070074 */
75static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
76 gfp_t gfp)
77{
John Johansend9087c42017-01-27 03:53:53 -080078 cred_label(new) = aa_get_newest_label(cred_label(old));
John Johansenb5e95b42010-07-29 14:48:07 -070079 return 0;
80}
81
82/*
83 * transfer the apparmor data to a blank set of creds
84 */
85static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
86{
John Johansend9087c42017-01-27 03:53:53 -080087 cred_label(new) = aa_get_newest_label(cred_label(old));
John Johansenb5e95b42010-07-29 14:48:07 -070088}
John Johansenb5e95b42010-07-29 14:48:07 -070089
John Johansen3b529a72017-01-20 01:59:25 -080090static void apparmor_task_free(struct task_struct *task)
91{
92
93 aa_free_task_ctx(task_ctx(task));
94 task_ctx(task) = NULL;
95}
96
97static int apparmor_task_alloc(struct task_struct *task,
98 unsigned long clone_flags)
99{
100 struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL);
101
102 if (!new)
103 return -ENOMEM;
104
John Johansende62de52017-10-08 00:43:02 -0700105 aa_dup_task_ctx(new, task_ctx(current));
John Johansen3b529a72017-01-20 01:59:25 -0800106 task_ctx(task) = new;
107
108 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -0700109}
110
111static int apparmor_ptrace_access_check(struct task_struct *child,
112 unsigned int mode)
113{
John Johansenb2d09ae2017-06-09 14:22:14 -0700114 struct aa_label *tracer, *tracee;
115 int error;
116
117 tracer = begin_current_label_crit_section();
118 tracee = aa_get_task_label(child);
119 error = aa_may_ptrace(tracer, tracee,
120 mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
121 aa_put_label(tracee);
122 end_current_label_crit_section(tracer);
123
124 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700125}
126
127static int apparmor_ptrace_traceme(struct task_struct *parent)
128{
John Johansenb2d09ae2017-06-09 14:22:14 -0700129 struct aa_label *tracer, *tracee;
130 int error;
131
132 tracee = begin_current_label_crit_section();
133 tracer = aa_get_task_label(parent);
134 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
135 aa_put_label(tracer);
136 end_current_label_crit_section(tracee);
137
138 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700139}
140
141/* Derived from security/commoncap.c:cap_capget */
142static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
143 kernel_cap_t *inheritable, kernel_cap_t *permitted)
144{
John Johansen637f6882017-06-09 08:14:28 -0700145 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700146 const struct cred *cred;
147
148 rcu_read_lock();
149 cred = __task_cred(target);
John Johansen637f6882017-06-09 08:14:28 -0700150 label = aa_get_newest_cred_label(cred);
John Johansenc70c86c2017-06-09 14:07:02 -0700151
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700152 /*
153 * cap_capget is stacked ahead of this and will
154 * initialize effective and permitted.
155 */
John Johansenc70c86c2017-06-09 14:07:02 -0700156 if (!unconfined(label)) {
157 struct aa_profile *profile;
158 struct label_it i;
159
160 label_for_each_confined(i, label, profile) {
161 if (COMPLAIN_MODE(profile))
162 continue;
163 *effective = cap_intersect(*effective,
164 profile->caps.allow);
165 *permitted = cap_intersect(*permitted,
166 profile->caps.allow);
167 }
John Johansenb5e95b42010-07-29 14:48:07 -0700168 }
169 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -0700170 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700171
172 return 0;
173}
174
Eric Paris6a9de492012-01-03 12:25:14 -0500175static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
176 int cap, int audit)
John Johansenb5e95b42010-07-29 14:48:07 -0700177{
John Johansen637f6882017-06-09 08:14:28 -0700178 struct aa_label *label;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700179 int error = 0;
180
John Johansen637f6882017-06-09 08:14:28 -0700181 label = aa_get_newest_cred_label(cred);
182 if (!unconfined(label))
John Johansenc70c86c2017-06-09 14:07:02 -0700183 error = aa_capable(label, cap, audit);
John Johansen637f6882017-06-09 08:14:28 -0700184 aa_put_label(label);
John Johansencf797c02017-06-09 02:08:28 -0700185
John Johansenb5e95b42010-07-29 14:48:07 -0700186 return error;
187}
188
189/**
190 * common_perm - basic common permission check wrapper fn for paths
191 * @op: operation being checked
192 * @path: path to check permission of (NOT NULL)
193 * @mask: requested permissions mask
194 * @cond: conditional info for the permission request (NOT NULL)
195 *
196 * Returns: %0 else error code if error or permission denied
197 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800198static int common_perm(const char *op, const struct path *path, u32 mask,
John Johansenb5e95b42010-07-29 14:48:07 -0700199 struct path_cond *cond)
200{
John Johansen637f6882017-06-09 08:14:28 -0700201 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700202 int error = 0;
203
John Johansen637f6882017-06-09 08:14:28 -0700204 label = __begin_current_label_crit_section();
205 if (!unconfined(label))
John Johansenaebd8732017-06-09 16:02:25 -0700206 error = aa_path_perm(op, label, path, 0, mask, cond);
John Johansen637f6882017-06-09 08:14:28 -0700207 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700208
209 return error;
210}
211
212/**
John Johansen31f75bf2017-01-16 00:43:07 -0800213 * common_perm_cond - common permission wrapper around inode cond
214 * @op: operation being checked
215 * @path: location to check (NOT NULL)
216 * @mask: requested permissions mask
217 *
218 * Returns: %0 else error code if error or permission denied
219 */
220static int common_perm_cond(const char *op, const struct path *path, u32 mask)
221{
222 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
223 d_backing_inode(path->dentry)->i_mode
224 };
225
226 if (!path_mediated_fs(path->dentry))
227 return 0;
228
229 return common_perm(op, path, mask, &cond);
230}
231
232/**
John Johansenb5e95b42010-07-29 14:48:07 -0700233 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
234 * @op: operation being checked
235 * @dir: directory of the dentry (NOT NULL)
236 * @dentry: dentry to check (NOT NULL)
237 * @mask: requested permissions mask
238 * @cond: conditional info for the permission request (NOT NULL)
239 *
240 * Returns: %0 else error code if error or permission denied
241 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800242static int common_perm_dir_dentry(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700243 struct dentry *dentry, u32 mask,
244 struct path_cond *cond)
245{
Kees Cook8486adf2016-12-16 17:04:13 -0800246 struct path path = { .mnt = dir->mnt, .dentry = dentry };
John Johansenb5e95b42010-07-29 14:48:07 -0700247
248 return common_perm(op, &path, mask, cond);
249}
250
251/**
John Johansenb5e95b42010-07-29 14:48:07 -0700252 * common_perm_rm - common permission wrapper for operations doing rm
253 * @op: operation being checked
254 * @dir: directory that the dentry is in (NOT NULL)
255 * @dentry: dentry being rm'd (NOT NULL)
256 * @mask: requested permission mask
257 *
258 * Returns: %0 else error code if error or permission denied
259 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800260static int common_perm_rm(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700261 struct dentry *dentry, u32 mask)
262{
David Howellsc6f493d2015-03-17 22:26:22 +0000263 struct inode *inode = d_backing_inode(dentry);
John Johansenb5e95b42010-07-29 14:48:07 -0700264 struct path_cond cond = { };
265
John Johansenefeee832017-01-16 00:42:28 -0800266 if (!inode || !path_mediated_fs(dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700267 return 0;
268
269 cond.uid = inode->i_uid;
270 cond.mode = inode->i_mode;
271
272 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
273}
274
275/**
276 * common_perm_create - common permission wrapper for operations doing create
277 * @op: operation being checked
278 * @dir: directory that dentry will be created in (NOT NULL)
279 * @dentry: dentry to create (NOT NULL)
280 * @mask: request permission mask
281 * @mode: created file mode
282 *
283 * Returns: %0 else error code if error or permission denied
284 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800285static int common_perm_create(const char *op, const struct path *dir,
Al Virod6b49f72016-03-25 15:10:04 -0400286 struct dentry *dentry, u32 mask, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700287{
288 struct path_cond cond = { current_fsuid(), mode };
289
John Johansenefeee832017-01-16 00:42:28 -0800290 if (!path_mediated_fs(dir->dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700291 return 0;
292
293 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
294}
295
Al Viro989f74e2016-03-25 15:13:39 -0400296static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700297{
298 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
299}
300
Al Virod3607752016-03-25 15:21:09 -0400301static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500302 umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700303{
304 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
305 S_IFDIR);
306}
307
Al Viro989f74e2016-03-25 15:13:39 -0400308static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700309{
310 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
311}
312
Al Virod3607752016-03-25 15:21:09 -0400313static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500314 umode_t mode, unsigned int dev)
John Johansenb5e95b42010-07-29 14:48:07 -0700315{
316 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
317}
318
Al Viro81f4c502016-03-25 14:22:01 -0400319static int apparmor_path_truncate(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700320{
John Johansene53cfe62017-05-26 15:07:22 -0700321 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700322}
323
Al Virod3607752016-03-25 15:21:09 -0400324static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
John Johansenb5e95b42010-07-29 14:48:07 -0700325 const char *old_name)
326{
327 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
328 S_IFLNK);
329}
330
Al Viro3ccee462016-03-25 15:27:45 -0400331static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700332 struct dentry *new_dentry)
333{
John Johansen637f6882017-06-09 08:14:28 -0700334 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700335 int error = 0;
336
John Johansenefeee832017-01-16 00:42:28 -0800337 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700338 return 0;
339
John Johansen637f6882017-06-09 08:14:28 -0700340 label = begin_current_label_crit_section();
341 if (!unconfined(label))
John Johansen80143702017-06-09 16:06:21 -0700342 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
John Johansen637f6882017-06-09 08:14:28 -0700343 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700344
John Johansenb5e95b42010-07-29 14:48:07 -0700345 return error;
346}
347
Al Viro3ccee462016-03-25 15:27:45 -0400348static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
349 const struct path *new_dir, struct dentry *new_dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700350{
John Johansen637f6882017-06-09 08:14:28 -0700351 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700352 int error = 0;
353
John Johansenefeee832017-01-16 00:42:28 -0800354 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700355 return 0;
356
John Johansen637f6882017-06-09 08:14:28 -0700357 label = begin_current_label_crit_section();
358 if (!unconfined(label)) {
Kees Cook8486adf2016-12-16 17:04:13 -0800359 struct path old_path = { .mnt = old_dir->mnt,
360 .dentry = old_dentry };
361 struct path new_path = { .mnt = new_dir->mnt,
362 .dentry = new_dentry };
David Howellsc6f493d2015-03-17 22:26:22 +0000363 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
364 d_backing_inode(old_dentry)->i_mode
John Johansenb5e95b42010-07-29 14:48:07 -0700365 };
366
John Johansenaebd8732017-06-09 16:02:25 -0700367 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
John Johansene53cfe62017-05-26 15:07:22 -0700368 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
369 AA_MAY_SETATTR | AA_MAY_DELETE,
John Johansenb5e95b42010-07-29 14:48:07 -0700370 &cond);
371 if (!error)
John Johansenaebd8732017-06-09 16:02:25 -0700372 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
John Johansene53cfe62017-05-26 15:07:22 -0700373 0, MAY_WRITE | AA_MAY_SETATTR |
John Johansenb5e95b42010-07-29 14:48:07 -0700374 AA_MAY_CREATE, &cond);
375
376 }
John Johansen637f6882017-06-09 08:14:28 -0700377 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700378
John Johansenb5e95b42010-07-29 14:48:07 -0700379 return error;
380}
381
Al Virobe01f9f2016-03-25 14:56:23 -0400382static int apparmor_path_chmod(const struct path *path, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700383{
John Johansen31f75bf2017-01-16 00:43:07 -0800384 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
John Johansenb5e95b42010-07-29 14:48:07 -0700385}
386
Al Viro7fd25da2016-03-25 14:44:41 -0400387static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
John Johansenb5e95b42010-07-29 14:48:07 -0700388{
John Johansen31f75bf2017-01-16 00:43:07 -0800389 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
John Johansenb5e95b42010-07-29 14:48:07 -0700390}
391
Al Viro3f7036a2015-03-08 19:28:30 -0400392static int apparmor_inode_getattr(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700393{
John Johansene53cfe62017-05-26 15:07:22 -0700394 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700395}
396
Eric Paris83d49852012-04-04 13:45:40 -0400397static int apparmor_file_open(struct file *file, const struct cred *cred)
John Johansenb5e95b42010-07-29 14:48:07 -0700398{
John Johansen637f6882017-06-09 08:14:28 -0700399 struct aa_file_ctx *fctx = file_ctx(file);
400 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700401 int error = 0;
402
John Johansenefeee832017-01-16 00:42:28 -0800403 if (!path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700404 return 0;
405
406 /* If in exec, permission is handled by bprm hooks.
407 * Cache permissions granted by the previous exec check, with
408 * implicit read and executable mmap which are required to
409 * actually execute the image.
410 */
411 if (current->in_execve) {
John Johansen55a26eb2017-01-16 00:43:00 -0800412 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
John Johansenb5e95b42010-07-29 14:48:07 -0700413 return 0;
414 }
415
John Johansen637f6882017-06-09 08:14:28 -0700416 label = aa_get_newest_cred_label(cred);
417 if (!unconfined(label)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500418 struct inode *inode = file_inode(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700419 struct path_cond cond = { inode->i_uid, inode->i_mode };
420
John Johansenaebd8732017-06-09 16:02:25 -0700421 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
John Johansenb5e95b42010-07-29 14:48:07 -0700422 aa_map_file_to_perms(file), &cond);
423 /* todo cache full allowed permissions set and state */
John Johansen55a26eb2017-01-16 00:43:00 -0800424 fctx->allow = aa_map_file_to_perms(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700425 }
John Johansen637f6882017-06-09 08:14:28 -0700426 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700427
428 return error;
429}
430
431static int apparmor_file_alloc_security(struct file *file)
432{
John Johansencf797c02017-06-09 02:08:28 -0700433 int error = 0;
434
John Johansenb5e95b42010-07-29 14:48:07 -0700435 /* freed by apparmor_file_free_security */
John Johansen637f6882017-06-09 08:14:28 -0700436 struct aa_label *label = begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700437 file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
John Johansen2835a132017-06-09 11:43:45 -0700438 if (!file_ctx(file))
439 error = -ENOMEM;
John Johansen637f6882017-06-09 08:14:28 -0700440 end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700441
John Johansencf797c02017-06-09 02:08:28 -0700442 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700443}
444
445static void apparmor_file_free_security(struct file *file)
446{
John Johansen2835a132017-06-09 11:43:45 -0700447 aa_free_file_ctx(file_ctx(file));
John Johansenb5e95b42010-07-29 14:48:07 -0700448}
449
John Johansen47f6e5c2017-01-16 00:43:01 -0800450static int common_file_perm(const char *op, struct file *file, u32 mask)
John Johansenb5e95b42010-07-29 14:48:07 -0700451{
John Johansen190a9512017-06-09 14:59:51 -0700452 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700453 int error = 0;
454
John Johansen192ca6b2017-06-09 11:58:42 -0700455 /* don't reaudit files closed during inheritance */
456 if (file->f_path.dentry == aa_null.dentry)
457 return -EACCES;
458
John Johansen637f6882017-06-09 08:14:28 -0700459 label = __begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700460 error = aa_file_perm(op, label, file, mask);
John Johansen637f6882017-06-09 08:14:28 -0700461 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700462
463 return error;
464}
465
John Johansen064dc942017-06-09 17:15:56 -0700466static int apparmor_file_receive(struct file *file)
467{
468 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
469}
470
John Johansenb5e95b42010-07-29 14:48:07 -0700471static int apparmor_file_permission(struct file *file, int mask)
472{
473 return common_file_perm(OP_FPERM, file, mask);
474}
475
476static int apparmor_file_lock(struct file *file, unsigned int cmd)
477{
478 u32 mask = AA_MAY_LOCK;
479
480 if (cmd == F_WRLCK)
481 mask |= MAY_WRITE;
482
483 return common_file_perm(OP_FLOCK, file, mask);
484}
485
John Johansen47f6e5c2017-01-16 00:43:01 -0800486static int common_mmap(const char *op, struct file *file, unsigned long prot,
John Johansenb5e95b42010-07-29 14:48:07 -0700487 unsigned long flags)
488{
John Johansenb5e95b42010-07-29 14:48:07 -0700489 int mask = 0;
490
John Johansen637f6882017-06-09 08:14:28 -0700491 if (!file || !file_ctx(file))
John Johansenb5e95b42010-07-29 14:48:07 -0700492 return 0;
493
494 if (prot & PROT_READ)
495 mask |= MAY_READ;
496 /*
497 * Private mappings don't require write perms since they don't
498 * write back to the files
499 */
500 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
501 mask |= MAY_WRITE;
502 if (prot & PROT_EXEC)
503 mask |= AA_EXEC_MMAP;
504
John Johansenb5e95b42010-07-29 14:48:07 -0700505 return common_file_perm(op, file, mask);
506}
507
Al Viroe5467852012-05-30 13:30:51 -0400508static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
509 unsigned long prot, unsigned long flags)
John Johansenb5e95b42010-07-29 14:48:07 -0700510{
John Johansenb5e95b42010-07-29 14:48:07 -0700511 return common_mmap(OP_FMMAP, file, prot, flags);
512}
513
514static int apparmor_file_mprotect(struct vm_area_struct *vma,
515 unsigned long reqprot, unsigned long prot)
516{
517 return common_mmap(OP_FMPROT, vma->vm_file, prot,
518 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
519}
520
John Johansen2ea3ffb2017-07-18 23:04:47 -0700521static int apparmor_sb_mount(const char *dev_name, const struct path *path,
522 const char *type, unsigned long flags, void *data)
523{
524 struct aa_label *label;
525 int error = 0;
526
527 /* Discard magic */
528 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
529 flags &= ~MS_MGC_MSK;
530
531 flags &= ~AA_MS_IGNORE_MASK;
532
533 label = __begin_current_label_crit_section();
534 if (!unconfined(label)) {
535 if (flags & MS_REMOUNT)
536 error = aa_remount(label, path, flags, data);
537 else if (flags & MS_BIND)
538 error = aa_bind_mount(label, path, dev_name, flags);
539 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
540 MS_UNBINDABLE))
541 error = aa_mount_change_type(label, path, flags);
542 else if (flags & MS_MOVE)
543 error = aa_move_mount(label, path, dev_name);
544 else
545 error = aa_new_mount(label, dev_name, path, type,
546 flags, data);
547 }
548 __end_current_label_crit_section(label);
549
550 return error;
551}
552
553static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
554{
555 struct aa_label *label;
556 int error = 0;
557
558 label = __begin_current_label_crit_section();
559 if (!unconfined(label))
560 error = aa_umount(label, mnt, flags);
561 __end_current_label_crit_section(label);
562
563 return error;
564}
565
566static int apparmor_sb_pivotroot(const struct path *old_path,
567 const struct path *new_path)
568{
569 struct aa_label *label;
570 int error = 0;
571
572 label = aa_get_current_label();
573 if (!unconfined(label))
574 error = aa_pivotroot(label, old_path, new_path);
575 aa_put_label(label);
576
577 return error;
578}
579
John Johansenb5e95b42010-07-29 14:48:07 -0700580static int apparmor_getprocattr(struct task_struct *task, char *name,
581 char **value)
582{
583 int error = -ENOENT;
John Johansenb5e95b42010-07-29 14:48:07 -0700584 /* released below */
585 const struct cred *cred = get_task_cred(task);
John Johansende62de52017-10-08 00:43:02 -0700586 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen637f6882017-06-09 08:14:28 -0700587 struct aa_label *label = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -0700588
589 if (strcmp(name, "current") == 0)
John Johansend9087c42017-01-27 03:53:53 -0800590 label = aa_get_newest_label(cred_label(cred));
John Johansen55a26eb2017-01-16 00:43:00 -0800591 else if (strcmp(name, "prev") == 0 && ctx->previous)
John Johansen637f6882017-06-09 08:14:28 -0700592 label = aa_get_newest_label(ctx->previous);
John Johansen55a26eb2017-01-16 00:43:00 -0800593 else if (strcmp(name, "exec") == 0 && ctx->onexec)
John Johansen637f6882017-06-09 08:14:28 -0700594 label = aa_get_newest_label(ctx->onexec);
John Johansenb5e95b42010-07-29 14:48:07 -0700595 else
596 error = -EINVAL;
597
John Johansen637f6882017-06-09 08:14:28 -0700598 if (label)
John Johansen76a1d262017-06-09 12:47:17 -0700599 error = aa_getprocattr(label, value);
John Johansen77b071b2013-07-10 21:07:43 -0700600
John Johansen637f6882017-06-09 08:14:28 -0700601 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700602 put_cred(cred);
603
604 return error;
605}
606
Stephen Smalleyb21507e2017-01-09 10:07:31 -0500607static int apparmor_setprocattr(const char *name, void *value,
608 size_t size)
John Johansenb5e95b42010-07-29 14:48:07 -0700609{
Vegard Nossume89b8082016-07-07 13:41:11 -0700610 char *command, *largs = NULL, *args = value;
John Johansenb5e95b42010-07-29 14:48:07 -0700611 size_t arg_size;
612 int error;
John Johansenef88a7a2017-01-16 00:43:02 -0800613 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700614
615 if (size == 0)
616 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700617
Vegard Nossume89b8082016-07-07 13:41:11 -0700618 /* AppArmor requires that the buffer must be null terminated atm */
619 if (args[size - 1] != '\0') {
620 /* null terminate */
621 largs = args = kmalloc(size + 1, GFP_KERNEL);
622 if (!args)
623 return -ENOMEM;
624 memcpy(args, value, size);
625 args[size] = '\0';
626 }
627
628 error = -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700629 args = strim(args);
630 command = strsep(&args, " ");
631 if (!args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700632 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700633 args = skip_spaces(args);
634 if (!*args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700635 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700636
John Johansend4d03f72016-07-09 23:46:33 -0700637 arg_size = size - (args - (largs ? largs : (char *) value));
John Johansenb5e95b42010-07-29 14:48:07 -0700638 if (strcmp(name, "current") == 0) {
639 if (strcmp(command, "changehat") == 0) {
640 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700641 AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700642 } else if (strcmp(command, "permhat") == 0) {
643 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700644 AA_CHANGE_TEST);
John Johansenb5e95b42010-07-29 14:48:07 -0700645 } else if (strcmp(command, "changeprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700646 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700647 } else if (strcmp(command, "permprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700648 error = aa_change_profile(args, AA_CHANGE_TEST);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700649 } else if (strcmp(command, "stack") == 0) {
650 error = aa_change_profile(args, AA_CHANGE_STACK);
John Johansen3eea57c2013-02-27 03:44:40 -0800651 } else
652 goto fail;
John Johansenb5e95b42010-07-29 14:48:07 -0700653 } else if (strcmp(name, "exec") == 0) {
John Johansen3eea57c2013-02-27 03:44:40 -0800654 if (strcmp(command, "exec") == 0)
John Johansendf8073c2017-06-09 11:36:48 -0700655 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700656 else if (strcmp(command, "stack") == 0)
657 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
658 AA_CHANGE_STACK));
John Johansen3eea57c2013-02-27 03:44:40 -0800659 else
660 goto fail;
661 } else
John Johansenb5e95b42010-07-29 14:48:07 -0700662 /* only support the "current" and "exec" process attributes */
Vegard Nossume89b8082016-07-07 13:41:11 -0700663 goto fail;
John Johansen3eea57c2013-02-27 03:44:40 -0800664
John Johansenb5e95b42010-07-29 14:48:07 -0700665 if (!error)
666 error = size;
Vegard Nossume89b8082016-07-07 13:41:11 -0700667out:
668 kfree(largs);
John Johansenb5e95b42010-07-29 14:48:07 -0700669 return error;
John Johansen3eea57c2013-02-27 03:44:40 -0800670
671fail:
John Johansen637f6882017-06-09 08:14:28 -0700672 aad(&sa)->label = begin_current_label_crit_section();
John Johansenef88a7a2017-01-16 00:43:02 -0800673 aad(&sa)->info = name;
674 aad(&sa)->error = error = -EINVAL;
John Johansen3eea57c2013-02-27 03:44:40 -0800675 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
John Johansen637f6882017-06-09 08:14:28 -0700676 end_current_label_crit_section(aad(&sa)->label);
Vegard Nossume89b8082016-07-07 13:41:11 -0700677 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700678}
679
John Johansenfe864822017-06-09 05:27:50 -0700680/**
681 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
682 * @bprm: binprm for the exec (NOT NULL)
683 */
684static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
685{
John Johansen637f6882017-06-09 08:14:28 -0700686 struct aa_label *label = aa_current_raw_label();
John Johansend9087c42017-01-27 03:53:53 -0800687 struct aa_label *new_label = cred_label(bprm->cred);
John Johansenfe864822017-06-09 05:27:50 -0700688
689 /* bail out if unconfined or not changing profile */
John Johansend9087c42017-01-27 03:53:53 -0800690 if ((new_label->proxy == label->proxy) ||
691 (unconfined(new_label)))
John Johansenfe864822017-06-09 05:27:50 -0700692 return;
693
John Johansen192ca6b2017-06-09 11:58:42 -0700694 aa_inherit_files(bprm->cred, current->files);
695
John Johansenfe864822017-06-09 05:27:50 -0700696 current->pdeath_signal = 0;
697
John Johansen637f6882017-06-09 08:14:28 -0700698 /* reset soft limits and set hard limits for the new label */
John Johansend9087c42017-01-27 03:53:53 -0800699 __aa_transition_rlimits(label, new_label);
John Johansenfe864822017-06-09 05:27:50 -0700700}
701
702/**
703 * apparmor_bprm_committed_cred - do cleanup after new creds committed
704 * @bprm: binprm for the exec (NOT NULL)
705 */
706static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
707{
John Johansen3b529a72017-01-20 01:59:25 -0800708 /* clear out temporary/transitional state from the context */
John Johansende62de52017-10-08 00:43:02 -0700709 aa_clear_task_ctx_trans(task_ctx(current));
John Johansen3b529a72017-01-20 01:59:25 -0800710
John Johansenfe864822017-06-09 05:27:50 -0700711 return;
712}
713
John Johansena7ae3642017-09-11 11:29:53 -0700714static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
715{
716 struct aa_label *label = aa_get_task_label(p);
717 *secid = label->secid;
718 aa_put_label(label);
719}
720
Jiri Slaby7cb4dc92010-08-11 11:28:02 +0200721static int apparmor_task_setrlimit(struct task_struct *task,
722 unsigned int resource, struct rlimit *new_rlim)
John Johansenb5e95b42010-07-29 14:48:07 -0700723{
John Johansen637f6882017-06-09 08:14:28 -0700724 struct aa_label *label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700725 int error = 0;
726
John Johansen637f6882017-06-09 08:14:28 -0700727 if (!unconfined(label))
John Johansen86b92cb2017-06-09 14:15:20 -0700728 error = aa_task_setrlimit(label, task, resource, new_rlim);
John Johansen637f6882017-06-09 08:14:28 -0700729 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700730
731 return error;
732}
733
John Johansencd1dbf72017-07-18 22:56:22 -0700734static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400735 int sig, const struct cred *cred)
John Johansencd1dbf72017-07-18 22:56:22 -0700736{
737 struct aa_label *cl, *tl;
738 int error;
739
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400740 if (cred) {
741 /*
742 * Dealing with USB IO specific behavior
John Johansencd1dbf72017-07-18 22:56:22 -0700743 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400744 cl = aa_get_newest_cred_label(cred);
745 tl = aa_get_task_label(target);
746 error = aa_may_signal(cl, tl, sig);
747 aa_put_label(cl);
748 aa_put_label(tl);
749 return error;
750 }
751
John Johansencd1dbf72017-07-18 22:56:22 -0700752 cl = __begin_current_label_crit_section();
753 tl = aa_get_task_label(target);
754 error = aa_may_signal(cl, tl, sig);
755 aa_put_label(tl);
756 __end_current_label_crit_section(cl);
757
758 return error;
759}
760
John Johansen56974a62017-07-18 23:18:33 -0700761/**
762 * apparmor_sk_alloc_security - allocate and attach the sk_security field
763 */
764static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
765{
766 struct aa_sk_ctx *ctx;
767
768 ctx = kzalloc(sizeof(*ctx), flags);
769 if (!ctx)
770 return -ENOMEM;
771
772 SK_CTX(sk) = ctx;
773
774 return 0;
775}
776
777/**
778 * apparmor_sk_free_security - free the sk_security field
779 */
780static void apparmor_sk_free_security(struct sock *sk)
781{
782 struct aa_sk_ctx *ctx = SK_CTX(sk);
783
784 SK_CTX(sk) = NULL;
785 aa_put_label(ctx->label);
786 aa_put_label(ctx->peer);
787 kfree(ctx);
788}
789
790/**
791 * apparmor_clone_security - clone the sk_security field
792 */
793static void apparmor_sk_clone_security(const struct sock *sk,
794 struct sock *newsk)
795{
796 struct aa_sk_ctx *ctx = SK_CTX(sk);
797 struct aa_sk_ctx *new = SK_CTX(newsk);
798
799 new->label = aa_get_label(ctx->label);
800 new->peer = aa_get_label(ctx->peer);
801}
802
803/**
804 * apparmor_socket_create - check perms before creating a new socket
805 */
806static int apparmor_socket_create(int family, int type, int protocol, int kern)
807{
808 struct aa_label *label;
809 int error = 0;
810
811 AA_BUG(in_interrupt());
812
813 label = begin_current_label_crit_section();
814 if (!(kern || unconfined(label)))
815 error = af_select(family,
816 create_perm(label, family, type, protocol),
817 aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
818 family, type, protocol));
819 end_current_label_crit_section(label);
820
821 return error;
822}
823
824/**
825 * apparmor_socket_post_create - setup the per-socket security struct
826 *
827 * Note:
828 * - kernel sockets currently labeled unconfined but we may want to
829 * move to a special kernel label
830 * - socket may not have sk here if created with sock_create_lite or
831 * sock_alloc. These should be accept cases which will be handled in
832 * sock_graft.
833 */
834static int apparmor_socket_post_create(struct socket *sock, int family,
835 int type, int protocol, int kern)
836{
837 struct aa_label *label;
838
839 if (kern) {
840 struct aa_ns *ns = aa_get_current_ns();
841
842 label = aa_get_label(ns_unconfined(ns));
843 aa_put_ns(ns);
844 } else
845 label = aa_get_current_label();
846
847 if (sock->sk) {
848 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
849
850 aa_put_label(ctx->label);
851 ctx->label = aa_get_label(label);
852 }
853 aa_put_label(label);
854
855 return 0;
856}
857
858/**
859 * apparmor_socket_bind - check perms before bind addr to socket
860 */
861static int apparmor_socket_bind(struct socket *sock,
862 struct sockaddr *address, int addrlen)
863{
864 AA_BUG(!sock);
865 AA_BUG(!sock->sk);
866 AA_BUG(!address);
867 AA_BUG(in_interrupt());
868
869 return af_select(sock->sk->sk_family,
870 bind_perm(sock, address, addrlen),
871 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
872}
873
874/**
875 * apparmor_socket_connect - check perms before connecting @sock to @address
876 */
877static int apparmor_socket_connect(struct socket *sock,
878 struct sockaddr *address, int addrlen)
879{
880 AA_BUG(!sock);
881 AA_BUG(!sock->sk);
882 AA_BUG(!address);
883 AA_BUG(in_interrupt());
884
885 return af_select(sock->sk->sk_family,
886 connect_perm(sock, address, addrlen),
887 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
888}
889
890/**
891 * apparmor_socket_list - check perms before allowing listen
892 */
893static int apparmor_socket_listen(struct socket *sock, int backlog)
894{
895 AA_BUG(!sock);
896 AA_BUG(!sock->sk);
897 AA_BUG(in_interrupt());
898
899 return af_select(sock->sk->sk_family,
900 listen_perm(sock, backlog),
901 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
902}
903
904/**
905 * apparmor_socket_accept - check perms before accepting a new connection.
906 *
907 * Note: while @newsock is created and has some information, the accept
908 * has not been done.
909 */
910static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
911{
912 AA_BUG(!sock);
913 AA_BUG(!sock->sk);
914 AA_BUG(!newsock);
915 AA_BUG(in_interrupt());
916
917 return af_select(sock->sk->sk_family,
918 accept_perm(sock, newsock),
919 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
920}
921
922static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
923 struct msghdr *msg, int size)
924{
925 AA_BUG(!sock);
926 AA_BUG(!sock->sk);
927 AA_BUG(!msg);
928 AA_BUG(in_interrupt());
929
930 return af_select(sock->sk->sk_family,
931 msg_perm(op, request, sock, msg, size),
932 aa_sk_perm(op, request, sock->sk));
933}
934
935/**
936 * apparmor_socket_sendmsg - check perms before sending msg to another socket
937 */
938static int apparmor_socket_sendmsg(struct socket *sock,
939 struct msghdr *msg, int size)
940{
941 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
942}
943
944/**
945 * apparmor_socket_recvmsg - check perms before receiving a message
946 */
947static int apparmor_socket_recvmsg(struct socket *sock,
948 struct msghdr *msg, int size, int flags)
949{
950 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
951}
952
953/* revaliation, get/set attr, shutdown */
954static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
955{
956 AA_BUG(!sock);
957 AA_BUG(!sock->sk);
958 AA_BUG(in_interrupt());
959
960 return af_select(sock->sk->sk_family,
961 sock_perm(op, request, sock),
962 aa_sk_perm(op, request, sock->sk));
963}
964
965/**
966 * apparmor_socket_getsockname - check perms before getting the local address
967 */
968static int apparmor_socket_getsockname(struct socket *sock)
969{
970 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
971}
972
973/**
974 * apparmor_socket_getpeername - check perms before getting remote address
975 */
976static int apparmor_socket_getpeername(struct socket *sock)
977{
978 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
979}
980
981/* revaliation, get/set attr, opt */
982static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
983 int level, int optname)
984{
985 AA_BUG(!sock);
986 AA_BUG(!sock->sk);
987 AA_BUG(in_interrupt());
988
989 return af_select(sock->sk->sk_family,
990 opt_perm(op, request, sock, level, optname),
991 aa_sk_perm(op, request, sock->sk));
992}
993
994/**
995 * apparmor_getsockopt - check perms before getting socket options
996 */
997static int apparmor_socket_getsockopt(struct socket *sock, int level,
998 int optname)
999{
1000 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1001 level, optname);
1002}
1003
1004/**
1005 * apparmor_setsockopt - check perms before setting socket options
1006 */
1007static int apparmor_socket_setsockopt(struct socket *sock, int level,
1008 int optname)
1009{
1010 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1011 level, optname);
1012}
1013
1014/**
1015 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1016 */
1017static int apparmor_socket_shutdown(struct socket *sock, int how)
1018{
1019 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1020}
1021
1022/**
1023 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
1024 *
1025 * Note: can not sleep may be called with locks held
1026 *
1027 * dont want protocol specific in __skb_recv_datagram()
1028 * to deny an incoming connection socket_sock_rcv_skb()
1029 */
1030static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1031{
1032 return 0;
1033}
1034
1035
1036static struct aa_label *sk_peer_label(struct sock *sk)
1037{
1038 struct aa_sk_ctx *ctx = SK_CTX(sk);
1039
1040 if (ctx->peer)
1041 return ctx->peer;
1042
1043 return ERR_PTR(-ENOPROTOOPT);
1044}
1045
1046/**
1047 * apparmor_socket_getpeersec_stream - get security context of peer
1048 *
1049 * Note: for tcp only valid if using ipsec or cipso on lan
1050 */
1051static int apparmor_socket_getpeersec_stream(struct socket *sock,
1052 char __user *optval,
1053 int __user *optlen,
1054 unsigned int len)
1055{
1056 char *name;
1057 int slen, error = 0;
1058 struct aa_label *label;
1059 struct aa_label *peer;
1060
1061 label = begin_current_label_crit_section();
1062 peer = sk_peer_label(sock->sk);
1063 if (IS_ERR(peer)) {
1064 error = PTR_ERR(peer);
1065 goto done;
1066 }
1067 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1068 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1069 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1070 /* don't include terminating \0 in slen, it breaks some apps */
1071 if (slen < 0) {
1072 error = -ENOMEM;
1073 } else {
1074 if (slen > len) {
1075 error = -ERANGE;
1076 } else if (copy_to_user(optval, name, slen)) {
1077 error = -EFAULT;
1078 goto out;
1079 }
1080 if (put_user(slen, optlen))
1081 error = -EFAULT;
1082out:
1083 kfree(name);
1084
1085 }
1086
1087done:
1088 end_current_label_crit_section(label);
1089
1090 return error;
1091}
1092
1093/**
1094 * apparmor_socket_getpeersec_dgram - get security label of packet
1095 * @sock: the peer socket
1096 * @skb: packet data
1097 * @secid: pointer to where to put the secid of the packet
1098 *
1099 * Sets the netlabel socket state on sk from parent
1100 */
1101static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1102 struct sk_buff *skb, u32 *secid)
1103
1104{
1105 /* TODO: requires secid support */
1106 return -ENOPROTOOPT;
1107}
1108
1109/**
1110 * apparmor_sock_graft - Initialize newly created socket
1111 * @sk: child sock
1112 * @parent: parent socket
1113 *
1114 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1115 * just set sk security information off of current creating process label
1116 * Labeling of sk for accept case - probably should be sock based
1117 * instead of task, because of the case where an implicitly labeled
1118 * socket is shared by different tasks.
1119 */
1120static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1121{
1122 struct aa_sk_ctx *ctx = SK_CTX(sk);
1123
1124 if (!ctx->label)
1125 ctx->label = aa_get_current_label();
1126}
1127
James Morrisca97d932017-02-15 00:18:51 +11001128static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07001129 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1130 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1131 LSM_HOOK_INIT(capget, apparmor_capget),
1132 LSM_HOOK_INIT(capable, apparmor_capable),
John Johansenb5e95b42010-07-29 14:48:07 -07001133
John Johansen2ea3ffb2017-07-18 23:04:47 -07001134 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1135 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1136 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1137
Casey Schauflere20b0432015-05-02 15:11:36 -07001138 LSM_HOOK_INIT(path_link, apparmor_path_link),
1139 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1140 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1141 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1142 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1143 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1144 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1145 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1146 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1147 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1148 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
John Johansenb5e95b42010-07-29 14:48:07 -07001149
Casey Schauflere20b0432015-05-02 15:11:36 -07001150 LSM_HOOK_INIT(file_open, apparmor_file_open),
John Johansen064dc942017-06-09 17:15:56 -07001151 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
Casey Schauflere20b0432015-05-02 15:11:36 -07001152 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1153 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1154 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1155 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07001156 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1157 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
John Johansenb5e95b42010-07-29 14:48:07 -07001158
Casey Schauflere20b0432015-05-02 15:11:36 -07001159 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1160 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
John Johansenb5e95b42010-07-29 14:48:07 -07001161
John Johansen56974a62017-07-18 23:18:33 -07001162 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1163 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1164 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1165
1166 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1167 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1168 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1169 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1170 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1171 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1172 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1173 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1174 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1175 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1176 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1177 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1178 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1179 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1180 LSM_HOOK_INIT(socket_getpeersec_stream,
1181 apparmor_socket_getpeersec_stream),
1182 LSM_HOOK_INIT(socket_getpeersec_dgram,
1183 apparmor_socket_getpeersec_dgram),
1184 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1185
Casey Schauflere20b0432015-05-02 15:11:36 -07001186 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1187 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1188 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1189 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
John Johansenb5e95b42010-07-29 14:48:07 -07001190
Casey Schauflere20b0432015-05-02 15:11:36 -07001191 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
1192 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1193 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
John Johansenb5e95b42010-07-29 14:48:07 -07001194
John Johansen3b529a72017-01-20 01:59:25 -08001195 LSM_HOOK_INIT(task_free, apparmor_task_free),
1196 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
John Johansena7ae3642017-09-11 11:29:53 -07001197 LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07001198 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
John Johansencd1dbf72017-07-18 22:56:22 -07001199 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
John Johansenc0929212017-07-31 17:36:45 -07001200
1201 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1202 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1203 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
John Johansenb5e95b42010-07-29 14:48:07 -07001204};
1205
1206/*
1207 * AppArmor sysfs module parameters
1208 */
1209
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001210static int param_set_aabool(const char *val, const struct kernel_param *kp);
1211static int param_get_aabool(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301212#define param_check_aabool param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301213static const struct kernel_param_ops param_ops_aabool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +09301214 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001215 .set = param_set_aabool,
1216 .get = param_get_aabool
1217};
John Johansenb5e95b42010-07-29 14:48:07 -07001218
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001219static int param_set_aauint(const char *val, const struct kernel_param *kp);
1220static int param_get_aauint(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301221#define param_check_aauint param_check_uint
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301222static const struct kernel_param_ops param_ops_aauint = {
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001223 .set = param_set_aauint,
1224 .get = param_get_aauint
1225};
John Johansenb5e95b42010-07-29 14:48:07 -07001226
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001227static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1228static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301229#define param_check_aalockpolicy param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301230static const struct kernel_param_ops param_ops_aalockpolicy = {
Jani Nikula6a4c2642014-08-27 06:21:23 +09301231 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001232 .set = param_set_aalockpolicy,
1233 .get = param_get_aalockpolicy
1234};
John Johansenb5e95b42010-07-29 14:48:07 -07001235
Kees Cooke4dca7b2017-10-17 19:04:42 -07001236static int param_set_audit(const char *val, const struct kernel_param *kp);
1237static int param_get_audit(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -07001238
Kees Cooke4dca7b2017-10-17 19:04:42 -07001239static int param_set_mode(const char *val, const struct kernel_param *kp);
1240static int param_get_mode(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -07001241
1242/* Flag values, also controllable via /sys/module/apparmor/parameters
1243 * We define special types as we want to do additional mediation.
1244 */
1245
1246/* AppArmor global enforcement switch - complain, enforce, kill */
1247enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1248module_param_call(mode, param_set_mode, param_get_mode,
1249 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1250
John Johansen6059f712014-10-24 09:16:14 -07001251/* whether policy verification hashing is enabled */
Arnd Bergmann7616ac72016-07-25 10:59:07 -07001252bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
John Johansen3ccb76c2017-01-16 13:21:27 -08001253#ifdef CONFIG_SECURITY_APPARMOR_HASH
John Johansen6059f712014-10-24 09:16:14 -07001254module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
Arnd Bergmann7616ac72016-07-25 10:59:07 -07001255#endif
John Johansen6059f712014-10-24 09:16:14 -07001256
John Johansenb5e95b42010-07-29 14:48:07 -07001257/* Debug mode */
Valentin Rothbergeea7a052017-04-06 06:55:20 -07001258bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
John Johansenb5e95b42010-07-29 14:48:07 -07001259module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1260
1261/* Audit mode */
1262enum audit_mode aa_g_audit;
1263module_param_call(audit, param_set_audit, param_get_audit,
1264 &aa_g_audit, S_IRUSR | S_IWUSR);
1265
1266/* Determines if audit header is included in audited messages. This
1267 * provides more context if the audit daemon is not running
1268 */
Thomas Meyer954317f2017-10-07 16:02:21 +02001269bool aa_g_audit_header = true;
John Johansenb5e95b42010-07-29 14:48:07 -07001270module_param_named(audit_header, aa_g_audit_header, aabool,
1271 S_IRUSR | S_IWUSR);
1272
1273/* lock out loading/removal of policy
1274 * TODO: add in at boot loading of policy, which is the only way to
1275 * load policy, if lock_policy is set
1276 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301277bool aa_g_lock_policy;
John Johansenb5e95b42010-07-29 14:48:07 -07001278module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1279 S_IRUSR | S_IWUSR);
1280
1281/* Syscall logging mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301282bool aa_g_logsyscall;
John Johansenb5e95b42010-07-29 14:48:07 -07001283module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1284
1285/* Maximum pathname length before accesses will start getting rejected */
1286unsigned int aa_g_path_max = 2 * PATH_MAX;
John Johansen622f6e32017-04-06 06:55:24 -07001287module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
John Johansenb5e95b42010-07-29 14:48:07 -07001288
1289/* Determines how paranoid loading of policy is and how much verification
1290 * on the loaded policy is done.
John Johansenabbf8732017-01-16 00:42:37 -08001291 * DEPRECATED: read only as strict checking of load is always done now
1292 * that none root users (user namespaces) can load policy.
John Johansenb5e95b42010-07-29 14:48:07 -07001293 */
Thomas Meyer954317f2017-10-07 16:02:21 +02001294bool aa_g_paranoid_load = true;
John Johansenabbf8732017-01-16 00:42:37 -08001295module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -07001296
1297/* Boot time disable flag */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301298static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
John Johansenc6116162013-07-10 21:03:43 -07001299module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -07001300
1301static int __init apparmor_enabled_setup(char *str)
1302{
1303 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +09001304 int error = kstrtoul(str, 0, &enabled);
John Johansenb5e95b42010-07-29 14:48:07 -07001305 if (!error)
1306 apparmor_enabled = enabled ? 1 : 0;
1307 return 1;
1308}
1309
1310__setup("apparmor=", apparmor_enabled_setup);
1311
1312/* set global flag turning off the ability to load policy */
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001313static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001314{
John Johansen545de8f2017-04-06 06:55:23 -07001315 if (!apparmor_enabled)
1316 return -EINVAL;
1317 if (apparmor_initialized && !policy_admin_capable(NULL))
John Johansenb5e95b42010-07-29 14:48:07 -07001318 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001319 return param_set_bool(val, kp);
1320}
1321
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001322static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001323{
John Johansenca4bd5a2017-01-16 00:43:11 -08001324 if (!apparmor_enabled)
1325 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001326 if (apparmor_initialized && !policy_view_capable(NULL))
1327 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001328 return param_get_bool(buffer, kp);
1329}
1330
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001331static int param_set_aabool(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001332{
John Johansenca4bd5a2017-01-16 00:43:11 -08001333 if (!apparmor_enabled)
1334 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001335 if (apparmor_initialized && !policy_admin_capable(NULL))
1336 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001337 return param_set_bool(val, kp);
1338}
1339
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001340static int param_get_aabool(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001341{
John Johansenca4bd5a2017-01-16 00:43:11 -08001342 if (!apparmor_enabled)
1343 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001344 if (apparmor_initialized && !policy_view_capable(NULL))
1345 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001346 return param_get_bool(buffer, kp);
1347}
1348
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001349static int param_set_aauint(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001350{
John Johansen39d84822017-03-30 05:25:23 -07001351 int error;
1352
John Johansenca4bd5a2017-01-16 00:43:11 -08001353 if (!apparmor_enabled)
1354 return -EINVAL;
John Johansen39d84822017-03-30 05:25:23 -07001355 /* file is ro but enforce 2nd line check */
1356 if (apparmor_initialized)
John Johansen545de8f2017-04-06 06:55:23 -07001357 return -EPERM;
John Johansen39d84822017-03-30 05:25:23 -07001358
1359 error = param_set_uint(val, kp);
1360 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1361
1362 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001363}
1364
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001365static int param_get_aauint(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001366{
John Johansenca4bd5a2017-01-16 00:43:11 -08001367 if (!apparmor_enabled)
1368 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001369 if (apparmor_initialized && !policy_view_capable(NULL))
1370 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001371 return param_get_uint(buffer, kp);
1372}
1373
Kees Cooke4dca7b2017-10-17 19:04:42 -07001374static int param_get_audit(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001375{
John Johansenb5e95b42010-07-29 14:48:07 -07001376 if (!apparmor_enabled)
1377 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001378 if (apparmor_initialized && !policy_view_capable(NULL))
1379 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001380 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1381}
1382
Kees Cooke4dca7b2017-10-17 19:04:42 -07001383static int param_set_audit(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001384{
1385 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001386
1387 if (!apparmor_enabled)
1388 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001389 if (!val)
1390 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001391 if (apparmor_initialized && !policy_admin_capable(NULL))
1392 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001393
1394 for (i = 0; i < AUDIT_MAX_INDEX; i++) {
1395 if (strcmp(val, audit_mode_names[i]) == 0) {
1396 aa_g_audit = i;
1397 return 0;
1398 }
1399 }
1400
1401 return -EINVAL;
1402}
1403
Kees Cooke4dca7b2017-10-17 19:04:42 -07001404static int param_get_mode(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001405{
John Johansenb5e95b42010-07-29 14:48:07 -07001406 if (!apparmor_enabled)
1407 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001408 if (apparmor_initialized && !policy_view_capable(NULL))
1409 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001410
John Johansen0d259f02013-07-10 21:13:43 -07001411 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
John Johansenb5e95b42010-07-29 14:48:07 -07001412}
1413
Kees Cooke4dca7b2017-10-17 19:04:42 -07001414static int param_set_mode(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001415{
1416 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001417
1418 if (!apparmor_enabled)
1419 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001420 if (!val)
1421 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001422 if (apparmor_initialized && !policy_admin_capable(NULL))
1423 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001424
John Johansen0d259f02013-07-10 21:13:43 -07001425 for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
1426 if (strcmp(val, aa_profile_mode_names[i]) == 0) {
John Johansenb5e95b42010-07-29 14:48:07 -07001427 aa_g_profile_mode = i;
1428 return 0;
1429 }
1430 }
1431
1432 return -EINVAL;
1433}
1434
1435/*
1436 * AppArmor init functions
1437 */
1438
1439/**
John Johansen55a26eb2017-01-16 00:43:00 -08001440 * set_init_ctx - set a task context and profile on the first task.
John Johansenb5e95b42010-07-29 14:48:07 -07001441 *
1442 * TODO: allow setting an alternate profile than unconfined
1443 */
John Johansen55a26eb2017-01-16 00:43:00 -08001444static int __init set_init_ctx(void)
John Johansenb5e95b42010-07-29 14:48:07 -07001445{
1446 struct cred *cred = (struct cred *)current->real_cred;
John Johansen55a26eb2017-01-16 00:43:00 -08001447 struct aa_task_ctx *ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001448
John Johansenf1752212017-01-27 04:09:40 -08001449 ctx = aa_alloc_task_ctx(GFP_KERNEL);
John Johansen55a26eb2017-01-16 00:43:00 -08001450 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -07001451 return -ENOMEM;
1452
John Johansend9087c42017-01-27 03:53:53 -08001453 cred_label(cred) = aa_get_label(ns_unconfined(root_ns));
John Johansenf1752212017-01-27 04:09:40 -08001454 task_ctx(current) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001455
1456 return 0;
1457}
1458
John Johansend4669f02017-01-16 00:43:10 -08001459static void destroy_buffers(void)
1460{
1461 u32 i, j;
1462
1463 for_each_possible_cpu(i) {
1464 for_each_cpu_buffer(j) {
1465 kfree(per_cpu(aa_buffers, i).buf[j]);
1466 per_cpu(aa_buffers, i).buf[j] = NULL;
1467 }
1468 }
1469}
1470
1471static int __init alloc_buffers(void)
1472{
1473 u32 i, j;
1474
1475 for_each_possible_cpu(i) {
1476 for_each_cpu_buffer(j) {
1477 char *buffer;
1478
1479 if (cpu_to_node(i) > num_online_nodes())
1480 /* fallback to kmalloc for offline nodes */
1481 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1482 else
1483 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1484 cpu_to_node(i));
1485 if (!buffer) {
1486 destroy_buffers();
1487 return -ENOMEM;
1488 }
1489 per_cpu(aa_buffers, i).buf[j] = buffer;
1490 }
1491 }
1492
1493 return 0;
1494}
1495
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001496#ifdef CONFIG_SYSCTL
1497static int apparmor_dointvec(struct ctl_table *table, int write,
1498 void __user *buffer, size_t *lenp, loff_t *ppos)
1499{
1500 if (!policy_admin_capable(NULL))
1501 return -EPERM;
1502 if (!apparmor_enabled)
1503 return -EINVAL;
1504
1505 return proc_dointvec(table, write, buffer, lenp, ppos);
1506}
1507
1508static struct ctl_path apparmor_sysctl_path[] = {
1509 { .procname = "kernel", },
1510 { }
1511};
1512
1513static struct ctl_table apparmor_sysctl_table[] = {
1514 {
1515 .procname = "unprivileged_userns_apparmor_policy",
1516 .data = &unprivileged_userns_apparmor_policy,
1517 .maxlen = sizeof(int),
1518 .mode = 0600,
1519 .proc_handler = apparmor_dointvec,
1520 },
1521 { }
1522};
1523
1524static int __init apparmor_init_sysctl(void)
1525{
1526 return register_sysctl_paths(apparmor_sysctl_path,
1527 apparmor_sysctl_table) ? 0 : -ENOMEM;
1528}
1529#else
1530static inline int apparmor_init_sysctl(void)
1531{
1532 return 0;
1533}
1534#endif /* CONFIG_SYSCTL */
1535
John Johansenb5e95b42010-07-29 14:48:07 -07001536static int __init apparmor_init(void)
1537{
1538 int error;
1539
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001540 if (!apparmor_enabled || !security_module_enable("apparmor")) {
John Johansenb5e95b42010-07-29 14:48:07 -07001541 aa_info_message("AppArmor disabled by boot time parameter");
Thomas Meyer954317f2017-10-07 16:02:21 +02001542 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001543 return 0;
1544 }
1545
John Johansen11c236b2017-01-16 00:42:42 -08001546 error = aa_setup_dfa_engine();
1547 if (error) {
1548 AA_ERROR("Unable to setup dfa engine\n");
1549 goto alloc_out;
1550 }
1551
John Johansenb5e95b42010-07-29 14:48:07 -07001552 error = aa_alloc_root_ns();
1553 if (error) {
1554 AA_ERROR("Unable to allocate default profile namespace\n");
1555 goto alloc_out;
1556 }
1557
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001558 error = apparmor_init_sysctl();
1559 if (error) {
1560 AA_ERROR("Unable to register sysctls\n");
1561 goto alloc_out;
1562
1563 }
1564
John Johansend4669f02017-01-16 00:43:10 -08001565 error = alloc_buffers();
1566 if (error) {
1567 AA_ERROR("Unable to allocate work buffers\n");
1568 goto buffers_out;
1569 }
1570
John Johansen55a26eb2017-01-16 00:43:00 -08001571 error = set_init_ctx();
John Johansenb5e95b42010-07-29 14:48:07 -07001572 if (error) {
1573 AA_ERROR("Failed to set context on init task\n");
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001574 aa_free_root_ns();
John Johansend4669f02017-01-16 00:43:10 -08001575 goto buffers_out;
John Johansenb5e95b42010-07-29 14:48:07 -07001576 }
Casey Schauflerd69dece52017-01-18 17:09:05 -08001577 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1578 "apparmor");
John Johansenb5e95b42010-07-29 14:48:07 -07001579
1580 /* Report that AppArmor successfully initialized */
1581 apparmor_initialized = 1;
1582 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1583 aa_info_message("AppArmor initialized: complain mode enabled");
1584 else if (aa_g_profile_mode == APPARMOR_KILL)
1585 aa_info_message("AppArmor initialized: kill mode enabled");
1586 else
1587 aa_info_message("AppArmor initialized");
1588
1589 return error;
1590
John Johansend4669f02017-01-16 00:43:10 -08001591buffers_out:
1592 destroy_buffers();
1593
John Johansenb5e95b42010-07-29 14:48:07 -07001594alloc_out:
1595 aa_destroy_aafs();
John Johansen11c236b2017-01-16 00:42:42 -08001596 aa_teardown_dfa_engine();
John Johansenb5e95b42010-07-29 14:48:07 -07001597
Thomas Meyer954317f2017-10-07 16:02:21 +02001598 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001599 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001600}
1601
1602security_initcall(apparmor_init);