1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
5 */
6
7 #ifndef _LINUX_IMA_H
8 #define _LINUX_IMA_H
9
10 #include <linux/kernel_read_file.h>
11 #include <linux/fs.h>
12 #include <linux/security.h>
13 #include <linux/kexec.h>
14 #include <crypto/hash_info.h>
15 struct linux_binprm;
16
17 #ifdef CONFIG_IMA
18 extern enum hash_algo ima_get_current_hash_algo(void);
19 extern int ima_bprm_check(struct linux_binprm *bprm);
20 extern int ima_file_check(struct file *file, int mask);
21 extern void ima_post_create_tmpfile(struct user_namespace *mnt_userns,
22 struct inode *inode);
23 extern void ima_file_free(struct file *file);
24 extern int ima_file_mmap(struct file *file, unsigned long reqprot,
25 unsigned long prot, unsigned long flags);
26 extern int ima_file_mprotect(struct vm_area_struct *vma, unsigned long prot);
27 extern int ima_load_data(enum kernel_load_data_id id, bool contents);
28 extern int ima_post_load_data(char *buf, loff_t size,
29 enum kernel_load_data_id id, char *description);
30 extern int ima_read_file(struct file *file, enum kernel_read_file_id id,
31 bool contents);
32 extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
33 enum kernel_read_file_id id);
34 extern void ima_post_path_mknod(struct user_namespace *mnt_userns,
35 struct dentry *dentry);
36 extern int ima_file_hash(struct file *file, char *buf, size_t buf_size);
37 extern int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size);
38 extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size);
39 extern int ima_measure_critical_data(const char *event_label,
40 const char *event_name,
41 const void *buf, size_t buf_len,
42 bool hash, u8 *digest, size_t digest_len);
43
44 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
45 extern void ima_appraise_parse_cmdline(void);
46 #else
ima_appraise_parse_cmdline(void)47 static inline void ima_appraise_parse_cmdline(void) {}
48 #endif
49
50 #ifdef CONFIG_IMA_KEXEC
51 extern void ima_add_kexec_buffer(struct kimage *image);
52 #endif
53
54 #ifdef CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT
55 extern bool arch_ima_get_secureboot(void);
56 extern const char * const *arch_get_ima_policy(void);
57 #else
arch_ima_get_secureboot(void)58 static inline bool arch_ima_get_secureboot(void)
59 {
60 return false;
61 }
62
arch_get_ima_policy(void)63 static inline const char * const *arch_get_ima_policy(void)
64 {
65 return NULL;
66 }
67 #endif
68
69 #else
ima_get_current_hash_algo(void)70 static inline enum hash_algo ima_get_current_hash_algo(void)
71 {
72 return HASH_ALGO__LAST;
73 }
74
ima_bprm_check(struct linux_binprm * bprm)75 static inline int ima_bprm_check(struct linux_binprm *bprm)
76 {
77 return 0;
78 }
79
ima_file_check(struct file * file,int mask)80 static inline int ima_file_check(struct file *file, int mask)
81 {
82 return 0;
83 }
84
ima_post_create_tmpfile(struct user_namespace * mnt_userns,struct inode * inode)85 static inline void ima_post_create_tmpfile(struct user_namespace *mnt_userns,
86 struct inode *inode)
87 {
88 }
89
ima_file_free(struct file * file)90 static inline void ima_file_free(struct file *file)
91 {
92 return;
93 }
94
ima_file_mmap(struct file * file,unsigned long reqprot,unsigned long prot,unsigned long flags)95 static inline int ima_file_mmap(struct file *file, unsigned long reqprot,
96 unsigned long prot, unsigned long flags)
97 {
98 return 0;
99 }
100
ima_file_mprotect(struct vm_area_struct * vma,unsigned long prot)101 static inline int ima_file_mprotect(struct vm_area_struct *vma,
102 unsigned long prot)
103 {
104 return 0;
105 }
106
ima_load_data(enum kernel_load_data_id id,bool contents)107 static inline int ima_load_data(enum kernel_load_data_id id, bool contents)
108 {
109 return 0;
110 }
111
ima_post_load_data(char * buf,loff_t size,enum kernel_load_data_id id,char * description)112 static inline int ima_post_load_data(char *buf, loff_t size,
113 enum kernel_load_data_id id,
114 char *description)
115 {
116 return 0;
117 }
118
ima_read_file(struct file * file,enum kernel_read_file_id id,bool contents)119 static inline int ima_read_file(struct file *file, enum kernel_read_file_id id,
120 bool contents)
121 {
122 return 0;
123 }
124
ima_post_read_file(struct file * file,void * buf,loff_t size,enum kernel_read_file_id id)125 static inline int ima_post_read_file(struct file *file, void *buf, loff_t size,
126 enum kernel_read_file_id id)
127 {
128 return 0;
129 }
130
ima_post_path_mknod(struct user_namespace * mnt_userns,struct dentry * dentry)131 static inline void ima_post_path_mknod(struct user_namespace *mnt_userns,
132 struct dentry *dentry)
133 {
134 return;
135 }
136
ima_file_hash(struct file * file,char * buf,size_t buf_size)137 static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size)
138 {
139 return -EOPNOTSUPP;
140 }
141
ima_inode_hash(struct inode * inode,char * buf,size_t buf_size)142 static inline int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size)
143 {
144 return -EOPNOTSUPP;
145 }
146
ima_kexec_cmdline(int kernel_fd,const void * buf,int size)147 static inline void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) {}
148
ima_measure_critical_data(const char * event_label,const char * event_name,const void * buf,size_t buf_len,bool hash,u8 * digest,size_t digest_len)149 static inline int ima_measure_critical_data(const char *event_label,
150 const char *event_name,
151 const void *buf, size_t buf_len,
152 bool hash, u8 *digest,
153 size_t digest_len)
154 {
155 return -ENOENT;
156 }
157
158 #endif /* CONFIG_IMA */
159
160 #ifndef CONFIG_IMA_KEXEC
161 struct kimage;
162
ima_add_kexec_buffer(struct kimage * image)163 static inline void ima_add_kexec_buffer(struct kimage *image)
164 {}
165 #endif
166
167 #ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
168 extern void ima_post_key_create_or_update(struct key *keyring,
169 struct key *key,
170 const void *payload, size_t plen,
171 unsigned long flags, bool create);
172 #else
ima_post_key_create_or_update(struct key * keyring,struct key * key,const void * payload,size_t plen,unsigned long flags,bool create)173 static inline void ima_post_key_create_or_update(struct key *keyring,
174 struct key *key,
175 const void *payload,
176 size_t plen,
177 unsigned long flags,
178 bool create) {}
179 #endif /* CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS */
180
181 #ifdef CONFIG_IMA_APPRAISE
182 extern bool is_ima_appraise_enabled(void);
183 extern void ima_inode_post_setattr(struct user_namespace *mnt_userns,
184 struct dentry *dentry);
185 extern int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
186 const void *xattr_value, size_t xattr_value_len);
187 extern int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name);
188 #else
is_ima_appraise_enabled(void)189 static inline bool is_ima_appraise_enabled(void)
190 {
191 return 0;
192 }
193
ima_inode_post_setattr(struct user_namespace * mnt_userns,struct dentry * dentry)194 static inline void ima_inode_post_setattr(struct user_namespace *mnt_userns,
195 struct dentry *dentry)
196 {
197 return;
198 }
199
ima_inode_setxattr(struct dentry * dentry,const char * xattr_name,const void * xattr_value,size_t xattr_value_len)200 static inline int ima_inode_setxattr(struct dentry *dentry,
201 const char *xattr_name,
202 const void *xattr_value,
203 size_t xattr_value_len)
204 {
205 return 0;
206 }
207
ima_inode_removexattr(struct dentry * dentry,const char * xattr_name)208 static inline int ima_inode_removexattr(struct dentry *dentry,
209 const char *xattr_name)
210 {
211 return 0;
212 }
213 #endif /* CONFIG_IMA_APPRAISE */
214
215 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
216 extern bool ima_appraise_signature(enum kernel_read_file_id func);
217 #else
ima_appraise_signature(enum kernel_read_file_id func)218 static inline bool ima_appraise_signature(enum kernel_read_file_id func)
219 {
220 return false;
221 }
222 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */
223 #endif /* _LINUX_IMA_H */
224