blob: 9a65eeaf7dfa22ab3b76d05b1445c56031195cd3 [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 Johansen637f6882017-06-09 08:14:28 -070054 * free the associated aa_task_ctx and put its labels
John Johansenb5e95b42010-07-29 14:48:07 -070055 */
56static void apparmor_cred_free(struct cred *cred)
57{
John Johansen55a26eb2017-01-16 00:43:00 -080058 aa_free_task_context(cred_ctx(cred));
59 cred_ctx(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{
67 /* freed by apparmor_cred_free */
John Johansen55a26eb2017-01-16 00:43:00 -080068 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
69
70 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -070071 return -ENOMEM;
72
John Johansen55a26eb2017-01-16 00:43:00 -080073 cred_ctx(cred) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -070074 return 0;
75}
76
77/*
John Johansen55a26eb2017-01-16 00:43:00 -080078 * prepare new aa_task_ctx for modification by prepare_cred block
John Johansenb5e95b42010-07-29 14:48:07 -070079 */
80static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
81 gfp_t gfp)
82{
83 /* freed by apparmor_cred_free */
John Johansen55a26eb2017-01-16 00:43:00 -080084 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
85
86 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -070087 return -ENOMEM;
88
John Johansen55a26eb2017-01-16 00:43:00 -080089 aa_dup_task_context(ctx, cred_ctx(old));
90 cred_ctx(new) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -070091 return 0;
92}
93
94/*
95 * transfer the apparmor data to a blank set of creds
96 */
97static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
98{
John Johansen55a26eb2017-01-16 00:43:00 -080099 const struct aa_task_ctx *old_ctx = cred_ctx(old);
100 struct aa_task_ctx *new_ctx = cred_ctx(new);
John Johansenb5e95b42010-07-29 14:48:07 -0700101
John Johansen55a26eb2017-01-16 00:43:00 -0800102 aa_dup_task_context(new_ctx, old_ctx);
John Johansenb5e95b42010-07-29 14:48:07 -0700103}
104
105static int apparmor_ptrace_access_check(struct task_struct *child,
106 unsigned int mode)
107{
John Johansenb2d09ae2017-06-09 14:22:14 -0700108 struct aa_label *tracer, *tracee;
109 int error;
110
111 tracer = begin_current_label_crit_section();
112 tracee = aa_get_task_label(child);
113 error = aa_may_ptrace(tracer, tracee,
114 mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
115 aa_put_label(tracee);
116 end_current_label_crit_section(tracer);
117
118 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700119}
120
121static int apparmor_ptrace_traceme(struct task_struct *parent)
122{
John Johansenb2d09ae2017-06-09 14:22:14 -0700123 struct aa_label *tracer, *tracee;
124 int error;
125
126 tracee = begin_current_label_crit_section();
127 tracer = aa_get_task_label(parent);
128 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
129 aa_put_label(tracer);
130 end_current_label_crit_section(tracee);
131
132 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700133}
134
135/* Derived from security/commoncap.c:cap_capget */
136static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
137 kernel_cap_t *inheritable, kernel_cap_t *permitted)
138{
John Johansen637f6882017-06-09 08:14:28 -0700139 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700140 const struct cred *cred;
141
142 rcu_read_lock();
143 cred = __task_cred(target);
John Johansen637f6882017-06-09 08:14:28 -0700144 label = aa_get_newest_cred_label(cred);
John Johansenc70c86c2017-06-09 14:07:02 -0700145
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700146 /*
147 * cap_capget is stacked ahead of this and will
148 * initialize effective and permitted.
149 */
John Johansenc70c86c2017-06-09 14:07:02 -0700150 if (!unconfined(label)) {
151 struct aa_profile *profile;
152 struct label_it i;
153
154 label_for_each_confined(i, label, profile) {
155 if (COMPLAIN_MODE(profile))
156 continue;
157 *effective = cap_intersect(*effective,
158 profile->caps.allow);
159 *permitted = cap_intersect(*permitted,
160 profile->caps.allow);
161 }
John Johansenb5e95b42010-07-29 14:48:07 -0700162 }
163 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -0700164 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700165
166 return 0;
167}
168
Eric Paris6a9de492012-01-03 12:25:14 -0500169static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
170 int cap, int audit)
John Johansenb5e95b42010-07-29 14:48:07 -0700171{
John Johansen637f6882017-06-09 08:14:28 -0700172 struct aa_label *label;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700173 int error = 0;
174
John Johansen637f6882017-06-09 08:14:28 -0700175 label = aa_get_newest_cred_label(cred);
176 if (!unconfined(label))
John Johansenc70c86c2017-06-09 14:07:02 -0700177 error = aa_capable(label, cap, audit);
John Johansen637f6882017-06-09 08:14:28 -0700178 aa_put_label(label);
John Johansencf797c02017-06-09 02:08:28 -0700179
John Johansenb5e95b42010-07-29 14:48:07 -0700180 return error;
181}
182
183/**
184 * common_perm - basic common permission check wrapper fn for paths
185 * @op: operation being checked
186 * @path: path to check permission of (NOT NULL)
187 * @mask: requested permissions mask
188 * @cond: conditional info for the permission request (NOT NULL)
189 *
190 * Returns: %0 else error code if error or permission denied
191 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800192static int common_perm(const char *op, const struct path *path, u32 mask,
John Johansenb5e95b42010-07-29 14:48:07 -0700193 struct path_cond *cond)
194{
John Johansen637f6882017-06-09 08:14:28 -0700195 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700196 int error = 0;
197
John Johansen637f6882017-06-09 08:14:28 -0700198 label = __begin_current_label_crit_section();
199 if (!unconfined(label))
John Johansenaebd8732017-06-09 16:02:25 -0700200 error = aa_path_perm(op, label, path, 0, mask, cond);
John Johansen637f6882017-06-09 08:14:28 -0700201 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700202
203 return error;
204}
205
206/**
John Johansen31f75bf2017-01-16 00:43:07 -0800207 * common_perm_cond - common permission wrapper around inode cond
208 * @op: operation being checked
209 * @path: location to check (NOT NULL)
210 * @mask: requested permissions mask
211 *
212 * Returns: %0 else error code if error or permission denied
213 */
214static int common_perm_cond(const char *op, const struct path *path, u32 mask)
215{
216 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
217 d_backing_inode(path->dentry)->i_mode
218 };
219
220 if (!path_mediated_fs(path->dentry))
221 return 0;
222
223 return common_perm(op, path, mask, &cond);
224}
225
226/**
John Johansenb5e95b42010-07-29 14:48:07 -0700227 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
228 * @op: operation being checked
229 * @dir: directory of the dentry (NOT NULL)
230 * @dentry: dentry to check (NOT NULL)
231 * @mask: requested permissions mask
232 * @cond: conditional info for the permission request (NOT NULL)
233 *
234 * Returns: %0 else error code if error or permission denied
235 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800236static int common_perm_dir_dentry(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700237 struct dentry *dentry, u32 mask,
238 struct path_cond *cond)
239{
Kees Cook8486adf2016-12-16 17:04:13 -0800240 struct path path = { .mnt = dir->mnt, .dentry = dentry };
John Johansenb5e95b42010-07-29 14:48:07 -0700241
242 return common_perm(op, &path, mask, cond);
243}
244
245/**
John Johansenb5e95b42010-07-29 14:48:07 -0700246 * common_perm_rm - common permission wrapper for operations doing rm
247 * @op: operation being checked
248 * @dir: directory that the dentry is in (NOT NULL)
249 * @dentry: dentry being rm'd (NOT NULL)
250 * @mask: requested permission mask
251 *
252 * Returns: %0 else error code if error or permission denied
253 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800254static int common_perm_rm(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700255 struct dentry *dentry, u32 mask)
256{
David Howellsc6f493d2015-03-17 22:26:22 +0000257 struct inode *inode = d_backing_inode(dentry);
John Johansenb5e95b42010-07-29 14:48:07 -0700258 struct path_cond cond = { };
259
John Johansenefeee832017-01-16 00:42:28 -0800260 if (!inode || !path_mediated_fs(dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700261 return 0;
262
263 cond.uid = inode->i_uid;
264 cond.mode = inode->i_mode;
265
266 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
267}
268
269/**
270 * common_perm_create - common permission wrapper for operations doing create
271 * @op: operation being checked
272 * @dir: directory that dentry will be created in (NOT NULL)
273 * @dentry: dentry to create (NOT NULL)
274 * @mask: request permission mask
275 * @mode: created file mode
276 *
277 * Returns: %0 else error code if error or permission denied
278 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800279static int common_perm_create(const char *op, const struct path *dir,
Al Virod6b49f72016-03-25 15:10:04 -0400280 struct dentry *dentry, u32 mask, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700281{
282 struct path_cond cond = { current_fsuid(), mode };
283
John Johansenefeee832017-01-16 00:42:28 -0800284 if (!path_mediated_fs(dir->dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700285 return 0;
286
287 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
288}
289
Al Viro989f74e2016-03-25 15:13:39 -0400290static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700291{
292 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
293}
294
Al Virod3607752016-03-25 15:21:09 -0400295static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500296 umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700297{
298 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
299 S_IFDIR);
300}
301
Al Viro989f74e2016-03-25 15:13:39 -0400302static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700303{
304 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
305}
306
Al Virod3607752016-03-25 15:21:09 -0400307static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500308 umode_t mode, unsigned int dev)
John Johansenb5e95b42010-07-29 14:48:07 -0700309{
310 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
311}
312
Al Viro81f4c502016-03-25 14:22:01 -0400313static int apparmor_path_truncate(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700314{
John Johansene53cfe62017-05-26 15:07:22 -0700315 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700316}
317
Al Virod3607752016-03-25 15:21:09 -0400318static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
John Johansenb5e95b42010-07-29 14:48:07 -0700319 const char *old_name)
320{
321 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
322 S_IFLNK);
323}
324
Al Viro3ccee462016-03-25 15:27:45 -0400325static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700326 struct dentry *new_dentry)
327{
John Johansen637f6882017-06-09 08:14:28 -0700328 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700329 int error = 0;
330
John Johansenefeee832017-01-16 00:42:28 -0800331 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700332 return 0;
333
John Johansen637f6882017-06-09 08:14:28 -0700334 label = begin_current_label_crit_section();
335 if (!unconfined(label))
John Johansen80143702017-06-09 16:06:21 -0700336 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
John Johansen637f6882017-06-09 08:14:28 -0700337 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700338
John Johansenb5e95b42010-07-29 14:48:07 -0700339 return error;
340}
341
Al Viro3ccee462016-03-25 15:27:45 -0400342static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
343 const struct path *new_dir, struct dentry *new_dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700344{
John Johansen637f6882017-06-09 08:14:28 -0700345 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700346 int error = 0;
347
John Johansenefeee832017-01-16 00:42:28 -0800348 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700349 return 0;
350
John Johansen637f6882017-06-09 08:14:28 -0700351 label = begin_current_label_crit_section();
352 if (!unconfined(label)) {
Kees Cook8486adf2016-12-16 17:04:13 -0800353 struct path old_path = { .mnt = old_dir->mnt,
354 .dentry = old_dentry };
355 struct path new_path = { .mnt = new_dir->mnt,
356 .dentry = new_dentry };
David Howellsc6f493d2015-03-17 22:26:22 +0000357 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
358 d_backing_inode(old_dentry)->i_mode
John Johansenb5e95b42010-07-29 14:48:07 -0700359 };
360
John Johansenaebd8732017-06-09 16:02:25 -0700361 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
John Johansene53cfe62017-05-26 15:07:22 -0700362 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
363 AA_MAY_SETATTR | AA_MAY_DELETE,
John Johansenb5e95b42010-07-29 14:48:07 -0700364 &cond);
365 if (!error)
John Johansenaebd8732017-06-09 16:02:25 -0700366 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
John Johansene53cfe62017-05-26 15:07:22 -0700367 0, MAY_WRITE | AA_MAY_SETATTR |
John Johansenb5e95b42010-07-29 14:48:07 -0700368 AA_MAY_CREATE, &cond);
369
370 }
John Johansen637f6882017-06-09 08:14:28 -0700371 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700372
John Johansenb5e95b42010-07-29 14:48:07 -0700373 return error;
374}
375
Al Virobe01f9f2016-03-25 14:56:23 -0400376static int apparmor_path_chmod(const struct path *path, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700377{
John Johansen31f75bf2017-01-16 00:43:07 -0800378 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
John Johansenb5e95b42010-07-29 14:48:07 -0700379}
380
Al Viro7fd25da2016-03-25 14:44:41 -0400381static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
John Johansenb5e95b42010-07-29 14:48:07 -0700382{
John Johansen31f75bf2017-01-16 00:43:07 -0800383 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
John Johansenb5e95b42010-07-29 14:48:07 -0700384}
385
Al Viro3f7036a2015-03-08 19:28:30 -0400386static int apparmor_inode_getattr(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700387{
John Johansene53cfe62017-05-26 15:07:22 -0700388 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700389}
390
Eric Paris83d49852012-04-04 13:45:40 -0400391static int apparmor_file_open(struct file *file, const struct cred *cred)
John Johansenb5e95b42010-07-29 14:48:07 -0700392{
John Johansen637f6882017-06-09 08:14:28 -0700393 struct aa_file_ctx *fctx = file_ctx(file);
394 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700395 int error = 0;
396
John Johansenefeee832017-01-16 00:42:28 -0800397 if (!path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700398 return 0;
399
400 /* If in exec, permission is handled by bprm hooks.
401 * Cache permissions granted by the previous exec check, with
402 * implicit read and executable mmap which are required to
403 * actually execute the image.
404 */
405 if (current->in_execve) {
John Johansen55a26eb2017-01-16 00:43:00 -0800406 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
John Johansenb5e95b42010-07-29 14:48:07 -0700407 return 0;
408 }
409
John Johansen637f6882017-06-09 08:14:28 -0700410 label = aa_get_newest_cred_label(cred);
411 if (!unconfined(label)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500412 struct inode *inode = file_inode(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700413 struct path_cond cond = { inode->i_uid, inode->i_mode };
414
John Johansenaebd8732017-06-09 16:02:25 -0700415 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
John Johansenb5e95b42010-07-29 14:48:07 -0700416 aa_map_file_to_perms(file), &cond);
417 /* todo cache full allowed permissions set and state */
John Johansen55a26eb2017-01-16 00:43:00 -0800418 fctx->allow = aa_map_file_to_perms(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700419 }
John Johansen637f6882017-06-09 08:14:28 -0700420 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700421
422 return error;
423}
424
425static int apparmor_file_alloc_security(struct file *file)
426{
John Johansencf797c02017-06-09 02:08:28 -0700427 int error = 0;
428
John Johansenb5e95b42010-07-29 14:48:07 -0700429 /* freed by apparmor_file_free_security */
John Johansen637f6882017-06-09 08:14:28 -0700430 struct aa_label *label = begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700431 file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
John Johansen2835a132017-06-09 11:43:45 -0700432 if (!file_ctx(file))
433 error = -ENOMEM;
John Johansen637f6882017-06-09 08:14:28 -0700434 end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700435
John Johansencf797c02017-06-09 02:08:28 -0700436 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700437}
438
439static void apparmor_file_free_security(struct file *file)
440{
John Johansen2835a132017-06-09 11:43:45 -0700441 aa_free_file_ctx(file_ctx(file));
John Johansenb5e95b42010-07-29 14:48:07 -0700442}
443
John Johansen47f6e5c2017-01-16 00:43:01 -0800444static int common_file_perm(const char *op, struct file *file, u32 mask)
John Johansenb5e95b42010-07-29 14:48:07 -0700445{
John Johansen190a9512017-06-09 14:59:51 -0700446 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700447 int error = 0;
448
John Johansen192ca6b2017-06-09 11:58:42 -0700449 /* don't reaudit files closed during inheritance */
450 if (file->f_path.dentry == aa_null.dentry)
451 return -EACCES;
452
John Johansen637f6882017-06-09 08:14:28 -0700453 label = __begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700454 error = aa_file_perm(op, label, file, mask);
John Johansen637f6882017-06-09 08:14:28 -0700455 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700456
457 return error;
458}
459
John Johansen064dc942017-06-09 17:15:56 -0700460static int apparmor_file_receive(struct file *file)
461{
462 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
463}
464
John Johansenb5e95b42010-07-29 14:48:07 -0700465static int apparmor_file_permission(struct file *file, int mask)
466{
467 return common_file_perm(OP_FPERM, file, mask);
468}
469
470static int apparmor_file_lock(struct file *file, unsigned int cmd)
471{
472 u32 mask = AA_MAY_LOCK;
473
474 if (cmd == F_WRLCK)
475 mask |= MAY_WRITE;
476
477 return common_file_perm(OP_FLOCK, file, mask);
478}
479
John Johansen47f6e5c2017-01-16 00:43:01 -0800480static int common_mmap(const char *op, struct file *file, unsigned long prot,
John Johansenb5e95b42010-07-29 14:48:07 -0700481 unsigned long flags)
482{
John Johansenb5e95b42010-07-29 14:48:07 -0700483 int mask = 0;
484
John Johansen637f6882017-06-09 08:14:28 -0700485 if (!file || !file_ctx(file))
John Johansenb5e95b42010-07-29 14:48:07 -0700486 return 0;
487
488 if (prot & PROT_READ)
489 mask |= MAY_READ;
490 /*
491 * Private mappings don't require write perms since they don't
492 * write back to the files
493 */
494 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
495 mask |= MAY_WRITE;
496 if (prot & PROT_EXEC)
497 mask |= AA_EXEC_MMAP;
498
John Johansenb5e95b42010-07-29 14:48:07 -0700499 return common_file_perm(op, file, mask);
500}
501
Al Viroe5467852012-05-30 13:30:51 -0400502static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
503 unsigned long prot, unsigned long flags)
John Johansenb5e95b42010-07-29 14:48:07 -0700504{
John Johansenb5e95b42010-07-29 14:48:07 -0700505 return common_mmap(OP_FMMAP, file, prot, flags);
506}
507
508static int apparmor_file_mprotect(struct vm_area_struct *vma,
509 unsigned long reqprot, unsigned long prot)
510{
511 return common_mmap(OP_FMPROT, vma->vm_file, prot,
512 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
513}
514
John Johansen2ea3ffb2017-07-18 23:04:47 -0700515static int apparmor_sb_mount(const char *dev_name, const struct path *path,
516 const char *type, unsigned long flags, void *data)
517{
518 struct aa_label *label;
519 int error = 0;
520
521 /* Discard magic */
522 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
523 flags &= ~MS_MGC_MSK;
524
525 flags &= ~AA_MS_IGNORE_MASK;
526
527 label = __begin_current_label_crit_section();
528 if (!unconfined(label)) {
529 if (flags & MS_REMOUNT)
530 error = aa_remount(label, path, flags, data);
531 else if (flags & MS_BIND)
532 error = aa_bind_mount(label, path, dev_name, flags);
533 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
534 MS_UNBINDABLE))
535 error = aa_mount_change_type(label, path, flags);
536 else if (flags & MS_MOVE)
537 error = aa_move_mount(label, path, dev_name);
538 else
539 error = aa_new_mount(label, dev_name, path, type,
540 flags, data);
541 }
542 __end_current_label_crit_section(label);
543
544 return error;
545}
546
547static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
548{
549 struct aa_label *label;
550 int error = 0;
551
552 label = __begin_current_label_crit_section();
553 if (!unconfined(label))
554 error = aa_umount(label, mnt, flags);
555 __end_current_label_crit_section(label);
556
557 return error;
558}
559
560static int apparmor_sb_pivotroot(const struct path *old_path,
561 const struct path *new_path)
562{
563 struct aa_label *label;
564 int error = 0;
565
566 label = aa_get_current_label();
567 if (!unconfined(label))
568 error = aa_pivotroot(label, old_path, new_path);
569 aa_put_label(label);
570
571 return error;
572}
573
John Johansenb5e95b42010-07-29 14:48:07 -0700574static int apparmor_getprocattr(struct task_struct *task, char *name,
575 char **value)
576{
577 int error = -ENOENT;
John Johansenb5e95b42010-07-29 14:48:07 -0700578 /* released below */
579 const struct cred *cred = get_task_cred(task);
John Johansen55a26eb2017-01-16 00:43:00 -0800580 struct aa_task_ctx *ctx = cred_ctx(cred);
John Johansen637f6882017-06-09 08:14:28 -0700581 struct aa_label *label = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -0700582
583 if (strcmp(name, "current") == 0)
John Johansen637f6882017-06-09 08:14:28 -0700584 label = aa_get_newest_label(ctx->label);
John Johansen55a26eb2017-01-16 00:43:00 -0800585 else if (strcmp(name, "prev") == 0 && ctx->previous)
John Johansen637f6882017-06-09 08:14:28 -0700586 label = aa_get_newest_label(ctx->previous);
John Johansen55a26eb2017-01-16 00:43:00 -0800587 else if (strcmp(name, "exec") == 0 && ctx->onexec)
John Johansen637f6882017-06-09 08:14:28 -0700588 label = aa_get_newest_label(ctx->onexec);
John Johansenb5e95b42010-07-29 14:48:07 -0700589 else
590 error = -EINVAL;
591
John Johansen637f6882017-06-09 08:14:28 -0700592 if (label)
John Johansen76a1d262017-06-09 12:47:17 -0700593 error = aa_getprocattr(label, value);
John Johansen77b071b2013-07-10 21:07:43 -0700594
John Johansen637f6882017-06-09 08:14:28 -0700595 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700596 put_cred(cred);
597
598 return error;
599}
600
Stephen Smalleyb21507e2017-01-09 10:07:31 -0500601static int apparmor_setprocattr(const char *name, void *value,
602 size_t size)
John Johansenb5e95b42010-07-29 14:48:07 -0700603{
Vegard Nossume89b8082016-07-07 13:41:11 -0700604 char *command, *largs = NULL, *args = value;
John Johansenb5e95b42010-07-29 14:48:07 -0700605 size_t arg_size;
606 int error;
John Johansenef88a7a2017-01-16 00:43:02 -0800607 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700608
609 if (size == 0)
610 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700611
Vegard Nossume89b8082016-07-07 13:41:11 -0700612 /* AppArmor requires that the buffer must be null terminated atm */
613 if (args[size - 1] != '\0') {
614 /* null terminate */
615 largs = args = kmalloc(size + 1, GFP_KERNEL);
616 if (!args)
617 return -ENOMEM;
618 memcpy(args, value, size);
619 args[size] = '\0';
620 }
621
622 error = -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700623 args = strim(args);
624 command = strsep(&args, " ");
625 if (!args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700626 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700627 args = skip_spaces(args);
628 if (!*args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700629 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700630
John Johansend4d03f72016-07-09 23:46:33 -0700631 arg_size = size - (args - (largs ? largs : (char *) value));
John Johansenb5e95b42010-07-29 14:48:07 -0700632 if (strcmp(name, "current") == 0) {
633 if (strcmp(command, "changehat") == 0) {
634 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700635 AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700636 } else if (strcmp(command, "permhat") == 0) {
637 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700638 AA_CHANGE_TEST);
John Johansenb5e95b42010-07-29 14:48:07 -0700639 } else if (strcmp(command, "changeprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700640 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700641 } else if (strcmp(command, "permprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700642 error = aa_change_profile(args, AA_CHANGE_TEST);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700643 } else if (strcmp(command, "stack") == 0) {
644 error = aa_change_profile(args, AA_CHANGE_STACK);
John Johansen3eea57c2013-02-27 03:44:40 -0800645 } else
646 goto fail;
John Johansenb5e95b42010-07-29 14:48:07 -0700647 } else if (strcmp(name, "exec") == 0) {
John Johansen3eea57c2013-02-27 03:44:40 -0800648 if (strcmp(command, "exec") == 0)
John Johansendf8073c2017-06-09 11:36:48 -0700649 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700650 else if (strcmp(command, "stack") == 0)
651 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
652 AA_CHANGE_STACK));
John Johansen3eea57c2013-02-27 03:44:40 -0800653 else
654 goto fail;
655 } else
John Johansenb5e95b42010-07-29 14:48:07 -0700656 /* only support the "current" and "exec" process attributes */
Vegard Nossume89b8082016-07-07 13:41:11 -0700657 goto fail;
John Johansen3eea57c2013-02-27 03:44:40 -0800658
John Johansenb5e95b42010-07-29 14:48:07 -0700659 if (!error)
660 error = size;
Vegard Nossume89b8082016-07-07 13:41:11 -0700661out:
662 kfree(largs);
John Johansenb5e95b42010-07-29 14:48:07 -0700663 return error;
John Johansen3eea57c2013-02-27 03:44:40 -0800664
665fail:
John Johansen637f6882017-06-09 08:14:28 -0700666 aad(&sa)->label = begin_current_label_crit_section();
John Johansenef88a7a2017-01-16 00:43:02 -0800667 aad(&sa)->info = name;
668 aad(&sa)->error = error = -EINVAL;
John Johansen3eea57c2013-02-27 03:44:40 -0800669 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
John Johansen637f6882017-06-09 08:14:28 -0700670 end_current_label_crit_section(aad(&sa)->label);
Vegard Nossume89b8082016-07-07 13:41:11 -0700671 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700672}
673
John Johansenfe864822017-06-09 05:27:50 -0700674/**
675 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
676 * @bprm: binprm for the exec (NOT NULL)
677 */
678static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
679{
John Johansen637f6882017-06-09 08:14:28 -0700680 struct aa_label *label = aa_current_raw_label();
John Johansenfe864822017-06-09 05:27:50 -0700681 struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
682
683 /* bail out if unconfined or not changing profile */
John Johansen637f6882017-06-09 08:14:28 -0700684 if ((new_ctx->label->proxy == label->proxy) ||
685 (unconfined(new_ctx->label)))
John Johansenfe864822017-06-09 05:27:50 -0700686 return;
687
John Johansen192ca6b2017-06-09 11:58:42 -0700688 aa_inherit_files(bprm->cred, current->files);
689
John Johansenfe864822017-06-09 05:27:50 -0700690 current->pdeath_signal = 0;
691
John Johansen637f6882017-06-09 08:14:28 -0700692 /* reset soft limits and set hard limits for the new label */
John Johansen86b92cb2017-06-09 14:15:20 -0700693 __aa_transition_rlimits(label, new_ctx->label);
John Johansenfe864822017-06-09 05:27:50 -0700694}
695
696/**
697 * apparmor_bprm_committed_cred - do cleanup after new creds committed
698 * @bprm: binprm for the exec (NOT NULL)
699 */
700static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
701{
702 /* TODO: cleanup signals - ipc mediation */
703 return;
704}
705
Jiri Slaby7cb4dc92010-08-11 11:28:02 +0200706static int apparmor_task_setrlimit(struct task_struct *task,
707 unsigned int resource, struct rlimit *new_rlim)
John Johansenb5e95b42010-07-29 14:48:07 -0700708{
John Johansen637f6882017-06-09 08:14:28 -0700709 struct aa_label *label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700710 int error = 0;
711
John Johansen637f6882017-06-09 08:14:28 -0700712 if (!unconfined(label))
John Johansen86b92cb2017-06-09 14:15:20 -0700713 error = aa_task_setrlimit(label, task, resource, new_rlim);
John Johansen637f6882017-06-09 08:14:28 -0700714 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700715
716 return error;
717}
718
John Johansencd1dbf72017-07-18 22:56:22 -0700719static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
720 int sig, u32 secid)
721{
722 struct aa_label *cl, *tl;
723 int error;
724
725 if (secid)
726 /* TODO: after secid to label mapping is done.
727 * Dealing with USB IO specific behavior
728 */
729 return 0;
730 cl = __begin_current_label_crit_section();
731 tl = aa_get_task_label(target);
732 error = aa_may_signal(cl, tl, sig);
733 aa_put_label(tl);
734 __end_current_label_crit_section(cl);
735
736 return error;
737}
738
James Morrisca97d932017-02-15 00:18:51 +1100739static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -0700740 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
741 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
742 LSM_HOOK_INIT(capget, apparmor_capget),
743 LSM_HOOK_INIT(capable, apparmor_capable),
John Johansenb5e95b42010-07-29 14:48:07 -0700744
John Johansen2ea3ffb2017-07-18 23:04:47 -0700745 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
746 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
747 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
748
Casey Schauflere20b0432015-05-02 15:11:36 -0700749 LSM_HOOK_INIT(path_link, apparmor_path_link),
750 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
751 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
752 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
753 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
754 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
755 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
756 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
757 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
758 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
759 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
John Johansenb5e95b42010-07-29 14:48:07 -0700760
Casey Schauflere20b0432015-05-02 15:11:36 -0700761 LSM_HOOK_INIT(file_open, apparmor_file_open),
John Johansen064dc942017-06-09 17:15:56 -0700762 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
Casey Schauflere20b0432015-05-02 15:11:36 -0700763 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
764 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
765 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
766 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
Casey Schauflere20b0432015-05-02 15:11:36 -0700767 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
768 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
John Johansenb5e95b42010-07-29 14:48:07 -0700769
Casey Schauflere20b0432015-05-02 15:11:36 -0700770 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
771 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
John Johansenb5e95b42010-07-29 14:48:07 -0700772
Casey Schauflere20b0432015-05-02 15:11:36 -0700773 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
774 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
775 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
776 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
John Johansenb5e95b42010-07-29 14:48:07 -0700777
Casey Schauflere20b0432015-05-02 15:11:36 -0700778 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
779 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
780 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
John Johansenb5e95b42010-07-29 14:48:07 -0700781
Casey Schauflere20b0432015-05-02 15:11:36 -0700782 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
John Johansencd1dbf72017-07-18 22:56:22 -0700783 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
John Johansenb5e95b42010-07-29 14:48:07 -0700784};
785
786/*
787 * AppArmor sysfs module parameters
788 */
789
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000790static int param_set_aabool(const char *val, const struct kernel_param *kp);
791static int param_get_aabool(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030792#define param_check_aabool param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930793static const struct kernel_param_ops param_ops_aabool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930794 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000795 .set = param_set_aabool,
796 .get = param_get_aabool
797};
John Johansenb5e95b42010-07-29 14:48:07 -0700798
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000799static int param_set_aauint(const char *val, const struct kernel_param *kp);
800static int param_get_aauint(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030801#define param_check_aauint param_check_uint
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930802static const struct kernel_param_ops param_ops_aauint = {
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000803 .set = param_set_aauint,
804 .get = param_get_aauint
805};
John Johansenb5e95b42010-07-29 14:48:07 -0700806
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000807static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
808static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030809#define param_check_aalockpolicy param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930810static const struct kernel_param_ops param_ops_aalockpolicy = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930811 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000812 .set = param_set_aalockpolicy,
813 .get = param_get_aalockpolicy
814};
John Johansenb5e95b42010-07-29 14:48:07 -0700815
Kees Cooke4dca7b2017-10-17 19:04:42 -0700816static int param_set_audit(const char *val, const struct kernel_param *kp);
817static int param_get_audit(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -0700818
Kees Cooke4dca7b2017-10-17 19:04:42 -0700819static int param_set_mode(const char *val, const struct kernel_param *kp);
820static int param_get_mode(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -0700821
822/* Flag values, also controllable via /sys/module/apparmor/parameters
823 * We define special types as we want to do additional mediation.
824 */
825
826/* AppArmor global enforcement switch - complain, enforce, kill */
827enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
828module_param_call(mode, param_set_mode, param_get_mode,
829 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
830
John Johansen6059f712014-10-24 09:16:14 -0700831/* whether policy verification hashing is enabled */
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700832bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
John Johansen3ccb76c2017-01-16 13:21:27 -0800833#ifdef CONFIG_SECURITY_APPARMOR_HASH
John Johansen6059f712014-10-24 09:16:14 -0700834module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700835#endif
John Johansen6059f712014-10-24 09:16:14 -0700836
John Johansenb5e95b42010-07-29 14:48:07 -0700837/* Debug mode */
Valentin Rothbergeea7a052017-04-06 06:55:20 -0700838bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
John Johansenb5e95b42010-07-29 14:48:07 -0700839module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
840
841/* Audit mode */
842enum audit_mode aa_g_audit;
843module_param_call(audit, param_set_audit, param_get_audit,
844 &aa_g_audit, S_IRUSR | S_IWUSR);
845
846/* Determines if audit header is included in audited messages. This
847 * provides more context if the audit daemon is not running
848 */
Thomas Meyer954317f2017-10-07 16:02:21 +0200849bool aa_g_audit_header = true;
John Johansenb5e95b42010-07-29 14:48:07 -0700850module_param_named(audit_header, aa_g_audit_header, aabool,
851 S_IRUSR | S_IWUSR);
852
853/* lock out loading/removal of policy
854 * TODO: add in at boot loading of policy, which is the only way to
855 * load policy, if lock_policy is set
856 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030857bool aa_g_lock_policy;
John Johansenb5e95b42010-07-29 14:48:07 -0700858module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
859 S_IRUSR | S_IWUSR);
860
861/* Syscall logging mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030862bool aa_g_logsyscall;
John Johansenb5e95b42010-07-29 14:48:07 -0700863module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
864
865/* Maximum pathname length before accesses will start getting rejected */
866unsigned int aa_g_path_max = 2 * PATH_MAX;
John Johansen622f6e32017-04-06 06:55:24 -0700867module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
John Johansenb5e95b42010-07-29 14:48:07 -0700868
869/* Determines how paranoid loading of policy is and how much verification
870 * on the loaded policy is done.
John Johansenabbf8732017-01-16 00:42:37 -0800871 * DEPRECATED: read only as strict checking of load is always done now
872 * that none root users (user namespaces) can load policy.
John Johansenb5e95b42010-07-29 14:48:07 -0700873 */
Thomas Meyer954317f2017-10-07 16:02:21 +0200874bool aa_g_paranoid_load = true;
John Johansenabbf8732017-01-16 00:42:37 -0800875module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -0700876
877/* Boot time disable flag */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030878static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
John Johansenc6116162013-07-10 21:03:43 -0700879module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -0700880
881static int __init apparmor_enabled_setup(char *str)
882{
883 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900884 int error = kstrtoul(str, 0, &enabled);
John Johansenb5e95b42010-07-29 14:48:07 -0700885 if (!error)
886 apparmor_enabled = enabled ? 1 : 0;
887 return 1;
888}
889
890__setup("apparmor=", apparmor_enabled_setup);
891
892/* set global flag turning off the ability to load policy */
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000893static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700894{
John Johansen545de8f2017-04-06 06:55:23 -0700895 if (!apparmor_enabled)
896 return -EINVAL;
897 if (apparmor_initialized && !policy_admin_capable(NULL))
John Johansenb5e95b42010-07-29 14:48:07 -0700898 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700899 return param_set_bool(val, kp);
900}
901
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000902static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700903{
John Johansenca4bd5a2017-01-16 00:43:11 -0800904 if (!apparmor_enabled)
905 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700906 if (apparmor_initialized && !policy_view_capable(NULL))
907 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700908 return param_get_bool(buffer, kp);
909}
910
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000911static int param_set_aabool(const char *val, 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_admin_capable(NULL))
916 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700917 return param_set_bool(val, kp);
918}
919
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000920static int param_get_aabool(char *buffer, 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_view_capable(NULL))
925 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700926 return param_get_bool(buffer, kp);
927}
928
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000929static int param_set_aauint(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700930{
John Johansen39d84822017-03-30 05:25:23 -0700931 int error;
932
John Johansenca4bd5a2017-01-16 00:43:11 -0800933 if (!apparmor_enabled)
934 return -EINVAL;
John Johansen39d84822017-03-30 05:25:23 -0700935 /* file is ro but enforce 2nd line check */
936 if (apparmor_initialized)
John Johansen545de8f2017-04-06 06:55:23 -0700937 return -EPERM;
John Johansen39d84822017-03-30 05:25:23 -0700938
939 error = param_set_uint(val, kp);
940 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
941
942 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700943}
944
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000945static int param_get_aauint(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700946{
John Johansenca4bd5a2017-01-16 00:43:11 -0800947 if (!apparmor_enabled)
948 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700949 if (apparmor_initialized && !policy_view_capable(NULL))
950 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700951 return param_get_uint(buffer, kp);
952}
953
Kees Cooke4dca7b2017-10-17 19:04:42 -0700954static int param_get_audit(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700955{
John Johansenb5e95b42010-07-29 14:48:07 -0700956 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 sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
961}
962
Kees Cooke4dca7b2017-10-17 19:04:42 -0700963static int param_set_audit(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700964{
965 int i;
John Johansenb5e95b42010-07-29 14:48:07 -0700966
967 if (!apparmor_enabled)
968 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700969 if (!val)
970 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700971 if (apparmor_initialized && !policy_admin_capable(NULL))
972 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700973
974 for (i = 0; i < AUDIT_MAX_INDEX; i++) {
975 if (strcmp(val, audit_mode_names[i]) == 0) {
976 aa_g_audit = i;
977 return 0;
978 }
979 }
980
981 return -EINVAL;
982}
983
Kees Cooke4dca7b2017-10-17 19:04:42 -0700984static int param_get_mode(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700985{
John Johansenb5e95b42010-07-29 14:48:07 -0700986 if (!apparmor_enabled)
987 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700988 if (apparmor_initialized && !policy_view_capable(NULL))
989 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700990
John Johansen0d259f02013-07-10 21:13:43 -0700991 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
John Johansenb5e95b42010-07-29 14:48:07 -0700992}
993
Kees Cooke4dca7b2017-10-17 19:04:42 -0700994static int param_set_mode(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700995{
996 int i;
John Johansenb5e95b42010-07-29 14:48:07 -0700997
998 if (!apparmor_enabled)
999 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001000 if (!val)
1001 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001002 if (apparmor_initialized && !policy_admin_capable(NULL))
1003 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001004
John Johansen0d259f02013-07-10 21:13:43 -07001005 for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
1006 if (strcmp(val, aa_profile_mode_names[i]) == 0) {
John Johansenb5e95b42010-07-29 14:48:07 -07001007 aa_g_profile_mode = i;
1008 return 0;
1009 }
1010 }
1011
1012 return -EINVAL;
1013}
1014
1015/*
1016 * AppArmor init functions
1017 */
1018
1019/**
John Johansen55a26eb2017-01-16 00:43:00 -08001020 * set_init_ctx - set a task context and profile on the first task.
John Johansenb5e95b42010-07-29 14:48:07 -07001021 *
1022 * TODO: allow setting an alternate profile than unconfined
1023 */
John Johansen55a26eb2017-01-16 00:43:00 -08001024static int __init set_init_ctx(void)
John Johansenb5e95b42010-07-29 14:48:07 -07001025{
1026 struct cred *cred = (struct cred *)current->real_cred;
John Johansen55a26eb2017-01-16 00:43:00 -08001027 struct aa_task_ctx *ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001028
John Johansen55a26eb2017-01-16 00:43:00 -08001029 ctx = aa_alloc_task_context(GFP_KERNEL);
1030 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -07001031 return -ENOMEM;
1032
John Johansen637f6882017-06-09 08:14:28 -07001033 ctx->label = aa_get_label(ns_unconfined(root_ns));
John Johansen55a26eb2017-01-16 00:43:00 -08001034 cred_ctx(cred) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001035
1036 return 0;
1037}
1038
John Johansend4669f02017-01-16 00:43:10 -08001039static void destroy_buffers(void)
1040{
1041 u32 i, j;
1042
1043 for_each_possible_cpu(i) {
1044 for_each_cpu_buffer(j) {
1045 kfree(per_cpu(aa_buffers, i).buf[j]);
1046 per_cpu(aa_buffers, i).buf[j] = NULL;
1047 }
1048 }
1049}
1050
1051static int __init alloc_buffers(void)
1052{
1053 u32 i, j;
1054
1055 for_each_possible_cpu(i) {
1056 for_each_cpu_buffer(j) {
1057 char *buffer;
1058
1059 if (cpu_to_node(i) > num_online_nodes())
1060 /* fallback to kmalloc for offline nodes */
1061 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1062 else
1063 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1064 cpu_to_node(i));
1065 if (!buffer) {
1066 destroy_buffers();
1067 return -ENOMEM;
1068 }
1069 per_cpu(aa_buffers, i).buf[j] = buffer;
1070 }
1071 }
1072
1073 return 0;
1074}
1075
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001076#ifdef CONFIG_SYSCTL
1077static int apparmor_dointvec(struct ctl_table *table, int write,
1078 void __user *buffer, size_t *lenp, loff_t *ppos)
1079{
1080 if (!policy_admin_capable(NULL))
1081 return -EPERM;
1082 if (!apparmor_enabled)
1083 return -EINVAL;
1084
1085 return proc_dointvec(table, write, buffer, lenp, ppos);
1086}
1087
1088static struct ctl_path apparmor_sysctl_path[] = {
1089 { .procname = "kernel", },
1090 { }
1091};
1092
1093static struct ctl_table apparmor_sysctl_table[] = {
1094 {
1095 .procname = "unprivileged_userns_apparmor_policy",
1096 .data = &unprivileged_userns_apparmor_policy,
1097 .maxlen = sizeof(int),
1098 .mode = 0600,
1099 .proc_handler = apparmor_dointvec,
1100 },
1101 { }
1102};
1103
1104static int __init apparmor_init_sysctl(void)
1105{
1106 return register_sysctl_paths(apparmor_sysctl_path,
1107 apparmor_sysctl_table) ? 0 : -ENOMEM;
1108}
1109#else
1110static inline int apparmor_init_sysctl(void)
1111{
1112 return 0;
1113}
1114#endif /* CONFIG_SYSCTL */
1115
John Johansenb5e95b42010-07-29 14:48:07 -07001116static int __init apparmor_init(void)
1117{
1118 int error;
1119
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001120 if (!apparmor_enabled || !security_module_enable("apparmor")) {
John Johansenb5e95b42010-07-29 14:48:07 -07001121 aa_info_message("AppArmor disabled by boot time parameter");
Thomas Meyer954317f2017-10-07 16:02:21 +02001122 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001123 return 0;
1124 }
1125
John Johansen11c236b2017-01-16 00:42:42 -08001126 error = aa_setup_dfa_engine();
1127 if (error) {
1128 AA_ERROR("Unable to setup dfa engine\n");
1129 goto alloc_out;
1130 }
1131
John Johansenb5e95b42010-07-29 14:48:07 -07001132 error = aa_alloc_root_ns();
1133 if (error) {
1134 AA_ERROR("Unable to allocate default profile namespace\n");
1135 goto alloc_out;
1136 }
1137
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001138 error = apparmor_init_sysctl();
1139 if (error) {
1140 AA_ERROR("Unable to register sysctls\n");
1141 goto alloc_out;
1142
1143 }
1144
John Johansend4669f02017-01-16 00:43:10 -08001145 error = alloc_buffers();
1146 if (error) {
1147 AA_ERROR("Unable to allocate work buffers\n");
1148 goto buffers_out;
1149 }
1150
John Johansen55a26eb2017-01-16 00:43:00 -08001151 error = set_init_ctx();
John Johansenb5e95b42010-07-29 14:48:07 -07001152 if (error) {
1153 AA_ERROR("Failed to set context on init task\n");
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001154 aa_free_root_ns();
John Johansend4669f02017-01-16 00:43:10 -08001155 goto buffers_out;
John Johansenb5e95b42010-07-29 14:48:07 -07001156 }
Casey Schauflerd69dece52017-01-18 17:09:05 -08001157 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1158 "apparmor");
John Johansenb5e95b42010-07-29 14:48:07 -07001159
1160 /* Report that AppArmor successfully initialized */
1161 apparmor_initialized = 1;
1162 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1163 aa_info_message("AppArmor initialized: complain mode enabled");
1164 else if (aa_g_profile_mode == APPARMOR_KILL)
1165 aa_info_message("AppArmor initialized: kill mode enabled");
1166 else
1167 aa_info_message("AppArmor initialized");
1168
1169 return error;
1170
John Johansend4669f02017-01-16 00:43:10 -08001171buffers_out:
1172 destroy_buffers();
1173
John Johansenb5e95b42010-07-29 14:48:07 -07001174alloc_out:
1175 aa_destroy_aafs();
John Johansen11c236b2017-01-16 00:42:42 -08001176 aa_teardown_dfa_engine();
John Johansenb5e95b42010-07-29 14:48:07 -07001177
Thomas Meyer954317f2017-10-07 16:02:21 +02001178 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001179 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001180}
1181
1182security_initcall(apparmor_init);