1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef __AA_FILE_H
16#define __AA_FILE_H
17
18#include <linux/spinlock.h>
19
20#include "domain.h"
21#include "match.h"
22#include "perms.h"
23
24struct aa_profile;
25struct path;
26
27#define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
28
29#define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
30 AA_MAY_CREATE | AA_MAY_DELETE | \
31 AA_MAY_GETATTR | AA_MAY_SETATTR | \
32 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
33 AA_EXEC_MMAP | AA_MAY_LINK)
34
35#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security)
36
37
38
39
40
41
42struct aa_file_ctx {
43 spinlock_t lock;
44 struct aa_label __rcu *label;
45 u32 allow;
46};
47
48
49
50
51
52
53
54
55static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
56 gfp_t gfp)
57{
58 struct aa_file_ctx *ctx;
59
60 ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
61 if (ctx) {
62 spin_lock_init(&ctx->lock);
63 rcu_assign_pointer(ctx->label, aa_get_label(label));
64 }
65 return ctx;
66}
67
68
69
70
71
72static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
73{
74 if (ctx) {
75 aa_put_label(rcu_access_pointer(ctx->label));
76 kzfree(ctx);
77 }
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);
83}
84
85
86
87
88
89
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
97#define AA_X_TABLE 0x0800
98
99#define AA_X_UNSAFE 0x1000
100#define AA_X_CHILD 0x2000
101#define AA_X_INHERIT 0x4000
102#define AA_X_UNCONFINED 0x8000
103
104
105struct path_cond {
106 kuid_t uid;
107 umode_t mode;
108};
109
110#define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
111
112
113
114
115static inline u16 dfa_map_xindex(u16 mask)
116{
117 u16 old_index = (mask >> 10) & 0xf;
118 u16 index = 0;
119
120 if (mask & 0x100)
121 index |= AA_X_UNSAFE;
122 if (mask & 0x200)
123 index |= AA_X_INHERIT;
124 if (mask & 0x80)
125 index |= AA_X_UNCONFINED;
126
127 if (old_index == 1) {
128 index |= AA_X_UNCONFINED;
129 } else if (old_index == 2) {
130 index |= AA_X_NAME;
131 } else if (old_index == 3) {
132 index |= AA_X_NAME | AA_X_CHILD;
133 } else if (old_index) {
134 index |= AA_X_TABLE;
135 index |= old_index - 4;
136 }
137
138 return index;
139}
140
141
142
143
144#define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
145 ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
146#define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
147#define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
148#define dfa_user_xindex(dfa, state) \
149 (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
150
151#define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
152 0x7f) | \
153 ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
154#define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
155#define dfa_other_quiet(dfa, state) \
156 ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
157#define dfa_other_xindex(dfa, state) \
158 dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
159
160int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
161 const char *op, u32 request, const char *name,
162 const char *target, struct aa_label *tlabel, kuid_t ouid,
163 const char *info, int error);
164
165
166
167
168
169
170
171
172
173
174
175
176struct aa_file_rules {
177 unsigned int start;
178 struct aa_dfa *dfa;
179
180 struct aa_domain trans;
181
182};
183
184struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
185 struct path_cond *cond);
186unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
187 const char *name, struct path_cond *cond,
188 struct aa_perms *perms);
189
190int __aa_path_perm(const char *op, struct aa_profile *profile,
191 const char *name, u32 request, struct path_cond *cond,
192 int flags, struct aa_perms *perms);
193int aa_path_perm(const char *op, struct aa_label *label,
194 const struct path *path, int flags, u32 request,
195 struct path_cond *cond);
196
197int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
198 const struct path *new_dir, struct dentry *new_dentry);
199
200int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
201 u32 request);
202
203void aa_inherit_files(const struct cred *cred, struct files_struct *files);
204
205static 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
211
212
213
214
215
216
217static inline u32 aa_map_file_to_perms(struct file *file)
218{
219 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;
226
227 if ((flags & O_APPEND) && (perms & MAY_WRITE))
228 perms = (perms & ~MAY_WRITE) | MAY_APPEND;
229
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
239