blob: 3ba08530c92e66e4fb1d3a806ed511b82d2f0eef [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"
41
42/* Flag indicating whether initialization completed */
John Johansen545de8f2017-04-06 06:55:23 -070043int apparmor_initialized;
John Johansenb5e95b42010-07-29 14:48:07 -070044
John Johansend4669f02017-01-16 00:43:10 -080045DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
46
47
John Johansenb5e95b42010-07-29 14:48:07 -070048/*
49 * LSM hook functions
50 */
51
52/*
John Johansen637f6882017-06-09 08:14:28 -070053 * free the associated aa_task_ctx and put its labels
John Johansenb5e95b42010-07-29 14:48:07 -070054 */
55static void apparmor_cred_free(struct cred *cred)
56{
John Johansen55a26eb2017-01-16 00:43:00 -080057 aa_free_task_context(cred_ctx(cred));
58 cred_ctx(cred) = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -070059}
60
61/*
62 * allocate the apparmor part of blank credentials
63 */
64static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
65{
66 /* freed by apparmor_cred_free */
John Johansen55a26eb2017-01-16 00:43:00 -080067 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
68
69 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -070070 return -ENOMEM;
71
John Johansen55a26eb2017-01-16 00:43:00 -080072 cred_ctx(cred) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -070073 return 0;
74}
75
76/*
John Johansen55a26eb2017-01-16 00:43:00 -080077 * prepare new aa_task_ctx for modification by prepare_cred block
John Johansenb5e95b42010-07-29 14:48:07 -070078 */
79static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
80 gfp_t gfp)
81{
82 /* freed by apparmor_cred_free */
John Johansen55a26eb2017-01-16 00:43:00 -080083 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
84
85 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -070086 return -ENOMEM;
87
John Johansen55a26eb2017-01-16 00:43:00 -080088 aa_dup_task_context(ctx, cred_ctx(old));
89 cred_ctx(new) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -070090 return 0;
91}
92
93/*
94 * transfer the apparmor data to a blank set of creds
95 */
96static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
97{
John Johansen55a26eb2017-01-16 00:43:00 -080098 const struct aa_task_ctx *old_ctx = cred_ctx(old);
99 struct aa_task_ctx *new_ctx = cred_ctx(new);
John Johansenb5e95b42010-07-29 14:48:07 -0700100
John Johansen55a26eb2017-01-16 00:43:00 -0800101 aa_dup_task_context(new_ctx, old_ctx);
John Johansenb5e95b42010-07-29 14:48:07 -0700102}
103
104static int apparmor_ptrace_access_check(struct task_struct *child,
105 unsigned int mode)
106{
John Johansenb5e95b42010-07-29 14:48:07 -0700107 return aa_ptrace(current, child, mode);
108}
109
110static int apparmor_ptrace_traceme(struct task_struct *parent)
111{
John Johansenb5e95b42010-07-29 14:48:07 -0700112 return aa_ptrace(parent, current, PTRACE_MODE_ATTACH);
113}
114
115/* Derived from security/commoncap.c:cap_capget */
116static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
117 kernel_cap_t *inheritable, kernel_cap_t *permitted)
118{
John Johansen637f6882017-06-09 08:14:28 -0700119 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700120 struct aa_profile *profile;
121 const struct cred *cred;
122
123 rcu_read_lock();
124 cred = __task_cred(target);
John Johansen637f6882017-06-09 08:14:28 -0700125 label = aa_get_newest_cred_label(cred);
126 profile = labels_profile(label);
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700127 /*
128 * cap_capget is stacked ahead of this and will
129 * initialize effective and permitted.
130 */
John Johansen637f6882017-06-09 08:14:28 -0700131 if (!profile_unconfined(profile) && !COMPLAIN_MODE(profile)) {
John Johansenb5e95b42010-07-29 14:48:07 -0700132 *effective = cap_intersect(*effective, profile->caps.allow);
133 *permitted = cap_intersect(*permitted, profile->caps.allow);
134 }
135 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -0700136 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700137
138 return 0;
139}
140
Eric Paris6a9de492012-01-03 12:25:14 -0500141static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
142 int cap, int audit)
John Johansenb5e95b42010-07-29 14:48:07 -0700143{
John Johansen637f6882017-06-09 08:14:28 -0700144 struct aa_label *label;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700145 int error = 0;
146
John Johansen637f6882017-06-09 08:14:28 -0700147 label = aa_get_newest_cred_label(cred);
148 if (!unconfined(label))
149 error = aa_capable(labels_profile(label), cap, audit);
150 aa_put_label(label);
John Johansencf797c02017-06-09 02:08:28 -0700151
John Johansenb5e95b42010-07-29 14:48:07 -0700152 return error;
153}
154
155/**
156 * common_perm - basic common permission check wrapper fn for paths
157 * @op: operation being checked
158 * @path: path to check permission of (NOT NULL)
159 * @mask: requested permissions mask
160 * @cond: conditional info for the permission request (NOT NULL)
161 *
162 * Returns: %0 else error code if error or permission denied
163 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800164static int common_perm(const char *op, const struct path *path, u32 mask,
John Johansenb5e95b42010-07-29 14:48:07 -0700165 struct path_cond *cond)
166{
John Johansen637f6882017-06-09 08:14:28 -0700167 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700168 int error = 0;
169
John Johansen637f6882017-06-09 08:14:28 -0700170 label = __begin_current_label_crit_section();
171 if (!unconfined(label))
172 error = aa_path_perm(op, labels_profile(label), path, 0, mask,
173 cond);
174 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700175
176 return error;
177}
178
179/**
John Johansen31f75bf2017-01-16 00:43:07 -0800180 * common_perm_cond - common permission wrapper around inode cond
181 * @op: operation being checked
182 * @path: location to check (NOT NULL)
183 * @mask: requested permissions mask
184 *
185 * Returns: %0 else error code if error or permission denied
186 */
187static int common_perm_cond(const char *op, const struct path *path, u32 mask)
188{
189 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
190 d_backing_inode(path->dentry)->i_mode
191 };
192
193 if (!path_mediated_fs(path->dentry))
194 return 0;
195
196 return common_perm(op, path, mask, &cond);
197}
198
199/**
John Johansenb5e95b42010-07-29 14:48:07 -0700200 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
201 * @op: operation being checked
202 * @dir: directory of the dentry (NOT NULL)
203 * @dentry: dentry to check (NOT NULL)
204 * @mask: requested permissions mask
205 * @cond: conditional info for the permission request (NOT NULL)
206 *
207 * Returns: %0 else error code if error or permission denied
208 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800209static int common_perm_dir_dentry(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700210 struct dentry *dentry, u32 mask,
211 struct path_cond *cond)
212{
Kees Cook8486adf2016-12-16 17:04:13 -0800213 struct path path = { .mnt = dir->mnt, .dentry = dentry };
John Johansenb5e95b42010-07-29 14:48:07 -0700214
215 return common_perm(op, &path, mask, cond);
216}
217
218/**
John Johansenb5e95b42010-07-29 14:48:07 -0700219 * common_perm_rm - common permission wrapper for operations doing rm
220 * @op: operation being checked
221 * @dir: directory that the dentry is in (NOT NULL)
222 * @dentry: dentry being rm'd (NOT NULL)
223 * @mask: requested permission mask
224 *
225 * Returns: %0 else error code if error or permission denied
226 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800227static int common_perm_rm(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700228 struct dentry *dentry, u32 mask)
229{
David Howellsc6f493d2015-03-17 22:26:22 +0000230 struct inode *inode = d_backing_inode(dentry);
John Johansenb5e95b42010-07-29 14:48:07 -0700231 struct path_cond cond = { };
232
John Johansenefeee832017-01-16 00:42:28 -0800233 if (!inode || !path_mediated_fs(dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700234 return 0;
235
236 cond.uid = inode->i_uid;
237 cond.mode = inode->i_mode;
238
239 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
240}
241
242/**
243 * common_perm_create - common permission wrapper for operations doing create
244 * @op: operation being checked
245 * @dir: directory that dentry will be created in (NOT NULL)
246 * @dentry: dentry to create (NOT NULL)
247 * @mask: request permission mask
248 * @mode: created file mode
249 *
250 * Returns: %0 else error code if error or permission denied
251 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800252static int common_perm_create(const char *op, const struct path *dir,
Al Virod6b49f72016-03-25 15:10:04 -0400253 struct dentry *dentry, u32 mask, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700254{
255 struct path_cond cond = { current_fsuid(), mode };
256
John Johansenefeee832017-01-16 00:42:28 -0800257 if (!path_mediated_fs(dir->dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700258 return 0;
259
260 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
261}
262
Al Viro989f74e2016-03-25 15:13:39 -0400263static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700264{
265 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
266}
267
Al Virod3607752016-03-25 15:21:09 -0400268static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500269 umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700270{
271 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
272 S_IFDIR);
273}
274
Al Viro989f74e2016-03-25 15:13:39 -0400275static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700276{
277 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
278}
279
Al Virod3607752016-03-25 15:21:09 -0400280static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500281 umode_t mode, unsigned int dev)
John Johansenb5e95b42010-07-29 14:48:07 -0700282{
283 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
284}
285
Al Viro81f4c502016-03-25 14:22:01 -0400286static int apparmor_path_truncate(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700287{
John Johansene53cfe62017-05-26 15:07:22 -0700288 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700289}
290
Al Virod3607752016-03-25 15:21:09 -0400291static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
John Johansenb5e95b42010-07-29 14:48:07 -0700292 const char *old_name)
293{
294 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
295 S_IFLNK);
296}
297
Al Viro3ccee462016-03-25 15:27:45 -0400298static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700299 struct dentry *new_dentry)
300{
John Johansen637f6882017-06-09 08:14:28 -0700301 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700302 int error = 0;
303
John Johansenefeee832017-01-16 00:42:28 -0800304 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700305 return 0;
306
John Johansen637f6882017-06-09 08:14:28 -0700307 label = begin_current_label_crit_section();
308 if (!unconfined(label))
309 error = aa_path_link(labels_profile(label), old_dentry, new_dir,
310 new_dentry);
311 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700312
John Johansenb5e95b42010-07-29 14:48:07 -0700313 return error;
314}
315
Al Viro3ccee462016-03-25 15:27:45 -0400316static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
317 const struct path *new_dir, struct dentry *new_dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700318{
John Johansen637f6882017-06-09 08:14:28 -0700319 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700320 int error = 0;
321
John Johansenefeee832017-01-16 00:42:28 -0800322 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700323 return 0;
324
John Johansen637f6882017-06-09 08:14:28 -0700325 label = begin_current_label_crit_section();
326 if (!unconfined(label)) {
Kees Cook8486adf2016-12-16 17:04:13 -0800327 struct path old_path = { .mnt = old_dir->mnt,
328 .dentry = old_dentry };
329 struct path new_path = { .mnt = new_dir->mnt,
330 .dentry = new_dentry };
David Howellsc6f493d2015-03-17 22:26:22 +0000331 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
332 d_backing_inode(old_dentry)->i_mode
John Johansenb5e95b42010-07-29 14:48:07 -0700333 };
334
John Johansen637f6882017-06-09 08:14:28 -0700335 error = aa_path_perm(OP_RENAME_SRC, labels_profile(label),
336 &old_path, 0,
John Johansene53cfe62017-05-26 15:07:22 -0700337 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
338 AA_MAY_SETATTR | AA_MAY_DELETE,
John Johansenb5e95b42010-07-29 14:48:07 -0700339 &cond);
340 if (!error)
John Johansen637f6882017-06-09 08:14:28 -0700341 error = aa_path_perm(OP_RENAME_DEST,
342 labels_profile(label),
343 &new_path,
John Johansene53cfe62017-05-26 15:07:22 -0700344 0, MAY_WRITE | AA_MAY_SETATTR |
John Johansenb5e95b42010-07-29 14:48:07 -0700345 AA_MAY_CREATE, &cond);
346
347 }
John Johansen637f6882017-06-09 08:14:28 -0700348 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700349
John Johansenb5e95b42010-07-29 14:48:07 -0700350 return error;
351}
352
Al Virobe01f9f2016-03-25 14:56:23 -0400353static int apparmor_path_chmod(const struct path *path, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700354{
John Johansen31f75bf2017-01-16 00:43:07 -0800355 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
John Johansenb5e95b42010-07-29 14:48:07 -0700356}
357
Al Viro7fd25da2016-03-25 14:44:41 -0400358static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
John Johansenb5e95b42010-07-29 14:48:07 -0700359{
John Johansen31f75bf2017-01-16 00:43:07 -0800360 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
John Johansenb5e95b42010-07-29 14:48:07 -0700361}
362
Al Viro3f7036a2015-03-08 19:28:30 -0400363static int apparmor_inode_getattr(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700364{
John Johansene53cfe62017-05-26 15:07:22 -0700365 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700366}
367
Eric Paris83d49852012-04-04 13:45:40 -0400368static int apparmor_file_open(struct file *file, const struct cred *cred)
John Johansenb5e95b42010-07-29 14:48:07 -0700369{
John Johansen637f6882017-06-09 08:14:28 -0700370 struct aa_file_ctx *fctx = file_ctx(file);
371 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700372 int error = 0;
373
John Johansenefeee832017-01-16 00:42:28 -0800374 if (!path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700375 return 0;
376
377 /* If in exec, permission is handled by bprm hooks.
378 * Cache permissions granted by the previous exec check, with
379 * implicit read and executable mmap which are required to
380 * actually execute the image.
381 */
382 if (current->in_execve) {
John Johansen55a26eb2017-01-16 00:43:00 -0800383 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
John Johansenb5e95b42010-07-29 14:48:07 -0700384 return 0;
385 }
386
John Johansen637f6882017-06-09 08:14:28 -0700387 label = aa_get_newest_cred_label(cred);
388 if (!unconfined(label)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500389 struct inode *inode = file_inode(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700390 struct path_cond cond = { inode->i_uid, inode->i_mode };
391
John Johansen637f6882017-06-09 08:14:28 -0700392 error = aa_path_perm(OP_OPEN, labels_profile(label),
393 &file->f_path, 0,
John Johansenb5e95b42010-07-29 14:48:07 -0700394 aa_map_file_to_perms(file), &cond);
395 /* todo cache full allowed permissions set and state */
John Johansen55a26eb2017-01-16 00:43:00 -0800396 fctx->allow = aa_map_file_to_perms(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700397 }
John Johansen637f6882017-06-09 08:14:28 -0700398 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700399
400 return error;
401}
402
403static int apparmor_file_alloc_security(struct file *file)
404{
John Johansencf797c02017-06-09 02:08:28 -0700405 int error = 0;
406
John Johansenb5e95b42010-07-29 14:48:07 -0700407 /* freed by apparmor_file_free_security */
John Johansen637f6882017-06-09 08:14:28 -0700408 struct aa_label *label = begin_current_label_crit_section();
John Johansen2835a132017-06-09 11:43:45 -0700409 file->f_security = aa_alloc_file_ctx(GFP_KERNEL);
410 if (!file_ctx(file))
411 error = -ENOMEM;
John Johansen637f6882017-06-09 08:14:28 -0700412 end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700413
John Johansencf797c02017-06-09 02:08:28 -0700414 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700415}
416
417static void apparmor_file_free_security(struct file *file)
418{
John Johansen2835a132017-06-09 11:43:45 -0700419 aa_free_file_ctx(file_ctx(file));
John Johansenb5e95b42010-07-29 14:48:07 -0700420}
421
John Johansen47f6e5c2017-01-16 00:43:01 -0800422static int common_file_perm(const char *op, struct file *file, u32 mask)
John Johansenb5e95b42010-07-29 14:48:07 -0700423{
John Johansen55a26eb2017-01-16 00:43:00 -0800424 struct aa_file_ctx *fctx = file->f_security;
John Johansen637f6882017-06-09 08:14:28 -0700425 struct aa_label *label, *flabel;
John Johansenb5e95b42010-07-29 14:48:07 -0700426 int error = 0;
427
John Johansen192ca6b2017-06-09 11:58:42 -0700428 /* don't reaudit files closed during inheritance */
429 if (file->f_path.dentry == aa_null.dentry)
430 return -EACCES;
431
John Johansen637f6882017-06-09 08:14:28 -0700432 flabel = aa_cred_raw_label(file->f_cred);
433 AA_BUG(!flabel);
John Johansenb5e95b42010-07-29 14:48:07 -0700434
435 if (!file->f_path.mnt ||
John Johansenefeee832017-01-16 00:42:28 -0800436 !path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700437 return 0;
438
John Johansen637f6882017-06-09 08:14:28 -0700439 label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700440
441 /* revalidate access, if task is unconfined, or the cached cred
442 * doesn't match or if the request is for more permissions than
443 * was granted.
444 *
445 * Note: the test for !unconfined(fprofile) is to handle file
446 * delegation from unconfined tasks
447 */
John Johansen637f6882017-06-09 08:14:28 -0700448 if (!unconfined(label) && !unconfined(flabel) &&
449 ((flabel != label) || (mask & ~fctx->allow)))
450 error = aa_file_perm(op, labels_profile(label), file, mask);
451 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700452
453 return error;
454}
455
456static int apparmor_file_permission(struct file *file, int mask)
457{
458 return common_file_perm(OP_FPERM, file, mask);
459}
460
461static int apparmor_file_lock(struct file *file, unsigned int cmd)
462{
463 u32 mask = AA_MAY_LOCK;
464
465 if (cmd == F_WRLCK)
466 mask |= MAY_WRITE;
467
468 return common_file_perm(OP_FLOCK, file, mask);
469}
470
John Johansen47f6e5c2017-01-16 00:43:01 -0800471static int common_mmap(const char *op, struct file *file, unsigned long prot,
John Johansenb5e95b42010-07-29 14:48:07 -0700472 unsigned long flags)
473{
John Johansenb5e95b42010-07-29 14:48:07 -0700474 int mask = 0;
475
John Johansen637f6882017-06-09 08:14:28 -0700476 if (!file || !file_ctx(file))
John Johansenb5e95b42010-07-29 14:48:07 -0700477 return 0;
478
479 if (prot & PROT_READ)
480 mask |= MAY_READ;
481 /*
482 * Private mappings don't require write perms since they don't
483 * write back to the files
484 */
485 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
486 mask |= MAY_WRITE;
487 if (prot & PROT_EXEC)
488 mask |= AA_EXEC_MMAP;
489
John Johansenb5e95b42010-07-29 14:48:07 -0700490 return common_file_perm(op, file, mask);
491}
492
Al Viroe5467852012-05-30 13:30:51 -0400493static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
494 unsigned long prot, unsigned long flags)
John Johansenb5e95b42010-07-29 14:48:07 -0700495{
John Johansenb5e95b42010-07-29 14:48:07 -0700496 return common_mmap(OP_FMMAP, file, prot, flags);
497}
498
499static int apparmor_file_mprotect(struct vm_area_struct *vma,
500 unsigned long reqprot, unsigned long prot)
501{
502 return common_mmap(OP_FMPROT, vma->vm_file, prot,
503 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
504}
505
506static int apparmor_getprocattr(struct task_struct *task, char *name,
507 char **value)
508{
509 int error = -ENOENT;
John Johansenb5e95b42010-07-29 14:48:07 -0700510 /* released below */
511 const struct cred *cred = get_task_cred(task);
John Johansen55a26eb2017-01-16 00:43:00 -0800512 struct aa_task_ctx *ctx = cred_ctx(cred);
John Johansen637f6882017-06-09 08:14:28 -0700513 struct aa_label *label = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -0700514
515 if (strcmp(name, "current") == 0)
John Johansen637f6882017-06-09 08:14:28 -0700516 label = aa_get_newest_label(ctx->label);
John Johansen55a26eb2017-01-16 00:43:00 -0800517 else if (strcmp(name, "prev") == 0 && ctx->previous)
John Johansen637f6882017-06-09 08:14:28 -0700518 label = aa_get_newest_label(ctx->previous);
John Johansen55a26eb2017-01-16 00:43:00 -0800519 else if (strcmp(name, "exec") == 0 && ctx->onexec)
John Johansen637f6882017-06-09 08:14:28 -0700520 label = aa_get_newest_label(ctx->onexec);
John Johansenb5e95b42010-07-29 14:48:07 -0700521 else
522 error = -EINVAL;
523
John Johansen637f6882017-06-09 08:14:28 -0700524 if (label)
525 error = aa_getprocattr(labels_profile(label), value);
John Johansen77b071b2013-07-10 21:07:43 -0700526
John Johansen637f6882017-06-09 08:14:28 -0700527 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700528 put_cred(cred);
529
530 return error;
531}
532
Stephen Smalleyb21507e2017-01-09 10:07:31 -0500533static int apparmor_setprocattr(const char *name, void *value,
534 size_t size)
John Johansenb5e95b42010-07-29 14:48:07 -0700535{
Vegard Nossume89b8082016-07-07 13:41:11 -0700536 char *command, *largs = NULL, *args = value;
John Johansenb5e95b42010-07-29 14:48:07 -0700537 size_t arg_size;
538 int error;
John Johansenef88a7a2017-01-16 00:43:02 -0800539 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700540
541 if (size == 0)
542 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700543
Vegard Nossume89b8082016-07-07 13:41:11 -0700544 /* AppArmor requires that the buffer must be null terminated atm */
545 if (args[size - 1] != '\0') {
546 /* null terminate */
547 largs = args = kmalloc(size + 1, GFP_KERNEL);
548 if (!args)
549 return -ENOMEM;
550 memcpy(args, value, size);
551 args[size] = '\0';
552 }
553
554 error = -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700555 args = strim(args);
556 command = strsep(&args, " ");
557 if (!args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700558 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700559 args = skip_spaces(args);
560 if (!*args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700561 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700562
John Johansend4d03f72016-07-09 23:46:33 -0700563 arg_size = size - (args - (largs ? largs : (char *) value));
John Johansenb5e95b42010-07-29 14:48:07 -0700564 if (strcmp(name, "current") == 0) {
565 if (strcmp(command, "changehat") == 0) {
566 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700567 AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700568 } else if (strcmp(command, "permhat") == 0) {
569 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700570 AA_CHANGE_TEST);
John Johansenb5e95b42010-07-29 14:48:07 -0700571 } else if (strcmp(command, "changeprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700572 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700573 } else if (strcmp(command, "permprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700574 error = aa_change_profile(args, AA_CHANGE_TEST);
John Johansen3eea57c2013-02-27 03:44:40 -0800575 } else
576 goto fail;
John Johansenb5e95b42010-07-29 14:48:07 -0700577 } else if (strcmp(name, "exec") == 0) {
John Johansen3eea57c2013-02-27 03:44:40 -0800578 if (strcmp(command, "exec") == 0)
John Johansendf8073c2017-06-09 11:36:48 -0700579 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
John Johansen3eea57c2013-02-27 03:44:40 -0800580 else
581 goto fail;
582 } else
John Johansenb5e95b42010-07-29 14:48:07 -0700583 /* only support the "current" and "exec" process attributes */
Vegard Nossume89b8082016-07-07 13:41:11 -0700584 goto fail;
John Johansen3eea57c2013-02-27 03:44:40 -0800585
John Johansenb5e95b42010-07-29 14:48:07 -0700586 if (!error)
587 error = size;
Vegard Nossume89b8082016-07-07 13:41:11 -0700588out:
589 kfree(largs);
John Johansenb5e95b42010-07-29 14:48:07 -0700590 return error;
John Johansen3eea57c2013-02-27 03:44:40 -0800591
592fail:
John Johansen637f6882017-06-09 08:14:28 -0700593 aad(&sa)->label = begin_current_label_crit_section();
John Johansenef88a7a2017-01-16 00:43:02 -0800594 aad(&sa)->info = name;
595 aad(&sa)->error = error = -EINVAL;
John Johansen3eea57c2013-02-27 03:44:40 -0800596 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
John Johansen637f6882017-06-09 08:14:28 -0700597 end_current_label_crit_section(aad(&sa)->label);
Vegard Nossume89b8082016-07-07 13:41:11 -0700598 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700599}
600
John Johansenfe864822017-06-09 05:27:50 -0700601/**
602 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
603 * @bprm: binprm for the exec (NOT NULL)
604 */
605static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
606{
John Johansen637f6882017-06-09 08:14:28 -0700607 struct aa_label *label = aa_current_raw_label();
John Johansenfe864822017-06-09 05:27:50 -0700608 struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
609
610 /* bail out if unconfined or not changing profile */
John Johansen637f6882017-06-09 08:14:28 -0700611 if ((new_ctx->label->proxy == label->proxy) ||
612 (unconfined(new_ctx->label)))
John Johansenfe864822017-06-09 05:27:50 -0700613 return;
614
John Johansen192ca6b2017-06-09 11:58:42 -0700615 aa_inherit_files(bprm->cred, current->files);
616
John Johansenfe864822017-06-09 05:27:50 -0700617 current->pdeath_signal = 0;
618
John Johansen637f6882017-06-09 08:14:28 -0700619 /* reset soft limits and set hard limits for the new label */
620 __aa_transition_rlimits(labels_profile(label),
621 labels_profile(new_ctx->label));
John Johansenfe864822017-06-09 05:27:50 -0700622}
623
624/**
625 * apparmor_bprm_committed_cred - do cleanup after new creds committed
626 * @bprm: binprm for the exec (NOT NULL)
627 */
628static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
629{
630 /* TODO: cleanup signals - ipc mediation */
631 return;
632}
633
Jiri Slaby7cb4dc92010-08-11 11:28:02 +0200634static int apparmor_task_setrlimit(struct task_struct *task,
635 unsigned int resource, struct rlimit *new_rlim)
John Johansenb5e95b42010-07-29 14:48:07 -0700636{
John Johansen637f6882017-06-09 08:14:28 -0700637 struct aa_label *label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700638 int error = 0;
639
John Johansen637f6882017-06-09 08:14:28 -0700640 if (!unconfined(label))
641 error = aa_task_setrlimit(labels_profile(label), task,
642 resource, new_rlim);
643 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700644
645 return error;
646}
647
James Morrisca97d932017-02-15 00:18:51 +1100648static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -0700649 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
650 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
651 LSM_HOOK_INIT(capget, apparmor_capget),
652 LSM_HOOK_INIT(capable, apparmor_capable),
John Johansenb5e95b42010-07-29 14:48:07 -0700653
Casey Schauflere20b0432015-05-02 15:11:36 -0700654 LSM_HOOK_INIT(path_link, apparmor_path_link),
655 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
656 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
657 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
658 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
659 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
660 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
661 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
662 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
663 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
664 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
John Johansenb5e95b42010-07-29 14:48:07 -0700665
Casey Schauflere20b0432015-05-02 15:11:36 -0700666 LSM_HOOK_INIT(file_open, apparmor_file_open),
667 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
668 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
669 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
670 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
Casey Schauflere20b0432015-05-02 15:11:36 -0700671 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
672 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
John Johansenb5e95b42010-07-29 14:48:07 -0700673
Casey Schauflere20b0432015-05-02 15:11:36 -0700674 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
675 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
John Johansenb5e95b42010-07-29 14:48:07 -0700676
Casey Schauflere20b0432015-05-02 15:11:36 -0700677 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
678 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
679 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
680 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
John Johansenb5e95b42010-07-29 14:48:07 -0700681
Casey Schauflere20b0432015-05-02 15:11:36 -0700682 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
683 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
684 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
685 LSM_HOOK_INIT(bprm_secureexec, apparmor_bprm_secureexec),
John Johansenb5e95b42010-07-29 14:48:07 -0700686
Casey Schauflere20b0432015-05-02 15:11:36 -0700687 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
John Johansenb5e95b42010-07-29 14:48:07 -0700688};
689
690/*
691 * AppArmor sysfs module parameters
692 */
693
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000694static int param_set_aabool(const char *val, const struct kernel_param *kp);
695static int param_get_aabool(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030696#define param_check_aabool param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930697static const struct kernel_param_ops param_ops_aabool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930698 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000699 .set = param_set_aabool,
700 .get = param_get_aabool
701};
John Johansenb5e95b42010-07-29 14:48:07 -0700702
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000703static int param_set_aauint(const char *val, const struct kernel_param *kp);
704static int param_get_aauint(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030705#define param_check_aauint param_check_uint
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930706static const struct kernel_param_ops param_ops_aauint = {
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000707 .set = param_set_aauint,
708 .get = param_get_aauint
709};
John Johansenb5e95b42010-07-29 14:48:07 -0700710
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000711static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
712static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +1030713#define param_check_aalockpolicy param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930714static const struct kernel_param_ops param_ops_aalockpolicy = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930715 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000716 .set = param_set_aalockpolicy,
717 .get = param_get_aalockpolicy
718};
John Johansenb5e95b42010-07-29 14:48:07 -0700719
720static int param_set_audit(const char *val, struct kernel_param *kp);
721static int param_get_audit(char *buffer, struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -0700722
723static int param_set_mode(const char *val, struct kernel_param *kp);
724static int param_get_mode(char *buffer, struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -0700725
726/* Flag values, also controllable via /sys/module/apparmor/parameters
727 * We define special types as we want to do additional mediation.
728 */
729
730/* AppArmor global enforcement switch - complain, enforce, kill */
731enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
732module_param_call(mode, param_set_mode, param_get_mode,
733 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
734
John Johansen6059f712014-10-24 09:16:14 -0700735/* whether policy verification hashing is enabled */
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700736bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
John Johansen3ccb76c2017-01-16 13:21:27 -0800737#ifdef CONFIG_SECURITY_APPARMOR_HASH
John Johansen6059f712014-10-24 09:16:14 -0700738module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700739#endif
John Johansen6059f712014-10-24 09:16:14 -0700740
John Johansenb5e95b42010-07-29 14:48:07 -0700741/* Debug mode */
Valentin Rothbergeea7a052017-04-06 06:55:20 -0700742bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
John Johansenb5e95b42010-07-29 14:48:07 -0700743module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
744
745/* Audit mode */
746enum audit_mode aa_g_audit;
747module_param_call(audit, param_set_audit, param_get_audit,
748 &aa_g_audit, S_IRUSR | S_IWUSR);
749
750/* Determines if audit header is included in audited messages. This
751 * provides more context if the audit daemon is not running
752 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030753bool aa_g_audit_header = 1;
John Johansenb5e95b42010-07-29 14:48:07 -0700754module_param_named(audit_header, aa_g_audit_header, aabool,
755 S_IRUSR | S_IWUSR);
756
757/* lock out loading/removal of policy
758 * TODO: add in at boot loading of policy, which is the only way to
759 * load policy, if lock_policy is set
760 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030761bool aa_g_lock_policy;
John Johansenb5e95b42010-07-29 14:48:07 -0700762module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
763 S_IRUSR | S_IWUSR);
764
765/* Syscall logging mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030766bool aa_g_logsyscall;
John Johansenb5e95b42010-07-29 14:48:07 -0700767module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
768
769/* Maximum pathname length before accesses will start getting rejected */
770unsigned int aa_g_path_max = 2 * PATH_MAX;
John Johansen622f6e32017-04-06 06:55:24 -0700771module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
John Johansenb5e95b42010-07-29 14:48:07 -0700772
773/* Determines how paranoid loading of policy is and how much verification
774 * on the loaded policy is done.
John Johansenabbf8732017-01-16 00:42:37 -0800775 * DEPRECATED: read only as strict checking of load is always done now
776 * that none root users (user namespaces) can load policy.
John Johansenb5e95b42010-07-29 14:48:07 -0700777 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030778bool aa_g_paranoid_load = 1;
John Johansenabbf8732017-01-16 00:42:37 -0800779module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -0700780
781/* Boot time disable flag */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030782static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
John Johansenc6116162013-07-10 21:03:43 -0700783module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -0700784
785static int __init apparmor_enabled_setup(char *str)
786{
787 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900788 int error = kstrtoul(str, 0, &enabled);
John Johansenb5e95b42010-07-29 14:48:07 -0700789 if (!error)
790 apparmor_enabled = enabled ? 1 : 0;
791 return 1;
792}
793
794__setup("apparmor=", apparmor_enabled_setup);
795
796/* set global flag turning off the ability to load policy */
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000797static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700798{
John Johansen545de8f2017-04-06 06:55:23 -0700799 if (!apparmor_enabled)
800 return -EINVAL;
801 if (apparmor_initialized && !policy_admin_capable(NULL))
John Johansenb5e95b42010-07-29 14:48:07 -0700802 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700803 return param_set_bool(val, kp);
804}
805
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000806static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700807{
John Johansenca4bd5a2017-01-16 00:43:11 -0800808 if (!apparmor_enabled)
809 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700810 if (apparmor_initialized && !policy_view_capable(NULL))
811 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700812 return param_get_bool(buffer, kp);
813}
814
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000815static int param_set_aabool(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700816{
John Johansenca4bd5a2017-01-16 00:43:11 -0800817 if (!apparmor_enabled)
818 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700819 if (apparmor_initialized && !policy_admin_capable(NULL))
820 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700821 return param_set_bool(val, kp);
822}
823
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000824static int param_get_aabool(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700825{
John Johansenca4bd5a2017-01-16 00:43:11 -0800826 if (!apparmor_enabled)
827 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700828 if (apparmor_initialized && !policy_view_capable(NULL))
829 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700830 return param_get_bool(buffer, kp);
831}
832
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000833static int param_set_aauint(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700834{
John Johansen39d84822017-03-30 05:25:23 -0700835 int error;
836
John Johansenca4bd5a2017-01-16 00:43:11 -0800837 if (!apparmor_enabled)
838 return -EINVAL;
John Johansen39d84822017-03-30 05:25:23 -0700839 /* file is ro but enforce 2nd line check */
840 if (apparmor_initialized)
John Johansen545de8f2017-04-06 06:55:23 -0700841 return -EPERM;
John Johansen39d84822017-03-30 05:25:23 -0700842
843 error = param_set_uint(val, kp);
844 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
845
846 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700847}
848
Stephen Rothwell101d6c82010-08-02 12:00:43 +1000849static int param_get_aauint(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -0700850{
John Johansenca4bd5a2017-01-16 00:43:11 -0800851 if (!apparmor_enabled)
852 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700853 if (apparmor_initialized && !policy_view_capable(NULL))
854 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700855 return param_get_uint(buffer, kp);
856}
857
858static int param_get_audit(char *buffer, struct kernel_param *kp)
859{
John Johansenb5e95b42010-07-29 14:48:07 -0700860 if (!apparmor_enabled)
861 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700862 if (apparmor_initialized && !policy_view_capable(NULL))
863 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700864 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
865}
866
867static int param_set_audit(const char *val, struct kernel_param *kp)
868{
869 int i;
John Johansenb5e95b42010-07-29 14:48:07 -0700870
871 if (!apparmor_enabled)
872 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700873 if (!val)
874 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700875 if (apparmor_initialized && !policy_admin_capable(NULL))
876 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700877
878 for (i = 0; i < AUDIT_MAX_INDEX; i++) {
879 if (strcmp(val, audit_mode_names[i]) == 0) {
880 aa_g_audit = i;
881 return 0;
882 }
883 }
884
885 return -EINVAL;
886}
887
888static int param_get_mode(char *buffer, struct kernel_param *kp)
889{
John Johansenb5e95b42010-07-29 14:48:07 -0700890 if (!apparmor_enabled)
891 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700892 if (apparmor_initialized && !policy_view_capable(NULL))
893 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700894
John Johansen0d259f02013-07-10 21:13:43 -0700895 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
John Johansenb5e95b42010-07-29 14:48:07 -0700896}
897
898static int param_set_mode(const char *val, struct kernel_param *kp)
899{
900 int i;
John Johansenb5e95b42010-07-29 14:48:07 -0700901
902 if (!apparmor_enabled)
903 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700904 if (!val)
905 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -0700906 if (apparmor_initialized && !policy_admin_capable(NULL))
907 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -0700908
John Johansen0d259f02013-07-10 21:13:43 -0700909 for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
910 if (strcmp(val, aa_profile_mode_names[i]) == 0) {
John Johansenb5e95b42010-07-29 14:48:07 -0700911 aa_g_profile_mode = i;
912 return 0;
913 }
914 }
915
916 return -EINVAL;
917}
918
919/*
920 * AppArmor init functions
921 */
922
923/**
John Johansen55a26eb2017-01-16 00:43:00 -0800924 * set_init_ctx - set a task context and profile on the first task.
John Johansenb5e95b42010-07-29 14:48:07 -0700925 *
926 * TODO: allow setting an alternate profile than unconfined
927 */
John Johansen55a26eb2017-01-16 00:43:00 -0800928static int __init set_init_ctx(void)
John Johansenb5e95b42010-07-29 14:48:07 -0700929{
930 struct cred *cred = (struct cred *)current->real_cred;
John Johansen55a26eb2017-01-16 00:43:00 -0800931 struct aa_task_ctx *ctx;
John Johansenb5e95b42010-07-29 14:48:07 -0700932
John Johansen55a26eb2017-01-16 00:43:00 -0800933 ctx = aa_alloc_task_context(GFP_KERNEL);
934 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -0700935 return -ENOMEM;
936
John Johansen637f6882017-06-09 08:14:28 -0700937 ctx->label = aa_get_label(ns_unconfined(root_ns));
John Johansen55a26eb2017-01-16 00:43:00 -0800938 cred_ctx(cred) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -0700939
940 return 0;
941}
942
John Johansend4669f02017-01-16 00:43:10 -0800943static void destroy_buffers(void)
944{
945 u32 i, j;
946
947 for_each_possible_cpu(i) {
948 for_each_cpu_buffer(j) {
949 kfree(per_cpu(aa_buffers, i).buf[j]);
950 per_cpu(aa_buffers, i).buf[j] = NULL;
951 }
952 }
953}
954
955static int __init alloc_buffers(void)
956{
957 u32 i, j;
958
959 for_each_possible_cpu(i) {
960 for_each_cpu_buffer(j) {
961 char *buffer;
962
963 if (cpu_to_node(i) > num_online_nodes())
964 /* fallback to kmalloc for offline nodes */
965 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
966 else
967 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
968 cpu_to_node(i));
969 if (!buffer) {
970 destroy_buffers();
971 return -ENOMEM;
972 }
973 per_cpu(aa_buffers, i).buf[j] = buffer;
974 }
975 }
976
977 return 0;
978}
979
Tyler Hickse3ea1ca2016-03-16 19:19:10 -0500980#ifdef CONFIG_SYSCTL
981static int apparmor_dointvec(struct ctl_table *table, int write,
982 void __user *buffer, size_t *lenp, loff_t *ppos)
983{
984 if (!policy_admin_capable(NULL))
985 return -EPERM;
986 if (!apparmor_enabled)
987 return -EINVAL;
988
989 return proc_dointvec(table, write, buffer, lenp, ppos);
990}
991
992static struct ctl_path apparmor_sysctl_path[] = {
993 { .procname = "kernel", },
994 { }
995};
996
997static struct ctl_table apparmor_sysctl_table[] = {
998 {
999 .procname = "unprivileged_userns_apparmor_policy",
1000 .data = &unprivileged_userns_apparmor_policy,
1001 .maxlen = sizeof(int),
1002 .mode = 0600,
1003 .proc_handler = apparmor_dointvec,
1004 },
1005 { }
1006};
1007
1008static int __init apparmor_init_sysctl(void)
1009{
1010 return register_sysctl_paths(apparmor_sysctl_path,
1011 apparmor_sysctl_table) ? 0 : -ENOMEM;
1012}
1013#else
1014static inline int apparmor_init_sysctl(void)
1015{
1016 return 0;
1017}
1018#endif /* CONFIG_SYSCTL */
1019
John Johansenb5e95b42010-07-29 14:48:07 -07001020static int __init apparmor_init(void)
1021{
1022 int error;
1023
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001024 if (!apparmor_enabled || !security_module_enable("apparmor")) {
John Johansenb5e95b42010-07-29 14:48:07 -07001025 aa_info_message("AppArmor disabled by boot time parameter");
1026 apparmor_enabled = 0;
1027 return 0;
1028 }
1029
John Johansen11c236b2017-01-16 00:42:42 -08001030 error = aa_setup_dfa_engine();
1031 if (error) {
1032 AA_ERROR("Unable to setup dfa engine\n");
1033 goto alloc_out;
1034 }
1035
John Johansenb5e95b42010-07-29 14:48:07 -07001036 error = aa_alloc_root_ns();
1037 if (error) {
1038 AA_ERROR("Unable to allocate default profile namespace\n");
1039 goto alloc_out;
1040 }
1041
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001042 error = apparmor_init_sysctl();
1043 if (error) {
1044 AA_ERROR("Unable to register sysctls\n");
1045 goto alloc_out;
1046
1047 }
1048
John Johansend4669f02017-01-16 00:43:10 -08001049 error = alloc_buffers();
1050 if (error) {
1051 AA_ERROR("Unable to allocate work buffers\n");
1052 goto buffers_out;
1053 }
1054
John Johansen55a26eb2017-01-16 00:43:00 -08001055 error = set_init_ctx();
John Johansenb5e95b42010-07-29 14:48:07 -07001056 if (error) {
1057 AA_ERROR("Failed to set context on init task\n");
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001058 aa_free_root_ns();
John Johansend4669f02017-01-16 00:43:10 -08001059 goto buffers_out;
John Johansenb5e95b42010-07-29 14:48:07 -07001060 }
Casey Schauflerd69dece52017-01-18 17:09:05 -08001061 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1062 "apparmor");
John Johansenb5e95b42010-07-29 14:48:07 -07001063
1064 /* Report that AppArmor successfully initialized */
1065 apparmor_initialized = 1;
1066 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1067 aa_info_message("AppArmor initialized: complain mode enabled");
1068 else if (aa_g_profile_mode == APPARMOR_KILL)
1069 aa_info_message("AppArmor initialized: kill mode enabled");
1070 else
1071 aa_info_message("AppArmor initialized");
1072
1073 return error;
1074
John Johansend4669f02017-01-16 00:43:10 -08001075buffers_out:
1076 destroy_buffers();
1077
John Johansenb5e95b42010-07-29 14:48:07 -07001078alloc_out:
1079 aa_destroy_aafs();
John Johansen11c236b2017-01-16 00:42:42 -08001080 aa_teardown_dfa_engine();
John Johansenb5e95b42010-07-29 14:48:07 -07001081
1082 apparmor_enabled = 0;
1083 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001084}
1085
1086security_initcall(apparmor_init);