John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 1 | /* |
| 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 Johansen | 57e36bb | 2017-01-16 00:42:26 -0800 | [diff] [blame] | 22 | /* 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 Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 33 | /* |
| 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 Johansen | 680cd62 | 2017-01-16 00:42:27 -0800 | [diff] [blame] | 38 | #define DEBUG_ON (aa_g_debug) |
| 39 | #define dbg_printk(__fmt, __args...) pr_debug(__fmt, ##__args) |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 40 | #define AA_DEBUG(fmt, args...) \ |
| 41 | do { \ |
John Johansen | 680cd62 | 2017-01-16 00:42:27 -0800 | [diff] [blame] | 42 | if (DEBUG_ON) \ |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 43 | pr_debug_ratelimited("AppArmor: " fmt, ##args); \ |
| 44 | } while (0) |
| 45 | |
John Johansen | 680cd62 | 2017-01-16 00:42:27 -0800 | [diff] [blame] | 46 | #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 Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 56 | #define AA_ERROR(fmt, args...) \ |
| 57 | pr_err_ratelimited("AppArmor: " fmt, ##args) |
| 58 | |
| 59 | /* Flag indicating whether initialization completed */ |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 60 | extern int apparmor_initialized; |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 61 | |
| 62 | /* fn's in lib */ |
John Johansen | b91deb9 | 2017-05-22 02:47:22 -0700 | [diff] [blame^] | 63 | const char *skipn_spaces(const char *str, size_t n); |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 64 | char *aa_split_fqname(char *args, char **ns_name); |
John Johansen | 3b0aaf5 | 2017-01-16 00:42:23 -0800 | [diff] [blame] | 65 | const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, |
| 66 | size_t *ns_len); |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 67 | void aa_info_message(const char *str); |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 68 | |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 69 | /** |
| 70 | * aa_strneq - compare null terminated @str to a non null terminated substring |
| 71 | * @str: a null terminated string |
| 72 | * @sub: a substring, not necessarily null terminated |
| 73 | * @len: length of @sub to compare |
| 74 | * |
| 75 | * The @str string must be full consumed for this to be considered a match |
| 76 | */ |
| 77 | static inline bool aa_strneq(const char *str, const char *sub, int len) |
| 78 | { |
| 79 | return !strncmp(str, sub, len) && !str[len]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * aa_dfa_null_transition - step to next state after null character |
| 84 | * @dfa: the dfa to match against |
| 85 | * @start: the state of the dfa to start matching in |
| 86 | * |
| 87 | * aa_dfa_null_transition transitions to the next state after a null |
| 88 | * character which is not used in standard matching and is only |
| 89 | * used to separate pairs. |
| 90 | */ |
| 91 | static inline unsigned int aa_dfa_null_transition(struct aa_dfa *dfa, |
| 92 | unsigned int start) |
| 93 | { |
| 94 | /* the null transition only needs the string's null terminator byte */ |
| 95 | return aa_dfa_next(dfa, start, 0); |
| 96 | } |
| 97 | |
John Johansen | efeee83 | 2017-01-16 00:42:28 -0800 | [diff] [blame] | 98 | static inline bool path_mediated_fs(struct dentry *dentry) |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 99 | { |
| 100 | return !(dentry->d_sb->s_flags & MS_NOUSER); |
| 101 | } |
| 102 | |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 103 | /* struct aa_policy - common part of both namespaces and profiles |
| 104 | * @name: name of the object |
| 105 | * @hname - The hierarchical name |
| 106 | * @list: list policy object is on |
| 107 | * @profiles: head of the profiles list contained in the object |
| 108 | */ |
| 109 | struct aa_policy { |
John Johansen | bbe4a7c | 2017-01-16 00:42:30 -0800 | [diff] [blame] | 110 | const char *name; |
| 111 | const char *hname; |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 112 | struct list_head list; |
| 113 | struct list_head profiles; |
| 114 | }; |
| 115 | |
| 116 | /** |
John Johansen | 6e474e3 | 2017-01-16 00:42:29 -0800 | [diff] [blame] | 117 | * basename - find the last component of an hname |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 118 | * @name: hname to find the base profile name component of (NOT NULL) |
| 119 | * |
| 120 | * Returns: the tail (base profile name) name component of an hname |
| 121 | */ |
John Johansen | 6e474e3 | 2017-01-16 00:42:29 -0800 | [diff] [blame] | 122 | static inline const char *basename(const char *hname) |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 123 | { |
| 124 | char *split; |
| 125 | |
| 126 | hname = strim((char *)hname); |
| 127 | for (split = strstr(hname, "//"); split; split = strstr(hname, "//")) |
| 128 | hname = split + 2; |
| 129 | |
| 130 | return hname; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * __policy_find - find a policy by @name on a policy list |
| 135 | * @head: list to search (NOT NULL) |
| 136 | * @name: name to search for (NOT NULL) |
| 137 | * |
| 138 | * Requires: rcu_read_lock be held |
| 139 | * |
| 140 | * Returns: unrefcounted policy that match @name or NULL if not found |
| 141 | */ |
| 142 | static inline struct aa_policy *__policy_find(struct list_head *head, |
| 143 | const char *name) |
| 144 | { |
| 145 | struct aa_policy *policy; |
| 146 | |
| 147 | list_for_each_entry_rcu(policy, head, list) { |
| 148 | if (!strcmp(policy->name, name)) |
| 149 | return policy; |
| 150 | } |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * __policy_strn_find - find a policy that's name matches @len chars of @str |
| 156 | * @head: list to search (NOT NULL) |
| 157 | * @str: string to search for (NOT NULL) |
| 158 | * @len: length of match required |
| 159 | * |
| 160 | * Requires: rcu_read_lock be held |
| 161 | * |
| 162 | * Returns: unrefcounted policy that match @str or NULL if not found |
| 163 | * |
| 164 | * if @len == strlen(@strlen) then this is equiv to __policy_find |
| 165 | * other wise it allows searching for policy by a partial match of name |
| 166 | */ |
| 167 | static inline struct aa_policy *__policy_strn_find(struct list_head *head, |
| 168 | const char *str, int len) |
| 169 | { |
| 170 | struct aa_policy *policy; |
| 171 | |
| 172 | list_for_each_entry_rcu(policy, head, list) { |
| 173 | if (aa_strneq(policy->name, str, len)) |
| 174 | return policy; |
| 175 | } |
| 176 | |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | bool aa_policy_init(struct aa_policy *policy, const char *prefix, |
John Johansen | d102d89 | 2017-01-16 00:42:31 -0800 | [diff] [blame] | 181 | const char *name, gfp_t gfp); |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 182 | void aa_policy_destroy(struct aa_policy *policy); |
| 183 | |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 184 | #endif /* AA_LIB_H */ |