1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor policy loading interface function definitions.
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  */
10 
11 #ifndef __POLICY_INTERFACE_H
12 #define __POLICY_INTERFACE_H
13 
14 #include <linux/list.h>
15 #include <linux/kref.h>
16 #include <linux/dcache.h>
17 #include <linux/workqueue.h>
18 
19 struct aa_load_ent {
20 	struct list_head list;
21 	struct aa_profile *new;
22 	struct aa_profile *old;
23 	struct aa_profile *rename;
24 	const char *ns_name;
25 };
26 
27 void aa_load_ent_free(struct aa_load_ent *ent);
28 struct aa_load_ent *aa_load_ent_alloc(void);
29 
30 #define PACKED_FLAG_HAT		1
31 #define PACKED_FLAG_DEBUG1	2
32 #define PACKED_FLAG_DEBUG2	4
33 
34 #define PACKED_MODE_ENFORCE	0
35 #define PACKED_MODE_COMPLAIN	1
36 #define PACKED_MODE_KILL	2
37 #define PACKED_MODE_UNCONFINED	3
38 
39 struct aa_ns;
40 
41 enum {
42 	AAFS_LOADDATA_ABI = 0,
43 	AAFS_LOADDATA_REVISION,
44 	AAFS_LOADDATA_HASH,
45 	AAFS_LOADDATA_DATA,
46 	AAFS_LOADDATA_COMPRESSED_SIZE,
47 	AAFS_LOADDATA_DIR,		/* must be last actual entry */
48 	AAFS_LOADDATA_NDENTS		/* count of entries */
49 };
50 
51 /*
52  * struct aa_loaddata - buffer of policy raw_data set
53  *
54  * there is no loaddata ref for being on ns list, nor a ref from
55  * d_inode(@dentry) when grab a ref from these, @ns->lock must be held
56  * && __aa_get_loaddata() needs to be used, and the return value
57  * checked, if NULL the loaddata is already being reaped and should be
58  * considered dead.
59  */
60 struct aa_loaddata {
61 	struct kref count;
62 	struct list_head list;
63 	struct work_struct work;
64 	struct dentry *dents[AAFS_LOADDATA_NDENTS];
65 	struct aa_ns *ns;
66 	char *name;
67 	size_t size;			/* the original size of the payload */
68 	size_t compressed_size;		/* the compressed size of the payload */
69 	long revision;			/* the ns policy revision this caused */
70 	int abi;
71 	unsigned char *hash;
72 
73 	/* Pointer to payload. If @compressed_size > 0, then this is the
74 	 * compressed version of the payload, else it is the uncompressed
75 	 * version (with the size indicated by @size).
76 	 */
77 	char *data;
78 };
79 
80 int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, const char **ns);
81 
82 /**
83  * __aa_get_loaddata - get a reference count to uncounted data reference
84  * @data: reference to get a count on
85  *
86  * Returns: pointer to reference OR NULL if race is lost and reference is
87  *          being repeated.
88  * Requires: @data->ns->lock held, and the return code MUST be checked
89  *
90  * Use only from inode->i_private and @data->list found references
91  */
92 static inline struct aa_loaddata *
__aa_get_loaddata(struct aa_loaddata * data)93 __aa_get_loaddata(struct aa_loaddata *data)
94 {
95 	if (data && kref_get_unless_zero(&(data->count)))
96 		return data;
97 
98 	return NULL;
99 }
100 
101 /**
102  * aa_get_loaddata - get a reference count from a counted data reference
103  * @data: reference to get a count on
104  *
105  * Returns: point to reference
106  * Requires: @data to have a valid reference count on it. It is a bug
107  *           if the race to reap can be encountered when it is used.
108  */
109 static inline struct aa_loaddata *
aa_get_loaddata(struct aa_loaddata * data)110 aa_get_loaddata(struct aa_loaddata *data)
111 {
112 	struct aa_loaddata *tmp = __aa_get_loaddata(data);
113 
114 	AA_BUG(data && !tmp);
115 
116 	return tmp;
117 }
118 
119 void __aa_loaddata_update(struct aa_loaddata *data, long revision);
120 bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r);
121 void aa_loaddata_kref(struct kref *kref);
122 struct aa_loaddata *aa_loaddata_alloc(size_t size);
aa_put_loaddata(struct aa_loaddata * data)123 static inline void aa_put_loaddata(struct aa_loaddata *data)
124 {
125 	if (data)
126 		kref_put(&data->count, aa_loaddata_kref);
127 }
128 
129 #endif /* __POLICY_INTERFACE_H */
130