xref: /openbmc/linux/security/apparmor/include/file.h (revision 453431a5)
1b886d83cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
26380bd8dSJohn Johansen /*
36380bd8dSJohn Johansen  * AppArmor security module
46380bd8dSJohn Johansen  *
56380bd8dSJohn Johansen  * This file contains AppArmor file mediation function definitions.
66380bd8dSJohn Johansen  *
76380bd8dSJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
86380bd8dSJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
96380bd8dSJohn Johansen  */
106380bd8dSJohn Johansen 
116380bd8dSJohn Johansen #ifndef __AA_FILE_H
126380bd8dSJohn Johansen #define __AA_FILE_H
136380bd8dSJohn Johansen 
14190a9518SJohn Johansen #include <linux/spinlock.h>
15190a9518SJohn Johansen 
166380bd8dSJohn Johansen #include "domain.h"
176380bd8dSJohn Johansen #include "match.h"
18fc7e0b26SJohn Johansen #include "perms.h"
196380bd8dSJohn Johansen 
206380bd8dSJohn Johansen struct aa_profile;
2137721e1bSAlexey Dobriyan struct path;
226380bd8dSJohn Johansen 
23e53cfe6cSJohn Johansen #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
246380bd8dSJohn Johansen 
256380bd8dSJohn Johansen #define AA_AUDIT_FILE_MASK	(MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
266380bd8dSJohn Johansen 				 AA_MAY_CREATE | AA_MAY_DELETE |	\
27e53cfe6cSJohn Johansen 				 AA_MAY_GETATTR | AA_MAY_SETATTR | \
286380bd8dSJohn Johansen 				 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
296380bd8dSJohn Johansen 				 AA_EXEC_MMAP | AA_MAY_LINK)
306380bd8dSJohn Johansen 
3133bf60caSCasey Schaufler static inline struct aa_file_ctx *file_ctx(struct file *file)
3233bf60caSCasey Schaufler {
3333bf60caSCasey Schaufler 	return file->f_security + apparmor_blob_sizes.lbs_file;
3433bf60caSCasey Schaufler }
352835a13bSJohn Johansen 
36af7caa8fSJohn Johansen /* struct aa_file_ctx - the AppArmor context the file was opened in
37190a9518SJohn Johansen  * @lock: lock to update the ctx
38190a9518SJohn Johansen  * @label: label currently cached on the ctx
39af7caa8fSJohn Johansen  * @perms: the permission the file was opened with
40af7caa8fSJohn Johansen  */
41af7caa8fSJohn Johansen struct aa_file_ctx {
42190a9518SJohn Johansen 	spinlock_t lock;
43190a9518SJohn Johansen 	struct aa_label __rcu *label;
44e53cfe6cSJohn Johansen 	u32 allow;
45af7caa8fSJohn Johansen };
46af7caa8fSJohn Johansen 
47af7caa8fSJohn Johansen /**
482835a13bSJohn Johansen  * aa_alloc_file_ctx - allocate file_ctx
492835a13bSJohn Johansen  * @label: initial label of task creating the file
50af7caa8fSJohn Johansen  * @gfp: gfp flags for allocation
51af7caa8fSJohn Johansen  *
52af7caa8fSJohn Johansen  * Returns: file_ctx or NULL on failure
53af7caa8fSJohn Johansen  */
54190a9518SJohn Johansen static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
55190a9518SJohn Johansen 						    gfp_t gfp)
56af7caa8fSJohn Johansen {
572835a13bSJohn Johansen 	struct aa_file_ctx *ctx;
582835a13bSJohn Johansen 
592835a13bSJohn Johansen 	ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
60190a9518SJohn Johansen 	if (ctx) {
61190a9518SJohn Johansen 		spin_lock_init(&ctx->lock);
62190a9518SJohn Johansen 		rcu_assign_pointer(ctx->label, aa_get_label(label));
63190a9518SJohn Johansen 	}
642835a13bSJohn Johansen 	return ctx;
65af7caa8fSJohn Johansen }
66af7caa8fSJohn Johansen 
67af7caa8fSJohn Johansen /**
682835a13bSJohn Johansen  * aa_free_file_ctx - free a file_ctx
69af7caa8fSJohn Johansen  * @ctx: file_ctx to free  (MAYBE_NULL)
70af7caa8fSJohn Johansen  */
712835a13bSJohn Johansen static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
72af7caa8fSJohn Johansen {
73190a9518SJohn Johansen 	if (ctx) {
74190a9518SJohn Johansen 		aa_put_label(rcu_access_pointer(ctx->label));
75453431a5SWaiman Long 		kfree_sensitive(ctx);
76af7caa8fSJohn Johansen 	}
77190a9518SJohn Johansen }
78190a9518SJohn Johansen 
79190a9518SJohn Johansen static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
80190a9518SJohn Johansen {
81190a9518SJohn Johansen 	return aa_get_label_rcu(&ctx->label);
82190a9518SJohn Johansen }
83af7caa8fSJohn Johansen 
846380bd8dSJohn Johansen /*
856380bd8dSJohn Johansen  * The xindex is broken into 3 parts
866380bd8dSJohn Johansen  * - index - an index into either the exec name table or the variable table
876380bd8dSJohn Johansen  * - exec type - which determines how the executable name and index are used
886380bd8dSJohn Johansen  * - flags - which modify how the destination name is applied
896380bd8dSJohn Johansen  */
906380bd8dSJohn Johansen #define AA_X_INDEX_MASK		0x03ff
916380bd8dSJohn Johansen 
926380bd8dSJohn Johansen #define AA_X_TYPE_MASK		0x0c00
936380bd8dSJohn Johansen #define AA_X_TYPE_SHIFT		10
946380bd8dSJohn Johansen #define AA_X_NONE		0x0000
956380bd8dSJohn Johansen #define AA_X_NAME		0x0400	/* use executable name px */
966380bd8dSJohn Johansen #define AA_X_TABLE		0x0800	/* use a specified name ->n# */
976380bd8dSJohn Johansen 
986380bd8dSJohn Johansen #define AA_X_UNSAFE		0x1000
996380bd8dSJohn Johansen #define AA_X_CHILD		0x2000	/* make >AA_X_NONE apply to children */
1006380bd8dSJohn Johansen #define AA_X_INHERIT		0x4000
1016380bd8dSJohn Johansen #define AA_X_UNCONFINED		0x8000
1026380bd8dSJohn Johansen 
1036380bd8dSJohn Johansen /* need to make conditional which ones are being set */
1046380bd8dSJohn Johansen struct path_cond {
1052db81452SEric W. Biederman 	kuid_t uid;
1066380bd8dSJohn Johansen 	umode_t mode;
1076380bd8dSJohn Johansen };
1086380bd8dSJohn Johansen 
1096380bd8dSJohn Johansen #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
1106380bd8dSJohn Johansen 
1116380bd8dSJohn Johansen /* FIXME: split perms from dfa and match this to description
1126380bd8dSJohn Johansen  *        also add delegation info.
1136380bd8dSJohn Johansen  */
1146380bd8dSJohn Johansen static inline u16 dfa_map_xindex(u16 mask)
1156380bd8dSJohn Johansen {
1166380bd8dSJohn Johansen 	u16 old_index = (mask >> 10) & 0xf;
1176380bd8dSJohn Johansen 	u16 index = 0;
1186380bd8dSJohn Johansen 
1196380bd8dSJohn Johansen 	if (mask & 0x100)
1206380bd8dSJohn Johansen 		index |= AA_X_UNSAFE;
1216380bd8dSJohn Johansen 	if (mask & 0x200)
1226380bd8dSJohn Johansen 		index |= AA_X_INHERIT;
1236380bd8dSJohn Johansen 	if (mask & 0x80)
1246380bd8dSJohn Johansen 		index |= AA_X_UNCONFINED;
1256380bd8dSJohn Johansen 
1266380bd8dSJohn Johansen 	if (old_index == 1) {
1276380bd8dSJohn Johansen 		index |= AA_X_UNCONFINED;
1286380bd8dSJohn Johansen 	} else if (old_index == 2) {
1296380bd8dSJohn Johansen 		index |= AA_X_NAME;
1306380bd8dSJohn Johansen 	} else if (old_index == 3) {
1316380bd8dSJohn Johansen 		index |= AA_X_NAME | AA_X_CHILD;
1328b964eaeSJohn Johansen 	} else if (old_index) {
1336380bd8dSJohn Johansen 		index |= AA_X_TABLE;
1346380bd8dSJohn Johansen 		index |= old_index - 4;
1356380bd8dSJohn Johansen 	}
1366380bd8dSJohn Johansen 
1376380bd8dSJohn Johansen 	return index;
1386380bd8dSJohn Johansen }
1396380bd8dSJohn Johansen 
1406380bd8dSJohn Johansen /*
1416380bd8dSJohn Johansen  * map old dfa inline permissions to new format
1426380bd8dSJohn Johansen  */
1436380bd8dSJohn Johansen #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
1446380bd8dSJohn Johansen 				    ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
1456380bd8dSJohn Johansen #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
1466380bd8dSJohn Johansen #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
1476380bd8dSJohn Johansen #define dfa_user_xindex(dfa, state) \
1486380bd8dSJohn Johansen 	(dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
1496380bd8dSJohn Johansen 
1506380bd8dSJohn Johansen #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
1516380bd8dSJohn Johansen 				      0x7f) |				\
1526380bd8dSJohn Johansen 				     ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
1536380bd8dSJohn Johansen #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
1546380bd8dSJohn Johansen #define dfa_other_quiet(dfa, state) \
1556380bd8dSJohn Johansen 	((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
1566380bd8dSJohn Johansen #define dfa_other_xindex(dfa, state) \
1576380bd8dSJohn Johansen 	dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
1586380bd8dSJohn Johansen 
1592d679f3cSJohn Johansen int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
160ef88a7acSJohn Johansen 		  const char *op, u32 request, const char *name,
16198c3d182SJohn Johansen 		  const char *target, struct aa_label *tlabel, kuid_t ouid,
16298c3d182SJohn Johansen 		  const char *info, int error);
1636380bd8dSJohn Johansen 
1646380bd8dSJohn Johansen /**
1656380bd8dSJohn Johansen  * struct aa_file_rules - components used for file rule permissions
1666380bd8dSJohn Johansen  * @dfa: dfa to match path names and conditionals against
1676380bd8dSJohn Johansen  * @perms: permission table indexed by the matched state accept entry of @dfa
1686380bd8dSJohn Johansen  * @trans: transition table for indexed by named x transitions
1696380bd8dSJohn Johansen  *
1706380bd8dSJohn Johansen  * File permission are determined by matching a path against @dfa and then
1716380bd8dSJohn Johansen  * then using the value of the accept entry for the matching state as
1726380bd8dSJohn Johansen  * an index into @perms.  If a named exec transition is required it is
1736380bd8dSJohn Johansen  * looked up in the transition table.
1746380bd8dSJohn Johansen  */
1756380bd8dSJohn Johansen struct aa_file_rules {
1766380bd8dSJohn Johansen 	unsigned int start;
1776380bd8dSJohn Johansen 	struct aa_dfa *dfa;
1786380bd8dSJohn Johansen 	/* struct perms perms; */
1796380bd8dSJohn Johansen 	struct aa_domain trans;
1806380bd8dSJohn Johansen 	/* TODO: add delegate table */
1816380bd8dSJohn Johansen };
1826380bd8dSJohn Johansen 
1832d679f3cSJohn Johansen struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
1842d679f3cSJohn Johansen 				    struct path_cond *cond);
1856380bd8dSJohn Johansen unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
1866380bd8dSJohn Johansen 			  const char *name, struct path_cond *cond,
1872d679f3cSJohn Johansen 			  struct aa_perms *perms);
1886380bd8dSJohn Johansen 
189aebd873eSJohn Johansen int __aa_path_perm(const char *op, struct aa_profile *profile,
190aebd873eSJohn Johansen 		   const char *name, u32 request, struct path_cond *cond,
191aebd873eSJohn Johansen 		   int flags, struct aa_perms *perms);
192aebd873eSJohn Johansen int aa_path_perm(const char *op, struct aa_label *label,
19347f6e5ccSJohn Johansen 		 const struct path *path, int flags, u32 request,
19447f6e5ccSJohn Johansen 		 struct path_cond *cond);
1956380bd8dSJohn Johansen 
1968014370fSJohn Johansen int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
1973539aaf6SAl Viro 		 const struct path *new_dir, struct dentry *new_dentry);
1986380bd8dSJohn Johansen 
199190a9518SJohn Johansen int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
200341c1fdaSJohn Johansen 		 u32 request, bool in_atomic);
2016380bd8dSJohn Johansen 
202192ca6b5SJohn Johansen void aa_inherit_files(const struct cred *cred, struct files_struct *files);
203192ca6b5SJohn Johansen 
2046380bd8dSJohn Johansen static inline void aa_free_file_rules(struct aa_file_rules *rules)
2056380bd8dSJohn Johansen {
2066380bd8dSJohn Johansen 	aa_put_dfa(rules->dfa);
2076380bd8dSJohn Johansen 	aa_free_domain_entries(&rules->trans);
2086380bd8dSJohn Johansen }
2096380bd8dSJohn Johansen 
2106380bd8dSJohn Johansen /**
2116380bd8dSJohn Johansen  * aa_map_file_perms - map file flags to AppArmor permissions
2126380bd8dSJohn Johansen  * @file: open file to map flags to AppArmor permissions
2136380bd8dSJohn Johansen  *
2146380bd8dSJohn Johansen  * Returns: apparmor permission set for the file
2156380bd8dSJohn Johansen  */
2166380bd8dSJohn Johansen static inline u32 aa_map_file_to_perms(struct file *file)
2176380bd8dSJohn Johansen {
21853fe8b99SJohn Johansen 	int flags = file->f_flags;
21953fe8b99SJohn Johansen 	u32 perms = 0;
22053fe8b99SJohn Johansen 
22153fe8b99SJohn Johansen 	if (file->f_mode & FMODE_WRITE)
22253fe8b99SJohn Johansen 		perms |= MAY_WRITE;
22353fe8b99SJohn Johansen 	if (file->f_mode & FMODE_READ)
22453fe8b99SJohn Johansen 		perms |= MAY_READ;
2256380bd8dSJohn Johansen 
2266380bd8dSJohn Johansen 	if ((flags & O_APPEND) && (perms & MAY_WRITE))
2276380bd8dSJohn Johansen 		perms = (perms & ~MAY_WRITE) | MAY_APPEND;
2286380bd8dSJohn Johansen 	/* trunc implies write permission */
2296380bd8dSJohn Johansen 	if (flags & O_TRUNC)
2306380bd8dSJohn Johansen 		perms |= MAY_WRITE;
2316380bd8dSJohn Johansen 	if (flags & O_CREAT)
2326380bd8dSJohn Johansen 		perms |= AA_MAY_CREATE;
2336380bd8dSJohn Johansen 
2346380bd8dSJohn Johansen 	return perms;
2356380bd8dSJohn Johansen }
2366380bd8dSJohn Johansen 
2376380bd8dSJohn Johansen #endif /* __AA_FILE_H */
238