ima.h 5.9 KB

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