blob: fda36f3e3820d1b88e7c3fbecb82cbde0b5a1b8d [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>
William Huae025be02017-01-15 16:49:28 -080026#include <linux/kmemleak.h>
John Johansenb5e95b42010-07-29 14:48:07 -070027#include <net/sock.h>
28
29#include "include/apparmor.h"
30#include "include/apparmorfs.h"
31#include "include/audit.h"
32#include "include/capability.h"
33#include "include/context.h"
34#include "include/file.h"
35#include "include/ipc.h"
36#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 Johansenb5e95b42010-07-29 14:48:07 -070042
43/* Flag indicating whether initialization completed */
John Johansen545de8f2017-04-06 06:55:23 -070044int apparmor_initialized;
John Johansenb5e95b42010-07-29 14:48:07 -070045
John Johansend4669f02017-01-16 00:43:10 -080046DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
47
48
John Johansenb5e95b42010-07-29 14:48:07 -070049/*
50 * LSM hook functions
51 */
52
53/*
John Johansend9087c42017-01-27 03:53:53 -080054 * put the associated labels
John Johansenb5e95b42010-07-29 14:48:07 -070055 */
56static void apparmor_cred_free(struct cred *cred)
57{
John Johansend9087c42017-01-27 03:53:53 -080058 aa_put_label(cred_label(cred));
59 cred_label(cred) = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -070060}
61
62/*
63 * allocate the apparmor part of blank credentials
64 */
65static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
66{
John Johansend9087c42017-01-27 03:53:53 -080067 cred_label(cred) = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -070068 return 0;
69}
70
71/*
John Johansend9087c42017-01-27 03:53:53 -080072 * prepare new cred label for modification by prepare_cred block
John Johansenb5e95b42010-07-29 14:48:07 -070073 */
74static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
75 gfp_t gfp)
76{
John Johansend9087c42017-01-27 03:53:53 -080077 cred_label(new) = aa_get_newest_label(cred_label(old));
John Johansenb5e95b42010-07-29 14:48:07 -070078 return 0;
79}
80
81/*
82 * transfer the apparmor data to a blank set of creds
83 */
84static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
85{
John Johansend9087c42017-01-27 03:53:53 -080086 cred_label(new) = aa_get_newest_label(cred_label(old));
John Johansenb5e95b42010-07-29 14:48:07 -070087}
88
John Johansen3b529a72017-01-20 01:59:25 -080089static void apparmor_task_free(struct task_struct *task)
90{
91
92 aa_free_task_ctx(task_ctx(task));
93 task_ctx(task) = NULL;
94}
95
96static int apparmor_task_alloc(struct task_struct *task,
97 unsigned long clone_flags)
98{
99 struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL);
100
101 if (!new)
102 return -ENOMEM;
103
104 aa_dup_task_ctx(new, current_task_ctx());
105 task_ctx(task) = new;
106
107 return 0;
108}
109
John Johansenb5e95b42010-07-29 14:48:07 -0700110static int apparmor_ptrace_access_check(struct task_struct *child,
111 unsigned int mode)
112{
John Johansenb2d09ae2017-06-09 14:22:14 -0700113 struct aa_label *tracer, *tracee;
114 int error;
115
116 tracer = begin_current_label_crit_section();
117 tracee = aa_get_task_label(child);
118 error = aa_may_ptrace(tracer, tracee,
119 mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
120 aa_put_label(tracee);
121 end_current_label_crit_section(tracer);
122
123 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700124}
125
126static int apparmor_ptrace_traceme(struct task_struct *parent)
127{
John Johansenb2d09ae2017-06-09 14:22:14 -0700128 struct aa_label *tracer, *tracee;
129 int error;
130
131 tracee = begin_current_label_crit_section();
132 tracer = aa_get_task_label(parent);
133 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
134 aa_put_label(tracer);
135 end_current_label_crit_section(tracee);
136
137 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700138}
139
140/* Derived from security/commoncap.c:cap_capget */
141static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
142 kernel_cap_t *inheritable, kernel_cap_t *permitted)
143{
John Johansen637f6882017-06-09 08:14:28 -0700144 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700145 const struct cred *cred;
146
147 rcu_read_lock();
148 cred = __task_cred(target);
John Johansen637f6882017-06-09 08:14:28 -0700149 label = aa_get_newest_cred_label(cred);
John Johansenc70c86c2017-06-09 14:07:02 -0700150
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700151 /*
152 * cap_capget is stacked ahead of this and will
153 * initialize effective and permitted.
154 */
John Johansenc70c86c2017-06-09 14:07:02 -0700155 if (!unconfined(label)) {
156 struct aa_profile *profile;
157 struct label_it i;
158
159 label_for_each_confined(i, label, profile) {
160 if (COMPLAIN_MODE(profile))
161 continue;
162 *effective = cap_intersect(*effective,
163 profile->caps.allow);
164 *permitted = cap_intersect(*permitted,
165 profile->caps.allow);
166 }
John Johansenb5e95b42010-07-29 14:48:07 -0700167 }
168 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -0700169 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700170
171 return 0;
172}
173
Eric Paris6a9de492012-01-03 12:25:14 -0500174static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
175 int cap, int audit)
John Johansenb5e95b42010-07-29 14:48:07 -0700176{
John Johansen637f6882017-06-09 08:14:28 -0700177 struct aa_label *label;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700178 int error = 0;
179
John Johansen637f6882017-06-09 08:14:28 -0700180 label = aa_get_newest_cred_label(cred);
181 if (!unconfined(label))
John Johansenc70c86c2017-06-09 14:07:02 -0700182 error = aa_capable(label, cap, audit);
John Johansen637f6882017-06-09 08:14:28 -0700183 aa_put_label(label);
John Johansencf797c02017-06-09 02:08:28 -0700184
John Johansenb5e95b42010-07-29 14:48:07 -0700185 return error;
186}
187
188/**
189 * common_perm - basic common permission check wrapper fn for paths
190 * @op: operation being checked
191 * @path: path to check permission of (NOT NULL)
192 * @mask: requested permissions mask
193 * @cond: conditional info for the permission request (NOT NULL)
194 *
195 * Returns: %0 else error code if error or permission denied
196 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800197static int common_perm(const char *op, const struct path *path, u32 mask,
John Johansenb5e95b42010-07-29 14:48:07 -0700198 struct path_cond *cond)
199{
John Johansen637f6882017-06-09 08:14:28 -0700200 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700201 int error = 0;
202
John Johansen637f6882017-06-09 08:14:28 -0700203 label = __begin_current_label_crit_section();
204 if (!unconfined(label))
John Johansenaebd8732017-06-09 16:02:25 -0700205 error = aa_path_perm(op, label, path, 0, mask, cond);
John Johansen637f6882017-06-09 08:14:28 -0700206 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700207
208 return error;
209}
210
211/**
John Johansen31f75bf2017-01-16 00:43:07 -0800212 * common_perm_cond - common permission wrapper around inode cond
213 * @op: operation being checked
214 * @path: location to check (NOT NULL)
215 * @mask: requested permissions mask
216 *
217 * Returns: %0 else error code if error or permission denied
218 */
219static int common_perm_cond(const char *op, const struct path *path, u32 mask)
220{
221 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
222 d_backing_inode(path->dentry)->i_mode
223 };
224
225 if (!path_mediated_fs(path->dentry))
226 return 0;
227
228 return common_perm(op, path, mask, &cond);
229}
230
231/**
John Johansenb5e95b42010-07-29 14:48:07 -0700232 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
233 * @op: operation being checked
234 * @dir: directory of the dentry (NOT NULL)
235 * @dentry: dentry to check (NOT NULL)
236 * @mask: requested permissions mask
237 * @cond: conditional info for the permission request (NOT NULL)
238 *
239 * Returns: %0 else error code if error or permission denied
240 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800241static int common_perm_dir_dentry(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700242 struct dentry *dentry, u32 mask,
243 struct path_cond *cond)
244{
Kees Cook8486adf2016-12-16 17:04:13 -0800245 struct path path = { .mnt = dir->mnt, .dentry = dentry };
John Johansenb5e95b42010-07-29 14:48:07 -0700246
247 return common_perm(op, &path, mask, cond);
248}
249
250/**
John Johansenb5e95b42010-07-29 14:48:07 -0700251 * common_perm_rm - common permission wrapper for operations doing rm
252 * @op: operation being checked
253 * @dir: directory that the dentry is in (NOT NULL)
254 * @dentry: dentry being rm'd (NOT NULL)
255 * @mask: requested permission mask
256 *
257 * Returns: %0 else error code if error or permission denied
258 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800259static int common_perm_rm(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700260 struct dentry *dentry, u32 mask)
261{
David Howellsc6f493d2015-03-17 22:26:22 +0000262 struct inode *inode = d_backing_inode(dentry);
John Johansenb5e95b42010-07-29 14:48:07 -0700263 struct path_cond cond = { };
264
John Johansenefeee832017-01-16 00:42:28 -0800265 if (!inode || !path_mediated_fs(dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700266 return 0;
267
268 cond.uid = inode->i_uid;
269 cond.mode = inode->i_mode;
270
271 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
272}
273
274/**
275 * common_perm_create - common permission wrapper for operations doing create
276 * @op: operation being checked
277 * @dir: directory that dentry will be created in (NOT NULL)
278 * @dentry: dentry to create (NOT NULL)
279 * @mask: request permission mask
280 * @mode: created file mode
281 *
282 * Returns: %0 else error code if error or permission denied
283 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800284static int common_perm_create(const char *op, const struct path *dir,
Al Virod6b49f72016-03-25 15:10:04 -0400285 struct dentry *dentry, u32 mask, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700286{
287 struct path_cond cond = { current_fsuid(), mode };
288
John Johansenefeee832017-01-16 00:42:28 -0800289 if (!path_mediated_fs(dir->dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700290 return 0;
291
292 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
293}
294
Al Viro989f74e2016-03-25 15:13:39 -0400295static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700296{
297 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
298}
299
Al Virod3607752016-03-25 15:21:09 -0400300static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500301 umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700302{
303 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
304 S_IFDIR);
305}
306
Al Viro989f74e2016-03-25 15:13:39 -0400307static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700308{
309 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
310}
311
Al Virod3607752016-03-25 15:21:09 -0400312static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500313 umode_t mode, unsigned int dev)
John Johansenb5e95b42010-07-29 14:48:07 -0700314{
315 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
316}
317
Al Viro81f4c502016-03-25 14:22:01 -0400318static int apparmor_path_truncate(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700319{
John Johansene53cfe62017-05-26 15:07:22 -0700320 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700321}
322
Al Virod3607752016-03-25 15:21:09 -0400323static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
John Johansenb5e95b42010-07-29 14:48:07 -0700324 const char *old_name)
325{
326 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
327 S_IFLNK);
328}
329
Al Viro3ccee462016-03-25 15:27:45 -0400330static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700331 struct dentry *new_dentry)
332{
John Johansen637f6882017-06-09 08:14:28 -0700333 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700334 int error = 0;
335
John Johansenefeee832017-01-16 00:42:28 -0800336 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700337 return 0;
338
John Johansen637f6882017-06-09 08:14:28 -0700339 label = begin_current_label_crit_section();
340 if (!unconfined(label))
John Johansen80143702017-06-09 16:06:21 -0700341 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
John Johansen637f6882017-06-09 08:14:28 -0700342 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700343
John Johansenb5e95b42010-07-29 14:48:07 -0700344 return error;
345}
346
Al Viro3ccee462016-03-25 15:27:45 -0400347static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
348 const struct path *new_dir, struct dentry *new_dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700349{
John Johansen637f6882017-06-09 08:14:28 -0700350 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700351 int error = 0;
352
John Johansenefeee832017-01-16 00:42:28 -0800353 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700354 return 0;
355
John Johansen637f6882017-06-09 08:14:28 -0700356 label = begin_current_label_crit_section();
357 if (!unconfined(label)) {
Kees Cook8486adf2016-12-16 17:04:13 -0800358 struct path old_path = { .mnt = old_dir->mnt,
359 .dentry = old_dentry };
360 struct path new_path = { .mnt = new_dir->mnt,
361 .dentry = new_dentry };
David Howellsc6f493d2015-03-17 22:26:22 +0000362 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
363 d_backing_inode(old_dentry)->i_mode
John Johansenb5e95b42010-07-29 14:48:07 -0700364 };
365
John Johansenaebd8732017-06-09 16:02:25 -0700366 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
John Johansene53cfe62017-05-26 15:07:22 -0700367 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
368 AA_MAY_SETATTR | AA_MAY_DELETE,
John Johansenb5e95b42010-07-29 14:48:07 -0700369 &cond);
370 if (!error)
John Johansenaebd8732017-06-09 16:02:25 -0700371 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
John Johansene53cfe62017-05-26 15:07:22 -0700372 0, MAY_WRITE | AA_MAY_SETATTR |
John Johansenb5e95b42010-07-29 14:48:07 -0700373 AA_MAY_CREATE, &cond);
374
375 }
John Johansen637f6882017-06-09 08:14:28 -0700376 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700377
John Johansenb5e95b42010-07-29 14:48:07 -0700378 return error;
379}
380
Al Virobe01f9f2016-03-25 14:56:23 -0400381static int apparmor_path_chmod(const struct path *path, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700382{
John Johansen31f75bf2017-01-16 00:43:07 -0800383 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
John Johansenb5e95b42010-07-29 14:48:07 -0700384}
385
Al Viro7fd25da2016-03-25 14:44:41 -0400386static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
John Johansenb5e95b42010-07-29 14:48:07 -0700387{
John Johansen31f75bf2017-01-16 00:43:07 -0800388 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
John Johansenb5e95b42010-07-29 14:48:07 -0700389}
390
Al Viro3f7036a2015-03-08 19:28:30 -0400391static int apparmor_inode_getattr(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700392{
John Johansene53cfe62017-05-26 15:07:22 -0700393 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700394}
395
Eric Paris83d49852012-04-04 13:45:40 -0400396static int apparmor_file_open(struct file *file, const struct cred *cred)
John Johansenb5e95b42010-07-29 14:48:07 -0700397{
John Johansen637f6882017-06-09 08:14:28 -0700398 struct aa_file_ctx *fctx = file_ctx(file);
399 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700400 int error = 0;
401
John Johansenefeee832017-01-16 00:42:28 -0800402 if (!path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700403 return 0;
404
405 /* If in exec, permission is handled by bprm hooks.
406 * Cache permissions granted by the previous exec check, with
407 * implicit read and executable mmap which are required to
408 * actually execute the image.
409 */
410 if (current->in_execve) {
John Johansen55a26eb2017-01-16 00:43:00 -0800411 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
John Johansenb5e95b42010-07-29 14:48:07 -0700412 return 0;
413 }
414
John Johansen637f6882017-06-09 08:14:28 -0700415 label = aa_get_newest_cred_label(cred);
416 if (!unconfined(label)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500417 struct inode *inode = file_inode(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700418 struct path_cond cond = { inode->i_uid, inode->i_mode };
419
John Johansenaebd8732017-06-09 16:02:25 -0700420 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
John Johansenb5e95b42010-07-29 14:48:07 -0700421 aa_map_file_to_perms(file), &cond);
422 /* todo cache full allowed permissions set and state */
John Johansen55a26eb2017-01-16 00:43:00 -0800423 fctx->allow = aa_map_file_to_perms(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700424 }
John Johansen637f6882017-06-09 08:14:28 -0700425 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700426
427 return error;
428}
429
430static int apparmor_file_alloc_security(struct file *file)
431{
John Johansencf797c02017-06-09 02:08:28 -0700432 int error = 0;
433
John Johansenb5e95b42010-07-29 14:48:07 -0700434 /* freed by apparmor_file_free_security */
John Johansen637f6882017-06-09 08:14:28 -0700435 struct aa_label *label = begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700436 file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
John Johansen2835a132017-06-09 11:43:45 -0700437 if (!file_ctx(file))
438 error = -ENOMEM;
John Johansen637f6882017-06-09 08:14:28 -0700439 end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700440
John Johansencf797c02017-06-09 02:08:28 -0700441 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700442}
443
444static void apparmor_file_free_security(struct file *file)
445{
John Johansen2835a132017-06-09 11:43:45 -0700446 aa_free_file_ctx(file_ctx(file));
John Johansenb5e95b42010-07-29 14:48:07 -0700447}
448
John Johansen47f6e5c2017-01-16 00:43:01 -0800449static int common_file_perm(const char *op, struct file *file, u32 mask)
John Johansenb5e95b42010-07-29 14:48:07 -0700450{
John Johansen190a9512017-06-09 14:59:51 -0700451 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700452 int error = 0;
453
John Johansen192ca6b2017-06-09 11:58:42 -0700454 /* don't reaudit files closed during inheritance */
455 if (file->f_path.dentry == aa_null.dentry)
456 return -EACCES;
457
John Johansen637f6882017-06-09 08:14:28 -0700458 label = __begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700459 error = aa_file_perm(op, label, file, mask);
John Johansen637f6882017-06-09 08:14:28 -0700460 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700461
462 return error;
463}
464
John Johansen064dc942017-06-09 17:15:56 -0700465static int apparmor_file_receive(struct file *file)
466{
467 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
468}
469
John Johansenb5e95b42010-07-29 14:48:07 -0700470static int apparmor_file_permission(struct file *file, int mask)
471{
472 return common_file_perm(OP_FPERM, file, mask);
473}
474
475static int apparmor_file_lock(struct file *file, unsigned int cmd)
476{
477 u32 mask = AA_MAY_LOCK;
478
479 if (cmd == F_WRLCK)
480 mask |= MAY_WRITE;
481
482 return common_file_perm(OP_FLOCK, file, mask);
483}
484
John Johansen47f6e5c2017-01-16 00:43:01 -0800485static int common_mmap(const char *op, struct file *file, unsigned long prot,
John Johansenb5e95b42010-07-29 14:48:07 -0700486 unsigned long flags)
487{
John Johansenb5e95b42010-07-29 14:48:07 -0700488 int mask = 0;
489
John Johansen637f6882017-06-09 08:14:28 -0700490 if (!file || !file_ctx(file))
John Johansenb5e95b42010-07-29 14:48:07 -0700491 return 0;
492
493 if (prot & PROT_READ)
494 mask |= MAY_READ;
495 /*
496 * Private mappings don't require write perms since they don't
497 * write back to the files
498 */
499 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
500 mask |= MAY_WRITE;
501 if (prot & PROT_EXEC)
502 mask |= AA_EXEC_MMAP;
503
John Johansenb5e95b42010-07-29 14:48:07 -0700504 return common_file_perm(op, file, mask);
505}
506
Al Viroe5467852012-05-30 13:30:51 -0400507static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
508 unsigned long prot, unsigned long flags)
John Johansenb5e95b42010-07-29 14:48:07 -0700509{
John Johansenb5e95b42010-07-29 14:48:07 -0700510 return common_mmap(OP_FMMAP, file, prot, flags);
511}
512
513static int apparmor_file_mprotect(struct vm_area_struct *vma,
514 unsigned long reqprot, unsigned long prot)
515{
516 return common_mmap(OP_FMPROT, vma->vm_file, prot,
517 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
518}
519
John Johansen2ea3ffb2017-07-18 23:04:47 -0700520static int apparmor_sb_mount(const char *dev_name, const struct path *path,
521 const char *type, unsigned long flags, void *data)
522{
523 struct aa_label *label;
524 int error = 0;
525
526 /* Discard magic */
527 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
528 flags &= ~MS_MGC_MSK;
529
530 flags &= ~AA_MS_IGNORE_MASK;
531
532 label = __begin_current_label_crit_section();
533 if (!unconfined(label)) {
534 if (flags & MS_REMOUNT)
535 error = aa_remount(label, path, flags, data);
536 else if (flags & MS_BIND)
537 error = aa_bind_mount(label, path, dev_name, flags);
538 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
539 MS_UNBINDABLE))
540 error = aa_mount_change_type(label, path, flags);
541 else if (flags & MS_MOVE)
542 error = aa_move_mount(label, path, dev_name);
543 else
544 error = aa_new_mount(label, dev_name, path, type,
545 flags, data);
546 }
547 __end_current_label_crit_section(label);
548
549 return error;
550}
551
552static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
553{
554 struct aa_label *label;
555 int error = 0;
556
557 label = __begin_current_label_crit_section();
558 if (!unconfined(label))
559 error = aa_umount(label, mnt, flags);
560 __end_current_label_crit_section(label);
561
562 return error;
563}
564
565static int apparmor_sb_pivotroot(const struct path *old_path,
566 const struct path *new_path)
567{
568 struct aa_label *label;
569 int error = 0;
570
571 label = aa_get_current_label();
572 if (!unconfined(label))
573 error = aa_pivotroot(label, old_path, new_path);
574 aa_put_label(label);
575
576 return error;
577}
578
John Johansenb5e95b42010-07-29 14:48:07 -0700579static int apparmor_getprocattr(struct task_struct *task, char *name,
580 char **value)
581{
582 int error = -ENOENT;
John Johansenb5e95b42010-07-29 14:48:07 -0700583 /* released below */
584 const struct cred *cred = get_task_cred(task);
John Johansenf1752212017-01-27 04:09:40 -0800585 struct aa_task_ctx *ctx = current_task_ctx();
John Johansen637f6882017-06-09 08:14:28 -0700586 struct aa_label *label = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -0700587
588 if (strcmp(name, "current") == 0)
John Johansend9087c42017-01-27 03:53:53 -0800589 label = aa_get_newest_label(cred_label(cred));
John Johansenf1752212017-01-27 04:09:40 -0800590 else if (strcmp(name, "prev") == 0 && ctx->previous)
591 label = aa_get_newest_label(ctx->previous);
592 else if (strcmp(name, "exec") == 0 && ctx->onexec)
593 label = aa_get_newest_label(ctx->onexec);
John Johansenb5e95b42010-07-29 14:48:07 -0700594 else
595 error = -EINVAL;
596
John Johansen637f6882017-06-09 08:14:28 -0700597 if (label)
John Johansen76a1d262017-06-09 12:47:17 -0700598 error = aa_getprocattr(label, value);
John Johansen77b071b2013-07-10 21:07:43 -0700599
John Johansen637f6882017-06-09 08:14:28 -0700600 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700601 put_cred(cred);
602
603 return error;
604}
605
Stephen Smalleyb21507e2017-01-09 10:07:31 -0500606static int apparmor_setprocattr(const char *name, void *value,
607 size_t size)
John Johansenb5e95b42010-07-29 14:48:07 -0700608{
Vegard Nossume89b8082016-07-07 13:41:11 -0700609 char *command, *largs = NULL, *args = value;
John Johansenb5e95b42010-07-29 14:48:07 -0700610 size_t arg_size;
611 int error;
John Johansenef88a7a2017-01-16 00:43:02 -0800612 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700613
614 if (size == 0)
615 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700616
Vegard Nossume89b8082016-07-07 13:41:11 -0700617 /* AppArmor requires that the buffer must be null terminated atm */
618 if (args[size - 1] != '\0') {
619 /* null terminate */
620 largs = args = kmalloc(size + 1, GFP_KERNEL);
621 if (!args)
622 return -ENOMEM;
623 memcpy(args, value, size);
624 args[size] = '\0';
625 }
626
627 error = -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700628 args = strim(args);
629 command = strsep(&args, " ");
630 if (!args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700631 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700632 args = skip_spaces(args);
633 if (!*args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700634 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700635
John Johansend4d03f72016-07-09 23:46:33 -0700636 arg_size = size - (args - (largs ? largs : (char *) value));
John Johansenb5e95b42010-07-29 14:48:07 -0700637 if (strcmp(name, "current") == 0) {
638 if (strcmp(command, "changehat") == 0) {
639 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700640 AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700641 } else if (strcmp(command, "permhat") == 0) {
642 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700643 AA_CHANGE_TEST);
John Johansenb5e95b42010-07-29 14:48:07 -0700644 } else if (strcmp(command, "changeprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700645 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700646 } else if (strcmp(command, "permprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700647 error = aa_change_profile(args, AA_CHANGE_TEST);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700648 } else if (strcmp(command, "stack") == 0) {
649 error = aa_change_profile(args, AA_CHANGE_STACK);
John Johansen3eea57c2013-02-27 03:44:40 -0800650 } else
651 goto fail;
John Johansenb5e95b42010-07-29 14:48:07 -0700652 } else if (strcmp(name, "exec") == 0) {
John Johansen3eea57c2013-02-27 03:44:40 -0800653 if (strcmp(command, "exec") == 0)
John Johansendf8073c2017-06-09 11:36:48 -0700654 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700655 else if (strcmp(command, "stack") == 0)
656 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
657 AA_CHANGE_STACK));
John Johansen3eea57c2013-02-27 03:44:40 -0800658 else
659 goto fail;
660 } else
John Johansenb5e95b42010-07-29 14:48:07 -0700661 /* only support the "current" and "exec" process attributes */
Vegard Nossume89b8082016-07-07 13:41:11 -0700662 goto fail;
John Johansen3eea57c2013-02-27 03:44:40 -0800663
John Johansenb5e95b42010-07-29 14:48:07 -0700664 if (!error)
665 error = size;
Vegard Nossume89b8082016-07-07 13:41:11 -0700666out:
667 kfree(largs);
John Johansenb5e95b42010-07-29 14:48:07 -0700668 return error;
John Johansen3eea57c2013-02-27 03:44:40 -0800669
670fail:
John Johansen637f6882017-06-09 08:14:28 -0700671 aad(&sa)->label = begin_current_label_crit_section();
John Johansenef88a7a2017-01-16 00:43:02 -0800672 aad(&sa)->info = name;
673 aad(&sa)->error = error = -EINVAL;
John Johansen3eea57c2013-02-27 03:44:40 -0800674 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
John Johansen637f6882017-06-09 08:14:28 -0700675 end_current_label_crit_section(aad(&sa)->label);
Vegard Nossume89b8082016-07-07 13:41:11 -0700676 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700677}
678
John Johansenfe864822017-06-09 05:27:50 -0700679/**
680 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
681 * @bprm: binprm for the exec (NOT NULL)
682 */
683static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
684{
John Johansen637f6882017-06-09 08:14:28 -0700685 struct aa_label *label = aa_current_raw_label();
John Johansend9087c42017-01-27 03:53:53 -0800686 struct aa_label *new_label = cred_label(bprm->cred);
John Johansenfe864822017-06-09 05:27:50 -0700687
688 /* bail out if unconfined or not changing profile */
John Johansend9087c42017-01-27 03:53:53 -0800689 if ((new_label->proxy == label->proxy) ||
690 (unconfined(new_label)))
John Johansenfe864822017-06-09 05:27:50 -0700691 return;
692
John Johansen192ca6b2017-06-09 11:58:42 -0700693 aa_inherit_files(bprm->cred, current->files);
694
John Johansenfe864822017-06-09 05:27:50 -0700695 current->pdeath_signal = 0;
696
John Johansen637f6882017-06-09 08:14:28 -0700697 /* reset soft limits and set hard limits for the new label */
John Johansend9087c42017-01-27 03:53:53 -0800698 __aa_transition_rlimits(label, new_label);
John Johansenfe864822017-06-09 05:27:50 -0700699}
700
701/**
702 * apparmor_bprm_committed_cred - do cleanup after new creds committed
703 * @bprm: binprm for the exec (NOT NULL)
704 */
705static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
706{
John Johansen3b529a72017-01-20 01:59:25 -0800707 /* clear out temporary/transitional state from the context */
708 aa_clear_task_ctx_trans(current_task_ctx());
709
John Johansenfe864822017-06-09 05:27:50 -0700710 return;
711}
712
Jiri Slaby7cb4dc92010-08-11 11:28:02 +0200713static int apparmor_task_setrlimit(struct task_struct *task,
714 unsigned int resource, struct rlimit *new_rlim)
John Johansenb5e95b42010-07-29 14:48:07 -0700715{
John Johansen637f6882017-06-09 08:14:28 -0700716 struct aa_label *label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700717 int error = 0;
718
John Johansen637f6882017-06-09 08:14:28 -0700719 if (!unconfined(label))
John Johansen86b92cb2017-06-09 14:15:20 -0700720 error = aa_task_setrlimit(label, task, resource, new_rlim);
John Johansen637f6882017-06-09 08:14:28 -0700721 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700722
723 return error;
724}
725
John Johansencd1dbf72017-07-18 22:56:22 -0700726static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
727 int sig, u32 secid)
728{
729 struct aa_label *cl, *tl;
730 int error;
731
732 if (secid)
733 /* TODO: after secid to label mapping is done.
734 * Dealing with USB IO specific behavior
735 */
736 return 0;
737 cl = __begin_current_label_crit_section();
738 tl = aa_get_task_label(target);
739 error = aa_may_signal(cl, tl, sig);
740 aa_put_label(tl);
741 __end_current_label_crit_section(cl);
742
743 return error;
744}
745
James Morrisca97d932017-02-15 00:18:51 +1100746static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -0700747 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
748 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
749 LSM_HOOK_INIT(capget, apparmor_capget),
750 LSM_HOOK_INIT(capable, apparmor_capable),
John Johansenb5e95b42010-07-29 14:48:07 -0700751
John Johansen2ea3ffb2017-07-18 23:04:47 -0700752 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
753 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
754 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
755
Casey Schauflere20b0432015-05-02 15:11:36 -0700756 LSM_HOOK_INIT(path_link, apparmor_path_link),
757 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
758 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
759 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
760 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
761 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
762 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
763 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
764 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
765 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
766 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
John Johansenb5e95b42010-07-29 14:48:07 -0700767
Casey Schauflere20b0432015-05-02 15:11:36 -0700768 LSM_HOOK_INIT(file_open, apparmor_file_open),
John Johansen064dc942017-06-09 17:15:56 -0700769 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
Casey Schauflere20b0432015-05-02 15:11:36 -0700770 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
771 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
772 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
773 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
Casey Schauflere20b0432015-05-02 15:11:36 -0700774 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
775 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
John Johansenb5e95b42010-07-29 14:48:07 -0700776
Casey Schauflere20b0432015-05-02 15:11:36 -0700777 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
778 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
John Johansenb5e95b42010-07-29 14:48:07 -0700779
Casey Schauflere20b0432015-05-02 15:11:36 -0700780 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
781 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
782 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
783 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
John Johansenb5e95b42010-07-29 14:48:07 -0700784
Casey Schauflere20b0432015-05-02 15:11:36 -0700785 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
786 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
787 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
John Johansenb5e95b42010-07-29 14:48:07 -0700788
John Johansen3b529a72017-01-20 01:59:25 -0800789 LSM_HOOK_INIT(task_free, apparmor_task_free),
790 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -0700791 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
John Johansencd1dbf72017-07-18 22:56:22 -0700792 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
John Johansenb5e95b42010-07-29 14:48:07 -0700793};
794
795/*
796 * AppArmor sysfs module parameters
797 */
798
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000799static int param_set_aabool(const char *val, const struct kernel_param *kp);
800static int param_get_aabool(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030801#define param_check_aabool param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930802static const struct kernel_param_ops param_ops_aabool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930803 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000804 .set = param_set_aabool,
805 .get = param_get_aabool
806};
John Johansenb5e95b42010-07-29 14:48:07 -0700807
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000808static int param_set_aauint(const char *val, const struct kernel_param *kp);
809static int param_get_aauint(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030810#define param_check_aauint param_check_uint
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930811static const struct kernel_param_ops param_ops_aauint = {
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000812 .set = param_set_aauint,
813 .get = param_get_aauint
814};
John Johansenb5e95b42010-07-29 14:48:07 -0700815
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000816static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
817static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030818#define param_check_aalockpolicy param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930819static const struct kernel_param_ops param_ops_aalockpolicy = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930820 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000821 .set = param_set_aalockpolicy,
822 .get = param_get_aalockpolicy
823};
John Johansenb5e95b42010-07-29 14:48:07 -0700824
Kees Cooke4dca7b2017-10-17 19:04:42 -0700825static int param_set_audit(const char *val, const struct kernel_param *kp);
826static int param_get_audit(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -0700827
Kees Cooke4dca7b2017-10-17 19:04:42 -0700828static int param_set_mode(const char *val, const struct kernel_param *kp);
829static int param_get_mode(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -0700830
831/* Flag values, also controllable via /sys/module/apparmor/parameters
832 * We define special types as we want to do additional mediation.
833 */
834
835/* AppArmor global enforcement switch - complain, enforce, kill */
836enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
837module_param_call(mode, param_set_mode, param_get_mode,
838 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
839
John Johansen6059f712014-10-24 09:16:14 -0700840/* whether policy verification hashing is enabled */
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700841bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
John Johansen3ccb76c2017-01-16 13:21:27 -0800842#ifdef CONFIG_SECURITY_APPARMOR_HASH
John Johansen6059f712014-10-24 09:16:14 -0700843module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700844#endif
John Johansen6059f712014-10-24 09:16:14 -0700845
John Johansenb5e95b42010-07-29 14:48:07 -0700846/* Debug mode */
Valentin Rothbergeea7a052017-04-06 06:55:20 -0700847bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
John Johansenb5e95b42010-07-29 14:48:07 -0700848module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
849
850/* Audit mode */
851enum audit_mode aa_g_audit;
852module_param_call(audit, param_set_audit, param_get_audit,
853 &aa_g_audit, S_IRUSR | S_IWUSR);
854
855/* Determines if audit header is included in audited messages. This
856 * provides more context if the audit daemon is not running
857 */
Thomas Meyer954317f2017-10-07 16:02:21 +0200858bool aa_g_audit_header = true;
John Johansenb5e95b42010-07-29 14:48:07 -0700859module_param_named(audit_header, aa_g_audit_header, aabool,
860 S_IRUSR | S_IWUSR);
861
862/* lock out loading/removal of policy
863 * TODO: add in at boot loading of policy, which is the only way to
864 * load policy, if lock_policy is set
865 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030866bool aa_g_lock_policy;
John Johansenb5e95b42010-07-29 14:48:07 -0700867module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
868 S_IRUSR | S_IWUSR);
869
870/* Syscall logging mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030871bool aa_g_logsyscall;
John Johansenb5e95b42010-07-29 14:48:07 -0700872module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
873
874/* Maximum pathname length before accesses will start getting rejected */
875unsigned int aa_g_path_max = 2 * PATH_MAX;
John Johansen622f6e32017-04-06 06:55:24 -0700876module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
John Johansenb5e95b42010-07-29 14:48:07 -0700877
878/* Determines how paranoid loading of policy is and how much verification
879 * on the loaded policy is done.
John Johansenabbf8732017-01-16 00:42:37 -0800880 * DEPRECATED: read only as strict checking of load is always done now
881 * that none root users (user namespaces) can load policy.
John Johansenb5e95b42010-07-29 14:48:07 -0700882 */
Thomas Meyer954317f2017-10-07 16:02:21 +0200883bool aa_g_paranoid_load = true;
John Johansenabbf8732017-01-16 00:42:37 -0800884module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -0700885
886/* Boot time disable flag */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030887static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
John Johansenc6116162013-07-10 21:03:43 -0700888module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -0700889
890static int __init apparmor_enabled_setup(char *str)
891{
892 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900893 int error = kstrtoul(str, 0, &enabled);
John Johansenb5e95b42010-07-29 14:48:07 -0700894 if (!error)
895 apparmor_enabled = enabled ? 1 : 0;
896 return 1;
897}
898
899__setup("apparmor=", apparmor_enabled_setup);
900
901/* set global flag turning off the ability to load policy */
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000902static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700903{
John Johansen545de8f2017-04-06 06:55:23 -0700904 if (!apparmor_enabled)
905 return -EINVAL;
906 if (apparmor_initialized && !policy_admin_capable(NULL))
John Johansenb5e95b42010-07-29 14:48:07 -0700907 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700908 return param_set_bool(val, kp);
909}
910
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000911static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700912{
John Johansenca4bd5a2017-01-16 00:43:11 -0800913 if (!apparmor_enabled)
914 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700915 if (apparmor_initialized && !policy_view_capable(NULL))
916 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700917 return param_get_bool(buffer, kp);
918}
919
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000920static int param_set_aabool(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700921{
John Johansenca4bd5a2017-01-16 00:43:11 -0800922 if (!apparmor_enabled)
923 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700924 if (apparmor_initialized && !policy_admin_capable(NULL))
925 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700926 return param_set_bool(val, kp);
927}
928
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000929static int param_get_aabool(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700930{
John Johansenca4bd5a2017-01-16 00:43:11 -0800931 if (!apparmor_enabled)
932 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700933 if (apparmor_initialized && !policy_view_capable(NULL))
934 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700935 return param_get_bool(buffer, kp);
936}
937
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000938static int param_set_aauint(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700939{
John Johansen39d84822017-03-30 05:25:23 -0700940 int error;
941
John Johansenca4bd5a2017-01-16 00:43:11 -0800942 if (!apparmor_enabled)
943 return -EINVAL;
John Johansen39d84822017-03-30 05:25:23 -0700944 /* file is ro but enforce 2nd line check */
945 if (apparmor_initialized)
John Johansen545de8f2017-04-06 06:55:23 -0700946 return -EPERM;
John Johansen39d84822017-03-30 05:25:23 -0700947
948 error = param_set_uint(val, kp);
949 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
950
951 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700952}
953
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000954static int param_get_aauint(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700955{
John Johansenca4bd5a2017-01-16 00:43:11 -0800956 if (!apparmor_enabled)
957 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700958 if (apparmor_initialized && !policy_view_capable(NULL))
959 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700960 return param_get_uint(buffer, kp);
961}
962
Kees Cooke4dca7b2017-10-17 19:04:42 -0700963static int param_get_audit(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700964{
John Johansenb5e95b42010-07-29 14:48:07 -0700965 if (!apparmor_enabled)
966 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700967 if (apparmor_initialized && !policy_view_capable(NULL))
968 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700969 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
970}
971
Kees Cooke4dca7b2017-10-17 19:04:42 -0700972static int param_set_audit(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700973{
974 int i;
John Johansenb5e95b42010-07-29 14:48:07 -0700975
976 if (!apparmor_enabled)
977 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700978 if (!val)
979 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700980 if (apparmor_initialized && !policy_admin_capable(NULL))
981 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700982
983 for (i = 0; i < AUDIT_MAX_INDEX; i++) {
984 if (strcmp(val, audit_mode_names[i]) == 0) {
985 aa_g_audit = i;
986 return 0;
987 }
988 }
989
990 return -EINVAL;
991}
992
Kees Cooke4dca7b2017-10-17 19:04:42 -0700993static int param_get_mode(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700994{
John Johansenb5e95b42010-07-29 14:48:07 -0700995 if (!apparmor_enabled)
996 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700997 if (apparmor_initialized && !policy_view_capable(NULL))
998 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700999
John Johansen0d259f02013-07-10 21:13:43 -07001000 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
John Johansenb5e95b42010-07-29 14:48:07 -07001001}
1002
Kees Cooke4dca7b2017-10-17 19:04:42 -07001003static int param_set_mode(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001004{
1005 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001006
1007 if (!apparmor_enabled)
1008 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001009 if (!val)
1010 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001011 if (apparmor_initialized && !policy_admin_capable(NULL))
1012 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001013
John Johansen0d259f02013-07-10 21:13:43 -07001014 for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
1015 if (strcmp(val, aa_profile_mode_names[i]) == 0) {
John Johansenb5e95b42010-07-29 14:48:07 -07001016 aa_g_profile_mode = i;
1017 return 0;
1018 }
1019 }
1020
1021 return -EINVAL;
1022}
1023
1024/*
1025 * AppArmor init functions
1026 */
1027
1028/**
John Johansen55a26eb2017-01-16 00:43:00 -08001029 * set_init_ctx - set a task context and profile on the first task.
John Johansenb5e95b42010-07-29 14:48:07 -07001030 *
1031 * TODO: allow setting an alternate profile than unconfined
1032 */
John Johansen55a26eb2017-01-16 00:43:00 -08001033static int __init set_init_ctx(void)
John Johansenb5e95b42010-07-29 14:48:07 -07001034{
1035 struct cred *cred = (struct cred *)current->real_cred;
John Johansenf1752212017-01-27 04:09:40 -08001036 struct aa_task_ctx *ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001037
John Johansenf1752212017-01-27 04:09:40 -08001038 ctx = aa_alloc_task_ctx(GFP_KERNEL);
1039 if (!ctx)
John Johansend9087c42017-01-27 03:53:53 -08001040 return -ENOMEM;
John Johansenb5e95b42010-07-29 14:48:07 -07001041
John Johansend9087c42017-01-27 03:53:53 -08001042 cred_label(cred) = aa_get_label(ns_unconfined(root_ns));
John Johansenf1752212017-01-27 04:09:40 -08001043 task_ctx(current) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001044
1045 return 0;
1046}
1047
John Johansend4669f02017-01-16 00:43:10 -08001048static void destroy_buffers(void)
1049{
1050 u32 i, j;
1051
1052 for_each_possible_cpu(i) {
1053 for_each_cpu_buffer(j) {
1054 kfree(per_cpu(aa_buffers, i).buf[j]);
1055 per_cpu(aa_buffers, i).buf[j] = NULL;
1056 }
1057 }
1058}
1059
1060static int __init alloc_buffers(void)
1061{
1062 u32 i, j;
1063
1064 for_each_possible_cpu(i) {
1065 for_each_cpu_buffer(j) {
1066 char *buffer;
1067
1068 if (cpu_to_node(i) > num_online_nodes())
1069 /* fallback to kmalloc for offline nodes */
1070 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1071 else
1072 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1073 cpu_to_node(i));
1074 if (!buffer) {
1075 destroy_buffers();
1076 return -ENOMEM;
1077 }
1078 per_cpu(aa_buffers, i).buf[j] = buffer;
1079 }
1080 }
1081
1082 return 0;
1083}
1084
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001085#ifdef CONFIG_SYSCTL
1086static int apparmor_dointvec(struct ctl_table *table, int write,
1087 void __user *buffer, size_t *lenp, loff_t *ppos)
1088{
1089 if (!policy_admin_capable(NULL))
1090 return -EPERM;
1091 if (!apparmor_enabled)
1092 return -EINVAL;
1093
1094 return proc_dointvec(table, write, buffer, lenp, ppos);
1095}
1096
1097static struct ctl_path apparmor_sysctl_path[] = {
1098 { .procname = "kernel", },
1099 { }
1100};
1101
1102static struct ctl_table apparmor_sysctl_table[] = {
1103 {
1104 .procname = "unprivileged_userns_apparmor_policy",
1105 .data = &unprivileged_userns_apparmor_policy,
1106 .maxlen = sizeof(int),
1107 .mode = 0600,
1108 .proc_handler = apparmor_dointvec,
1109 },
1110 { }
1111};
1112
1113static int __init apparmor_init_sysctl(void)
1114{
1115 return register_sysctl_paths(apparmor_sysctl_path,
1116 apparmor_sysctl_table) ? 0 : -ENOMEM;
1117}
1118#else
1119static inline int apparmor_init_sysctl(void)
1120{
1121 return 0;
1122}
1123#endif /* CONFIG_SYSCTL */
1124
John Johansenb5e95b42010-07-29 14:48:07 -07001125static int __init apparmor_init(void)
1126{
1127 int error;
1128
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001129 if (!apparmor_enabled || !security_module_enable("apparmor")) {
John Johansenb5e95b42010-07-29 14:48:07 -07001130 aa_info_message("AppArmor disabled by boot time parameter");
Thomas Meyer954317f2017-10-07 16:02:21 +02001131 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001132 return 0;
1133 }
1134
John Johansen11c236b2017-01-16 00:42:42 -08001135 error = aa_setup_dfa_engine();
1136 if (error) {
1137 AA_ERROR("Unable to setup dfa engine\n");
1138 goto alloc_out;
1139 }
1140
John Johansenb5e95b42010-07-29 14:48:07 -07001141 error = aa_alloc_root_ns();
1142 if (error) {
1143 AA_ERROR("Unable to allocate default profile namespace\n");
1144 goto alloc_out;
1145 }
1146
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001147 error = apparmor_init_sysctl();
1148 if (error) {
1149 AA_ERROR("Unable to register sysctls\n");
1150 goto alloc_out;
1151
1152 }
1153
John Johansend4669f02017-01-16 00:43:10 -08001154 error = alloc_buffers();
1155 if (error) {
1156 AA_ERROR("Unable to allocate work buffers\n");
1157 goto buffers_out;
1158 }
1159
John Johansen55a26eb2017-01-16 00:43:00 -08001160 error = set_init_ctx();
John Johansenb5e95b42010-07-29 14:48:07 -07001161 if (error) {
1162 AA_ERROR("Failed to set context on init task\n");
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001163 aa_free_root_ns();
John Johansend4669f02017-01-16 00:43:10 -08001164 goto buffers_out;
John Johansenb5e95b42010-07-29 14:48:07 -07001165 }
Casey Schauflerd69dece52017-01-18 17:09:05 -08001166 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1167 "apparmor");
John Johansenb5e95b42010-07-29 14:48:07 -07001168
1169 /* Report that AppArmor successfully initialized */
1170 apparmor_initialized = 1;
1171 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1172 aa_info_message("AppArmor initialized: complain mode enabled");
1173 else if (aa_g_profile_mode == APPARMOR_KILL)
1174 aa_info_message("AppArmor initialized: kill mode enabled");
1175 else
1176 aa_info_message("AppArmor initialized");
1177
1178 return error;
1179
John Johansend4669f02017-01-16 00:43:10 -08001180buffers_out:
1181 destroy_buffers();
1182
John Johansenb5e95b42010-07-29 14:48:07 -07001183alloc_out:
1184 aa_destroy_aafs();
John Johansen11c236b2017-01-16 00:42:42 -08001185 aa_teardown_dfa_engine();
John Johansenb5e95b42010-07-29 14:48:07 -07001186
Thomas Meyer954317f2017-10-07 16:02:21 +02001187 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001188 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001189}
1190
1191security_initcall(apparmor_init);