blob: 7c6026460272f931a899a9a22f796815023c8792 [file] [log] [blame]
John Johansen6380bd82010-07-29 14:48:04 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor file mediation function definitions.
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
15#ifndef __AA_FILE_H
16#define __AA_FILE_H
17
John Johansen190a9512017-06-09 14:59:51 -070018#include <linux/spinlock.h>
19
John Johansen6380bd82010-07-29 14:48:04 -070020#include "domain.h"
21#include "match.h"
John Johansenfc7e0b22017-05-26 01:57:09 -070022#include "perms.h"
John Johansen6380bd82010-07-29 14:48:04 -070023
24struct aa_profile;
Alexey Dobriyan37721e12011-01-10 08:17:10 +020025struct path;
John Johansen6380bd82010-07-29 14:48:04 -070026
John Johansene53cfe62017-05-26 15:07:22 -070027#define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
John Johansen6380bd82010-07-29 14:48:04 -070028
29#define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
30 AA_MAY_CREATE | AA_MAY_DELETE | \
John Johansene53cfe62017-05-26 15:07:22 -070031 AA_MAY_GETATTR | AA_MAY_SETATTR | \
John Johansen6380bd82010-07-29 14:48:04 -070032 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
33 AA_EXEC_MMAP | AA_MAY_LINK)
34
John Johansen2835a132017-06-09 11:43:45 -070035#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security)
36
John Johansenaf7caa82017-05-21 17:15:28 -070037/* struct aa_file_ctx - the AppArmor context the file was opened in
John Johansen190a9512017-06-09 14:59:51 -070038 * @lock: lock to update the ctx
39 * @label: label currently cached on the ctx
John Johansenaf7caa82017-05-21 17:15:28 -070040 * @perms: the permission the file was opened with
John Johansenaf7caa82017-05-21 17:15:28 -070041 */
42struct aa_file_ctx {
John Johansen190a9512017-06-09 14:59:51 -070043 spinlock_t lock;
44 struct aa_label __rcu *label;
John Johansene53cfe62017-05-26 15:07:22 -070045 u32 allow;
John Johansenaf7caa82017-05-21 17:15:28 -070046};
47
48/**
John Johansen2835a132017-06-09 11:43:45 -070049 * aa_alloc_file_ctx - allocate file_ctx
50 * @label: initial label of task creating the file
John Johansenaf7caa82017-05-21 17:15:28 -070051 * @gfp: gfp flags for allocation
52 *
53 * Returns: file_ctx or NULL on failure
54 */
John Johansen190a9512017-06-09 14:59:51 -070055static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
56 gfp_t gfp)
John Johansenaf7caa82017-05-21 17:15:28 -070057{
John Johansen2835a132017-06-09 11:43:45 -070058 struct aa_file_ctx *ctx;
59
60 ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
John Johansen190a9512017-06-09 14:59:51 -070061 if (ctx) {
62 spin_lock_init(&ctx->lock);
63 rcu_assign_pointer(ctx->label, aa_get_label(label));
64 }
John Johansen2835a132017-06-09 11:43:45 -070065 return ctx;
John Johansenaf7caa82017-05-21 17:15:28 -070066}
67
68/**
John Johansen2835a132017-06-09 11:43:45 -070069 * aa_free_file_ctx - free a file_ctx
John Johansenaf7caa82017-05-21 17:15:28 -070070 * @ctx: file_ctx to free (MAYBE_NULL)
71 */
John Johansen2835a132017-06-09 11:43:45 -070072static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
John Johansenaf7caa82017-05-21 17:15:28 -070073{
John Johansen190a9512017-06-09 14:59:51 -070074 if (ctx) {
75 aa_put_label(rcu_access_pointer(ctx->label));
John Johansenaf7caa82017-05-21 17:15:28 -070076 kzfree(ctx);
John Johansen190a9512017-06-09 14:59:51 -070077 }
78}
79
80static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
81{
82 return aa_get_label_rcu(&ctx->label);
John Johansenaf7caa82017-05-21 17:15:28 -070083}
84
John Johansen6380bd82010-07-29 14:48:04 -070085/*
86 * The xindex is broken into 3 parts
87 * - index - an index into either the exec name table or the variable table
88 * - exec type - which determines how the executable name and index are used
89 * - flags - which modify how the destination name is applied
90 */
91#define AA_X_INDEX_MASK 0x03ff
92
93#define AA_X_TYPE_MASK 0x0c00
94#define AA_X_TYPE_SHIFT 10
95#define AA_X_NONE 0x0000
96#define AA_X_NAME 0x0400 /* use executable name px */
97#define AA_X_TABLE 0x0800 /* use a specified name ->n# */
98
99#define AA_X_UNSAFE 0x1000
100#define AA_X_CHILD 0x2000 /* make >AA_X_NONE apply to children */
101#define AA_X_INHERIT 0x4000
102#define AA_X_UNCONFINED 0x8000
103
104/* AA_SECURE_X_NEEDED - is passed in the bprm->unsafe field */
105#define AA_SECURE_X_NEEDED 0x8000
106
107/* need to make conditional which ones are being set */
108struct path_cond {
Eric W. Biederman2db81452012-02-07 16:33:13 -0800109 kuid_t uid;
John Johansen6380bd82010-07-29 14:48:04 -0700110 umode_t mode;
111};
112
John Johansen6380bd82010-07-29 14:48:04 -0700113#define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
114
115/* FIXME: split perms from dfa and match this to description
116 * also add delegation info.
117 */
118static inline u16 dfa_map_xindex(u16 mask)
119{
120 u16 old_index = (mask >> 10) & 0xf;
121 u16 index = 0;
122
123 if (mask & 0x100)
124 index |= AA_X_UNSAFE;
125 if (mask & 0x200)
126 index |= AA_X_INHERIT;
127 if (mask & 0x80)
128 index |= AA_X_UNCONFINED;
129
130 if (old_index == 1) {
131 index |= AA_X_UNCONFINED;
132 } else if (old_index == 2) {
133 index |= AA_X_NAME;
134 } else if (old_index == 3) {
135 index |= AA_X_NAME | AA_X_CHILD;
John Johansen8b964ea2012-02-22 00:32:30 -0800136 } else if (old_index) {
John Johansen6380bd82010-07-29 14:48:04 -0700137 index |= AA_X_TABLE;
138 index |= old_index - 4;
139 }
140
141 return index;
142}
143
144/*
145 * map old dfa inline permissions to new format
146 */
147#define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
148 ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
149#define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
150#define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
151#define dfa_user_xindex(dfa, state) \
152 (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
153
154#define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
155 0x7f) | \
156 ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
157#define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
158#define dfa_other_quiet(dfa, state) \
159 ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
160#define dfa_other_xindex(dfa, state) \
161 dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
162
John Johansen2d679f32017-05-29 12:19:39 -0700163int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
John Johansenef88a7a2017-01-16 00:43:02 -0800164 const char *op, u32 request, const char *name,
John Johansen98c3d182017-06-09 15:48:20 -0700165 const char *target, struct aa_label *tlabel, kuid_t ouid,
166 const char *info, int error);
John Johansen6380bd82010-07-29 14:48:04 -0700167
168/**
169 * struct aa_file_rules - components used for file rule permissions
170 * @dfa: dfa to match path names and conditionals against
171 * @perms: permission table indexed by the matched state accept entry of @dfa
172 * @trans: transition table for indexed by named x transitions
173 *
174 * File permission are determined by matching a path against @dfa and then
175 * then using the value of the accept entry for the matching state as
176 * an index into @perms. If a named exec transition is required it is
177 * looked up in the transition table.
178 */
179struct aa_file_rules {
180 unsigned int start;
181 struct aa_dfa *dfa;
182 /* struct perms perms; */
183 struct aa_domain trans;
184 /* TODO: add delegate table */
185};
186
John Johansen2d679f32017-05-29 12:19:39 -0700187struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
188 struct path_cond *cond);
John Johansen6380bd82010-07-29 14:48:04 -0700189unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
190 const char *name, struct path_cond *cond,
John Johansen2d679f32017-05-29 12:19:39 -0700191 struct aa_perms *perms);
John Johansen6380bd82010-07-29 14:48:04 -0700192
John Johansen47f6e5c2017-01-16 00:43:01 -0800193int aa_path_perm(const char *op, struct aa_profile *profile,
194 const struct path *path, int flags, u32 request,
195 struct path_cond *cond);
John Johansen6380bd82010-07-29 14:48:04 -0700196
197int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
Al Viro3539aaf2016-03-25 15:07:03 -0400198 const struct path *new_dir, struct dentry *new_dentry);
John Johansen6380bd82010-07-29 14:48:04 -0700199
John Johansen190a9512017-06-09 14:59:51 -0700200int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
John Johansen6380bd82010-07-29 14:48:04 -0700201 u32 request);
202
John Johansen192ca6b2017-06-09 11:58:42 -0700203void aa_inherit_files(const struct cred *cred, struct files_struct *files);
204
John Johansen6380bd82010-07-29 14:48:04 -0700205static inline void aa_free_file_rules(struct aa_file_rules *rules)
206{
207 aa_put_dfa(rules->dfa);
208 aa_free_domain_entries(&rules->trans);
209}
210
John Johansen6380bd82010-07-29 14:48:04 -0700211/**
212 * aa_map_file_perms - map file flags to AppArmor permissions
213 * @file: open file to map flags to AppArmor permissions
214 *
215 * Returns: apparmor permission set for the file
216 */
217static inline u32 aa_map_file_to_perms(struct file *file)
218{
John Johansen53fe8b92013-02-21 13:25:44 -0800219 int flags = file->f_flags;
220 u32 perms = 0;
221
222 if (file->f_mode & FMODE_WRITE)
223 perms |= MAY_WRITE;
224 if (file->f_mode & FMODE_READ)
225 perms |= MAY_READ;
John Johansen6380bd82010-07-29 14:48:04 -0700226
227 if ((flags & O_APPEND) && (perms & MAY_WRITE))
228 perms = (perms & ~MAY_WRITE) | MAY_APPEND;
229 /* trunc implies write permission */
230 if (flags & O_TRUNC)
231 perms |= MAY_WRITE;
232 if (flags & O_CREAT)
233 perms |= AA_MAY_CREATE;
234
235 return perms;
236}
237
238#endif /* __AA_FILE_H */