ima_template_lib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 ima_event_data *event_data,
  173. struct ima_field_data *field_data)
  174. {
  175. struct {
  176. struct ima_digest_data hdr;
  177. char digest[IMA_MAX_DIGEST_SIZE];
  178. } hash;
  179. u8 *cur_digest = NULL;
  180. u32 cur_digestsize = 0;
  181. struct inode *inode;
  182. int result;
  183. memset(&hash, 0, sizeof(hash));
  184. if (event_data->violation) /* recording a violation. */
  185. goto out;
  186. if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
  187. cur_digest = event_data->iint->ima_hash->digest;
  188. cur_digestsize = event_data->iint->ima_hash->length;
  189. goto out;
  190. }
  191. if (!event_data->file) /* missing info to re-calculate the digest */
  192. return -EINVAL;
  193. inode = file_inode(event_data->file);
  194. hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
  195. ima_hash_algo : HASH_ALGO_SHA1;
  196. result = ima_calc_file_hash(event_data->file, &hash.hdr);
  197. if (result) {
  198. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  199. event_data->filename, "collect_data",
  200. "failed", result, 0);
  201. return result;
  202. }
  203. cur_digest = hash.hdr.digest;
  204. cur_digestsize = hash.hdr.length;
  205. out:
  206. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  207. HASH_ALGO__LAST, field_data);
  208. }
  209. /*
  210. * This function writes the digest of an event (without size limit).
  211. */
  212. int ima_eventdigest_ng_init(struct ima_event_data *event_data,
  213. struct ima_field_data *field_data)
  214. {
  215. u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
  216. u32 cur_digestsize = 0;
  217. if (event_data->violation) /* recording a violation. */
  218. goto out;
  219. cur_digest = event_data->iint->ima_hash->digest;
  220. cur_digestsize = event_data->iint->ima_hash->length;
  221. hash_algo = event_data->iint->ima_hash->algo;
  222. out:
  223. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  224. hash_algo, field_data);
  225. }
  226. static int ima_eventname_init_common(struct ima_event_data *event_data,
  227. struct ima_field_data *field_data,
  228. bool size_limit)
  229. {
  230. const char *cur_filename = NULL;
  231. u32 cur_filename_len = 0;
  232. BUG_ON(event_data->filename == NULL && event_data->file == NULL);
  233. if (event_data->filename) {
  234. cur_filename = event_data->filename;
  235. cur_filename_len = strlen(event_data->filename);
  236. if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
  237. goto out;
  238. }
  239. if (event_data->file) {
  240. cur_filename = event_data->file->f_path.dentry->d_name.name;
  241. cur_filename_len = strlen(cur_filename);
  242. } else
  243. /*
  244. * Truncate filename if the latter is too long and
  245. * the file descriptor is not available.
  246. */
  247. cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
  248. out:
  249. return ima_write_template_field_data(cur_filename, cur_filename_len,
  250. DATA_FMT_STRING, field_data);
  251. }
  252. /*
  253. * This function writes the name of an event (with size limit).
  254. */
  255. int ima_eventname_init(struct ima_event_data *event_data,
  256. struct ima_field_data *field_data)
  257. {
  258. return ima_eventname_init_common(event_data, field_data, true);
  259. }
  260. /*
  261. * This function writes the name of an event (without size limit).
  262. */
  263. int ima_eventname_ng_init(struct ima_event_data *event_data,
  264. struct ima_field_data *field_data)
  265. {
  266. return ima_eventname_init_common(event_data, field_data, false);
  267. }
  268. /*
  269. * ima_eventsig_init - include the file signature as part of the template data
  270. */
  271. int ima_eventsig_init(struct ima_event_data *event_data,
  272. struct ima_field_data *field_data)
  273. {
  274. enum data_formats fmt = DATA_FMT_HEX;
  275. struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
  276. int xattr_len = event_data->xattr_len;
  277. int rc = 0;
  278. if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
  279. goto out;
  280. rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
  281. field_data);
  282. out:
  283. return rc;
  284. }