ima_template_lib.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2013 Politecnico di Torino, Italy
  3. * TORSEC group -- http://security.polito.it
  4. *
  5. * Author: Roberto Sassu <roberto.sassu@polito.it>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, version 2 of the
  10. * License.
  11. *
  12. * File: ima_template_lib.c
  13. * Library of supported template fields.
  14. */
  15. #include <crypto/hash_info.h>
  16. #include "ima_template_lib.h"
  17. static bool ima_template_hash_algo_allowed(u8 algo)
  18. {
  19. if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
  20. return true;
  21. return false;
  22. }
  23. enum data_formats {
  24. DATA_FMT_DIGEST = 0,
  25. DATA_FMT_DIGEST_WITH_ALGO,
  26. DATA_FMT_STRING,
  27. DATA_FMT_HEX
  28. };
  29. static int ima_write_template_field_data(const void *data, const u32 datalen,
  30. enum data_formats datafmt,
  31. struct ima_field_data *field_data)
  32. {
  33. u8 *buf, *buf_ptr;
  34. u32 buflen = datalen;
  35. if (datafmt == DATA_FMT_STRING)
  36. buflen = datalen + 1;
  37. buf = kzalloc(buflen, GFP_KERNEL);
  38. if (!buf)
  39. return -ENOMEM;
  40. memcpy(buf, data, datalen);
  41. /*
  42. * Replace all space characters with underscore for event names and
  43. * strings. This avoid that, during the parsing of a measurements list,
  44. * filenames with spaces or that end with the suffix ' (deleted)' are
  45. * split into multiple template fields (the space is the delimitator
  46. * character for measurements lists in ASCII format).
  47. */
  48. if (datafmt == DATA_FMT_STRING) {
  49. for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
  50. if (*buf_ptr == ' ')
  51. *buf_ptr = '_';
  52. }
  53. field_data->data = buf;
  54. field_data->len = buflen;
  55. return 0;
  56. }
  57. static void ima_show_template_data_ascii(struct seq_file *m,
  58. enum ima_show_type show,
  59. enum data_formats datafmt,
  60. struct ima_field_data *field_data)
  61. {
  62. u8 *buf_ptr = field_data->data;
  63. u32 buflen = field_data->len;
  64. switch (datafmt) {
  65. case DATA_FMT_DIGEST_WITH_ALGO:
  66. buf_ptr = strnchr(field_data->data, buflen, ':');
  67. if (buf_ptr != field_data->data)
  68. seq_printf(m, "%s", field_data->data);
  69. /* skip ':' and '\0' */
  70. buf_ptr += 2;
  71. buflen -= buf_ptr - field_data->data;
  72. case DATA_FMT_DIGEST:
  73. case DATA_FMT_HEX:
  74. if (!buflen)
  75. break;
  76. ima_print_digest(m, buf_ptr, buflen);
  77. break;
  78. case DATA_FMT_STRING:
  79. seq_printf(m, "%s", buf_ptr);
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. static void ima_show_template_data_binary(struct seq_file *m,
  86. enum ima_show_type show,
  87. enum data_formats datafmt,
  88. struct ima_field_data *field_data)
  89. {
  90. u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
  91. strlen(field_data->data) : field_data->len;
  92. if (show != IMA_SHOW_BINARY_NO_FIELD_LEN)
  93. ima_putc(m, &len, sizeof(len));
  94. if (!len)
  95. return;
  96. ima_putc(m, field_data->data, len);
  97. }
  98. static void ima_show_template_field_data(struct seq_file *m,
  99. enum ima_show_type show,
  100. enum data_formats datafmt,
  101. struct ima_field_data *field_data)
  102. {
  103. switch (show) {
  104. case IMA_SHOW_ASCII:
  105. ima_show_template_data_ascii(m, show, datafmt, field_data);
  106. break;
  107. case IMA_SHOW_BINARY:
  108. case IMA_SHOW_BINARY_NO_FIELD_LEN:
  109. case IMA_SHOW_BINARY_OLD_STRING_FMT:
  110. ima_show_template_data_binary(m, show, datafmt, field_data);
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
  117. struct ima_field_data *field_data)
  118. {
  119. ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
  120. }
  121. void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
  122. struct ima_field_data *field_data)
  123. {
  124. ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
  125. field_data);
  126. }
  127. void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
  128. struct ima_field_data *field_data)
  129. {
  130. ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
  131. }
  132. void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
  133. struct ima_field_data *field_data)
  134. {
  135. ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
  136. }
  137. static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
  138. struct ima_field_data *field_data)
  139. {
  140. /*
  141. * digest formats:
  142. * - DATA_FMT_DIGEST: digest
  143. * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
  144. * where <hash algo> is provided if the hash algoritm is not
  145. * SHA1 or MD5
  146. */
  147. u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
  148. enum data_formats fmt = DATA_FMT_DIGEST;
  149. u32 offset = 0;
  150. if (hash_algo < HASH_ALGO__LAST) {
  151. fmt = DATA_FMT_DIGEST_WITH_ALGO;
  152. offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
  153. hash_algo_name[hash_algo]);
  154. buffer[offset] = ':';
  155. offset += 2;
  156. }
  157. if (digest)
  158. memcpy(buffer + offset, digest, digestsize);
  159. else
  160. /*
  161. * If digest is NULL, the event being recorded is a violation.
  162. * Make room for the digest by increasing the offset of
  163. * IMA_DIGEST_SIZE.
  164. */
  165. offset += IMA_DIGEST_SIZE;
  166. return ima_write_template_field_data(buffer, offset + digestsize,
  167. fmt, field_data);
  168. }
  169. /*
  170. * This function writes the digest of an event (with size limit).
  171. */
  172. int ima_eventdigest_init(struct integrity_iint_cache *iint, struct file *file,
  173. const unsigned char *filename,
  174. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  175. struct ima_field_data *field_data)
  176. {
  177. struct {
  178. struct ima_digest_data hdr;
  179. char digest[IMA_MAX_DIGEST_SIZE];
  180. } hash;
  181. u8 *cur_digest = NULL;
  182. u32 cur_digestsize = 0;
  183. struct inode *inode;
  184. int result;
  185. memset(&hash, 0, sizeof(hash));
  186. if (!iint) /* recording a violation. */
  187. goto out;
  188. if (ima_template_hash_algo_allowed(iint->ima_hash->algo)) {
  189. cur_digest = iint->ima_hash->digest;
  190. cur_digestsize = iint->ima_hash->length;
  191. goto out;
  192. }
  193. if (!file) /* missing info to re-calculate the digest */
  194. return -EINVAL;
  195. inode = file_inode(file);
  196. hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
  197. ima_hash_algo : HASH_ALGO_SHA1;
  198. result = ima_calc_file_hash(file, &hash.hdr);
  199. if (result) {
  200. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  201. filename, "collect_data",
  202. "failed", result, 0);
  203. return result;
  204. }
  205. cur_digest = hash.hdr.digest;
  206. cur_digestsize = hash.hdr.length;
  207. out:
  208. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  209. HASH_ALGO__LAST, field_data);
  210. }
  211. /*
  212. * This function writes the digest of an event (without size limit).
  213. */
  214. int ima_eventdigest_ng_init(struct integrity_iint_cache *iint,
  215. struct file *file, const unsigned char *filename,
  216. struct evm_ima_xattr_data *xattr_value,
  217. int xattr_len, struct ima_field_data *field_data)
  218. {
  219. u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
  220. u32 cur_digestsize = 0;
  221. /* If iint is NULL, we are recording a violation. */
  222. if (!iint)
  223. goto out;
  224. cur_digest = iint->ima_hash->digest;
  225. cur_digestsize = iint->ima_hash->length;
  226. hash_algo = iint->ima_hash->algo;
  227. out:
  228. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  229. hash_algo, field_data);
  230. }
  231. static int ima_eventname_init_common(struct integrity_iint_cache *iint,
  232. struct file *file,
  233. const unsigned char *filename,
  234. struct ima_field_data *field_data,
  235. bool size_limit)
  236. {
  237. const char *cur_filename = NULL;
  238. u32 cur_filename_len = 0;
  239. BUG_ON(filename == NULL && file == NULL);
  240. if (filename) {
  241. cur_filename = filename;
  242. cur_filename_len = strlen(filename);
  243. if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
  244. goto out;
  245. }
  246. if (file) {
  247. cur_filename = file->f_dentry->d_name.name;
  248. cur_filename_len = strlen(cur_filename);
  249. } else
  250. /*
  251. * Truncate filename if the latter is too long and
  252. * the file descriptor is not available.
  253. */
  254. cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
  255. out:
  256. return ima_write_template_field_data(cur_filename, cur_filename_len,
  257. DATA_FMT_STRING, field_data);
  258. }
  259. /*
  260. * This function writes the name of an event (with size limit).
  261. */
  262. int ima_eventname_init(struct integrity_iint_cache *iint, struct file *file,
  263. const unsigned char *filename,
  264. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  265. struct ima_field_data *field_data)
  266. {
  267. return ima_eventname_init_common(iint, file, filename,
  268. field_data, true);
  269. }
  270. /*
  271. * This function writes the name of an event (without size limit).
  272. */
  273. int ima_eventname_ng_init(struct integrity_iint_cache *iint, struct file *file,
  274. const unsigned char *filename,
  275. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  276. struct ima_field_data *field_data)
  277. {
  278. return ima_eventname_init_common(iint, file, filename,
  279. field_data, false);
  280. }
  281. /*
  282. * ima_eventsig_init - include the file signature as part of the template data
  283. */
  284. int ima_eventsig_init(struct integrity_iint_cache *iint, struct file *file,
  285. const unsigned char *filename,
  286. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  287. struct ima_field_data *field_data)
  288. {
  289. enum data_formats fmt = DATA_FMT_HEX;
  290. int rc = 0;
  291. if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
  292. goto out;
  293. rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
  294. field_data);
  295. out:
  296. return rc;
  297. }