ima_template_lib.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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, 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 integrity_iint_cache *iint, struct file *file,
  172. const unsigned char *filename,
  173. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  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 (!iint) /* recording a violation. */
  186. goto out;
  187. if (ima_template_hash_algo_allowed(iint->ima_hash->algo)) {
  188. cur_digest = iint->ima_hash->digest;
  189. cur_digestsize = iint->ima_hash->length;
  190. goto out;
  191. }
  192. if (!file) /* missing info to re-calculate the digest */
  193. return -EINVAL;
  194. inode = file_inode(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(file, &hash.hdr);
  198. if (result) {
  199. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  200. 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 integrity_iint_cache *iint,
  214. struct file *file, const unsigned char *filename,
  215. struct evm_ima_xattr_data *xattr_value,
  216. int xattr_len, struct ima_field_data *field_data)
  217. {
  218. u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
  219. u32 cur_digestsize = 0;
  220. /* If iint is NULL, we are recording a violation. */
  221. if (!iint)
  222. goto out;
  223. cur_digest = iint->ima_hash->digest;
  224. cur_digestsize = iint->ima_hash->length;
  225. hash_algo = iint->ima_hash->algo;
  226. out:
  227. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  228. hash_algo, field_data);
  229. }
  230. static int ima_eventname_init_common(struct integrity_iint_cache *iint,
  231. struct file *file,
  232. const unsigned char *filename,
  233. struct ima_field_data *field_data,
  234. bool size_limit)
  235. {
  236. const char *cur_filename = NULL;
  237. u32 cur_filename_len = 0;
  238. BUG_ON(filename == NULL && file == NULL);
  239. if (filename) {
  240. cur_filename = filename;
  241. cur_filename_len = strlen(filename);
  242. if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
  243. goto out;
  244. }
  245. if (file) {
  246. cur_filename = file->f_dentry->d_name.name;
  247. cur_filename_len = strlen(cur_filename);
  248. } else
  249. /*
  250. * Truncate filename if the latter is too long and
  251. * the file descriptor is not available.
  252. */
  253. cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
  254. out:
  255. return ima_write_template_field_data(cur_filename, cur_filename_len,
  256. DATA_FMT_STRING, field_data);
  257. }
  258. /*
  259. * This function writes the name of an event (with size limit).
  260. */
  261. int ima_eventname_init(struct integrity_iint_cache *iint, struct file *file,
  262. const unsigned char *filename,
  263. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  264. struct ima_field_data *field_data)
  265. {
  266. return ima_eventname_init_common(iint, file, filename,
  267. field_data, true);
  268. }
  269. /*
  270. * This function writes the name of an event (without size limit).
  271. */
  272. int ima_eventname_ng_init(struct integrity_iint_cache *iint, struct file *file,
  273. const unsigned char *filename,
  274. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  275. struct ima_field_data *field_data)
  276. {
  277. return ima_eventname_init_common(iint, file, filename,
  278. field_data, false);
  279. }
  280. /*
  281. * ima_eventsig_init - include the file signature as part of the template data
  282. */
  283. int ima_eventsig_init(struct integrity_iint_cache *iint, struct file *file,
  284. const unsigned char *filename,
  285. struct evm_ima_xattr_data *xattr_value, int xattr_len,
  286. struct ima_field_data *field_data)
  287. {
  288. enum data_formats fmt = DATA_FMT_HEX;
  289. int rc = 0;
  290. if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
  291. goto out;
  292. rc = ima_write_template_field_data(xattr_value, xattr_len, fmt,
  293. field_data);
  294. out:
  295. return rc;
  296. }