ima_template_lib.c 9.2 KB

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