ima_template_lib.c 9.1 KB

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