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 "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. ima_putc(m, &len, sizeof(len));
  93. if (!len)
  94. return;
  95. ima_putc(m, field_data->data, len);
  96. }
  97. static void ima_show_template_field_data(struct seq_file *m,
  98. enum ima_show_type show,
  99. enum data_formats datafmt,
  100. struct ima_field_data *field_data)
  101. {
  102. switch (show) {
  103. case IMA_SHOW_ASCII:
  104. ima_show_template_data_ascii(m, show, datafmt, field_data);
  105. break;
  106. case IMA_SHOW_BINARY:
  107. case IMA_SHOW_BINARY_NO_FIELD_LEN:
  108. case IMA_SHOW_BINARY_OLD_STRING_FMT:
  109. ima_show_template_data_binary(m, show, datafmt, field_data);
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
  116. struct ima_field_data *field_data)
  117. {
  118. ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
  119. }
  120. void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
  121. struct ima_field_data *field_data)
  122. {
  123. ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
  124. field_data);
  125. }
  126. void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
  127. struct ima_field_data *field_data)
  128. {
  129. ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
  130. }
  131. void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
  132. struct ima_field_data *field_data)
  133. {
  134. ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
  135. }
  136. static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
  137. struct ima_field_data *field_data)
  138. {
  139. /*
  140. * digest formats:
  141. * - DATA_FMT_DIGEST: digest
  142. * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
  143. * where <hash algo> is provided if the hash algoritm is not
  144. * SHA1 or MD5
  145. */
  146. u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
  147. enum data_formats fmt = DATA_FMT_DIGEST;
  148. u32 offset = 0;
  149. if (hash_algo < HASH_ALGO__LAST) {
  150. fmt = DATA_FMT_DIGEST_WITH_ALGO;
  151. offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
  152. hash_algo_name[hash_algo]);
  153. buffer[offset] = ':';
  154. offset += 2;
  155. }
  156. if (digest)
  157. memcpy(buffer + offset, digest, digestsize);
  158. else
  159. /*
  160. * If digest is NULL, the event being recorded is a violation.
  161. * Make room for the digest by increasing the offset of
  162. * IMA_DIGEST_SIZE.
  163. */
  164. offset += IMA_DIGEST_SIZE;
  165. return ima_write_template_field_data(buffer, offset + digestsize,
  166. fmt, field_data);
  167. }
  168. /*
  169. * This function writes the digest of an event (with size limit).
  170. */
  171. int ima_eventdigest_init(struct ima_event_data *event_data,
  172. struct ima_field_data *field_data)
  173. {
  174. struct {
  175. struct ima_digest_data hdr;
  176. char digest[IMA_MAX_DIGEST_SIZE];
  177. } hash;
  178. u8 *cur_digest = NULL;
  179. u32 cur_digestsize = 0;
  180. struct inode *inode;
  181. int result;
  182. memset(&hash, 0, sizeof(hash));
  183. if (event_data->violation) /* recording a violation. */
  184. goto out;
  185. if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
  186. cur_digest = event_data->iint->ima_hash->digest;
  187. cur_digestsize = event_data->iint->ima_hash->length;
  188. goto out;
  189. }
  190. if (!event_data->file) /* missing info to re-calculate the digest */
  191. return -EINVAL;
  192. inode = file_inode(event_data->file);
  193. hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
  194. ima_hash_algo : HASH_ALGO_SHA1;
  195. result = ima_calc_file_hash(event_data->file, &hash.hdr);
  196. if (result) {
  197. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  198. event_data->filename, "collect_data",
  199. "failed", result, 0);
  200. return result;
  201. }
  202. cur_digest = hash.hdr.digest;
  203. cur_digestsize = hash.hdr.length;
  204. out:
  205. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  206. HASH_ALGO__LAST, field_data);
  207. }
  208. /*
  209. * This function writes the digest of an event (without size limit).
  210. */
  211. int ima_eventdigest_ng_init(struct ima_event_data *event_data,
  212. struct ima_field_data *field_data)
  213. {
  214. u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
  215. u32 cur_digestsize = 0;
  216. if (event_data->violation) /* recording a violation. */
  217. goto out;
  218. cur_digest = event_data->iint->ima_hash->digest;
  219. cur_digestsize = event_data->iint->ima_hash->length;
  220. hash_algo = event_data->iint->ima_hash->algo;
  221. out:
  222. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  223. hash_algo, field_data);
  224. }
  225. static int ima_eventname_init_common(struct ima_event_data *event_data,
  226. struct ima_field_data *field_data,
  227. bool size_limit)
  228. {
  229. const char *cur_filename = NULL;
  230. u32 cur_filename_len = 0;
  231. BUG_ON(event_data->filename == NULL && event_data->file == NULL);
  232. if (event_data->filename) {
  233. cur_filename = event_data->filename;
  234. cur_filename_len = strlen(event_data->filename);
  235. if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
  236. goto out;
  237. }
  238. if (event_data->file) {
  239. cur_filename = event_data->file->f_path.dentry->d_name.name;
  240. cur_filename_len = strlen(cur_filename);
  241. } else
  242. /*
  243. * Truncate filename if the latter is too long and
  244. * the file descriptor is not available.
  245. */
  246. cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
  247. out:
  248. return ima_write_template_field_data(cur_filename, cur_filename_len,
  249. DATA_FMT_STRING, field_data);
  250. }
  251. /*
  252. * This function writes the name of an event (with size limit).
  253. */
  254. int ima_eventname_init(struct ima_event_data *event_data,
  255. struct ima_field_data *field_data)
  256. {
  257. return ima_eventname_init_common(event_data, field_data, true);
  258. }
  259. /*
  260. * This function writes the name of an event (without size limit).
  261. */
  262. int ima_eventname_ng_init(struct ima_event_data *event_data,
  263. struct ima_field_data *field_data)
  264. {
  265. return ima_eventname_init_common(event_data, field_data, false);
  266. }
  267. /*
  268. * ima_eventsig_init - include the file signature as part of the template data
  269. */
  270. int ima_eventsig_init(struct ima_event_data *event_data,
  271. struct ima_field_data *field_data)
  272. {
  273. enum data_formats fmt = DATA_FMT_HEX;
  274. struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
  275. int xattr_len = event_data->xattr_len;
  276. int rc = 0;
  277. if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
  278. goto out;
  279. rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
  280. field_data);
  281. out:
  282. return rc;
  283. }