cfc8b0887776f17617592d37f72f0faacaa9648b
[platform/kernel/linux-rpi.git] / security / integrity / ima / ima.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4  *
5  * Authors:
6  * Reiner Sailer <sailer@watson.ibm.com>
7  * Mimi Zohar <zohar@us.ibm.com>
8  *
9  * File: ima.h
10  *      internal Integrity Measurement Architecture (IMA) definitions
11  */
12
13 #ifndef __LINUX_IMA_H
14 #define __LINUX_IMA_H
15
16 #include <linux/types.h>
17 #include <linux/crypto.h>
18 #include <linux/fs.h>
19 #include <linux/security.h>
20 #include <linux/hash.h>
21 #include <linux/tpm.h>
22 #include <linux/audit.h>
23 #include <crypto/hash_info.h>
24
25 #include "../integrity.h"
26
27 #ifdef CONFIG_HAVE_IMA_KEXEC
28 #include <asm/ima.h>
29 #endif
30
31 enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN,
32                      IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
33 enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };
34
35 /* digest size for IMA, fits SHA1 or MD5 */
36 #define IMA_DIGEST_SIZE         SHA1_DIGEST_SIZE
37 #define IMA_EVENT_NAME_LEN_MAX  255
38
39 #define IMA_HASH_BITS 9
40 #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
41
42 #define IMA_TEMPLATE_FIELD_ID_MAX_LEN   16
43 #define IMA_TEMPLATE_NUM_FIELDS_MAX     15
44
45 #define IMA_TEMPLATE_IMA_NAME "ima"
46 #define IMA_TEMPLATE_IMA_FMT "d|n"
47
48 /* current content of the policy */
49 extern int ima_policy_flag;
50
51 /* set during initialization */
52 extern int ima_hash_algo;
53 extern int ima_appraise;
54 extern struct tpm_chip *ima_tpm_chip;
55
56 /* IMA event related data */
57 struct ima_event_data {
58         struct integrity_iint_cache *iint;
59         struct file *file;
60         const unsigned char *filename;
61         struct evm_ima_xattr_data *xattr_value;
62         int xattr_len;
63         const struct modsig *modsig;
64         const char *violation;
65         const void *buf;
66         int buf_len;
67 };
68
69 /* IMA template field data definition */
70 struct ima_field_data {
71         u8 *data;
72         u32 len;
73 };
74
75 /* IMA template field definition */
76 struct ima_template_field {
77         const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
78         int (*field_init)(struct ima_event_data *event_data,
79                           struct ima_field_data *field_data);
80         void (*field_show)(struct seq_file *m, enum ima_show_type show,
81                            struct ima_field_data *field_data);
82 };
83
84 /* IMA template descriptor definition */
85 struct ima_template_desc {
86         struct list_head list;
87         char *name;
88         char *fmt;
89         int num_fields;
90         const struct ima_template_field **fields;
91 };
92
93 struct ima_template_entry {
94         int pcr;
95         u8 digest[TPM_DIGEST_SIZE];     /* sha1 or md5 measurement hash */
96         struct ima_template_desc *template_desc; /* template descriptor */
97         u32 template_data_len;
98         struct ima_field_data template_data[0]; /* template related data */
99 };
100
101 struct ima_queue_entry {
102         struct hlist_node hnext;        /* place in hash collision list */
103         struct list_head later;         /* place in ima_measurements list */
104         struct ima_template_entry *entry;
105 };
106 extern struct list_head ima_measurements;       /* list of all measurements */
107
108 /* Some details preceding the binary serialized measurement list */
109 struct ima_kexec_hdr {
110         u16 version;
111         u16 _reserved0;
112         u32 _reserved1;
113         u64 buffer_size;
114         u64 count;
115 };
116
117 #ifdef CONFIG_HAVE_IMA_KEXEC
118 void ima_load_kexec_buffer(void);
119 #else
120 static inline void ima_load_kexec_buffer(void) {}
121 #endif /* CONFIG_HAVE_IMA_KEXEC */
122
123 /*
124  * The default binary_runtime_measurements list format is defined as the
125  * platform native format.  The canonical format is defined as little-endian.
126  */
127 extern bool ima_canonical_fmt;
128
129 /* Internal IMA function definitions */
130 int ima_init(void);
131 int ima_fs_init(void);
132 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
133                            const char *op, struct inode *inode,
134                            const unsigned char *filename);
135 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
136 int ima_calc_buffer_hash(const void *buf, loff_t len,
137                          struct ima_digest_data *hash);
138 int ima_calc_field_array_hash(struct ima_field_data *field_data,
139                               struct ima_template_desc *desc, int num_fields,
140                               struct ima_digest_data *hash);
141 int __init ima_calc_boot_aggregate(struct ima_digest_data *hash);
142 void ima_add_violation(struct file *file, const unsigned char *filename,
143                        struct integrity_iint_cache *iint,
144                        const char *op, const char *cause);
145 int ima_init_crypto(void);
146 void ima_putc(struct seq_file *m, void *data, int datalen);
147 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
148 int template_desc_init_fields(const char *template_fmt,
149                               const struct ima_template_field ***fields,
150                               int *num_fields);
151 struct ima_template_desc *ima_template_desc_current(void);
152 struct ima_template_desc *lookup_template_desc(const char *name);
153 int ima_restore_measurement_entry(struct ima_template_entry *entry);
154 int ima_restore_measurement_list(loff_t bufsize, void *buf);
155 int ima_measurements_show(struct seq_file *m, void *v);
156 unsigned long ima_get_binary_runtime_size(void);
157 int ima_init_template(void);
158 void ima_init_template_list(void);
159 int __init ima_init_digests(void);
160 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
161                           void *lsm_data);
162
163 /*
164  * used to protect h_table and sha_table
165  */
166 extern spinlock_t ima_queue_lock;
167
168 struct ima_h_table {
169         atomic_long_t len;      /* number of stored measurements in the list */
170         atomic_long_t violations;
171         struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
172 };
173 extern struct ima_h_table ima_htable;
174
175 static inline unsigned long ima_hash_key(u8 *digest)
176 {
177         return hash_long(*digest, IMA_HASH_BITS);
178 }
179
180 #define __ima_hooks(hook)               \
181         hook(NONE)                      \
182         hook(FILE_CHECK)                \
183         hook(MMAP_CHECK)                \
184         hook(BPRM_CHECK)                \
185         hook(CREDS_CHECK)               \
186         hook(POST_SETATTR)              \
187         hook(MODULE_CHECK)              \
188         hook(FIRMWARE_CHECK)            \
189         hook(KEXEC_KERNEL_CHECK)        \
190         hook(KEXEC_INITRAMFS_CHECK)     \
191         hook(POLICY_CHECK)              \
192         hook(KEXEC_CMDLINE)             \
193         hook(MAX_CHECK)
194 #define __ima_hook_enumify(ENUM)        ENUM,
195
196 enum ima_hooks {
197         __ima_hooks(__ima_hook_enumify)
198 };
199
200 extern const char *const func_tokens[];
201
202 struct modsig;
203
204 /* LIM API function definitions */
205 int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
206                    int mask, enum ima_hooks func, int *pcr,
207                    struct ima_template_desc **template_desc);
208 int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
209 int ima_collect_measurement(struct integrity_iint_cache *iint,
210                             struct file *file, void *buf, loff_t size,
211                             enum hash_algo algo, struct modsig *modsig);
212 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
213                            const unsigned char *filename,
214                            struct evm_ima_xattr_data *xattr_value,
215                            int xattr_len, const struct modsig *modsig, int pcr,
216                            struct ima_template_desc *template_desc);
217 void ima_audit_measurement(struct integrity_iint_cache *iint,
218                            const unsigned char *filename);
219 int ima_alloc_init_template(struct ima_event_data *event_data,
220                             struct ima_template_entry **entry,
221                             struct ima_template_desc *template_desc);
222 int ima_store_template(struct ima_template_entry *entry, int violation,
223                        struct inode *inode,
224                        const unsigned char *filename, int pcr);
225 void ima_free_template_entry(struct ima_template_entry *entry);
226 const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
227
228 /* IMA policy related functions */
229 int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
230                      enum ima_hooks func, int mask, int flags, int *pcr,
231                      struct ima_template_desc **template_desc);
232 void ima_init_policy(void);
233 void ima_update_policy(void);
234 void ima_update_policy_flag(void);
235 ssize_t ima_parse_add_rule(char *);
236 void ima_delete_rules(void);
237 int ima_check_policy(void);
238 void *ima_policy_start(struct seq_file *m, loff_t *pos);
239 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
240 void ima_policy_stop(struct seq_file *m, void *v);
241 int ima_policy_show(struct seq_file *m, void *v);
242
243 /* Appraise integrity measurements */
244 #define IMA_APPRAISE_ENFORCE    0x01
245 #define IMA_APPRAISE_FIX        0x02
246 #define IMA_APPRAISE_LOG        0x04
247 #define IMA_APPRAISE_MODULES    0x08
248 #define IMA_APPRAISE_FIRMWARE   0x10
249 #define IMA_APPRAISE_POLICY     0x20
250 #define IMA_APPRAISE_KEXEC      0x40
251
252 #ifdef CONFIG_IMA_APPRAISE
253 int ima_appraise_measurement(enum ima_hooks func,
254                              struct integrity_iint_cache *iint,
255                              struct file *file, const unsigned char *filename,
256                              struct evm_ima_xattr_data *xattr_value,
257                              int xattr_len, const struct modsig *modsig);
258 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
259 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
260 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
261                                            enum ima_hooks func);
262 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
263                                  int xattr_len);
264 int ima_read_xattr(struct dentry *dentry,
265                    struct evm_ima_xattr_data **xattr_value);
266
267 #else
268 static inline int ima_appraise_measurement(enum ima_hooks func,
269                                            struct integrity_iint_cache *iint,
270                                            struct file *file,
271                                            const unsigned char *filename,
272                                            struct evm_ima_xattr_data *xattr_value,
273                                            int xattr_len,
274                                            const struct modsig *modsig)
275 {
276         return INTEGRITY_UNKNOWN;
277 }
278
279 static inline int ima_must_appraise(struct inode *inode, int mask,
280                                     enum ima_hooks func)
281 {
282         return 0;
283 }
284
285 static inline void ima_update_xattr(struct integrity_iint_cache *iint,
286                                     struct file *file)
287 {
288 }
289
290 static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
291                                                          *iint,
292                                                          enum ima_hooks func)
293 {
294         return INTEGRITY_UNKNOWN;
295 }
296
297 static inline enum hash_algo
298 ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
299 {
300         return ima_hash_algo;
301 }
302
303 static inline int ima_read_xattr(struct dentry *dentry,
304                                  struct evm_ima_xattr_data **xattr_value)
305 {
306         return 0;
307 }
308
309 #endif /* CONFIG_IMA_APPRAISE */
310
311 #ifdef CONFIG_IMA_APPRAISE_MODSIG
312 bool ima_hook_supports_modsig(enum ima_hooks func);
313 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
314                     struct modsig **modsig);
315 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
316 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
317                           const u8 **digest, u32 *digest_size);
318 int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
319                        u32 *data_len);
320 void ima_free_modsig(struct modsig *modsig);
321 #else
322 static inline bool ima_hook_supports_modsig(enum ima_hooks func)
323 {
324         return false;
325 }
326
327 static inline int ima_read_modsig(enum ima_hooks func, const void *buf,
328                                   loff_t buf_len, struct modsig **modsig)
329 {
330         return -EOPNOTSUPP;
331 }
332
333 static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
334                                       loff_t size)
335 {
336 }
337
338 static inline int ima_get_modsig_digest(const struct modsig *modsig,
339                                         enum hash_algo *algo, const u8 **digest,
340                                         u32 *digest_size)
341 {
342         return -EOPNOTSUPP;
343 }
344
345 static inline int ima_get_raw_modsig(const struct modsig *modsig,
346                                      const void **data, u32 *data_len)
347 {
348         return -EOPNOTSUPP;
349 }
350
351 static inline void ima_free_modsig(struct modsig *modsig)
352 {
353 }
354 #endif /* CONFIG_IMA_APPRAISE_MODSIG */
355
356 /* LSM based policy rules require audit */
357 #ifdef CONFIG_IMA_LSM_RULES
358
359 #define security_filter_rule_init security_audit_rule_init
360 #define security_filter_rule_match security_audit_rule_match
361
362 #else
363
364 static inline int security_filter_rule_init(u32 field, u32 op, char *rulestr,
365                                             void **lsmrule)
366 {
367         return -EINVAL;
368 }
369
370 static inline int security_filter_rule_match(u32 secid, u32 field, u32 op,
371                                              void *lsmrule)
372 {
373         return -EINVAL;
374 }
375 #endif /* CONFIG_IMA_LSM_RULES */
376
377 #ifdef  CONFIG_IMA_READ_POLICY
378 #define POLICY_FILE_FLAGS       (S_IWUSR | S_IRUSR)
379 #else
380 #define POLICY_FILE_FLAGS       S_IWUSR
381 #endif /* CONFIG_IMA_READ_POLICY */
382
383 #endif /* __LINUX_IMA_H */