xref: /openbmc/linux/security/apparmor/include/file.h (revision 5a46e490)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor file mediation function definitions.
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  */
10 
11 #ifndef __AA_FILE_H
12 #define __AA_FILE_H
13 
14 #include <linux/spinlock.h>
15 
16 #include "domain.h"
17 #include "match.h"
18 #include "perms.h"
19 
20 struct aa_policydb;
21 struct aa_profile;
22 struct path;
23 
24 #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
25 
26 #define AA_AUDIT_FILE_MASK	(MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
27 				 AA_MAY_CREATE | AA_MAY_DELETE |	\
28 				 AA_MAY_GETATTR | AA_MAY_SETATTR | \
29 				 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
30 				 AA_EXEC_MMAP | AA_MAY_LINK)
31 
32 static inline struct aa_file_ctx *file_ctx(struct file *file)
33 {
34 	return file->f_security + apparmor_blob_sizes.lbs_file;
35 }
36 
37 /* struct aa_file_ctx - the AppArmor context the file was opened in
38  * @lock: lock to update the ctx
39  * @label: label currently cached on the ctx
40  * @perms: the permission the file was opened with
41  */
42 struct aa_file_ctx {
43 	spinlock_t lock;
44 	struct aa_label __rcu *label;
45 	u32 allow;
46 };
47 
48 /**
49  * aa_alloc_file_ctx - allocate file_ctx
50  * @label: initial label of task creating the file
51  * @gfp: gfp flags for allocation
52  *
53  * Returns: file_ctx or NULL on failure
54  */
55 static 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  * aa_free_file_ctx - free a file_ctx
70  * @ctx: file_ctx to free  (MAYBE_NULL)
71  */
72 static 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 		kfree_sensitive(ctx);
77 	}
78 }
79 
80 static 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  * 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		AA_INDEX_MASK
92 
93 #define AA_X_TYPE_MASK		0x0c000000
94 #define AA_X_NONE		AA_INDEX_NONE
95 #define AA_X_NAME		0x04000000 /* use executable name px */
96 #define AA_X_TABLE		0x08000000 /* use a specified name ->n# */
97 
98 #define AA_X_UNSAFE		0x10000000
99 #define AA_X_CHILD		0x20000000
100 #define AA_X_INHERIT		0x40000000
101 #define AA_X_UNCONFINED		0x80000000
102 
103 /* need to make conditional which ones are being set */
104 struct path_cond {
105 	kuid_t uid;
106 	umode_t mode;
107 };
108 
109 #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
110 
111 int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
112 		  const char *op, u32 request, const char *name,
113 		  const char *target, struct aa_label *tlabel, kuid_t ouid,
114 		  const char *info, int error);
115 
116 struct aa_perms *aa_lookup_fperms(struct aa_policydb *file_rules,
117 				  aa_state_t state, struct path_cond *cond);
118 aa_state_t aa_str_perms(struct aa_policydb *file_rules, aa_state_t start,
119 			const char *name, struct path_cond *cond,
120 			struct aa_perms *perms);
121 
122 int aa_path_perm(const char *op, struct aa_label *label,
123 		 const struct path *path, int flags, u32 request,
124 		 struct path_cond *cond);
125 
126 int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
127 		 const struct path *new_dir, struct dentry *new_dentry);
128 
129 int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
130 		 u32 request, bool in_atomic);
131 
132 void aa_inherit_files(const struct cred *cred, struct files_struct *files);
133 
134 
135 /**
136  * aa_map_file_perms - map file flags to AppArmor permissions
137  * @file: open file to map flags to AppArmor permissions
138  *
139  * Returns: apparmor permission set for the file
140  */
141 static inline u32 aa_map_file_to_perms(struct file *file)
142 {
143 	int flags = file->f_flags;
144 	u32 perms = 0;
145 
146 	if (file->f_mode & FMODE_WRITE)
147 		perms |= MAY_WRITE;
148 	if (file->f_mode & FMODE_READ)
149 		perms |= MAY_READ;
150 
151 	if ((flags & O_APPEND) && (perms & MAY_WRITE))
152 		perms = (perms & ~MAY_WRITE) | MAY_APPEND;
153 	/* trunc implies write permission */
154 	if (flags & O_TRUNC)
155 		perms |= MAY_WRITE;
156 	if (flags & O_CREAT)
157 		perms |= AA_MAY_CREATE;
158 
159 	return perms;
160 }
161 
162 #endif /* __AA_FILE_H */
163