blob: 6821187b06adfb02e0462dd7a9020c3c3cf46bf4 [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));
97 task_ctx(task) = NULL;
98}
99
100static int apparmor_task_alloc(struct task_struct *task,
101 unsigned long clone_flags)
102{
103 struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL);
104
105 if (!new)
106 return -ENOMEM;
107
John Johansende62de52017-10-08 00:43:02 -0700108 aa_dup_task_ctx(new, task_ctx(current));
John Johansen3b529a72017-01-20 01:59:25 -0800109 task_ctx(task) = new;
110
111 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -0700112}
113
114static int apparmor_ptrace_access_check(struct task_struct *child,
115 unsigned int mode)
116{
John Johansenb2d09ae2017-06-09 14:22:14 -0700117 struct aa_label *tracer, *tracee;
118 int error;
119
Jann Horn1f8266f2018-09-13 18:12:09 +0200120 tracer = __begin_current_label_crit_section();
John Johansenb2d09ae2017-06-09 14:22:14 -0700121 tracee = aa_get_task_label(child);
122 error = aa_may_ptrace(tracer, tracee,
John Johansen338d0be2018-06-07 00:45:30 -0700123 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
124 : AA_PTRACE_TRACE);
John Johansenb2d09ae2017-06-09 14:22:14 -0700125 aa_put_label(tracee);
Jann Horn1f8266f2018-09-13 18:12:09 +0200126 __end_current_label_crit_section(tracer);
John Johansenb2d09ae2017-06-09 14:22:14 -0700127
128 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700129}
130
131static int apparmor_ptrace_traceme(struct task_struct *parent)
132{
John Johansenb2d09ae2017-06-09 14:22:14 -0700133 struct aa_label *tracer, *tracee;
134 int error;
135
Jann Hornca3fde52018-09-29 03:49:26 +0200136 tracee = __begin_current_label_crit_section();
John Johansenb2d09ae2017-06-09 14:22:14 -0700137 tracer = aa_get_task_label(parent);
138 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
139 aa_put_label(tracer);
Jann Hornca3fde52018-09-29 03:49:26 +0200140 __end_current_label_crit_section(tracee);
John Johansenb2d09ae2017-06-09 14:22:14 -0700141
142 return error;
John Johansenb5e95b42010-07-29 14:48:07 -0700143}
144
145/* Derived from security/commoncap.c:cap_capget */
146static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
147 kernel_cap_t *inheritable, kernel_cap_t *permitted)
148{
John Johansen637f6882017-06-09 08:14:28 -0700149 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700150 const struct cred *cred;
151
152 rcu_read_lock();
153 cred = __task_cred(target);
John Johansen637f6882017-06-09 08:14:28 -0700154 label = aa_get_newest_cred_label(cred);
John Johansenc70c86c2017-06-09 14:07:02 -0700155
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700156 /*
157 * cap_capget is stacked ahead of this and will
158 * initialize effective and permitted.
159 */
John Johansenc70c86c2017-06-09 14:07:02 -0700160 if (!unconfined(label)) {
161 struct aa_profile *profile;
162 struct label_it i;
163
164 label_for_each_confined(i, label, profile) {
165 if (COMPLAIN_MODE(profile))
166 continue;
167 *effective = cap_intersect(*effective,
168 profile->caps.allow);
169 *permitted = cap_intersect(*permitted,
170 profile->caps.allow);
171 }
John Johansenb5e95b42010-07-29 14:48:07 -0700172 }
173 rcu_read_unlock();
John Johansen637f6882017-06-09 08:14:28 -0700174 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700175
176 return 0;
177}
178
Eric Paris6a9de492012-01-03 12:25:14 -0500179static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
180 int cap, int audit)
John Johansenb5e95b42010-07-29 14:48:07 -0700181{
John Johansen637f6882017-06-09 08:14:28 -0700182 struct aa_label *label;
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700183 int error = 0;
184
John Johansen637f6882017-06-09 08:14:28 -0700185 label = aa_get_newest_cred_label(cred);
186 if (!unconfined(label))
John Johansenc70c86c2017-06-09 14:07:02 -0700187 error = aa_capable(label, cap, audit);
John Johansen637f6882017-06-09 08:14:28 -0700188 aa_put_label(label);
John Johansencf797c02017-06-09 02:08:28 -0700189
John Johansenb5e95b42010-07-29 14:48:07 -0700190 return error;
191}
192
193/**
194 * common_perm - basic common permission check wrapper fn for paths
195 * @op: operation being checked
196 * @path: path to check permission of (NOT NULL)
197 * @mask: requested permissions mask
198 * @cond: conditional info for the permission request (NOT NULL)
199 *
200 * Returns: %0 else error code if error or permission denied
201 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800202static int common_perm(const char *op, const struct path *path, u32 mask,
John Johansenb5e95b42010-07-29 14:48:07 -0700203 struct path_cond *cond)
204{
John Johansen637f6882017-06-09 08:14:28 -0700205 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700206 int error = 0;
207
John Johansen637f6882017-06-09 08:14:28 -0700208 label = __begin_current_label_crit_section();
209 if (!unconfined(label))
John Johansenaebd8732017-06-09 16:02:25 -0700210 error = aa_path_perm(op, label, path, 0, mask, cond);
John Johansen637f6882017-06-09 08:14:28 -0700211 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700212
213 return error;
214}
215
216/**
John Johansen31f75bf2017-01-16 00:43:07 -0800217 * common_perm_cond - common permission wrapper around inode cond
218 * @op: operation being checked
219 * @path: location to check (NOT NULL)
220 * @mask: requested permissions mask
221 *
222 * Returns: %0 else error code if error or permission denied
223 */
224static int common_perm_cond(const char *op, const struct path *path, u32 mask)
225{
226 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
227 d_backing_inode(path->dentry)->i_mode
228 };
229
230 if (!path_mediated_fs(path->dentry))
231 return 0;
232
233 return common_perm(op, path, mask, &cond);
234}
235
236/**
John Johansenb5e95b42010-07-29 14:48:07 -0700237 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
238 * @op: operation being checked
239 * @dir: directory of the dentry (NOT NULL)
240 * @dentry: dentry to check (NOT NULL)
241 * @mask: requested permissions mask
242 * @cond: conditional info for the permission request (NOT NULL)
243 *
244 * Returns: %0 else error code if error or permission denied
245 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800246static int common_perm_dir_dentry(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700247 struct dentry *dentry, u32 mask,
248 struct path_cond *cond)
249{
Kees Cook8486adf2016-12-16 17:04:13 -0800250 struct path path = { .mnt = dir->mnt, .dentry = dentry };
John Johansenb5e95b42010-07-29 14:48:07 -0700251
252 return common_perm(op, &path, mask, cond);
253}
254
255/**
John Johansenb5e95b42010-07-29 14:48:07 -0700256 * common_perm_rm - common permission wrapper for operations doing rm
257 * @op: operation being checked
258 * @dir: directory that the dentry is in (NOT NULL)
259 * @dentry: dentry being rm'd (NOT NULL)
260 * @mask: requested permission mask
261 *
262 * Returns: %0 else error code if error or permission denied
263 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800264static int common_perm_rm(const char *op, const struct path *dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700265 struct dentry *dentry, u32 mask)
266{
David Howellsc6f493d2015-03-17 22:26:22 +0000267 struct inode *inode = d_backing_inode(dentry);
John Johansenb5e95b42010-07-29 14:48:07 -0700268 struct path_cond cond = { };
269
John Johansenefeee832017-01-16 00:42:28 -0800270 if (!inode || !path_mediated_fs(dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700271 return 0;
272
273 cond.uid = inode->i_uid;
274 cond.mode = inode->i_mode;
275
276 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
277}
278
279/**
280 * common_perm_create - common permission wrapper for operations doing create
281 * @op: operation being checked
282 * @dir: directory that dentry will be created in (NOT NULL)
283 * @dentry: dentry to create (NOT NULL)
284 * @mask: request permission mask
285 * @mode: created file mode
286 *
287 * Returns: %0 else error code if error or permission denied
288 */
John Johansen47f6e5c2017-01-16 00:43:01 -0800289static int common_perm_create(const char *op, const struct path *dir,
Al Virod6b49f72016-03-25 15:10:04 -0400290 struct dentry *dentry, u32 mask, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700291{
292 struct path_cond cond = { current_fsuid(), mode };
293
John Johansenefeee832017-01-16 00:42:28 -0800294 if (!path_mediated_fs(dir->dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700295 return 0;
296
297 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
298}
299
Al Viro989f74e2016-03-25 15:13:39 -0400300static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700301{
302 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
303}
304
Al Virod3607752016-03-25 15:21:09 -0400305static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500306 umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700307{
308 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
309 S_IFDIR);
310}
311
Al Viro989f74e2016-03-25 15:13:39 -0400312static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700313{
314 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
315}
316
Al Virod3607752016-03-25 15:21:09 -0400317static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500318 umode_t mode, unsigned int dev)
John Johansenb5e95b42010-07-29 14:48:07 -0700319{
320 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
321}
322
Al Viro81f4c502016-03-25 14:22:01 -0400323static int apparmor_path_truncate(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700324{
John Johansene53cfe62017-05-26 15:07:22 -0700325 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700326}
327
Al Virod3607752016-03-25 15:21:09 -0400328static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
John Johansenb5e95b42010-07-29 14:48:07 -0700329 const char *old_name)
330{
331 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
332 S_IFLNK);
333}
334
Al Viro3ccee462016-03-25 15:27:45 -0400335static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
John Johansenb5e95b42010-07-29 14:48:07 -0700336 struct dentry *new_dentry)
337{
John Johansen637f6882017-06-09 08:14:28 -0700338 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700339 int error = 0;
340
John Johansenefeee832017-01-16 00:42:28 -0800341 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700342 return 0;
343
John Johansen637f6882017-06-09 08:14:28 -0700344 label = begin_current_label_crit_section();
345 if (!unconfined(label))
John Johansen80143702017-06-09 16:06:21 -0700346 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
John Johansen637f6882017-06-09 08:14:28 -0700347 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700348
John Johansenb5e95b42010-07-29 14:48:07 -0700349 return error;
350}
351
Al Viro3ccee462016-03-25 15:27:45 -0400352static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
353 const struct path *new_dir, struct dentry *new_dentry)
John Johansenb5e95b42010-07-29 14:48:07 -0700354{
John Johansen637f6882017-06-09 08:14:28 -0700355 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700356 int error = 0;
357
John Johansenefeee832017-01-16 00:42:28 -0800358 if (!path_mediated_fs(old_dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700359 return 0;
360
John Johansen637f6882017-06-09 08:14:28 -0700361 label = begin_current_label_crit_section();
362 if (!unconfined(label)) {
Kees Cook8486adf2016-12-16 17:04:13 -0800363 struct path old_path = { .mnt = old_dir->mnt,
364 .dentry = old_dentry };
365 struct path new_path = { .mnt = new_dir->mnt,
366 .dentry = new_dentry };
David Howellsc6f493d2015-03-17 22:26:22 +0000367 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
368 d_backing_inode(old_dentry)->i_mode
John Johansenb5e95b42010-07-29 14:48:07 -0700369 };
370
John Johansenaebd8732017-06-09 16:02:25 -0700371 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
John Johansene53cfe62017-05-26 15:07:22 -0700372 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
373 AA_MAY_SETATTR | AA_MAY_DELETE,
John Johansenb5e95b42010-07-29 14:48:07 -0700374 &cond);
375 if (!error)
John Johansenaebd8732017-06-09 16:02:25 -0700376 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
John Johansene53cfe62017-05-26 15:07:22 -0700377 0, MAY_WRITE | AA_MAY_SETATTR |
John Johansenb5e95b42010-07-29 14:48:07 -0700378 AA_MAY_CREATE, &cond);
379
380 }
John Johansen637f6882017-06-09 08:14:28 -0700381 end_current_label_crit_section(label);
John Johansencf797c02017-06-09 02:08:28 -0700382
John Johansenb5e95b42010-07-29 14:48:07 -0700383 return error;
384}
385
Al Virobe01f9f2016-03-25 14:56:23 -0400386static int apparmor_path_chmod(const struct path *path, umode_t mode)
John Johansenb5e95b42010-07-29 14:48:07 -0700387{
John Johansen31f75bf2017-01-16 00:43:07 -0800388 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
John Johansenb5e95b42010-07-29 14:48:07 -0700389}
390
Al Viro7fd25da2016-03-25 14:44:41 -0400391static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
John Johansenb5e95b42010-07-29 14:48:07 -0700392{
John Johansen31f75bf2017-01-16 00:43:07 -0800393 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
John Johansenb5e95b42010-07-29 14:48:07 -0700394}
395
Al Viro3f7036a2015-03-08 19:28:30 -0400396static int apparmor_inode_getattr(const struct path *path)
John Johansenb5e95b42010-07-29 14:48:07 -0700397{
John Johansene53cfe62017-05-26 15:07:22 -0700398 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700399}
400
Al Viro94817692018-07-10 14:13:18 -0400401static int apparmor_file_open(struct file *file)
John Johansenb5e95b42010-07-29 14:48:07 -0700402{
John Johansen637f6882017-06-09 08:14:28 -0700403 struct aa_file_ctx *fctx = file_ctx(file);
404 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700405 int error = 0;
406
John Johansenefeee832017-01-16 00:42:28 -0800407 if (!path_mediated_fs(file->f_path.dentry))
John Johansenb5e95b42010-07-29 14:48:07 -0700408 return 0;
409
410 /* If in exec, permission is handled by bprm hooks.
411 * Cache permissions granted by the previous exec check, with
412 * implicit read and executable mmap which are required to
413 * actually execute the image.
414 */
415 if (current->in_execve) {
John Johansen55a26eb2017-01-16 00:43:00 -0800416 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
John Johansenb5e95b42010-07-29 14:48:07 -0700417 return 0;
418 }
419
Al Viro94817692018-07-10 14:13:18 -0400420 label = aa_get_newest_cred_label(file->f_cred);
John Johansen637f6882017-06-09 08:14:28 -0700421 if (!unconfined(label)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500422 struct inode *inode = file_inode(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700423 struct path_cond cond = { inode->i_uid, inode->i_mode };
424
John Johansenaebd8732017-06-09 16:02:25 -0700425 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
John Johansenb5e95b42010-07-29 14:48:07 -0700426 aa_map_file_to_perms(file), &cond);
427 /* todo cache full allowed permissions set and state */
John Johansen55a26eb2017-01-16 00:43:00 -0800428 fctx->allow = aa_map_file_to_perms(file);
John Johansenb5e95b42010-07-29 14:48:07 -0700429 }
John Johansen637f6882017-06-09 08:14:28 -0700430 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700431
432 return error;
433}
434
435static int apparmor_file_alloc_security(struct file *file)
436{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800437 struct aa_file_ctx *ctx = file_ctx(file);
John Johansen637f6882017-06-09 08:14:28 -0700438 struct aa_label *label = begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700439
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800440 spin_lock_init(&ctx->lock);
441 rcu_assign_pointer(ctx->label, aa_get_label(label));
442 end_current_label_crit_section(label);
443 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -0700444}
445
446static void apparmor_file_free_security(struct file *file)
447{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800448 struct aa_file_ctx *ctx = file_ctx(file);
449
450 if (ctx)
451 aa_put_label(rcu_access_pointer(ctx->label));
John Johansenb5e95b42010-07-29 14:48:07 -0700452}
453
John Johansen47f6e5c2017-01-16 00:43:01 -0800454static int common_file_perm(const char *op, struct file *file, u32 mask)
John Johansenb5e95b42010-07-29 14:48:07 -0700455{
John Johansen190a9512017-06-09 14:59:51 -0700456 struct aa_label *label;
John Johansenb5e95b42010-07-29 14:48:07 -0700457 int error = 0;
458
John Johansen192ca6b2017-06-09 11:58:42 -0700459 /* don't reaudit files closed during inheritance */
460 if (file->f_path.dentry == aa_null.dentry)
461 return -EACCES;
462
John Johansen637f6882017-06-09 08:14:28 -0700463 label = __begin_current_label_crit_section();
John Johansen190a9512017-06-09 14:59:51 -0700464 error = aa_file_perm(op, label, file, mask);
John Johansen637f6882017-06-09 08:14:28 -0700465 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700466
467 return error;
468}
469
John Johansen064dc942017-06-09 17:15:56 -0700470static int apparmor_file_receive(struct file *file)
471{
472 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
473}
474
John Johansenb5e95b42010-07-29 14:48:07 -0700475static int apparmor_file_permission(struct file *file, int mask)
476{
477 return common_file_perm(OP_FPERM, file, mask);
478}
479
480static int apparmor_file_lock(struct file *file, unsigned int cmd)
481{
482 u32 mask = AA_MAY_LOCK;
483
484 if (cmd == F_WRLCK)
485 mask |= MAY_WRITE;
486
487 return common_file_perm(OP_FLOCK, file, mask);
488}
489
John Johansen47f6e5c2017-01-16 00:43:01 -0800490static int common_mmap(const char *op, struct file *file, unsigned long prot,
John Johansenb5e95b42010-07-29 14:48:07 -0700491 unsigned long flags)
492{
John Johansenb5e95b42010-07-29 14:48:07 -0700493 int mask = 0;
494
John Johansen637f6882017-06-09 08:14:28 -0700495 if (!file || !file_ctx(file))
John Johansenb5e95b42010-07-29 14:48:07 -0700496 return 0;
497
498 if (prot & PROT_READ)
499 mask |= MAY_READ;
500 /*
501 * Private mappings don't require write perms since they don't
502 * write back to the files
503 */
504 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
505 mask |= MAY_WRITE;
506 if (prot & PROT_EXEC)
507 mask |= AA_EXEC_MMAP;
508
John Johansenb5e95b42010-07-29 14:48:07 -0700509 return common_file_perm(op, file, mask);
510}
511
Al Viroe5467852012-05-30 13:30:51 -0400512static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
513 unsigned long prot, unsigned long flags)
John Johansenb5e95b42010-07-29 14:48:07 -0700514{
John Johansenb5e95b42010-07-29 14:48:07 -0700515 return common_mmap(OP_FMMAP, file, prot, flags);
516}
517
518static int apparmor_file_mprotect(struct vm_area_struct *vma,
519 unsigned long reqprot, unsigned long prot)
520{
521 return common_mmap(OP_FMPROT, vma->vm_file, prot,
522 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
523}
524
John Johansen2ea3ffb2017-07-18 23:04:47 -0700525static int apparmor_sb_mount(const char *dev_name, const struct path *path,
526 const char *type, unsigned long flags, void *data)
527{
528 struct aa_label *label;
529 int error = 0;
530
531 /* Discard magic */
532 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
533 flags &= ~MS_MGC_MSK;
534
535 flags &= ~AA_MS_IGNORE_MASK;
536
537 label = __begin_current_label_crit_section();
538 if (!unconfined(label)) {
539 if (flags & MS_REMOUNT)
540 error = aa_remount(label, path, flags, data);
541 else if (flags & MS_BIND)
542 error = aa_bind_mount(label, path, dev_name, flags);
543 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
544 MS_UNBINDABLE))
545 error = aa_mount_change_type(label, path, flags);
546 else if (flags & MS_MOVE)
547 error = aa_move_mount(label, path, dev_name);
548 else
549 error = aa_new_mount(label, dev_name, path, type,
550 flags, data);
551 }
552 __end_current_label_crit_section(label);
553
554 return error;
555}
556
557static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
558{
559 struct aa_label *label;
560 int error = 0;
561
562 label = __begin_current_label_crit_section();
563 if (!unconfined(label))
564 error = aa_umount(label, mnt, flags);
565 __end_current_label_crit_section(label);
566
567 return error;
568}
569
570static int apparmor_sb_pivotroot(const struct path *old_path,
571 const struct path *new_path)
572{
573 struct aa_label *label;
574 int error = 0;
575
576 label = aa_get_current_label();
577 if (!unconfined(label))
578 error = aa_pivotroot(label, old_path, new_path);
579 aa_put_label(label);
580
581 return error;
582}
583
John Johansenb5e95b42010-07-29 14:48:07 -0700584static int apparmor_getprocattr(struct task_struct *task, char *name,
585 char **value)
586{
587 int error = -ENOENT;
John Johansenb5e95b42010-07-29 14:48:07 -0700588 /* released below */
589 const struct cred *cred = get_task_cred(task);
John Johansende62de52017-10-08 00:43:02 -0700590 struct aa_task_ctx *ctx = task_ctx(current);
John Johansen637f6882017-06-09 08:14:28 -0700591 struct aa_label *label = NULL;
John Johansenb5e95b42010-07-29 14:48:07 -0700592
593 if (strcmp(name, "current") == 0)
John Johansend9087c42017-01-27 03:53:53 -0800594 label = aa_get_newest_label(cred_label(cred));
John Johansen55a26eb2017-01-16 00:43:00 -0800595 else if (strcmp(name, "prev") == 0 && ctx->previous)
John Johansen637f6882017-06-09 08:14:28 -0700596 label = aa_get_newest_label(ctx->previous);
John Johansen55a26eb2017-01-16 00:43:00 -0800597 else if (strcmp(name, "exec") == 0 && ctx->onexec)
John Johansen637f6882017-06-09 08:14:28 -0700598 label = aa_get_newest_label(ctx->onexec);
John Johansenb5e95b42010-07-29 14:48:07 -0700599 else
600 error = -EINVAL;
601
John Johansen637f6882017-06-09 08:14:28 -0700602 if (label)
John Johansen76a1d262017-06-09 12:47:17 -0700603 error = aa_getprocattr(label, value);
John Johansen77b071b2013-07-10 21:07:43 -0700604
John Johansen637f6882017-06-09 08:14:28 -0700605 aa_put_label(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700606 put_cred(cred);
607
608 return error;
609}
610
Stephen Smalleyb21507e2017-01-09 10:07:31 -0500611static int apparmor_setprocattr(const char *name, void *value,
612 size_t size)
John Johansenb5e95b42010-07-29 14:48:07 -0700613{
Vegard Nossume89b8082016-07-07 13:41:11 -0700614 char *command, *largs = NULL, *args = value;
John Johansenb5e95b42010-07-29 14:48:07 -0700615 size_t arg_size;
616 int error;
John Johansenef88a7a2017-01-16 00:43:02 -0800617 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
John Johansenb5e95b42010-07-29 14:48:07 -0700618
619 if (size == 0)
620 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700621
Vegard Nossume89b8082016-07-07 13:41:11 -0700622 /* AppArmor requires that the buffer must be null terminated atm */
623 if (args[size - 1] != '\0') {
624 /* null terminate */
625 largs = args = kmalloc(size + 1, GFP_KERNEL);
626 if (!args)
627 return -ENOMEM;
628 memcpy(args, value, size);
629 args[size] = '\0';
630 }
631
632 error = -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -0700633 args = strim(args);
634 command = strsep(&args, " ");
635 if (!args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700636 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700637 args = skip_spaces(args);
638 if (!*args)
Vegard Nossume89b8082016-07-07 13:41:11 -0700639 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700640
John Johansend4d03f72016-07-09 23:46:33 -0700641 arg_size = size - (args - (largs ? largs : (char *) value));
John Johansenb5e95b42010-07-29 14:48:07 -0700642 if (strcmp(name, "current") == 0) {
643 if (strcmp(command, "changehat") == 0) {
644 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700645 AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700646 } else if (strcmp(command, "permhat") == 0) {
647 error = aa_setprocattr_changehat(args, arg_size,
John Johansendf8073c2017-06-09 11:36:48 -0700648 AA_CHANGE_TEST);
John Johansenb5e95b42010-07-29 14:48:07 -0700649 } else if (strcmp(command, "changeprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700650 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
John Johansenb5e95b42010-07-29 14:48:07 -0700651 } else if (strcmp(command, "permprofile") == 0) {
John Johansendf8073c2017-06-09 11:36:48 -0700652 error = aa_change_profile(args, AA_CHANGE_TEST);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700653 } else if (strcmp(command, "stack") == 0) {
654 error = aa_change_profile(args, AA_CHANGE_STACK);
John Johansen3eea57c2013-02-27 03:44:40 -0800655 } else
656 goto fail;
John Johansenb5e95b42010-07-29 14:48:07 -0700657 } else if (strcmp(name, "exec") == 0) {
John Johansen3eea57c2013-02-27 03:44:40 -0800658 if (strcmp(command, "exec") == 0)
John Johansendf8073c2017-06-09 11:36:48 -0700659 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
John Johansen6c5fc8f2017-06-09 17:22:50 -0700660 else if (strcmp(command, "stack") == 0)
661 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
662 AA_CHANGE_STACK));
John Johansen3eea57c2013-02-27 03:44:40 -0800663 else
664 goto fail;
665 } else
John Johansenb5e95b42010-07-29 14:48:07 -0700666 /* only support the "current" and "exec" process attributes */
Vegard Nossume89b8082016-07-07 13:41:11 -0700667 goto fail;
John Johansen3eea57c2013-02-27 03:44:40 -0800668
John Johansenb5e95b42010-07-29 14:48:07 -0700669 if (!error)
670 error = size;
Vegard Nossume89b8082016-07-07 13:41:11 -0700671out:
672 kfree(largs);
John Johansenb5e95b42010-07-29 14:48:07 -0700673 return error;
John Johansen3eea57c2013-02-27 03:44:40 -0800674
675fail:
John Johansen637f6882017-06-09 08:14:28 -0700676 aad(&sa)->label = begin_current_label_crit_section();
John Johansenef88a7a2017-01-16 00:43:02 -0800677 aad(&sa)->info = name;
678 aad(&sa)->error = error = -EINVAL;
John Johansen3eea57c2013-02-27 03:44:40 -0800679 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
John Johansen637f6882017-06-09 08:14:28 -0700680 end_current_label_crit_section(aad(&sa)->label);
Vegard Nossume89b8082016-07-07 13:41:11 -0700681 goto out;
John Johansenb5e95b42010-07-29 14:48:07 -0700682}
683
John Johansenfe864822017-06-09 05:27:50 -0700684/**
685 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
686 * @bprm: binprm for the exec (NOT NULL)
687 */
688static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
689{
John Johansen637f6882017-06-09 08:14:28 -0700690 struct aa_label *label = aa_current_raw_label();
John Johansend9087c42017-01-27 03:53:53 -0800691 struct aa_label *new_label = cred_label(bprm->cred);
John Johansenfe864822017-06-09 05:27:50 -0700692
693 /* bail out if unconfined or not changing profile */
John Johansend9087c42017-01-27 03:53:53 -0800694 if ((new_label->proxy == label->proxy) ||
695 (unconfined(new_label)))
John Johansenfe864822017-06-09 05:27:50 -0700696 return;
697
John Johansen192ca6b2017-06-09 11:58:42 -0700698 aa_inherit_files(bprm->cred, current->files);
699
John Johansenfe864822017-06-09 05:27:50 -0700700 current->pdeath_signal = 0;
701
John Johansen637f6882017-06-09 08:14:28 -0700702 /* reset soft limits and set hard limits for the new label */
John Johansend9087c42017-01-27 03:53:53 -0800703 __aa_transition_rlimits(label, new_label);
John Johansenfe864822017-06-09 05:27:50 -0700704}
705
706/**
707 * apparmor_bprm_committed_cred - do cleanup after new creds committed
708 * @bprm: binprm for the exec (NOT NULL)
709 */
710static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
711{
John Johansen3b529a72017-01-20 01:59:25 -0800712 /* clear out temporary/transitional state from the context */
John Johansende62de52017-10-08 00:43:02 -0700713 aa_clear_task_ctx_trans(task_ctx(current));
John Johansen3b529a72017-01-20 01:59:25 -0800714
John Johansenfe864822017-06-09 05:27:50 -0700715 return;
716}
717
John Johansena7ae3642017-09-11 11:29:53 -0700718static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
719{
720 struct aa_label *label = aa_get_task_label(p);
721 *secid = label->secid;
722 aa_put_label(label);
723}
724
Jiri Slaby7cb4dc92010-08-11 11:28:02 +0200725static int apparmor_task_setrlimit(struct task_struct *task,
726 unsigned int resource, struct rlimit *new_rlim)
John Johansenb5e95b42010-07-29 14:48:07 -0700727{
John Johansen637f6882017-06-09 08:14:28 -0700728 struct aa_label *label = __begin_current_label_crit_section();
John Johansenb5e95b42010-07-29 14:48:07 -0700729 int error = 0;
730
John Johansen637f6882017-06-09 08:14:28 -0700731 if (!unconfined(label))
John Johansen86b92cb2017-06-09 14:15:20 -0700732 error = aa_task_setrlimit(label, task, resource, new_rlim);
John Johansen637f6882017-06-09 08:14:28 -0700733 __end_current_label_crit_section(label);
John Johansenb5e95b42010-07-29 14:48:07 -0700734
735 return error;
736}
737
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200738static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400739 int sig, const struct cred *cred)
John Johansencd1dbf72017-07-18 22:56:22 -0700740{
741 struct aa_label *cl, *tl;
742 int error;
743
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400744 if (cred) {
745 /*
746 * Dealing with USB IO specific behavior
John Johansencd1dbf72017-07-18 22:56:22 -0700747 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400748 cl = aa_get_newest_cred_label(cred);
749 tl = aa_get_task_label(target);
750 error = aa_may_signal(cl, tl, sig);
751 aa_put_label(cl);
752 aa_put_label(tl);
753 return error;
754 }
755
John Johansencd1dbf72017-07-18 22:56:22 -0700756 cl = __begin_current_label_crit_section();
757 tl = aa_get_task_label(target);
758 error = aa_may_signal(cl, tl, sig);
759 aa_put_label(tl);
760 __end_current_label_crit_section(cl);
761
762 return error;
763}
764
John Johansen56974a62017-07-18 23:18:33 -0700765/**
766 * apparmor_sk_alloc_security - allocate and attach the sk_security field
767 */
768static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
769{
770 struct aa_sk_ctx *ctx;
771
772 ctx = kzalloc(sizeof(*ctx), flags);
773 if (!ctx)
774 return -ENOMEM;
775
776 SK_CTX(sk) = ctx;
777
778 return 0;
779}
780
781/**
782 * apparmor_sk_free_security - free the sk_security field
783 */
784static void apparmor_sk_free_security(struct sock *sk)
785{
786 struct aa_sk_ctx *ctx = SK_CTX(sk);
787
788 SK_CTX(sk) = NULL;
789 aa_put_label(ctx->label);
790 aa_put_label(ctx->peer);
791 kfree(ctx);
792}
793
794/**
795 * apparmor_clone_security - clone the sk_security field
796 */
797static void apparmor_sk_clone_security(const struct sock *sk,
798 struct sock *newsk)
799{
800 struct aa_sk_ctx *ctx = SK_CTX(sk);
801 struct aa_sk_ctx *new = SK_CTX(newsk);
802
803 new->label = aa_get_label(ctx->label);
804 new->peer = aa_get_label(ctx->peer);
805}
806
807/**
808 * apparmor_socket_create - check perms before creating a new socket
809 */
810static int apparmor_socket_create(int family, int type, int protocol, int kern)
811{
812 struct aa_label *label;
813 int error = 0;
814
815 AA_BUG(in_interrupt());
816
817 label = begin_current_label_crit_section();
818 if (!(kern || unconfined(label)))
819 error = af_select(family,
820 create_perm(label, family, type, protocol),
821 aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
822 family, type, protocol));
823 end_current_label_crit_section(label);
824
825 return error;
826}
827
828/**
829 * apparmor_socket_post_create - setup the per-socket security struct
830 *
831 * Note:
832 * - kernel sockets currently labeled unconfined but we may want to
833 * move to a special kernel label
834 * - socket may not have sk here if created with sock_create_lite or
835 * sock_alloc. These should be accept cases which will be handled in
836 * sock_graft.
837 */
838static int apparmor_socket_post_create(struct socket *sock, int family,
839 int type, int protocol, int kern)
840{
841 struct aa_label *label;
842
843 if (kern) {
844 struct aa_ns *ns = aa_get_current_ns();
845
846 label = aa_get_label(ns_unconfined(ns));
847 aa_put_ns(ns);
848 } else
849 label = aa_get_current_label();
850
851 if (sock->sk) {
852 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
853
854 aa_put_label(ctx->label);
855 ctx->label = aa_get_label(label);
856 }
857 aa_put_label(label);
858
859 return 0;
860}
861
862/**
863 * apparmor_socket_bind - check perms before bind addr to socket
864 */
865static int apparmor_socket_bind(struct socket *sock,
866 struct sockaddr *address, int addrlen)
867{
868 AA_BUG(!sock);
869 AA_BUG(!sock->sk);
870 AA_BUG(!address);
871 AA_BUG(in_interrupt());
872
873 return af_select(sock->sk->sk_family,
874 bind_perm(sock, address, addrlen),
875 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
876}
877
878/**
879 * apparmor_socket_connect - check perms before connecting @sock to @address
880 */
881static int apparmor_socket_connect(struct socket *sock,
882 struct sockaddr *address, int addrlen)
883{
884 AA_BUG(!sock);
885 AA_BUG(!sock->sk);
886 AA_BUG(!address);
887 AA_BUG(in_interrupt());
888
889 return af_select(sock->sk->sk_family,
890 connect_perm(sock, address, addrlen),
891 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
892}
893
894/**
895 * apparmor_socket_list - check perms before allowing listen
896 */
897static int apparmor_socket_listen(struct socket *sock, int backlog)
898{
899 AA_BUG(!sock);
900 AA_BUG(!sock->sk);
901 AA_BUG(in_interrupt());
902
903 return af_select(sock->sk->sk_family,
904 listen_perm(sock, backlog),
905 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
906}
907
908/**
909 * apparmor_socket_accept - check perms before accepting a new connection.
910 *
911 * Note: while @newsock is created and has some information, the accept
912 * has not been done.
913 */
914static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
915{
916 AA_BUG(!sock);
917 AA_BUG(!sock->sk);
918 AA_BUG(!newsock);
919 AA_BUG(in_interrupt());
920
921 return af_select(sock->sk->sk_family,
922 accept_perm(sock, newsock),
923 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
924}
925
926static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
927 struct msghdr *msg, int size)
928{
929 AA_BUG(!sock);
930 AA_BUG(!sock->sk);
931 AA_BUG(!msg);
932 AA_BUG(in_interrupt());
933
934 return af_select(sock->sk->sk_family,
935 msg_perm(op, request, sock, msg, size),
936 aa_sk_perm(op, request, sock->sk));
937}
938
939/**
940 * apparmor_socket_sendmsg - check perms before sending msg to another socket
941 */
942static int apparmor_socket_sendmsg(struct socket *sock,
943 struct msghdr *msg, int size)
944{
945 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
946}
947
948/**
949 * apparmor_socket_recvmsg - check perms before receiving a message
950 */
951static int apparmor_socket_recvmsg(struct socket *sock,
952 struct msghdr *msg, int size, int flags)
953{
954 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
955}
956
957/* revaliation, get/set attr, shutdown */
958static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
959{
960 AA_BUG(!sock);
961 AA_BUG(!sock->sk);
962 AA_BUG(in_interrupt());
963
964 return af_select(sock->sk->sk_family,
965 sock_perm(op, request, sock),
966 aa_sk_perm(op, request, sock->sk));
967}
968
969/**
970 * apparmor_socket_getsockname - check perms before getting the local address
971 */
972static int apparmor_socket_getsockname(struct socket *sock)
973{
974 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
975}
976
977/**
978 * apparmor_socket_getpeername - check perms before getting remote address
979 */
980static int apparmor_socket_getpeername(struct socket *sock)
981{
982 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
983}
984
985/* revaliation, get/set attr, opt */
986static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
987 int level, int optname)
988{
989 AA_BUG(!sock);
990 AA_BUG(!sock->sk);
991 AA_BUG(in_interrupt());
992
993 return af_select(sock->sk->sk_family,
994 opt_perm(op, request, sock, level, optname),
995 aa_sk_perm(op, request, sock->sk));
996}
997
998/**
999 * apparmor_getsockopt - check perms before getting socket options
1000 */
1001static int apparmor_socket_getsockopt(struct socket *sock, int level,
1002 int optname)
1003{
1004 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1005 level, optname);
1006}
1007
1008/**
1009 * apparmor_setsockopt - check perms before setting socket options
1010 */
1011static int apparmor_socket_setsockopt(struct socket *sock, int level,
1012 int optname)
1013{
1014 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1015 level, optname);
1016}
1017
1018/**
1019 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1020 */
1021static int apparmor_socket_shutdown(struct socket *sock, int how)
1022{
1023 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1024}
1025
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001026#ifdef CONFIG_NETWORK_SECMARK
John Johansen56974a62017-07-18 23:18:33 -07001027/**
1028 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
1029 *
1030 * Note: can not sleep may be called with locks held
1031 *
1032 * dont want protocol specific in __skb_recv_datagram()
1033 * to deny an incoming connection socket_sock_rcv_skb()
1034 */
1035static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1036{
Matthew Garrettab9f2112018-05-24 13:27:47 -07001037 struct aa_sk_ctx *ctx = SK_CTX(sk);
1038
1039 if (!skb->secmark)
1040 return 0;
1041
1042 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1043 skb->secmark, sk);
John Johansen56974a62017-07-18 23:18:33 -07001044}
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001045#endif
John Johansen56974a62017-07-18 23:18:33 -07001046
1047
1048static struct aa_label *sk_peer_label(struct sock *sk)
1049{
1050 struct aa_sk_ctx *ctx = SK_CTX(sk);
1051
1052 if (ctx->peer)
1053 return ctx->peer;
1054
1055 return ERR_PTR(-ENOPROTOOPT);
1056}
1057
1058/**
1059 * apparmor_socket_getpeersec_stream - get security context of peer
1060 *
1061 * Note: for tcp only valid if using ipsec or cipso on lan
1062 */
1063static int apparmor_socket_getpeersec_stream(struct socket *sock,
1064 char __user *optval,
1065 int __user *optlen,
1066 unsigned int len)
1067{
1068 char *name;
1069 int slen, error = 0;
1070 struct aa_label *label;
1071 struct aa_label *peer;
1072
1073 label = begin_current_label_crit_section();
1074 peer = sk_peer_label(sock->sk);
1075 if (IS_ERR(peer)) {
1076 error = PTR_ERR(peer);
1077 goto done;
1078 }
1079 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1080 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1081 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1082 /* don't include terminating \0 in slen, it breaks some apps */
1083 if (slen < 0) {
1084 error = -ENOMEM;
1085 } else {
1086 if (slen > len) {
1087 error = -ERANGE;
1088 } else if (copy_to_user(optval, name, slen)) {
1089 error = -EFAULT;
1090 goto out;
1091 }
1092 if (put_user(slen, optlen))
1093 error = -EFAULT;
1094out:
1095 kfree(name);
1096
1097 }
1098
1099done:
1100 end_current_label_crit_section(label);
1101
1102 return error;
1103}
1104
1105/**
1106 * apparmor_socket_getpeersec_dgram - get security label of packet
1107 * @sock: the peer socket
1108 * @skb: packet data
1109 * @secid: pointer to where to put the secid of the packet
1110 *
1111 * Sets the netlabel socket state on sk from parent
1112 */
1113static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1114 struct sk_buff *skb, u32 *secid)
1115
1116{
1117 /* TODO: requires secid support */
1118 return -ENOPROTOOPT;
1119}
1120
1121/**
1122 * apparmor_sock_graft - Initialize newly created socket
1123 * @sk: child sock
1124 * @parent: parent socket
1125 *
1126 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1127 * just set sk security information off of current creating process label
1128 * Labeling of sk for accept case - probably should be sock based
1129 * instead of task, because of the case where an implicitly labeled
1130 * socket is shared by different tasks.
1131 */
1132static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1133{
1134 struct aa_sk_ctx *ctx = SK_CTX(sk);
1135
1136 if (!ctx->label)
1137 ctx->label = aa_get_current_label();
1138}
1139
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001140#ifdef CONFIG_NETWORK_SECMARK
Matthew Garrettab9f2112018-05-24 13:27:47 -07001141static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb,
1142 struct request_sock *req)
1143{
1144 struct aa_sk_ctx *ctx = SK_CTX(sk);
1145
1146 if (!skb->secmark)
1147 return 0;
1148
1149 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1150 skb->secmark, sk);
1151}
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001152#endif
Matthew Garrettab9f2112018-05-24 13:27:47 -07001153
Casey Schauflerbbd36622018-11-12 09:30:56 -08001154/*
1155 * The cred blob is a pointer to, not an instance of, an aa_task_ctx.
1156 */
1157struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = {
1158 .lbs_cred = sizeof(struct aa_task_ctx *),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08001159 .lbs_file = sizeof(struct aa_file_ctx),
Casey Schauflerbbd36622018-11-12 09:30:56 -08001160};
1161
James Morrisca97d932017-02-15 00:18:51 +11001162static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07001163 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1164 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1165 LSM_HOOK_INIT(capget, apparmor_capget),
1166 LSM_HOOK_INIT(capable, apparmor_capable),
John Johansenb5e95b42010-07-29 14:48:07 -07001167
John Johansen2ea3ffb2017-07-18 23:04:47 -07001168 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1169 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1170 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1171
Casey Schauflere20b0432015-05-02 15:11:36 -07001172 LSM_HOOK_INIT(path_link, apparmor_path_link),
1173 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1174 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1175 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1176 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1177 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1178 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1179 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1180 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1181 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1182 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
John Johansenb5e95b42010-07-29 14:48:07 -07001183
Casey Schauflere20b0432015-05-02 15:11:36 -07001184 LSM_HOOK_INIT(file_open, apparmor_file_open),
John Johansen064dc942017-06-09 17:15:56 -07001185 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
Casey Schauflere20b0432015-05-02 15:11:36 -07001186 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1187 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1188 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1189 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07001190 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1191 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
John Johansenb5e95b42010-07-29 14:48:07 -07001192
Casey Schauflere20b0432015-05-02 15:11:36 -07001193 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1194 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
John Johansenb5e95b42010-07-29 14:48:07 -07001195
John Johansen56974a62017-07-18 23:18:33 -07001196 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1197 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1198 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1199
1200 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1201 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1202 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1203 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1204 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1205 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1206 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1207 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1208 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1209 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1210 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1211 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1212 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001213#ifdef CONFIG_NETWORK_SECMARK
John Johansen56974a62017-07-18 23:18:33 -07001214 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001215#endif
John Johansen56974a62017-07-18 23:18:33 -07001216 LSM_HOOK_INIT(socket_getpeersec_stream,
1217 apparmor_socket_getpeersec_stream),
1218 LSM_HOOK_INIT(socket_getpeersec_dgram,
1219 apparmor_socket_getpeersec_dgram),
1220 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001221#ifdef CONFIG_NETWORK_SECMARK
Matthew Garrettab9f2112018-05-24 13:27:47 -07001222 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001223#endif
John Johansen56974a62017-07-18 23:18:33 -07001224
Casey Schauflere20b0432015-05-02 15:11:36 -07001225 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1226 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1227 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1228 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
John Johansenb5e95b42010-07-29 14:48:07 -07001229
Casey Schauflere20b0432015-05-02 15:11:36 -07001230 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
1231 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1232 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
John Johansenb5e95b42010-07-29 14:48:07 -07001233
John Johansen3b529a72017-01-20 01:59:25 -08001234 LSM_HOOK_INIT(task_free, apparmor_task_free),
1235 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
John Johansena7ae3642017-09-11 11:29:53 -07001236 LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07001237 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
John Johansencd1dbf72017-07-18 22:56:22 -07001238 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
John Johansenc0929212017-07-31 17:36:45 -07001239
Matthew Garrette79c26d2018-04-16 11:23:58 -07001240#ifdef CONFIG_AUDIT
1241 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1242 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1243 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1244 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1245#endif
1246
John Johansenc0929212017-07-31 17:36:45 -07001247 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1248 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1249 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
John Johansenb5e95b42010-07-29 14:48:07 -07001250};
1251
1252/*
1253 * AppArmor sysfs module parameters
1254 */
1255
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001256static int param_set_aabool(const char *val, const struct kernel_param *kp);
1257static int param_get_aabool(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301258#define param_check_aabool param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301259static const struct kernel_param_ops param_ops_aabool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +09301260 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001261 .set = param_set_aabool,
1262 .get = param_get_aabool
1263};
John Johansenb5e95b42010-07-29 14:48:07 -07001264
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001265static int param_set_aauint(const char *val, const struct kernel_param *kp);
1266static int param_get_aauint(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301267#define param_check_aauint param_check_uint
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301268static const struct kernel_param_ops param_ops_aauint = {
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001269 .set = param_set_aauint,
1270 .get = param_get_aauint
1271};
John Johansenb5e95b42010-07-29 14:48:07 -07001272
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001273static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1274static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
Rusty Russellb8aa09f2011-12-15 13:41:32 +10301275#define param_check_aalockpolicy param_check_bool
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09301276static const struct kernel_param_ops param_ops_aalockpolicy = {
Jani Nikula6a4c2642014-08-27 06:21:23 +09301277 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001278 .set = param_set_aalockpolicy,
1279 .get = param_get_aalockpolicy
1280};
John Johansenb5e95b42010-07-29 14:48:07 -07001281
Kees Cooke4dca7b2017-10-17 19:04:42 -07001282static int param_set_audit(const char *val, const struct kernel_param *kp);
1283static int param_get_audit(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -07001284
Kees Cooke4dca7b2017-10-17 19:04:42 -07001285static int param_set_mode(const char *val, const struct kernel_param *kp);
1286static int param_get_mode(char *buffer, const struct kernel_param *kp);
John Johansenb5e95b42010-07-29 14:48:07 -07001287
1288/* Flag values, also controllable via /sys/module/apparmor/parameters
1289 * We define special types as we want to do additional mediation.
1290 */
1291
1292/* AppArmor global enforcement switch - complain, enforce, kill */
1293enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1294module_param_call(mode, param_set_mode, param_get_mode,
1295 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1296
John Johansen6059f712014-10-24 09:16:14 -07001297/* whether policy verification hashing is enabled */
Arnd Bergmann7616ac72016-07-25 10:59:07 -07001298bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
John Johansen3ccb76c2017-01-16 13:21:27 -08001299#ifdef CONFIG_SECURITY_APPARMOR_HASH
John Johansen6059f712014-10-24 09:16:14 -07001300module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
Arnd Bergmann7616ac72016-07-25 10:59:07 -07001301#endif
John Johansen6059f712014-10-24 09:16:14 -07001302
John Johansenb5e95b42010-07-29 14:48:07 -07001303/* Debug mode */
Valentin Rothbergeea7a052017-04-06 06:55:20 -07001304bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
John Johansenb5e95b42010-07-29 14:48:07 -07001305module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1306
1307/* Audit mode */
1308enum audit_mode aa_g_audit;
1309module_param_call(audit, param_set_audit, param_get_audit,
1310 &aa_g_audit, S_IRUSR | S_IWUSR);
1311
1312/* Determines if audit header is included in audited messages. This
1313 * provides more context if the audit daemon is not running
1314 */
Thomas Meyer954317f2017-10-07 16:02:21 +02001315bool aa_g_audit_header = true;
John Johansenb5e95b42010-07-29 14:48:07 -07001316module_param_named(audit_header, aa_g_audit_header, aabool,
1317 S_IRUSR | S_IWUSR);
1318
1319/* lock out loading/removal of policy
1320 * TODO: add in at boot loading of policy, which is the only way to
1321 * load policy, if lock_policy is set
1322 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301323bool aa_g_lock_policy;
John Johansenb5e95b42010-07-29 14:48:07 -07001324module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1325 S_IRUSR | S_IWUSR);
1326
1327/* Syscall logging mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +10301328bool aa_g_logsyscall;
John Johansenb5e95b42010-07-29 14:48:07 -07001329module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1330
1331/* Maximum pathname length before accesses will start getting rejected */
1332unsigned int aa_g_path_max = 2 * PATH_MAX;
John Johansen622f6e32017-04-06 06:55:24 -07001333module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
John Johansenb5e95b42010-07-29 14:48:07 -07001334
1335/* Determines how paranoid loading of policy is and how much verification
1336 * on the loaded policy is done.
John Johansenabbf8732017-01-16 00:42:37 -08001337 * DEPRECATED: read only as strict checking of load is always done now
1338 * that none root users (user namespaces) can load policy.
John Johansenb5e95b42010-07-29 14:48:07 -07001339 */
Thomas Meyer954317f2017-10-07 16:02:21 +02001340bool aa_g_paranoid_load = true;
John Johansenabbf8732017-01-16 00:42:37 -08001341module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
John Johansenb5e95b42010-07-29 14:48:07 -07001342
1343/* Boot time disable flag */
Kees Cook0102fb82018-10-01 17:08:57 -07001344static int apparmor_enabled __lsm_ro_after_init = 1;
Kees Cookc5459b82018-09-13 22:28:48 -07001345module_param_named(enabled, apparmor_enabled, int, 0444);
John Johansenb5e95b42010-07-29 14:48:07 -07001346
1347static int __init apparmor_enabled_setup(char *str)
1348{
1349 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +09001350 int error = kstrtoul(str, 0, &enabled);
John Johansenb5e95b42010-07-29 14:48:07 -07001351 if (!error)
1352 apparmor_enabled = enabled ? 1 : 0;
1353 return 1;
1354}
1355
1356__setup("apparmor=", apparmor_enabled_setup);
1357
1358/* set global flag turning off the ability to load policy */
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001359static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001360{
John Johansen545de8f2017-04-06 06:55:23 -07001361 if (!apparmor_enabled)
1362 return -EINVAL;
1363 if (apparmor_initialized && !policy_admin_capable(NULL))
John Johansenb5e95b42010-07-29 14:48:07 -07001364 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001365 return param_set_bool(val, kp);
1366}
1367
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001368static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001369{
John Johansenca4bd5a2017-01-16 00:43:11 -08001370 if (!apparmor_enabled)
1371 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001372 if (apparmor_initialized && !policy_view_capable(NULL))
1373 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001374 return param_get_bool(buffer, kp);
1375}
1376
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001377static int param_set_aabool(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001378{
John Johansenca4bd5a2017-01-16 00:43:11 -08001379 if (!apparmor_enabled)
1380 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001381 if (apparmor_initialized && !policy_admin_capable(NULL))
1382 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001383 return param_set_bool(val, kp);
1384}
1385
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001386static int param_get_aabool(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001387{
John Johansenca4bd5a2017-01-16 00:43:11 -08001388 if (!apparmor_enabled)
1389 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001390 if (apparmor_initialized && !policy_view_capable(NULL))
1391 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001392 return param_get_bool(buffer, kp);
1393}
1394
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001395static int param_set_aauint(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001396{
John Johansen39d84822017-03-30 05:25:23 -07001397 int error;
1398
John Johansenca4bd5a2017-01-16 00:43:11 -08001399 if (!apparmor_enabled)
1400 return -EINVAL;
John Johansen39d84822017-03-30 05:25:23 -07001401 /* file is ro but enforce 2nd line check */
1402 if (apparmor_initialized)
John Johansen545de8f2017-04-06 06:55:23 -07001403 return -EPERM;
John Johansen39d84822017-03-30 05:25:23 -07001404
1405 error = param_set_uint(val, kp);
1406 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1407
1408 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001409}
1410
Stephen Rothwell101d6c82010-08-02 12:00:43 +10001411static int param_get_aauint(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001412{
John Johansenca4bd5a2017-01-16 00:43:11 -08001413 if (!apparmor_enabled)
1414 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001415 if (apparmor_initialized && !policy_view_capable(NULL))
1416 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001417 return param_get_uint(buffer, kp);
1418}
1419
Kees Cooke4dca7b2017-10-17 19:04:42 -07001420static int param_get_audit(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001421{
John Johansenb5e95b42010-07-29 14:48:07 -07001422 if (!apparmor_enabled)
1423 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001424 if (apparmor_initialized && !policy_view_capable(NULL))
1425 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001426 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1427}
1428
Kees Cooke4dca7b2017-10-17 19:04:42 -07001429static int param_set_audit(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001430{
1431 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001432
1433 if (!apparmor_enabled)
1434 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001435 if (!val)
1436 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001437 if (apparmor_initialized && !policy_admin_capable(NULL))
1438 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001439
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001440 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1441 if (i < 0)
1442 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001443
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001444 aa_g_audit = i;
1445 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -07001446}
1447
Kees Cooke4dca7b2017-10-17 19:04:42 -07001448static int param_get_mode(char *buffer, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001449{
John Johansenb5e95b42010-07-29 14:48:07 -07001450 if (!apparmor_enabled)
1451 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001452 if (apparmor_initialized && !policy_view_capable(NULL))
1453 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001454
John Johansen0d259f02013-07-10 21:13:43 -07001455 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
John Johansenb5e95b42010-07-29 14:48:07 -07001456}
1457
Kees Cooke4dca7b2017-10-17 19:04:42 -07001458static int param_set_mode(const char *val, const struct kernel_param *kp)
John Johansenb5e95b42010-07-29 14:48:07 -07001459{
1460 int i;
John Johansenb5e95b42010-07-29 14:48:07 -07001461
1462 if (!apparmor_enabled)
1463 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001464 if (!val)
1465 return -EINVAL;
John Johansen545de8f2017-04-06 06:55:23 -07001466 if (apparmor_initialized && !policy_admin_capable(NULL))
1467 return -EPERM;
John Johansenb5e95b42010-07-29 14:48:07 -07001468
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001469 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1470 val);
1471 if (i < 0)
1472 return -EINVAL;
John Johansenb5e95b42010-07-29 14:48:07 -07001473
Andy Shevchenko5d8779a2018-05-07 16:39:03 +03001474 aa_g_profile_mode = i;
1475 return 0;
John Johansenb5e95b42010-07-29 14:48:07 -07001476}
1477
1478/*
1479 * AppArmor init functions
1480 */
1481
1482/**
John Johansen55a26eb2017-01-16 00:43:00 -08001483 * set_init_ctx - set a task context and profile on the first task.
John Johansenb5e95b42010-07-29 14:48:07 -07001484 *
1485 * TODO: allow setting an alternate profile than unconfined
1486 */
John Johansen55a26eb2017-01-16 00:43:00 -08001487static int __init set_init_ctx(void)
John Johansenb5e95b42010-07-29 14:48:07 -07001488{
1489 struct cred *cred = (struct cred *)current->real_cred;
John Johansen55a26eb2017-01-16 00:43:00 -08001490 struct aa_task_ctx *ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001491
John Johansenf1752212017-01-27 04:09:40 -08001492 ctx = aa_alloc_task_ctx(GFP_KERNEL);
John Johansen55a26eb2017-01-16 00:43:00 -08001493 if (!ctx)
John Johansenb5e95b42010-07-29 14:48:07 -07001494 return -ENOMEM;
1495
Casey Schauflerbbd36622018-11-12 09:30:56 -08001496 lsm_early_cred(cred);
Casey Schaufler69b5a442018-09-21 17:17:59 -07001497 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
John Johansenf1752212017-01-27 04:09:40 -08001498 task_ctx(current) = ctx;
John Johansenb5e95b42010-07-29 14:48:07 -07001499
1500 return 0;
1501}
1502
John Johansend4669f02017-01-16 00:43:10 -08001503static void destroy_buffers(void)
1504{
1505 u32 i, j;
1506
1507 for_each_possible_cpu(i) {
1508 for_each_cpu_buffer(j) {
1509 kfree(per_cpu(aa_buffers, i).buf[j]);
1510 per_cpu(aa_buffers, i).buf[j] = NULL;
1511 }
1512 }
1513}
1514
1515static int __init alloc_buffers(void)
1516{
1517 u32 i, j;
1518
1519 for_each_possible_cpu(i) {
1520 for_each_cpu_buffer(j) {
1521 char *buffer;
1522
1523 if (cpu_to_node(i) > num_online_nodes())
1524 /* fallback to kmalloc for offline nodes */
1525 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1526 else
1527 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1528 cpu_to_node(i));
1529 if (!buffer) {
1530 destroy_buffers();
1531 return -ENOMEM;
1532 }
1533 per_cpu(aa_buffers, i).buf[j] = buffer;
1534 }
1535 }
1536
1537 return 0;
1538}
1539
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001540#ifdef CONFIG_SYSCTL
1541static int apparmor_dointvec(struct ctl_table *table, int write,
1542 void __user *buffer, size_t *lenp, loff_t *ppos)
1543{
1544 if (!policy_admin_capable(NULL))
1545 return -EPERM;
1546 if (!apparmor_enabled)
1547 return -EINVAL;
1548
1549 return proc_dointvec(table, write, buffer, lenp, ppos);
1550}
1551
1552static struct ctl_path apparmor_sysctl_path[] = {
1553 { .procname = "kernel", },
1554 { }
1555};
1556
1557static struct ctl_table apparmor_sysctl_table[] = {
1558 {
1559 .procname = "unprivileged_userns_apparmor_policy",
1560 .data = &unprivileged_userns_apparmor_policy,
1561 .maxlen = sizeof(int),
1562 .mode = 0600,
1563 .proc_handler = apparmor_dointvec,
1564 },
1565 { }
1566};
1567
1568static int __init apparmor_init_sysctl(void)
1569{
1570 return register_sysctl_paths(apparmor_sysctl_path,
1571 apparmor_sysctl_table) ? 0 : -ENOMEM;
1572}
1573#else
1574static inline int apparmor_init_sysctl(void)
1575{
1576 return 0;
1577}
1578#endif /* CONFIG_SYSCTL */
1579
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001580#if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
Matthew Garrettab9f2112018-05-24 13:27:47 -07001581static unsigned int apparmor_ip_postroute(void *priv,
1582 struct sk_buff *skb,
1583 const struct nf_hook_state *state)
1584{
1585 struct aa_sk_ctx *ctx;
1586 struct sock *sk;
1587
1588 if (!skb->secmark)
1589 return NF_ACCEPT;
1590
1591 sk = skb_to_full_sk(skb);
1592 if (sk == NULL)
1593 return NF_ACCEPT;
1594
1595 ctx = SK_CTX(sk);
1596 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1597 skb->secmark, sk))
1598 return NF_ACCEPT;
1599
1600 return NF_DROP_ERR(-ECONNREFUSED);
1601
1602}
1603
1604static unsigned int apparmor_ipv4_postroute(void *priv,
1605 struct sk_buff *skb,
1606 const struct nf_hook_state *state)
1607{
1608 return apparmor_ip_postroute(priv, skb, state);
1609}
1610
1611static unsigned int apparmor_ipv6_postroute(void *priv,
1612 struct sk_buff *skb,
1613 const struct nf_hook_state *state)
1614{
1615 return apparmor_ip_postroute(priv, skb, state);
1616}
1617
1618static const struct nf_hook_ops apparmor_nf_ops[] = {
1619 {
1620 .hook = apparmor_ipv4_postroute,
1621 .pf = NFPROTO_IPV4,
1622 .hooknum = NF_INET_POST_ROUTING,
1623 .priority = NF_IP_PRI_SELINUX_FIRST,
1624 },
1625#if IS_ENABLED(CONFIG_IPV6)
1626 {
1627 .hook = apparmor_ipv6_postroute,
1628 .pf = NFPROTO_IPV6,
1629 .hooknum = NF_INET_POST_ROUTING,
1630 .priority = NF_IP6_PRI_SELINUX_FIRST,
1631 },
1632#endif
1633};
1634
1635static int __net_init apparmor_nf_register(struct net *net)
1636{
1637 int ret;
1638
1639 ret = nf_register_net_hooks(net, apparmor_nf_ops,
1640 ARRAY_SIZE(apparmor_nf_ops));
1641 return ret;
1642}
1643
1644static void __net_exit apparmor_nf_unregister(struct net *net)
1645{
1646 nf_unregister_net_hooks(net, apparmor_nf_ops,
1647 ARRAY_SIZE(apparmor_nf_ops));
1648}
1649
1650static struct pernet_operations apparmor_net_ops = {
1651 .init = apparmor_nf_register,
1652 .exit = apparmor_nf_unregister,
1653};
1654
1655static int __init apparmor_nf_ip_init(void)
1656{
1657 int err;
1658
1659 if (!apparmor_enabled)
1660 return 0;
1661
1662 err = register_pernet_subsys(&apparmor_net_ops);
1663 if (err)
1664 panic("Apparmor: register_pernet_subsys: error %d\n", err);
1665
1666 return 0;
1667}
1668__initcall(apparmor_nf_ip_init);
Arnd Bergmanne1af4772018-10-05 18:11:47 +02001669#endif
Matthew Garrettab9f2112018-05-24 13:27:47 -07001670
John Johansenb5e95b42010-07-29 14:48:07 -07001671static int __init apparmor_init(void)
1672{
1673 int error;
1674
John Johansena4c3f892018-06-04 19:44:59 -07001675 aa_secids_init();
1676
John Johansen11c236b2017-01-16 00:42:42 -08001677 error = aa_setup_dfa_engine();
1678 if (error) {
1679 AA_ERROR("Unable to setup dfa engine\n");
1680 goto alloc_out;
1681 }
1682
John Johansenb5e95b42010-07-29 14:48:07 -07001683 error = aa_alloc_root_ns();
1684 if (error) {
1685 AA_ERROR("Unable to allocate default profile namespace\n");
1686 goto alloc_out;
1687 }
1688
Tyler Hickse3ea1ca2016-03-16 19:19:10 -05001689 error = apparmor_init_sysctl();
1690 if (error) {
1691 AA_ERROR("Unable to register sysctls\n");
1692 goto alloc_out;
1693
1694 }
1695
John Johansend4669f02017-01-16 00:43:10 -08001696 error = alloc_buffers();
1697 if (error) {
1698 AA_ERROR("Unable to allocate work buffers\n");
1699 goto buffers_out;
1700 }
1701
John Johansen55a26eb2017-01-16 00:43:00 -08001702 error = set_init_ctx();
John Johansenb5e95b42010-07-29 14:48:07 -07001703 if (error) {
1704 AA_ERROR("Failed to set context on init task\n");
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001705 aa_free_root_ns();
John Johansend4669f02017-01-16 00:43:10 -08001706 goto buffers_out;
John Johansenb5e95b42010-07-29 14:48:07 -07001707 }
Casey Schauflerd69dece52017-01-18 17:09:05 -08001708 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1709 "apparmor");
John Johansenb5e95b42010-07-29 14:48:07 -07001710
1711 /* Report that AppArmor successfully initialized */
1712 apparmor_initialized = 1;
1713 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1714 aa_info_message("AppArmor initialized: complain mode enabled");
1715 else if (aa_g_profile_mode == APPARMOR_KILL)
1716 aa_info_message("AppArmor initialized: kill mode enabled");
1717 else
1718 aa_info_message("AppArmor initialized");
1719
1720 return error;
1721
John Johansend4669f02017-01-16 00:43:10 -08001722buffers_out:
1723 destroy_buffers();
1724
John Johansenb5e95b42010-07-29 14:48:07 -07001725alloc_out:
1726 aa_destroy_aafs();
John Johansen11c236b2017-01-16 00:42:42 -08001727 aa_teardown_dfa_engine();
John Johansenb5e95b42010-07-29 14:48:07 -07001728
Thomas Meyer954317f2017-10-07 16:02:21 +02001729 apparmor_enabled = false;
John Johansenb5e95b42010-07-29 14:48:07 -07001730 return error;
John Johansenb5e95b42010-07-29 14:48:07 -07001731}
1732
Kees Cook3d6e5f62018-10-10 17:18:23 -07001733DEFINE_LSM(apparmor) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07001734 .name = "apparmor",
Kees Cook14bd99c2018-09-19 19:57:06 -07001735 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07001736 .enabled = &apparmor_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08001737 .blobs = &apparmor_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07001738 .init = apparmor_init,
1739};