blob: 49d664ddff444810ef9c6e8a1b0276c5ba473c53 [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>
Matthew Garrettab9f2112018-05-24 13:27:47 -070026#include <linux/netfilter_ipv4.h>
27#include <linux/netfilter_ipv6.h>
John Johansenb5e95b42010-07-29 14:48:07 -070028#include <net/sock.h>
David Howellse262e32d2018-11-01 23:07:23 +000029#include <uapi/linux/mount.h>
John Johansenb5e95b42010-07-29 14:48:07 -070030
31#include "include/apparmor.h"
32#include "include/apparmorfs.h"
33#include "include/audit.h"
34#include "include/capability.h"
John Johansend8889d42017-10-11 01:04:48 -070035#include "include/cred.h"
John Johansenb5e95b42010-07-29 14:48:07 -070036#include "include/file.h"
37#include "include/ipc.h"
John Johansen56974a62017-07-18 23:18:33 -070038#include "include/net.h"
John Johansenb5e95b42010-07-29 14:48:07 -070039#include "include/path.h"
John Johansen637f6882017-06-09 08:14:28 -070040#include "include/label.h"
John Johansenb5e95b42010-07-29 14:48:07 -070041#include "include/policy.h"
John Johansencff281f2017-01-16 00:42:15 -080042#include "include/policy_ns.h"
John Johansenb5e95b42010-07-29 14:48:07 -070043#include "include/procattr.h"
John Johansen2ea3ffb2017-07-18 23:04:47 -070044#include "include/mount.h"
John Johansenc0929212017-07-31 17:36:45 -070045#include "include/secid.h"
John Johansenb5e95b42010-07-29 14:48:07 -070046
47/* Flag indicating whether initialization completed */
John Johansen545de8f2017-04-06 06:55:23 -070048int apparmor_initialized;
John Johansenb5e95b42010-07-29 14:48:07 -070049
John Johansend4669f02017-01-16 00:43:10 -080050DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
51
52
John Johansenb5e95b42010-07-29 14:48:07 -070053/*
54 * LSM hook functions
55 */
56
57/*
John Johansend9087c42017-01-27 03:53:53 -080058 * put the associated labels
John Johansenb5e95b42010-07-29 14:48:07 -070059 */
60static void apparmor_cred_free(struct cred *cred)
61{
John Johansend9087c42017-01-27 03:53:53 -080062 aa_put_label(cred_label(cred));
Casey Schaufler69b5a442018-09-21 17:17:59 -070063 set_cred_label(cred, NULL);
John Johansenb5e95b42010-07-29 14:48:07 -070064}
65
66/*
67 * allocate the apparmor part of blank credentials
68 */
69static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
70{
Casey Schaufler69b5a442018-09-21 17:17:59 -070071 set_cred_label(cred, NULL);
John Johansenb5e95b42010-07-29 14:48:07 -070072 return 0;
73}
74
75/*
John Johansend9087c42017-01-27 03:53:53 -080076 * prepare new cred label for modification by prepare_cred block
John Johansenb5e95b42010-07-29 14:48:07 -070077 */
78static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
79 gfp_t gfp)
80{
Casey Schaufler69b5a442018-09-21 17:17:59 -070081 set_cred_label(new, aa_get_newest_label(cred_label(old)));
John Johansenb5e95b42010-07-29 14:48:07 -070082 return 0;
83}
84
85/*
86 * transfer the apparmor data to a blank set of creds
87 */
88static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
89{
Casey Schaufler69b5a442018-09-21 17:17:59 -070090 set_cred_label(new, aa_get_newest_label(cred_label(old)));
John Johansenb5e95b42010-07-29 14:48:07 -070091}
John Johansenb5e95b42010-07-29 14:48:07 -070092
John Johansen3b529a72017-01-20 01:59:25 -080093static void apparmor_task_free(struct task_struct *task)
94{
95
96 aa_free_task_ctx(task_ctx(task));
John Johansen3b529a72017-01-20 01:59:25 -080097}
98
99static int apparmor_task_alloc(struct task_struct *task,
100 unsigned long clone_flags)
101{
Casey Schauflerf4ad8f22018-09-21 17:19:37 -0700102 struct aa_task_ctx *new = task_ctx(task);
John Johansen3b529a72017-01-20 01:59:25 -0800103
John Johansende62de52017-10-08 00:43:02 -0700104 aa_dup_task_ctx(new, task_ctx(current));
John Johansen3b529a72017-01-20 01:59:25 -0800105
106 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -0700107}
108
109static int apparmor_ptrace_access_check(struct task_struct *child,
110 unsigned int mode)
111{
John Johansenb2d09ae2017-06-09 14:22:14 -0700112 struct aa_label *tracer, *tracee;
113 int error;
114
Jann Horn1f8266f2018-09-13 18:12:09 +0200115 tracer = __begin_current_label_crit_section();
John Johansenb2d09ae2017-06-09 14:22:14 -0700116 tracee = aa_get_task_label(child);
117 error = aa_may_ptrace(tracer, tracee,
John Johansen338d0be2018-06-07 00:45:30 -0700118 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
119 : AA_PTRACE_TRACE);
John Johansenb2d09ae2017-06-09 14:22:14 -0700120 aa_put_label(tracee);
Jann Horn1f8266f2018-09-13 18:12:09 +0200121 __end_current_label_crit_section(tracer);
John Johansenb2d09ae2017-06-09 14:22:14 -0700122
123 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700124}
125
126static int apparmor_ptrace_traceme(struct task_struct *parent)
127{
John Johansenb2d09ae2017-06-09 14:22:14 -0700128 struct aa_label *tracer, *tracee;
129 int error;
130
Jann Hornca3fde52018-09-29 03:49:26 +0200131 tracee = __begin_current_label_crit_section();
John Johansenb2d09ae2017-06-09 14:22:14 -0700132 tracer = aa_get_task_label(parent);
133 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
134 aa_put_label(tracer);
Jann Hornca3fde52018-09-29 03:49:26 +0200135 __end_current_label_crit_section(tracee);
John Johansenb2d09ae2017-06-09 14:22:14 -0700136
137 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700138}
139
140/* Derived from security/commoncap.c:cap_capget */
141static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
142 kernel_cap_t *inheritable, kernel_cap_t *permitted)
143{
John Johansen637f6882017-06-09 08:14:28 -0700144 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700145 const struct cred *cred;
146
147 rcu_read_lock();
148 cred = __task_cred(target);
John Johansen637f6882017-06-09 08:14:28 -0700149 label = aa_get_newest_cred_label(cred);
John Johansenc70c86c2017-06-09 14:07:02 -0700150
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700151 /*
152 * cap_capget is stacked ahead of this and will
153 * initialize effective and permitted.
154 */
John Johansenc70c86c2017-06-09 14:07:02 -0700155 if (!unconfined(label)) {
156 struct aa_profile *profile;
157 struct label_it i;
158
159 label_for_each_confined(i, label, profile) {
160 if (COMPLAIN_MODE(profile))
161 continue;
162 *effective = cap_intersect(*effective,
163 profile->caps.allow);
164 *permitted = cap_intersect(*permitted,
165 profile->caps.allow);
166 }
John Johansenb5e95b42010-07-29 14:48:07 -0700167 }
168 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -0700169 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700170
171 return 0;
172}
173
Eric Paris6a9de492012-01-03 12:25:14 -0500174static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -0800175 int cap, unsigned int opts)
John Johansenb5e95b42010-07-29 14:48:07 -0700176{
John Johansen637f6882017-06-09 08:14:28 -0700177 struct aa_label *label;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700178 int error = 0;
179
John Johansen637f6882017-06-09 08:14:28 -0700180 label = aa_get_newest_cred_label(cred);
181 if (!unconfined(label))
Micah Mortonc1a85a02019-01-07 16:10:53 -0800182 error = aa_capable(label, cap, opts);
John Johansen637f6882017-06-09 08:14:28 -0700183 aa_put_label(label);
John Johansencf797c02017-06-09 02:08:28 -0700184
John Johansenb5e95b42010-07-29 14:48:07 -0700185 return error;
186}
187
188/**
189 * common_perm - basic common permission check wrapper fn for paths
190 * @op: operation being checked
191 * @path: path to check permission of (NOT NULL)
192 * @mask: requested permissions mask
193 * @cond: conditional info for the permission request (NOT NULL)
194 *
195 * Returns: %0 else error code if error or permission denied
196 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800197static int common_perm(const char *op, const struct path *path, u32 mask,
John Johansenb5e95b42010-07-29 14:48:07 -0700198 struct path_cond *cond)
199{
John Johansen637f6882017-06-09 08:14:28 -0700200 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700201 int error = 0;
202
John Johansen637f6882017-06-09 08:14:28 -0700203 label = __begin_current_label_crit_section();
204 if (!unconfined(label))
John Johansenaebd8732017-06-09 16:02:25 -0700205 error = aa_path_perm(op, label, path, 0, mask, cond);
John Johansen637f6882017-06-09 08:14:28 -0700206 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700207
208 return error;
209}
210
211/**
John Johansen31f75bf2017-01-16 00:43:07 -0800212 * common_perm_cond - common permission wrapper around inode cond
213 * @op: operation being checked
214 * @path: location to check (NOT NULL)
215 * @mask: requested permissions mask
216 *
217 * Returns: %0 else error code if error or permission denied
218 */
219static int common_perm_cond(const char *op, const struct path *path, u32 mask)
220{
221 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
222 d_backing_inode(path->dentry)->i_mode
223 };
224
225 if (!path_mediated_fs(path->dentry))
226 return 0;
227
228 return common_perm(op, path, mask, &cond);
229}
230
231/**
John Johansenb5e95b42010-07-29 14:48:07 -0700232 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
233 * @op: operation being checked
234 * @dir: directory of the dentry (NOT NULL)
235 * @dentry: dentry to check (NOT NULL)
236 * @mask: requested permissions mask
237 * @cond: conditional info for the permission request (NOT NULL)
238 *
239 * Returns: %0 else error code if error or permission denied
240 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800241static int common_perm_dir_dentry(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700242 struct dentry *dentry, u32 mask,
243 struct path_cond *cond)
244{
Kees Cook8486adf2016-12-16 17:04:13 -0800245 struct path path = { .mnt = dir->mnt, .dentry = dentry };
John Johansenb5e95b42010-07-29 14:48:07 -0700246
247 return common_perm(op, &path, mask, cond);
248}
249
250/**
John Johansenb5e95b42010-07-29 14:48:07 -0700251 * common_perm_rm - common permission wrapper for operations doing rm
252 * @op: operation being checked
253 * @dir: directory that the dentry is in (NOT NULL)
254 * @dentry: dentry being rm'd (NOT NULL)
255 * @mask: requested permission mask
256 *
257 * Returns: %0 else error code if error or permission denied
258 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800259static int common_perm_rm(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700260 struct dentry *dentry, u32 mask)
261{
David Howellsc6f493d2015-03-17 22:26:22 +0000262 struct inode *inode = d_backing_inode(dentry);
John Johansenb5e95b42010-07-29 14:48:07 -0700263 struct path_cond cond = { };
264
John Johansenefeee832017-01-16 00:42:28 -0800265 if (!inode || !path_mediated_fs(dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700266 return 0;
267
268 cond.uid = inode->i_uid;
269 cond.mode = inode->i_mode;
270
271 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
272}
273
274/**
275 * common_perm_create - common permission wrapper for operations doing create
276 * @op: operation being checked
277 * @dir: directory that dentry will be created in (NOT NULL)
278 * @dentry: dentry to create (NOT NULL)
279 * @mask: request permission mask
280 * @mode: created file mode
281 *
282 * Returns: %0 else error code if error or permission denied
283 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800284static int common_perm_create(const char *op, const struct path *dir,
Al Virod6b49f72016-03-25 15:10:04 -0400285 struct dentry *dentry, u32 mask, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700286{
287 struct path_cond cond = { current_fsuid(), mode };
288
John Johansenefeee832017-01-16 00:42:28 -0800289 if (!path_mediated_fs(dir->dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700290 return 0;
291
292 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
293}
294
Al Viro989f74e2016-03-25 15:13:39 -0400295static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700296{
297 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
298}
299
Al Virod3607752016-03-25 15:21:09 -0400300static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500301 umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700302{
303 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
304 S_IFDIR);
305}
306
Al Viro989f74e2016-03-25 15:13:39 -0400307static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700308{
309 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
310}
311
Al Virod3607752016-03-25 15:21:09 -0400312static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500313 umode_t mode, unsigned int dev)
John Johansenb5e95b42010-07-29 14:48:07 -0700314{
315 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
316}
317
Al Viro81f4c502016-03-25 14:22:01 -0400318static int apparmor_path_truncate(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700319{
John Johansene53cfe62017-05-26 15:07:22 -0700320 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700321}
322
Al Virod3607752016-03-25 15:21:09 -0400323static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
John Johansenb5e95b42010-07-29 14:48:07 -0700324 const char *old_name)
325{
326 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
327 S_IFLNK);
328}
329
Al Viro3ccee462016-03-25 15:27:45 -0400330static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700331 struct dentry *new_dentry)
332{
John Johansen637f6882017-06-09 08:14:28 -0700333 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700334 int error = 0;
335
John Johansenefeee832017-01-16 00:42:28 -0800336 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700337 return 0;
338
John Johansen637f6882017-06-09 08:14:28 -0700339 label = begin_current_label_crit_section();
340 if (!unconfined(label))
John Johansen80143702017-06-09 16:06:21 -0700341 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
John Johansen637f6882017-06-09 08:14:28 -0700342 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700343
John Johansenb5e95b42010-07-29 14:48:07 -0700344 return error;
345}
346
Al Viro3ccee462016-03-25 15:27:45 -0400347static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
348 const struct path *new_dir, struct dentry *new_dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700349{
John Johansen637f6882017-06-09 08:14:28 -0700350 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700351 int error = 0;
352
John Johansenefeee832017-01-16 00:42:28 -0800353 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700354 return 0;
355
John Johansen637f6882017-06-09 08:14:28 -0700356 label = begin_current_label_crit_section();
357 if (!unconfined(label)) {
Kees Cook8486adf2016-12-16 17:04:13 -0800358 struct path old_path = { .mnt = old_dir->mnt,
359 .dentry = old_dentry };
360 struct path new_path = { .mnt = new_dir->mnt,
361 .dentry = new_dentry };
David Howellsc6f493d2015-03-17 22:26:22 +0000362 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
363 d_backing_inode(old_dentry)->i_mode
John Johansenb5e95b42010-07-29 14:48:07 -0700364 };
365
John Johansenaebd8732017-06-09 16:02:25 -0700366 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
John Johansene53cfe62017-05-26 15:07:22 -0700367 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
368 AA_MAY_SETATTR | AA_MAY_DELETE,
John Johansenb5e95b42010-07-29 14:48:07 -0700369 &cond);
370 if (!error)
John Johansenaebd8732017-06-09 16:02:25 -0700371 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
John Johansene53cfe62017-05-26 15:07:22 -0700372 0, MAY_WRITE | AA_MAY_SETATTR |
John Johansenb5e95b42010-07-29 14:48:07 -0700373 AA_MAY_CREATE, &cond);
374
375 }
John Johansen637f6882017-06-09 08:14:28 -0700376 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700377
John Johansenb5e95b42010-07-29 14:48:07 -0700378 return error;
379}
380
Al Virobe01f9f2016-03-25 14:56:23 -0400381static int apparmor_path_chmod(const struct path *path, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700382{
John Johansen31f75bf2017-01-16 00:43:07 -0800383 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
John Johansenb5e95b42010-07-29 14:48:07 -0700384}
385
Al Viro7fd25da2016-03-25 14:44:41 -0400386static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
John Johansenb5e95b42010-07-29 14:48:07 -0700387{
John Johansen31f75bf2017-01-16 00:43:07 -0800388 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
John Johansenb5e95b42010-07-29 14:48:07 -0700389}
390
Al Viro3f7036a2015-03-08 19:28:30 -0400391static int apparmor_inode_getattr(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700392{
John Johansene53cfe62017-05-26 15:07:22 -0700393 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700394}
395
Al Viro94817692018-07-10 14:13:18 -0400396static int apparmor_file_open(struct file *file)
John Johansenb5e95b42010-07-29 14:48:07 -0700397{
John Johansen637f6882017-06-09 08:14:28 -0700398 struct aa_file_ctx *fctx = file_ctx(file);
399 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700400 int error = 0;
401
John Johansenefeee832017-01-16 00:42:28 -0800402 if (!path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700403 return 0;
404
405 /* If in exec, permission is handled by bprm hooks.
406 * Cache permissions granted by the previous exec check, with
407 * implicit read and executable mmap which are required to
408 * actually execute the image.
409 */
410 if (current->in_execve) {
John Johansen55a26eb2017-01-16 00:43:00 -0800411 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
John Johansenb5e95b42010-07-29 14:48:07 -0700412 return 0;
413 }
414
Al Viro94817692018-07-10 14:13:18 -0400415 label = aa_get_newest_cred_label(file->f_cred);
John Johansen637f6882017-06-09 08:14:28 -0700416 if (!unconfined(label)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500417 struct inode *inode = file_inode(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700418 struct path_cond cond = { inode->i_uid, inode->i_mode };
419
John Johansenaebd8732017-06-09 16:02:25 -0700420 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
John Johansenb5e95b42010-07-29 14:48:07 -0700421 aa_map_file_to_perms(file), &cond);
422 /* todo cache full allowed permissions set and state */
John Johansen55a26eb2017-01-16 00:43:00 -0800423 fctx->allow = aa_map_file_to_perms(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700424 }
John Johansen637f6882017-06-09 08:14:28 -0700425 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700426
427 return error;
428}
429
430static int apparmor_file_alloc_security(struct file *file)
431{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800432 struct aa_file_ctx *ctx = file_ctx(file);
John Johansen637f6882017-06-09 08:14:28 -0700433 struct aa_label *label = begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700434
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800435 spin_lock_init(&ctx->lock);
436 rcu_assign_pointer(ctx->label, aa_get_label(label));
437 end_current_label_crit_section(label);
438 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -0700439}
440
441static void apparmor_file_free_security(struct file *file)
442{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800443 struct aa_file_ctx *ctx = file_ctx(file);
444
445 if (ctx)
446 aa_put_label(rcu_access_pointer(ctx->label));
John Johansenb5e95b42010-07-29 14:48:07 -0700447}
448
John Johansen47f6e5c2017-01-16 00:43:01 -0800449static int common_file_perm(const char *op, struct file *file, u32 mask)
John Johansenb5e95b42010-07-29 14:48:07 -0700450{
John Johansen190a9512017-06-09 14:59:51 -0700451 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700452 int error = 0;
453
John Johansen192ca6b2017-06-09 11:58:42 -0700454 /* don't reaudit files closed during inheritance */
455 if (file->f_path.dentry == aa_null.dentry)
456 return -EACCES;
457
John Johansen637f6882017-06-09 08:14:28 -0700458 label = __begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700459 error = aa_file_perm(op, label, file, mask);
John Johansen637f6882017-06-09 08:14:28 -0700460 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700461
462 return error;
463}
464
John Johansen064dc942017-06-09 17:15:56 -0700465static int apparmor_file_receive(struct file *file)
466{
467 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
468}
469
John Johansenb5e95b42010-07-29 14:48:07 -0700470static int apparmor_file_permission(struct file *file, int mask)
471{
472 return common_file_perm(OP_FPERM, file, mask);
473}
474
475static int apparmor_file_lock(struct file *file, unsigned int cmd)
476{
477 u32 mask = AA_MAY_LOCK;
478
479 if (cmd == F_WRLCK)
480 mask |= MAY_WRITE;
481
482 return common_file_perm(OP_FLOCK, file, mask);
483}
484
John Johansen47f6e5c2017-01-16 00:43:01 -0800485static int common_mmap(const char *op, struct file *file, unsigned long prot,
John Johansenb5e95b42010-07-29 14:48:07 -0700486 unsigned long flags)
487{
John Johansenb5e95b42010-07-29 14:48:07 -0700488 int mask = 0;
489
John Johansen637f6882017-06-09 08:14:28 -0700490 if (!file || !file_ctx(file))
John Johansenb5e95b42010-07-29 14:48:07 -0700491 return 0;
492
493 if (prot & PROT_READ)
494 mask |= MAY_READ;
495 /*
496 * Private mappings don't require write perms since they don't
497 * write back to the files
498 */
499 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
500 mask |= MAY_WRITE;
501 if (prot & PROT_EXEC)
502 mask |= AA_EXEC_MMAP;
503
John Johansenb5e95b42010-07-29 14:48:07 -0700504 return common_file_perm(op, file, mask);
505}
506
Al Viroe5467852012-05-30 13:30:51 -0400507static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
508 unsigned long prot, unsigned long flags)
John Johansenb5e95b42010-07-29 14:48:07 -0700509{
John Johansenb5e95b42010-07-29 14:48:07 -0700510 return common_mmap(OP_FMMAP, file, prot, flags);
511}
512
513static int apparmor_file_mprotect(struct vm_area_struct *vma,
514 unsigned long reqprot, unsigned long prot)
515{
516 return common_mmap(OP_FMPROT, vma->vm_file, prot,
517 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
518}
519
John Johansen2ea3ffb2017-07-18 23:04:47 -0700520static int apparmor_sb_mount(const char *dev_name, const struct path *path,
521 const char *type, unsigned long flags, void *data)
522{
523 struct aa_label *label;
524 int error = 0;
525
526 /* Discard magic */
527 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
528 flags &= ~MS_MGC_MSK;
529
530 flags &= ~AA_MS_IGNORE_MASK;
531
532 label = __begin_current_label_crit_section();
533 if (!unconfined(label)) {
534 if (flags & MS_REMOUNT)
535 error = aa_remount(label, path, flags, data);
536 else if (flags & MS_BIND)
537 error = aa_bind_mount(label, path, dev_name, flags);
538 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
539 MS_UNBINDABLE))
540 error = aa_mount_change_type(label, path, flags);
541 else if (flags & MS_MOVE)
542 error = aa_move_mount(label, path, dev_name);
543 else
544 error = aa_new_mount(label, dev_name, path, type,
545 flags, data);
546 }
547 __end_current_label_crit_section(label);
548
549 return error;
550}
551
552static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
553{
554 struct aa_label *label;
555 int error = 0;
556
557 label = __begin_current_label_crit_section();
558 if (!unconfined(label))
559 error = aa_umount(label, mnt, flags);
560 __end_current_label_crit_section(label);
561
562 return error;
563}
564
565static int apparmor_sb_pivotroot(const struct path *old_path,
566 const struct path *new_path)
567{
568 struct aa_label *label;
569 int error = 0;
570
571 label = aa_get_current_label();
572 if (!unconfined(label))
573 error = aa_pivotroot(label, old_path, new_path);
574 aa_put_label(label);
575
576 return error;
577}
578
John Johansenb5e95b42010-07-29 14:48:07 -0700579static int apparmor_getprocattr(struct task_struct *task, char *name,
580 char **value)
581{
582 int error = -ENOENT;
John Johansenb5e95b42010-07-29 14:48:07 -0700583 /* released below */
584 const struct cred *cred = get_task_cred(task);
John Johansende62de52017-10-08 00:43:02 -0700585 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen637f6882017-06-09 08:14:28 -0700586 struct aa_label *label = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -0700587
588 if (strcmp(name, "current") == 0)
John Johansend9087c42017-01-27 03:53:53 -0800589 label = aa_get_newest_label(cred_label(cred));
John Johansen55a26eb2017-01-16 00:43:00 -0800590 else if (strcmp(name, "prev") == 0 && ctx->previous)
John Johansen637f6882017-06-09 08:14:28 -0700591 label = aa_get_newest_label(ctx->previous);
John Johansen55a26eb2017-01-16 00:43:00 -0800592 else if (strcmp(name, "exec") == 0 && ctx->onexec)
John Johansen637f6882017-06-09 08:14:28 -0700593 label = aa_get_newest_label(ctx->onexec);
John Johansenb5e95b42010-07-29 14:48:07 -0700594 else
595 error = -EINVAL;
596
John Johansen637f6882017-06-09 08:14:28 -0700597 if (label)
John Johansen76a1d262017-06-09 12:47:17 -0700598 error = aa_getprocattr(label, value);
John Johansen77b071b2013-07-10 21:07:43 -0700599
John Johansen637f6882017-06-09 08:14:28 -0700600 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700601 put_cred(cred);
602
603 return error;
604}
605
Stephen Smalleyb21507e2017-01-09 10:07:31 -0500606static int apparmor_setprocattr(const char *name, void *value,
607 size_t size)
John Johansenb5e95b42010-07-29 14:48:07 -0700608{
Vegard Nossume89b8082016-07-07 13:41:11 -0700609 char *command, *largs = NULL, *args = value;
John Johansenb5e95b42010-07-29 14:48:07 -0700610 size_t arg_size;
611 int error;
John Johansenef88a7a2017-01-16 00:43:02 -0800612 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700613
614 if (size == 0)
615 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700616
Vegard Nossume89b8082016-07-07 13:41:11 -0700617 /* AppArmor requires that the buffer must be null terminated atm */
618 if (args[size - 1] != '\0') {
619 /* null terminate */
620 largs = args = kmalloc(size + 1, GFP_KERNEL);
621 if (!args)
622 return -ENOMEM;
623 memcpy(args, value, size);
624 args[size] = '\0';
625 }
626
627 error = -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700628 args = strim(args);
629 command = strsep(&args, " ");
630 if (!args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700631 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700632 args = skip_spaces(args);
633 if (!*args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700634 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700635
John Johansend4d03f72016-07-09 23:46:33 -0700636 arg_size = size - (args - (largs ? largs : (char *) value));
John Johansenb5e95b42010-07-29 14:48:07 -0700637 if (strcmp(name, "current") == 0) {
638 if (strcmp(command, "changehat") == 0) {
639 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700640 AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700641 } else if (strcmp(command, "permhat") == 0) {
642 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700643 AA_CHANGE_TEST);
John Johansenb5e95b42010-07-29 14:48:07 -0700644 } else if (strcmp(command, "changeprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700645 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700646 } else if (strcmp(command, "permprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700647 error = aa_change_profile(args, AA_CHANGE_TEST);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700648 } else if (strcmp(command, "stack") == 0) {
649 error = aa_change_profile(args, AA_CHANGE_STACK);
John Johansen3eea57c2013-02-27 03:44:40 -0800650 } else
651 goto fail;
John Johansenb5e95b42010-07-29 14:48:07 -0700652 } else if (strcmp(name, "exec") == 0) {
John Johansen3eea57c2013-02-27 03:44:40 -0800653 if (strcmp(command, "exec") == 0)
John Johansendf8073c2017-06-09 11:36:48 -0700654 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700655 else if (strcmp(command, "stack") == 0)
656 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
657 AA_CHANGE_STACK));
John Johansen3eea57c2013-02-27 03:44:40 -0800658 else
659 goto fail;
660 } else
John Johansenb5e95b42010-07-29 14:48:07 -0700661 /* only support the "current" and "exec" process attributes */
Vegard Nossume89b8082016-07-07 13:41:11 -0700662 goto fail;
John Johansen3eea57c2013-02-27 03:44:40 -0800663
John Johansenb5e95b42010-07-29 14:48:07 -0700664 if (!error)
665 error = size;
Vegard Nossume89b8082016-07-07 13:41:11 -0700666out:
667 kfree(largs);
John Johansenb5e95b42010-07-29 14:48:07 -0700668 return error;
John Johansen3eea57c2013-02-27 03:44:40 -0800669
670fail:
John Johansen637f6882017-06-09 08:14:28 -0700671 aad(&sa)->label = begin_current_label_crit_section();
John Johansenef88a7a2017-01-16 00:43:02 -0800672 aad(&sa)->info = name;
673 aad(&sa)->error = error = -EINVAL;
John Johansen3eea57c2013-02-27 03:44:40 -0800674 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
John Johansen637f6882017-06-09 08:14:28 -0700675 end_current_label_crit_section(aad(&sa)->label);
Vegard Nossume89b8082016-07-07 13:41:11 -0700676 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700677}
678
John Johansenfe864822017-06-09 05:27:50 -0700679/**
680 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
681 * @bprm: binprm for the exec (NOT NULL)
682 */
683static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
684{
John Johansen637f6882017-06-09 08:14:28 -0700685 struct aa_label *label = aa_current_raw_label();
John Johansend9087c42017-01-27 03:53:53 -0800686 struct aa_label *new_label = cred_label(bprm->cred);
John Johansenfe864822017-06-09 05:27:50 -0700687
688 /* bail out if unconfined or not changing profile */
John Johansend9087c42017-01-27 03:53:53 -0800689 if ((new_label->proxy == label->proxy) ||
690 (unconfined(new_label)))
John Johansenfe864822017-06-09 05:27:50 -0700691 return;
692
John Johansen192ca6b2017-06-09 11:58:42 -0700693 aa_inherit_files(bprm->cred, current->files);
694
John Johansenfe864822017-06-09 05:27:50 -0700695 current->pdeath_signal = 0;
696
John Johansen637f6882017-06-09 08:14:28 -0700697 /* reset soft limits and set hard limits for the new label */
John Johansend9087c42017-01-27 03:53:53 -0800698 __aa_transition_rlimits(label, new_label);
John Johansenfe864822017-06-09 05:27:50 -0700699}
700
701/**
702 * apparmor_bprm_committed_cred - do cleanup after new creds committed
703 * @bprm: binprm for the exec (NOT NULL)
704 */
705static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
706{
John Johansen3b529a72017-01-20 01:59:25 -0800707 /* clear out temporary/transitional state from the context */
John Johansende62de52017-10-08 00:43:02 -0700708 aa_clear_task_ctx_trans(task_ctx(current));
John Johansen3b529a72017-01-20 01:59:25 -0800709
John Johansenfe864822017-06-09 05:27:50 -0700710 return;
711}
712
John Johansena7ae3642017-09-11 11:29:53 -0700713static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
714{
715 struct aa_label *label = aa_get_task_label(p);
716 *secid = label->secid;
717 aa_put_label(label);
718}
719
Jiri Slaby7cb4dc92010-08-11 11:28:02 +0200720static int apparmor_task_setrlimit(struct task_struct *task,
721 unsigned int resource, struct rlimit *new_rlim)
John Johansenb5e95b42010-07-29 14:48:07 -0700722{
John Johansen637f6882017-06-09 08:14:28 -0700723 struct aa_label *label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700724 int error = 0;
725
John Johansen637f6882017-06-09 08:14:28 -0700726 if (!unconfined(label))
John Johansen86b92cb2017-06-09 14:15:20 -0700727 error = aa_task_setrlimit(label, task, resource, new_rlim);
John Johansen637f6882017-06-09 08:14:28 -0700728 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700729
730 return error;
731}
732
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200733static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400734 int sig, const struct cred *cred)
John Johansencd1dbf72017-07-18 22:56:22 -0700735{
736 struct aa_label *cl, *tl;
737 int error;
738
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400739 if (cred) {
740 /*
741 * Dealing with USB IO specific behavior
John Johansencd1dbf72017-07-18 22:56:22 -0700742 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400743 cl = aa_get_newest_cred_label(cred);
744 tl = aa_get_task_label(target);
745 error = aa_may_signal(cl, tl, sig);
746 aa_put_label(cl);
747 aa_put_label(tl);
748 return error;
749 }
750
John Johansencd1dbf72017-07-18 22:56:22 -0700751 cl = __begin_current_label_crit_section();
752 tl = aa_get_task_label(target);
753 error = aa_may_signal(cl, tl, sig);
754 aa_put_label(tl);
755 __end_current_label_crit_section(cl);
756
757 return error;
758}
759
John Johansen56974a62017-07-18 23:18:33 -0700760/**
761 * apparmor_sk_alloc_security - allocate and attach the sk_security field
762 */
763static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
764{
765 struct aa_sk_ctx *ctx;
766
767 ctx = kzalloc(sizeof(*ctx), flags);
768 if (!ctx)
769 return -ENOMEM;
770
771 SK_CTX(sk) = ctx;
772
773 return 0;
774}
775
776/**
777 * apparmor_sk_free_security - free the sk_security field
778 */
779static void apparmor_sk_free_security(struct sock *sk)
780{
781 struct aa_sk_ctx *ctx = SK_CTX(sk);
782
783 SK_CTX(sk) = NULL;
784 aa_put_label(ctx->label);
785 aa_put_label(ctx->peer);
786 kfree(ctx);
787}
788
789/**
790 * apparmor_clone_security - clone the sk_security field
791 */
792static void apparmor_sk_clone_security(const struct sock *sk,
793 struct sock *newsk)
794{
795 struct aa_sk_ctx *ctx = SK_CTX(sk);
796 struct aa_sk_ctx *new = SK_CTX(newsk);
797
798 new->label = aa_get_label(ctx->label);
799 new->peer = aa_get_label(ctx->peer);
800}
801
802/**
803 * apparmor_socket_create - check perms before creating a new socket
804 */
805static int apparmor_socket_create(int family, int type, int protocol, int kern)
806{
807 struct aa_label *label;
808 int error = 0;
809
810 AA_BUG(in_interrupt());
811
812 label = begin_current_label_crit_section();
813 if (!(kern || unconfined(label)))
814 error = af_select(family,
815 create_perm(label, family, type, protocol),
816 aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
817 family, type, protocol));
818 end_current_label_crit_section(label);
819
820 return error;
821}
822
823/**
824 * apparmor_socket_post_create - setup the per-socket security struct
825 *
826 * Note:
827 * - kernel sockets currently labeled unconfined but we may want to
828 * move to a special kernel label
829 * - socket may not have sk here if created with sock_create_lite or
830 * sock_alloc. These should be accept cases which will be handled in
831 * sock_graft.
832 */
833static int apparmor_socket_post_create(struct socket *sock, int family,
834 int type, int protocol, int kern)
835{
836 struct aa_label *label;
837
838 if (kern) {
839 struct aa_ns *ns = aa_get_current_ns();
840
841 label = aa_get_label(ns_unconfined(ns));
842 aa_put_ns(ns);
843 } else
844 label = aa_get_current_label();
845
846 if (sock->sk) {
847 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
848
849 aa_put_label(ctx->label);
850 ctx->label = aa_get_label(label);
851 }
852 aa_put_label(label);
853
854 return 0;
855}
856
857/**
858 * apparmor_socket_bind - check perms before bind addr to socket
859 */
860static int apparmor_socket_bind(struct socket *sock,
861 struct sockaddr *address, int addrlen)
862{
863 AA_BUG(!sock);
864 AA_BUG(!sock->sk);
865 AA_BUG(!address);
866 AA_BUG(in_interrupt());
867
868 return af_select(sock->sk->sk_family,
869 bind_perm(sock, address, addrlen),
870 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
871}
872
873/**
874 * apparmor_socket_connect - check perms before connecting @sock to @address
875 */
876static int apparmor_socket_connect(struct socket *sock,
877 struct sockaddr *address, int addrlen)
878{
879 AA_BUG(!sock);
880 AA_BUG(!sock->sk);
881 AA_BUG(!address);
882 AA_BUG(in_interrupt());
883
884 return af_select(sock->sk->sk_family,
885 connect_perm(sock, address, addrlen),
886 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
887}
888
889/**
890 * apparmor_socket_list - check perms before allowing listen
891 */
892static int apparmor_socket_listen(struct socket *sock, int backlog)
893{
894 AA_BUG(!sock);
895 AA_BUG(!sock->sk);
896 AA_BUG(in_interrupt());
897
898 return af_select(sock->sk->sk_family,
899 listen_perm(sock, backlog),
900 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
901}
902
903/**
904 * apparmor_socket_accept - check perms before accepting a new connection.
905 *
906 * Note: while @newsock is created and has some information, the accept
907 * has not been done.
908 */
909static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
910{
911 AA_BUG(!sock);
912 AA_BUG(!sock->sk);
913 AA_BUG(!newsock);
914 AA_BUG(in_interrupt());
915
916 return af_select(sock->sk->sk_family,
917 accept_perm(sock, newsock),
918 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
919}
920
921static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
922 struct msghdr *msg, int size)
923{
924 AA_BUG(!sock);
925 AA_BUG(!sock->sk);
926 AA_BUG(!msg);
927 AA_BUG(in_interrupt());
928
929 return af_select(sock->sk->sk_family,
930 msg_perm(op, request, sock, msg, size),
931 aa_sk_perm(op, request, sock->sk));
932}
933
934/**
935 * apparmor_socket_sendmsg - check perms before sending msg to another socket
936 */
937static int apparmor_socket_sendmsg(struct socket *sock,
938 struct msghdr *msg, int size)
939{
940 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
941}
942
943/**
944 * apparmor_socket_recvmsg - check perms before receiving a message
945 */
946static int apparmor_socket_recvmsg(struct socket *sock,
947 struct msghdr *msg, int size, int flags)
948{
949 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
950}
951
952/* revaliation, get/set attr, shutdown */
953static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
954{
955 AA_BUG(!sock);
956 AA_BUG(!sock->sk);
957 AA_BUG(in_interrupt());
958
959 return af_select(sock->sk->sk_family,
960 sock_perm(op, request, sock),
961 aa_sk_perm(op, request, sock->sk));
962}
963
964/**
965 * apparmor_socket_getsockname - check perms before getting the local address
966 */
967static int apparmor_socket_getsockname(struct socket *sock)
968{
969 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
970}
971
972/**
973 * apparmor_socket_getpeername - check perms before getting remote address
974 */
975static int apparmor_socket_getpeername(struct socket *sock)
976{
977 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
978}
979
980/* revaliation, get/set attr, opt */
981static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
982 int level, int optname)
983{
984 AA_BUG(!sock);
985 AA_BUG(!sock->sk);
986 AA_BUG(in_interrupt());
987
988 return af_select(sock->sk->sk_family,
989 opt_perm(op, request, sock, level, optname),
990 aa_sk_perm(op, request, sock->sk));
991}
992
993/**
994 * apparmor_getsockopt - check perms before getting socket options
995 */
996static int apparmor_socket_getsockopt(struct socket *sock, int level,
997 int optname)
998{
999 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1000 level, optname);
1001}
1002
1003/**
1004 * apparmor_setsockopt - check perms before setting socket options
1005 */
1006static int apparmor_socket_setsockopt(struct socket *sock, int level,
1007 int optname)
1008{
1009 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1010 level, optname);
1011}
1012
1013/**
1014 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1015 */
1016static int apparmor_socket_shutdown(struct socket *sock, int how)
1017{
1018 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1019}
1020
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001021#ifdef CONFIG_NETWORK_SECMARK
John Johansen56974a62017-07-18 23:18:33 -07001022/**
1023 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
1024 *
1025 * Note: can not sleep may be called with locks held
1026 *
1027 * dont want protocol specific in __skb_recv_datagram()
1028 * to deny an incoming connection socket_sock_rcv_skb()
1029 */
1030static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1031{
Matthew Garrettab9f2112018-05-24 13:27:47 -07001032 struct aa_sk_ctx *ctx = SK_CTX(sk);
1033
1034 if (!skb->secmark)
1035 return 0;
1036
1037 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1038 skb->secmark, sk);
John Johansen56974a62017-07-18 23:18:33 -07001039}
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001040#endif
John Johansen56974a62017-07-18 23:18:33 -07001041
1042
1043static struct aa_label *sk_peer_label(struct sock *sk)
1044{
1045 struct aa_sk_ctx *ctx = SK_CTX(sk);
1046
1047 if (ctx->peer)
1048 return ctx->peer;
1049
1050 return ERR_PTR(-ENOPROTOOPT);
1051}
1052
1053/**
1054 * apparmor_socket_getpeersec_stream - get security context of peer
1055 *
1056 * Note: for tcp only valid if using ipsec or cipso on lan
1057 */
1058static int apparmor_socket_getpeersec_stream(struct socket *sock,
1059 char __user *optval,
1060 int __user *optlen,
1061 unsigned int len)
1062{
1063 char *name;
1064 int slen, error = 0;
1065 struct aa_label *label;
1066 struct aa_label *peer;
1067
1068 label = begin_current_label_crit_section();
1069 peer = sk_peer_label(sock->sk);
1070 if (IS_ERR(peer)) {
1071 error = PTR_ERR(peer);
1072 goto done;
1073 }
1074 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1075 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1076 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1077 /* don't include terminating \0 in slen, it breaks some apps */
1078 if (slen < 0) {
1079 error = -ENOMEM;
1080 } else {
1081 if (slen > len) {
1082 error = -ERANGE;
1083 } else if (copy_to_user(optval, name, slen)) {
1084 error = -EFAULT;
1085 goto out;
1086 }
1087 if (put_user(slen, optlen))
1088 error = -EFAULT;
1089out:
1090 kfree(name);
1091
1092 }
1093
1094done:
1095 end_current_label_crit_section(label);
1096
1097 return error;
1098}
1099
1100/**
1101 * apparmor_socket_getpeersec_dgram - get security label of packet
1102 * @sock: the peer socket
1103 * @skb: packet data
1104 * @secid: pointer to where to put the secid of the packet
1105 *
1106 * Sets the netlabel socket state on sk from parent
1107 */
1108static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1109 struct sk_buff *skb, u32 *secid)
1110
1111{
1112 /* TODO: requires secid support */
1113 return -ENOPROTOOPT;
1114}
1115
1116/**
1117 * apparmor_sock_graft - Initialize newly created socket
1118 * @sk: child sock
1119 * @parent: parent socket
1120 *
1121 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1122 * just set sk security information off of current creating process label
1123 * Labeling of sk for accept case - probably should be sock based
1124 * instead of task, because of the case where an implicitly labeled
1125 * socket is shared by different tasks.
1126 */
1127static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1128{
1129 struct aa_sk_ctx *ctx = SK_CTX(sk);
1130
1131 if (!ctx->label)
1132 ctx->label = aa_get_current_label();
1133}
1134
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001135#ifdef CONFIG_NETWORK_SECMARK
Matthew Garrettab9f2112018-05-24 13:27:47 -07001136static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb,
1137 struct request_sock *req)
1138{
1139 struct aa_sk_ctx *ctx = SK_CTX(sk);
1140
1141 if (!skb->secmark)
1142 return 0;
1143
1144 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1145 skb->secmark, sk);
1146}
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001147#endif
Matthew Garrettab9f2112018-05-24 13:27:47 -07001148
Casey Schauflerbbd36622018-11-12 09:30:56 -08001149/*
1150 * The cred blob is a pointer to, not an instance of, an aa_task_ctx.
1151 */
1152struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = {
1153 .lbs_cred = sizeof(struct aa_task_ctx *),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08001154 .lbs_file = sizeof(struct aa_file_ctx),
Casey Schauflerf4ad8f22018-09-21 17:19:37 -07001155 .lbs_task = sizeof(struct aa_task_ctx),
Casey Schauflerbbd36622018-11-12 09:30:56 -08001156};
1157
James Morrisca97d932017-02-15 00:18:51 +11001158static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07001159 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1160 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1161 LSM_HOOK_INIT(capget, apparmor_capget),
1162 LSM_HOOK_INIT(capable, apparmor_capable),
John Johansenb5e95b42010-07-29 14:48:07 -07001163
John Johansen2ea3ffb2017-07-18 23:04:47 -07001164 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1165 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1166 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1167
Casey Schauflere20b0432015-05-02 15:11:36 -07001168 LSM_HOOK_INIT(path_link, apparmor_path_link),
1169 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1170 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1171 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1172 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1173 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1174 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1175 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1176 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1177 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1178 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
John Johansenb5e95b42010-07-29 14:48:07 -07001179
Casey Schauflere20b0432015-05-02 15:11:36 -07001180 LSM_HOOK_INIT(file_open, apparmor_file_open),
John Johansen064dc942017-06-09 17:15:56 -07001181 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
Casey Schauflere20b0432015-05-02 15:11:36 -07001182 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1183 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1184 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1185 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07001186 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1187 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
John Johansenb5e95b42010-07-29 14:48:07 -07001188
Casey Schauflere20b0432015-05-02 15:11:36 -07001189 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1190 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
John Johansenb5e95b42010-07-29 14:48:07 -07001191
John Johansen56974a62017-07-18 23:18:33 -07001192 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1193 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1194 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1195
1196 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1197 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1198 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1199 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1200 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1201 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1202 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1203 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1204 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1205 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1206 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1207 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1208 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001209#ifdef CONFIG_NETWORK_SECMARK
John Johansen56974a62017-07-18 23:18:33 -07001210 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001211#endif
John Johansen56974a62017-07-18 23:18:33 -07001212 LSM_HOOK_INIT(socket_getpeersec_stream,
1213 apparmor_socket_getpeersec_stream),
1214 LSM_HOOK_INIT(socket_getpeersec_dgram,
1215 apparmor_socket_getpeersec_dgram),
1216 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001217#ifdef CONFIG_NETWORK_SECMARK
Matthew Garrettab9f2112018-05-24 13:27:47 -07001218 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001219#endif
John Johansen56974a62017-07-18 23:18:33 -07001220
Casey Schauflere20b0432015-05-02 15:11:36 -07001221 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1222 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1223 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1224 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
John Johansenb5e95b42010-07-29 14:48:07 -07001225
Casey Schauflere20b0432015-05-02 15:11:36 -07001226 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
1227 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1228 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
John Johansenb5e95b42010-07-29 14:48:07 -07001229
John Johansen3b529a72017-01-20 01:59:25 -08001230 LSM_HOOK_INIT(task_free, apparmor_task_free),
1231 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
John Johansena7ae3642017-09-11 11:29:53 -07001232 LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07001233 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
John Johansencd1dbf72017-07-18 22:56:22 -07001234 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
John Johansenc0929212017-07-31 17:36:45 -07001235
Matthew Garrette79c26d2018-04-16 11:23:58 -07001236#ifdef CONFIG_AUDIT
1237 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1238 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1239 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1240 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1241#endif
1242
John Johansenc0929212017-07-31 17:36:45 -07001243 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1244 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1245 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
John Johansenb5e95b42010-07-29 14:48:07 -07001246};
1247
1248/*
1249 * AppArmor sysfs module parameters
1250 */
1251
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001252static int param_set_aabool(const char *val, const struct kernel_param *kp);
1253static int param_get_aabool(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301254#define param_check_aabool param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301255static const struct kernel_param_ops param_ops_aabool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +09301256 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001257 .set = param_set_aabool,
1258 .get = param_get_aabool
1259};
John Johansenb5e95b42010-07-29 14:48:07 -07001260
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001261static int param_set_aauint(const char *val, const struct kernel_param *kp);
1262static int param_get_aauint(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301263#define param_check_aauint param_check_uint
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301264static const struct kernel_param_ops param_ops_aauint = {
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001265 .set = param_set_aauint,
1266 .get = param_get_aauint
1267};
John Johansenb5e95b42010-07-29 14:48:07 -07001268
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001269static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1270static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301271#define param_check_aalockpolicy param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301272static const struct kernel_param_ops param_ops_aalockpolicy = {
Jani Nikula6a4c2642014-08-27 06:21:23 +09301273 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001274 .set = param_set_aalockpolicy,
1275 .get = param_get_aalockpolicy
1276};
John Johansenb5e95b42010-07-29 14:48:07 -07001277
Kees Cooke4dca7b2017-10-17 19:04:42 -07001278static int param_set_audit(const char *val, const struct kernel_param *kp);
1279static int param_get_audit(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -07001280
Kees Cooke4dca7b2017-10-17 19:04:42 -07001281static int param_set_mode(const char *val, const struct kernel_param *kp);
1282static int param_get_mode(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -07001283
1284/* Flag values, also controllable via /sys/module/apparmor/parameters
1285 * We define special types as we want to do additional mediation.
1286 */
1287
1288/* AppArmor global enforcement switch - complain, enforce, kill */
1289enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1290module_param_call(mode, param_set_mode, param_get_mode,
1291 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1292
John Johansen6059f712014-10-24 09:16:14 -07001293/* whether policy verification hashing is enabled */
Arnd Bergmann7616ac72016-07-25 10:59:07 -07001294bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
John Johansen3ccb76c2017-01-16 13:21:27 -08001295#ifdef CONFIG_SECURITY_APPARMOR_HASH
John Johansen6059f712014-10-24 09:16:14 -07001296module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
Arnd Bergmann7616ac72016-07-25 10:59:07 -07001297#endif
John Johansen6059f712014-10-24 09:16:14 -07001298
John Johansenb5e95b42010-07-29 14:48:07 -07001299/* Debug mode */
Valentin Rothbergeea7a052017-04-06 06:55:20 -07001300bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
John Johansenb5e95b42010-07-29 14:48:07 -07001301module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1302
1303/* Audit mode */
1304enum audit_mode aa_g_audit;
1305module_param_call(audit, param_set_audit, param_get_audit,
1306 &aa_g_audit, S_IRUSR | S_IWUSR);
1307
1308/* Determines if audit header is included in audited messages. This
1309 * provides more context if the audit daemon is not running
1310 */
Thomas Meyer954317f2017-10-07 16:02:21 +02001311bool aa_g_audit_header = true;
John Johansenb5e95b42010-07-29 14:48:07 -07001312module_param_named(audit_header, aa_g_audit_header, aabool,
1313 S_IRUSR | S_IWUSR);
1314
1315/* lock out loading/removal of policy
1316 * TODO: add in at boot loading of policy, which is the only way to
1317 * load policy, if lock_policy is set
1318 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301319bool aa_g_lock_policy;
John Johansenb5e95b42010-07-29 14:48:07 -07001320module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1321 S_IRUSR | S_IWUSR);
1322
1323/* Syscall logging mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301324bool aa_g_logsyscall;
John Johansenb5e95b42010-07-29 14:48:07 -07001325module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1326
1327/* Maximum pathname length before accesses will start getting rejected */
1328unsigned int aa_g_path_max = 2 * PATH_MAX;
John Johansen622f6e32017-04-06 06:55:24 -07001329module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
John Johansenb5e95b42010-07-29 14:48:07 -07001330
1331/* Determines how paranoid loading of policy is and how much verification
1332 * on the loaded policy is done.
John Johansenabbf8732017-01-16 00:42:37 -08001333 * DEPRECATED: read only as strict checking of load is always done now
1334 * that none root users (user namespaces) can load policy.
John Johansenb5e95b42010-07-29 14:48:07 -07001335 */
Thomas Meyer954317f2017-10-07 16:02:21 +02001336bool aa_g_paranoid_load = true;
John Johansenabbf8732017-01-16 00:42:37 -08001337module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -07001338
1339/* Boot time disable flag */
Kees Cook0102fb82018-10-01 17:08:57 -07001340static int apparmor_enabled __lsm_ro_after_init = 1;
Kees Cookc5459b82018-09-13 22:28:48 -07001341module_param_named(enabled, apparmor_enabled, int, 0444);
John Johansenb5e95b42010-07-29 14:48:07 -07001342
1343static int __init apparmor_enabled_setup(char *str)
1344{
1345 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +09001346 int error = kstrtoul(str, 0, &enabled);
John Johansenb5e95b42010-07-29 14:48:07 -07001347 if (!error)
1348 apparmor_enabled = enabled ? 1 : 0;
1349 return 1;
1350}
1351
1352__setup("apparmor=", apparmor_enabled_setup);
1353
1354/* set global flag turning off the ability to load policy */
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001355static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001356{
John Johansen545de8f2017-04-06 06:55:23 -07001357 if (!apparmor_enabled)
1358 return -EINVAL;
1359 if (apparmor_initialized && !policy_admin_capable(NULL))
John Johansenb5e95b42010-07-29 14:48:07 -07001360 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001361 return param_set_bool(val, kp);
1362}
1363
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001364static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001365{
John Johansenca4bd5a2017-01-16 00:43:11 -08001366 if (!apparmor_enabled)
1367 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001368 if (apparmor_initialized && !policy_view_capable(NULL))
1369 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001370 return param_get_bool(buffer, kp);
1371}
1372
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001373static int param_set_aabool(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001374{
John Johansenca4bd5a2017-01-16 00:43:11 -08001375 if (!apparmor_enabled)
1376 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001377 if (apparmor_initialized && !policy_admin_capable(NULL))
1378 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001379 return param_set_bool(val, kp);
1380}
1381
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001382static int param_get_aabool(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001383{
John Johansenca4bd5a2017-01-16 00:43:11 -08001384 if (!apparmor_enabled)
1385 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001386 if (apparmor_initialized && !policy_view_capable(NULL))
1387 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001388 return param_get_bool(buffer, kp);
1389}
1390
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001391static int param_set_aauint(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001392{
John Johansen39d84822017-03-30 05:25:23 -07001393 int error;
1394
John Johansenca4bd5a2017-01-16 00:43:11 -08001395 if (!apparmor_enabled)
1396 return -EINVAL;
John Johansen39d84822017-03-30 05:25:23 -07001397 /* file is ro but enforce 2nd line check */
1398 if (apparmor_initialized)
John Johansen545de8f2017-04-06 06:55:23 -07001399 return -EPERM;
John Johansen39d84822017-03-30 05:25:23 -07001400
1401 error = param_set_uint(val, kp);
1402 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1403
1404 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001405}
1406
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001407static int param_get_aauint(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001408{
John Johansenca4bd5a2017-01-16 00:43:11 -08001409 if (!apparmor_enabled)
1410 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001411 if (apparmor_initialized && !policy_view_capable(NULL))
1412 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001413 return param_get_uint(buffer, kp);
1414}
1415
Kees Cooke4dca7b2017-10-17 19:04:42 -07001416static int param_get_audit(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001417{
John Johansenb5e95b42010-07-29 14:48:07 -07001418 if (!apparmor_enabled)
1419 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001420 if (apparmor_initialized && !policy_view_capable(NULL))
1421 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001422 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1423}
1424
Kees Cooke4dca7b2017-10-17 19:04:42 -07001425static int param_set_audit(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001426{
1427 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001428
1429 if (!apparmor_enabled)
1430 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001431 if (!val)
1432 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001433 if (apparmor_initialized && !policy_admin_capable(NULL))
1434 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001435
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001436 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1437 if (i < 0)
1438 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001439
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001440 aa_g_audit = i;
1441 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -07001442}
1443
Kees Cooke4dca7b2017-10-17 19:04:42 -07001444static int param_get_mode(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001445{
John Johansenb5e95b42010-07-29 14:48:07 -07001446 if (!apparmor_enabled)
1447 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001448 if (apparmor_initialized && !policy_view_capable(NULL))
1449 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001450
John Johansen0d259f02013-07-10 21:13:43 -07001451 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
John Johansenb5e95b42010-07-29 14:48:07 -07001452}
1453
Kees Cooke4dca7b2017-10-17 19:04:42 -07001454static int param_set_mode(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001455{
1456 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001457
1458 if (!apparmor_enabled)
1459 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001460 if (!val)
1461 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001462 if (apparmor_initialized && !policy_admin_capable(NULL))
1463 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001464
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001465 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1466 val);
1467 if (i < 0)
1468 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001469
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001470 aa_g_profile_mode = i;
1471 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -07001472}
1473
1474/*
1475 * AppArmor init functions
1476 */
1477
1478/**
John Johansen55a26eb2017-01-16 00:43:00 -08001479 * set_init_ctx - set a task context and profile on the first task.
John Johansenb5e95b42010-07-29 14:48:07 -07001480 *
1481 * TODO: allow setting an alternate profile than unconfined
1482 */
John Johansen55a26eb2017-01-16 00:43:00 -08001483static int __init set_init_ctx(void)
John Johansenb5e95b42010-07-29 14:48:07 -07001484{
1485 struct cred *cred = (struct cred *)current->real_cred;
John Johansenb5e95b42010-07-29 14:48:07 -07001486
Casey Schaufler69b5a442018-09-21 17:17:59 -07001487 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
John Johansenb5e95b42010-07-29 14:48:07 -07001488
1489 return 0;
1490}
1491
John Johansend4669f02017-01-16 00:43:10 -08001492static void destroy_buffers(void)
1493{
1494 u32 i, j;
1495
1496 for_each_possible_cpu(i) {
1497 for_each_cpu_buffer(j) {
1498 kfree(per_cpu(aa_buffers, i).buf[j]);
1499 per_cpu(aa_buffers, i).buf[j] = NULL;
1500 }
1501 }
1502}
1503
1504static int __init alloc_buffers(void)
1505{
1506 u32 i, j;
1507
1508 for_each_possible_cpu(i) {
1509 for_each_cpu_buffer(j) {
1510 char *buffer;
1511
1512 if (cpu_to_node(i) > num_online_nodes())
1513 /* fallback to kmalloc for offline nodes */
1514 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1515 else
1516 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1517 cpu_to_node(i));
1518 if (!buffer) {
1519 destroy_buffers();
1520 return -ENOMEM;
1521 }
1522 per_cpu(aa_buffers, i).buf[j] = buffer;
1523 }
1524 }
1525
1526 return 0;
1527}
1528
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001529#ifdef CONFIG_SYSCTL
1530static int apparmor_dointvec(struct ctl_table *table, int write,
1531 void __user *buffer, size_t *lenp, loff_t *ppos)
1532{
1533 if (!policy_admin_capable(NULL))
1534 return -EPERM;
1535 if (!apparmor_enabled)
1536 return -EINVAL;
1537
1538 return proc_dointvec(table, write, buffer, lenp, ppos);
1539}
1540
1541static struct ctl_path apparmor_sysctl_path[] = {
1542 { .procname = "kernel", },
1543 { }
1544};
1545
1546static struct ctl_table apparmor_sysctl_table[] = {
1547 {
1548 .procname = "unprivileged_userns_apparmor_policy",
1549 .data = &unprivileged_userns_apparmor_policy,
1550 .maxlen = sizeof(int),
1551 .mode = 0600,
1552 .proc_handler = apparmor_dointvec,
1553 },
1554 { }
1555};
1556
1557static int __init apparmor_init_sysctl(void)
1558{
1559 return register_sysctl_paths(apparmor_sysctl_path,
1560 apparmor_sysctl_table) ? 0 : -ENOMEM;
1561}
1562#else
1563static inline int apparmor_init_sysctl(void)
1564{
1565 return 0;
1566}
1567#endif /* CONFIG_SYSCTL */
1568
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001569#if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
Matthew Garrettab9f2112018-05-24 13:27:47 -07001570static unsigned int apparmor_ip_postroute(void *priv,
1571 struct sk_buff *skb,
1572 const struct nf_hook_state *state)
1573{
1574 struct aa_sk_ctx *ctx;
1575 struct sock *sk;
1576
1577 if (!skb->secmark)
1578 return NF_ACCEPT;
1579
1580 sk = skb_to_full_sk(skb);
1581 if (sk == NULL)
1582 return NF_ACCEPT;
1583
1584 ctx = SK_CTX(sk);
1585 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1586 skb->secmark, sk))
1587 return NF_ACCEPT;
1588
1589 return NF_DROP_ERR(-ECONNREFUSED);
1590
1591}
1592
1593static unsigned int apparmor_ipv4_postroute(void *priv,
1594 struct sk_buff *skb,
1595 const struct nf_hook_state *state)
1596{
1597 return apparmor_ip_postroute(priv, skb, state);
1598}
1599
Petr Vorela1a02062018-11-12 11:59:12 +01001600#if IS_ENABLED(CONFIG_IPV6)
Matthew Garrettab9f2112018-05-24 13:27:47 -07001601static unsigned int apparmor_ipv6_postroute(void *priv,
1602 struct sk_buff *skb,
1603 const struct nf_hook_state *state)
1604{
1605 return apparmor_ip_postroute(priv, skb, state);
1606}
Petr Vorela1a02062018-11-12 11:59:12 +01001607#endif
Matthew Garrettab9f2112018-05-24 13:27:47 -07001608
1609static const struct nf_hook_ops apparmor_nf_ops[] = {
1610 {
1611 .hook = apparmor_ipv4_postroute,
1612 .pf = NFPROTO_IPV4,
1613 .hooknum = NF_INET_POST_ROUTING,
1614 .priority = NF_IP_PRI_SELINUX_FIRST,
1615 },
1616#if IS_ENABLED(CONFIG_IPV6)
1617 {
1618 .hook = apparmor_ipv6_postroute,
1619 .pf = NFPROTO_IPV6,
1620 .hooknum = NF_INET_POST_ROUTING,
1621 .priority = NF_IP6_PRI_SELINUX_FIRST,
1622 },
1623#endif
1624};
1625
1626static int __net_init apparmor_nf_register(struct net *net)
1627{
1628 int ret;
1629
1630 ret = nf_register_net_hooks(net, apparmor_nf_ops,
1631 ARRAY_SIZE(apparmor_nf_ops));
1632 return ret;
1633}
1634
1635static void __net_exit apparmor_nf_unregister(struct net *net)
1636{
1637 nf_unregister_net_hooks(net, apparmor_nf_ops,
1638 ARRAY_SIZE(apparmor_nf_ops));
1639}
1640
1641static struct pernet_operations apparmor_net_ops = {
1642 .init = apparmor_nf_register,
1643 .exit = apparmor_nf_unregister,
1644};
1645
1646static int __init apparmor_nf_ip_init(void)
1647{
1648 int err;
1649
1650 if (!apparmor_enabled)
1651 return 0;
1652
1653 err = register_pernet_subsys(&apparmor_net_ops);
1654 if (err)
1655 panic("Apparmor: register_pernet_subsys: error %d\n", err);
1656
1657 return 0;
1658}
1659__initcall(apparmor_nf_ip_init);
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001660#endif
Matthew Garrettab9f2112018-05-24 13:27:47 -07001661
John Johansenb5e95b42010-07-29 14:48:07 -07001662static int __init apparmor_init(void)
1663{
1664 int error;
1665
John Johansena4c3f892018-06-04 19:44:59 -07001666 aa_secids_init();
1667
John Johansen11c236b2017-01-16 00:42:42 -08001668 error = aa_setup_dfa_engine();
1669 if (error) {
1670 AA_ERROR("Unable to setup dfa engine\n");
1671 goto alloc_out;
1672 }
1673
John Johansenb5e95b42010-07-29 14:48:07 -07001674 error = aa_alloc_root_ns();
1675 if (error) {
1676 AA_ERROR("Unable to allocate default profile namespace\n");
1677 goto alloc_out;
1678 }
1679
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001680 error = apparmor_init_sysctl();
1681 if (error) {
1682 AA_ERROR("Unable to register sysctls\n");
1683 goto alloc_out;
1684
1685 }
1686
John Johansend4669f02017-01-16 00:43:10 -08001687 error = alloc_buffers();
1688 if (error) {
1689 AA_ERROR("Unable to allocate work buffers\n");
1690 goto buffers_out;
1691 }
1692
John Johansen55a26eb2017-01-16 00:43:00 -08001693 error = set_init_ctx();
John Johansenb5e95b42010-07-29 14:48:07 -07001694 if (error) {
1695 AA_ERROR("Failed to set context on init task\n");
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001696 aa_free_root_ns();
John Johansend4669f02017-01-16 00:43:10 -08001697 goto buffers_out;
John Johansenb5e95b42010-07-29 14:48:07 -07001698 }
Casey Schauflerd69dece52017-01-18 17:09:05 -08001699 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1700 "apparmor");
John Johansenb5e95b42010-07-29 14:48:07 -07001701
1702 /* Report that AppArmor successfully initialized */
1703 apparmor_initialized = 1;
1704 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1705 aa_info_message("AppArmor initialized: complain mode enabled");
1706 else if (aa_g_profile_mode == APPARMOR_KILL)
1707 aa_info_message("AppArmor initialized: kill mode enabled");
1708 else
1709 aa_info_message("AppArmor initialized");
1710
1711 return error;
1712
John Johansend4669f02017-01-16 00:43:10 -08001713buffers_out:
1714 destroy_buffers();
1715
John Johansenb5e95b42010-07-29 14:48:07 -07001716alloc_out:
1717 aa_destroy_aafs();
John Johansen11c236b2017-01-16 00:42:42 -08001718 aa_teardown_dfa_engine();
John Johansenb5e95b42010-07-29 14:48:07 -07001719
Thomas Meyer954317f2017-10-07 16:02:21 +02001720 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001721 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001722}
1723
Kees Cook3d6e5f62018-10-10 17:18:23 -07001724DEFINE_LSM(apparmor) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07001725 .name = "apparmor",
Kees Cook14bd99c2018-09-19 19:57:06 -07001726 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07001727 .enabled = &apparmor_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08001728 .blobs = &apparmor_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07001729 .init = apparmor_init,
1730};