blob: 550a700563b43ecb6546509bd976998e3e2ac578 [file] [log] [blame]
John Johansen12557dc2017-01-16 00:42:13 -08001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor lib definitions
5 *
6 * 2017 Canonical Ltd.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
11 * License.
12 */
13
14#ifndef __AA_LIB_H
15#define __AA_LIB_H
16
17#include <linux/slab.h>
18#include <linux/fs.h>
19
20#include "match.h"
21
John Johansen57e36bb2017-01-16 00:42:26 -080022/* Provide our own test for whether a write lock is held for asserts
23 * this is because on none SMP systems write_can_lock will always
24 * resolve to true, which is what you want for code making decisions
25 * based on it, but wrong for asserts checking that the lock is held
26 */
27#ifdef CONFIG_SMP
28#define write_is_locked(X) !write_can_lock(X)
29#else
30#define write_is_locked(X) (1)
31#endif /* CONFIG_SMP */
32
John Johansen12557dc2017-01-16 00:42:13 -080033/*
34 * DEBUG remains global (no per profile flag) since it is mostly used in sysctl
35 * which is not related to profile accesses.
36 */
37
John Johansen680cd622017-01-16 00:42:27 -080038#define DEBUG_ON (aa_g_debug)
39#define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args)
John Johansen12557dc2017-01-16 00:42:13 -080040#define AA_DEBUG(fmt, args...) \
41 do { \
John Johansen680cd622017-01-16 00:42:27 -080042 if (DEBUG_ON) \
John Johansen12557dc2017-01-16 00:42:13 -080043 pr_debug_ratelimited("AppArmor: " fmt, ##args); \
44 } while (0)
45
John Johansen680cd622017-01-16 00:42:27 -080046#define AA_WARN(X) WARN((X), "APPARMOR WARN %s: %s\n", __func__, #X)
47
48#define AA_BUG(X, args...) AA_BUG_FMT((X), "" args)
49#ifdef CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS
50#define AA_BUG_FMT(X, fmt, args...) \
51 WARN((X), "AppArmor WARN %s: (" #X "): " fmt, __func__, ##args)
52#else
53#define AA_BUG_FMT(X, fmt, args...)
54#endif
55
John Johansen12557dc2017-01-16 00:42:13 -080056#define AA_ERROR(fmt, args...) \
57 pr_err_ratelimited("AppArmor: " fmt, ##args)
58
59/* Flag indicating whether initialization completed */
John Johansen545de8f2017-04-06 06:55:23 -070060extern int apparmor_initialized;
John Johansen12557dc2017-01-16 00:42:13 -080061
62/* fn's in lib */
63char *aa_split_fqname(char *args, char **ns_name);
John Johansen3b0aaf52017-01-16 00:42:23 -080064const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name,
65 size_t *ns_len);
John Johansen12557dc2017-01-16 00:42:13 -080066void aa_info_message(const char *str);
John Johansen12557dc2017-01-16 00:42:13 -080067
John Johansen12557dc2017-01-16 00:42:13 -080068/**
69 * aa_strneq - compare null terminated @str to a non null terminated substring
70 * @str: a null terminated string
71 * @sub: a substring, not necessarily null terminated
72 * @len: length of @sub to compare
73 *
74 * The @str string must be full consumed for this to be considered a match
75 */
76static inline bool aa_strneq(const char *str, const char *sub, int len)
77{
78 return !strncmp(str, sub, len) && !str[len];
79}
80
81/**
82 * aa_dfa_null_transition - step to next state after null character
83 * @dfa: the dfa to match against
84 * @start: the state of the dfa to start matching in
85 *
86 * aa_dfa_null_transition transitions to the next state after a null
87 * character which is not used in standard matching and is only
88 * used to separate pairs.
89 */
90static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa,
91 unsigned int start)
92{
93 /* the null transition only needs the string's null terminator byte */
94 return aa_dfa_next(dfa, start, 0);
95}
96
John Johansenefeee832017-01-16 00:42:28 -080097static inline bool path_mediated_fs(struct dentry *dentry)
John Johansen12557dc2017-01-16 00:42:13 -080098{
99 return !(dentry->d_sb->s_flags & MS_NOUSER);
100}
101
John Johansenfe6bb312017-01-16 00:42:14 -0800102/* struct aa_policy - common part of both namespaces and profiles
103 * @name: name of the object
104 * @hname - The hierarchical name
105 * @list: list policy object is on
106 * @profiles: head of the profiles list contained in the object
107 */
108struct aa_policy {
John Johansenbbe4a7c2017-01-16 00:42:30 -0800109 const char *name;
110 const char *hname;
John Johansenfe6bb312017-01-16 00:42:14 -0800111 struct list_head list;
112 struct list_head profiles;
113};
114
115/**
John Johansen6e474e32017-01-16 00:42:29 -0800116 * basename - find the last component of an hname
John Johansenfe6bb312017-01-16 00:42:14 -0800117 * @name: hname to find the base profile name component of (NOT NULL)
118 *
119 * Returns: the tail (base profile name) name component of an hname
120 */
John Johansen6e474e32017-01-16 00:42:29 -0800121static inline const char *basename(const char *hname)
John Johansenfe6bb312017-01-16 00:42:14 -0800122{
123 char *split;
124
125 hname = strim((char *)hname);
126 for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
127 hname = split + 2;
128
129 return hname;
130}
131
132/**
133 * __policy_find - find a policy by @name on a policy list
134 * @head: list to search (NOT NULL)
135 * @name: name to search for (NOT NULL)
136 *
137 * Requires: rcu_read_lock be held
138 *
139 * Returns: unrefcounted policy that match @name or NULL if not found
140 */
141static inline struct aa_policy *__policy_find(struct list_head *head,
142 const char *name)
143{
144 struct aa_policy *policy;
145
146 list_for_each_entry_rcu(policy, head, list) {
147 if (!strcmp(policy->name, name))
148 return policy;
149 }
150 return NULL;
151}
152
153/**
154 * __policy_strn_find - find a policy that's name matches @len chars of @str
155 * @head: list to search (NOT NULL)
156 * @str: string to search for (NOT NULL)
157 * @len: length of match required
158 *
159 * Requires: rcu_read_lock be held
160 *
161 * Returns: unrefcounted policy that match @str or NULL if not found
162 *
163 * if @len == strlen(@strlen) then this is equiv to __policy_find
164 * other wise it allows searching for policy by a partial match of name
165 */
166static inline struct aa_policy *__policy_strn_find(struct list_head *head,
167 const char *str, int len)
168{
169 struct aa_policy *policy;
170
171 list_for_each_entry_rcu(policy, head, list) {
172 if (aa_strneq(policy->name, str, len))
173 return policy;
174 }
175
176 return NULL;
177}
178
179bool aa_policy_init(struct aa_policy *policy, const char *prefix,
John Johansend102d892017-01-16 00:42:31 -0800180 const char *name, gfp_t gfp);
John Johansenfe6bb312017-01-16 00:42:14 -0800181void aa_policy_destroy(struct aa_policy *policy);
182
John Johansen12557dc2017-01-16 00:42:13 -0800183#endif /* AA_LIB_H */