xref: /OK3568_Linux_fs/kernel/security/apparmor/include/file.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * AppArmor security module
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file contains AppArmor file mediation function definitions.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 1998-2008 Novell/SUSE
8*4882a593Smuzhiyun  * Copyright 2009-2010 Canonical Ltd.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #ifndef __AA_FILE_H
12*4882a593Smuzhiyun #define __AA_FILE_H
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/spinlock.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "domain.h"
17*4882a593Smuzhiyun #include "match.h"
18*4882a593Smuzhiyun #include "perms.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct aa_profile;
21*4882a593Smuzhiyun struct path;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define AA_AUDIT_FILE_MASK	(MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
26*4882a593Smuzhiyun 				 AA_MAY_CREATE | AA_MAY_DELETE |	\
27*4882a593Smuzhiyun 				 AA_MAY_GETATTR | AA_MAY_SETATTR | \
28*4882a593Smuzhiyun 				 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
29*4882a593Smuzhiyun 				 AA_EXEC_MMAP | AA_MAY_LINK)
30*4882a593Smuzhiyun 
file_ctx(struct file * file)31*4882a593Smuzhiyun static inline struct aa_file_ctx *file_ctx(struct file *file)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	return file->f_security + apparmor_blob_sizes.lbs_file;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* struct aa_file_ctx - the AppArmor context the file was opened in
37*4882a593Smuzhiyun  * @lock: lock to update the ctx
38*4882a593Smuzhiyun  * @label: label currently cached on the ctx
39*4882a593Smuzhiyun  * @perms: the permission the file was opened with
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun struct aa_file_ctx {
42*4882a593Smuzhiyun 	spinlock_t lock;
43*4882a593Smuzhiyun 	struct aa_label __rcu *label;
44*4882a593Smuzhiyun 	u32 allow;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun  * aa_alloc_file_ctx - allocate file_ctx
49*4882a593Smuzhiyun  * @label: initial label of task creating the file
50*4882a593Smuzhiyun  * @gfp: gfp flags for allocation
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * Returns: file_ctx or NULL on failure
53*4882a593Smuzhiyun  */
aa_alloc_file_ctx(struct aa_label * label,gfp_t gfp)54*4882a593Smuzhiyun static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
55*4882a593Smuzhiyun 						    gfp_t gfp)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	struct aa_file_ctx *ctx;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
60*4882a593Smuzhiyun 	if (ctx) {
61*4882a593Smuzhiyun 		spin_lock_init(&ctx->lock);
62*4882a593Smuzhiyun 		rcu_assign_pointer(ctx->label, aa_get_label(label));
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 	return ctx;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun  * aa_free_file_ctx - free a file_ctx
69*4882a593Smuzhiyun  * @ctx: file_ctx to free  (MAYBE_NULL)
70*4882a593Smuzhiyun  */
aa_free_file_ctx(struct aa_file_ctx * ctx)71*4882a593Smuzhiyun static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	if (ctx) {
74*4882a593Smuzhiyun 		aa_put_label(rcu_access_pointer(ctx->label));
75*4882a593Smuzhiyun 		kfree_sensitive(ctx);
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
aa_get_file_label(struct aa_file_ctx * ctx)79*4882a593Smuzhiyun static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	return aa_get_label_rcu(&ctx->label);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * The xindex is broken into 3 parts
86*4882a593Smuzhiyun  * - index - an index into either the exec name table or the variable table
87*4882a593Smuzhiyun  * - exec type - which determines how the executable name and index are used
88*4882a593Smuzhiyun  * - flags - which modify how the destination name is applied
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun #define AA_X_INDEX_MASK		0x03ff
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #define AA_X_TYPE_MASK		0x0c00
93*4882a593Smuzhiyun #define AA_X_TYPE_SHIFT		10
94*4882a593Smuzhiyun #define AA_X_NONE		0x0000
95*4882a593Smuzhiyun #define AA_X_NAME		0x0400	/* use executable name px */
96*4882a593Smuzhiyun #define AA_X_TABLE		0x0800	/* use a specified name ->n# */
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun #define AA_X_UNSAFE		0x1000
99*4882a593Smuzhiyun #define AA_X_CHILD		0x2000	/* make >AA_X_NONE apply to children */
100*4882a593Smuzhiyun #define AA_X_INHERIT		0x4000
101*4882a593Smuzhiyun #define AA_X_UNCONFINED		0x8000
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* need to make conditional which ones are being set */
104*4882a593Smuzhiyun struct path_cond {
105*4882a593Smuzhiyun 	kuid_t uid;
106*4882a593Smuzhiyun 	umode_t mode;
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* FIXME: split perms from dfa and match this to description
112*4882a593Smuzhiyun  *        also add delegation info.
113*4882a593Smuzhiyun  */
dfa_map_xindex(u16 mask)114*4882a593Smuzhiyun static inline u16 dfa_map_xindex(u16 mask)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	u16 old_index = (mask >> 10) & 0xf;
117*4882a593Smuzhiyun 	u16 index = 0;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (mask & 0x100)
120*4882a593Smuzhiyun 		index |= AA_X_UNSAFE;
121*4882a593Smuzhiyun 	if (mask & 0x200)
122*4882a593Smuzhiyun 		index |= AA_X_INHERIT;
123*4882a593Smuzhiyun 	if (mask & 0x80)
124*4882a593Smuzhiyun 		index |= AA_X_UNCONFINED;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	if (old_index == 1) {
127*4882a593Smuzhiyun 		index |= AA_X_UNCONFINED;
128*4882a593Smuzhiyun 	} else if (old_index == 2) {
129*4882a593Smuzhiyun 		index |= AA_X_NAME;
130*4882a593Smuzhiyun 	} else if (old_index == 3) {
131*4882a593Smuzhiyun 		index |= AA_X_NAME | AA_X_CHILD;
132*4882a593Smuzhiyun 	} else if (old_index) {
133*4882a593Smuzhiyun 		index |= AA_X_TABLE;
134*4882a593Smuzhiyun 		index |= old_index - 4;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	return index;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun  * map old dfa inline permissions to new format
142*4882a593Smuzhiyun  */
143*4882a593Smuzhiyun #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
144*4882a593Smuzhiyun 				    ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
145*4882a593Smuzhiyun #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
146*4882a593Smuzhiyun #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
147*4882a593Smuzhiyun #define dfa_user_xindex(dfa, state) \
148*4882a593Smuzhiyun 	(dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
151*4882a593Smuzhiyun 				      0x7f) |				\
152*4882a593Smuzhiyun 				     ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
153*4882a593Smuzhiyun #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
154*4882a593Smuzhiyun #define dfa_other_quiet(dfa, state) \
155*4882a593Smuzhiyun 	((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
156*4882a593Smuzhiyun #define dfa_other_xindex(dfa, state) \
157*4882a593Smuzhiyun 	dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
160*4882a593Smuzhiyun 		  const char *op, u32 request, const char *name,
161*4882a593Smuzhiyun 		  const char *target, struct aa_label *tlabel, kuid_t ouid,
162*4882a593Smuzhiyun 		  const char *info, int error);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun  * struct aa_file_rules - components used for file rule permissions
166*4882a593Smuzhiyun  * @dfa: dfa to match path names and conditionals against
167*4882a593Smuzhiyun  * @perms: permission table indexed by the matched state accept entry of @dfa
168*4882a593Smuzhiyun  * @trans: transition table for indexed by named x transitions
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * File permission are determined by matching a path against @dfa and then
171*4882a593Smuzhiyun  * then using the value of the accept entry for the matching state as
172*4882a593Smuzhiyun  * an index into @perms.  If a named exec transition is required it is
173*4882a593Smuzhiyun  * looked up in the transition table.
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun struct aa_file_rules {
176*4882a593Smuzhiyun 	unsigned int start;
177*4882a593Smuzhiyun 	struct aa_dfa *dfa;
178*4882a593Smuzhiyun 	/* struct perms perms; */
179*4882a593Smuzhiyun 	struct aa_domain trans;
180*4882a593Smuzhiyun 	/* TODO: add delegate table */
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
184*4882a593Smuzhiyun 				    struct path_cond *cond);
185*4882a593Smuzhiyun unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
186*4882a593Smuzhiyun 			  const char *name, struct path_cond *cond,
187*4882a593Smuzhiyun 			  struct aa_perms *perms);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun int __aa_path_perm(const char *op, struct aa_profile *profile,
190*4882a593Smuzhiyun 		   const char *name, u32 request, struct path_cond *cond,
191*4882a593Smuzhiyun 		   int flags, struct aa_perms *perms);
192*4882a593Smuzhiyun int aa_path_perm(const char *op, struct aa_label *label,
193*4882a593Smuzhiyun 		 const struct path *path, int flags, u32 request,
194*4882a593Smuzhiyun 		 struct path_cond *cond);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
197*4882a593Smuzhiyun 		 const struct path *new_dir, struct dentry *new_dentry);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
200*4882a593Smuzhiyun 		 u32 request, bool in_atomic);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun void aa_inherit_files(const struct cred *cred, struct files_struct *files);
203*4882a593Smuzhiyun 
aa_free_file_rules(struct aa_file_rules * rules)204*4882a593Smuzhiyun static inline void aa_free_file_rules(struct aa_file_rules *rules)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	aa_put_dfa(rules->dfa);
207*4882a593Smuzhiyun 	aa_free_domain_entries(&rules->trans);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /**
211*4882a593Smuzhiyun  * aa_map_file_perms - map file flags to AppArmor permissions
212*4882a593Smuzhiyun  * @file: open file to map flags to AppArmor permissions
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * Returns: apparmor permission set for the file
215*4882a593Smuzhiyun  */
aa_map_file_to_perms(struct file * file)216*4882a593Smuzhiyun static inline u32 aa_map_file_to_perms(struct file *file)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	int flags = file->f_flags;
219*4882a593Smuzhiyun 	u32 perms = 0;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (file->f_mode & FMODE_WRITE)
222*4882a593Smuzhiyun 		perms |= MAY_WRITE;
223*4882a593Smuzhiyun 	if (file->f_mode & FMODE_READ)
224*4882a593Smuzhiyun 		perms |= MAY_READ;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if ((flags & O_APPEND) && (perms & MAY_WRITE))
227*4882a593Smuzhiyun 		perms = (perms & ~MAY_WRITE) | MAY_APPEND;
228*4882a593Smuzhiyun 	/* trunc implies write permission */
229*4882a593Smuzhiyun 	if (flags & O_TRUNC)
230*4882a593Smuzhiyun 		perms |= MAY_WRITE;
231*4882a593Smuzhiyun 	if (flags & O_CREAT)
232*4882a593Smuzhiyun 		perms |= AA_MAY_CREATE;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return perms;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun #endif /* __AA_FILE_H */
238