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