ima_template_lib.c 8.7 KB

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