ima_template_lib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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. u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
  94. ima_putc(m, &field_len, sizeof(field_len));
  95. }
  96. if (!len)
  97. return;
  98. ima_putc(m, field_data->data, len);
  99. }
  100. static void ima_show_template_field_data(struct seq_file *m,
  101. enum ima_show_type show,
  102. enum data_formats datafmt,
  103. struct ima_field_data *field_data)
  104. {
  105. switch (show) {
  106. case IMA_SHOW_ASCII:
  107. ima_show_template_data_ascii(m, show, datafmt, field_data);
  108. break;
  109. case IMA_SHOW_BINARY:
  110. case IMA_SHOW_BINARY_NO_FIELD_LEN:
  111. case IMA_SHOW_BINARY_OLD_STRING_FMT:
  112. ima_show_template_data_binary(m, show, datafmt, field_data);
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
  119. struct ima_field_data *field_data)
  120. {
  121. ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
  122. }
  123. void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
  124. struct ima_field_data *field_data)
  125. {
  126. ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
  127. field_data);
  128. }
  129. void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
  130. struct ima_field_data *field_data)
  131. {
  132. ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
  133. }
  134. void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
  135. struct ima_field_data *field_data)
  136. {
  137. ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
  138. }
  139. /**
  140. * ima_parse_buf() - Parses lengths and data from an input buffer
  141. * @bufstartp: Buffer start address.
  142. * @bufendp: Buffer end address.
  143. * @bufcurp: Pointer to remaining (non-parsed) data.
  144. * @maxfields: Length of fields array.
  145. * @fields: Array containing lengths and pointers of parsed data.
  146. * @curfields: Number of array items containing parsed data.
  147. * @len_mask: Bitmap (if bit is set, data length should not be parsed).
  148. * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
  149. * @bufname: String identifier of the input buffer.
  150. *
  151. * Return: 0 on success, -EINVAL on error.
  152. */
  153. int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
  154. int maxfields, struct ima_field_data *fields, int *curfields,
  155. unsigned long *len_mask, int enforce_mask, char *bufname)
  156. {
  157. void *bufp = bufstartp;
  158. int i;
  159. for (i = 0; i < maxfields; i++) {
  160. if (len_mask == NULL || !test_bit(i, len_mask)) {
  161. if (bufp > (bufendp - sizeof(u32)))
  162. break;
  163. fields[i].len = *(u32 *)bufp;
  164. if (ima_canonical_fmt)
  165. fields[i].len = le32_to_cpu(fields[i].len);
  166. bufp += sizeof(u32);
  167. }
  168. if (bufp > (bufendp - fields[i].len))
  169. break;
  170. fields[i].data = bufp;
  171. bufp += fields[i].len;
  172. }
  173. if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
  174. pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
  175. bufname, maxfields, i);
  176. return -EINVAL;
  177. }
  178. if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
  179. pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
  180. bufname, bufendp, bufp);
  181. return -EINVAL;
  182. }
  183. if (curfields)
  184. *curfields = i;
  185. if (bufcurp)
  186. *bufcurp = bufp;
  187. return 0;
  188. }
  189. static int ima_eventdigest_init_common(u8 *digest, u32 digestsize, u8 hash_algo,
  190. struct ima_field_data *field_data)
  191. {
  192. /*
  193. * digest formats:
  194. * - DATA_FMT_DIGEST: digest
  195. * - DATA_FMT_DIGEST_WITH_ALGO: [<hash algo>] + ':' + '\0' + digest,
  196. * where <hash algo> is provided if the hash algoritm is not
  197. * SHA1 or MD5
  198. */
  199. u8 buffer[CRYPTO_MAX_ALG_NAME + 2 + IMA_MAX_DIGEST_SIZE] = { 0 };
  200. enum data_formats fmt = DATA_FMT_DIGEST;
  201. u32 offset = 0;
  202. if (hash_algo < HASH_ALGO__LAST) {
  203. fmt = DATA_FMT_DIGEST_WITH_ALGO;
  204. offset += snprintf(buffer, CRYPTO_MAX_ALG_NAME + 1, "%s",
  205. hash_algo_name[hash_algo]);
  206. buffer[offset] = ':';
  207. offset += 2;
  208. }
  209. if (digest)
  210. memcpy(buffer + offset, digest, digestsize);
  211. else
  212. /*
  213. * If digest is NULL, the event being recorded is a violation.
  214. * Make room for the digest by increasing the offset of
  215. * IMA_DIGEST_SIZE.
  216. */
  217. offset += IMA_DIGEST_SIZE;
  218. return ima_write_template_field_data(buffer, offset + digestsize,
  219. fmt, field_data);
  220. }
  221. /*
  222. * This function writes the digest of an event (with size limit).
  223. */
  224. int ima_eventdigest_init(struct ima_event_data *event_data,
  225. struct ima_field_data *field_data)
  226. {
  227. struct {
  228. struct ima_digest_data hdr;
  229. char digest[IMA_MAX_DIGEST_SIZE];
  230. } hash;
  231. u8 *cur_digest = NULL;
  232. u32 cur_digestsize = 0;
  233. struct inode *inode;
  234. int result;
  235. memset(&hash, 0, sizeof(hash));
  236. if (event_data->violation) /* recording a violation. */
  237. goto out;
  238. if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
  239. cur_digest = event_data->iint->ima_hash->digest;
  240. cur_digestsize = event_data->iint->ima_hash->length;
  241. goto out;
  242. }
  243. if (!event_data->file) /* missing info to re-calculate the digest */
  244. return -EINVAL;
  245. inode = file_inode(event_data->file);
  246. hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
  247. ima_hash_algo : HASH_ALGO_SHA1;
  248. result = ima_calc_file_hash(event_data->file, &hash.hdr);
  249. if (result) {
  250. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  251. event_data->filename, "collect_data",
  252. "failed", result, 0);
  253. return result;
  254. }
  255. cur_digest = hash.hdr.digest;
  256. cur_digestsize = hash.hdr.length;
  257. out:
  258. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  259. HASH_ALGO__LAST, field_data);
  260. }
  261. /*
  262. * This function writes the digest of an event (without size limit).
  263. */
  264. int ima_eventdigest_ng_init(struct ima_event_data *event_data,
  265. struct ima_field_data *field_data)
  266. {
  267. u8 *cur_digest = NULL, hash_algo = HASH_ALGO_SHA1;
  268. u32 cur_digestsize = 0;
  269. if (event_data->violation) /* recording a violation. */
  270. goto out;
  271. cur_digest = event_data->iint->ima_hash->digest;
  272. cur_digestsize = event_data->iint->ima_hash->length;
  273. hash_algo = event_data->iint->ima_hash->algo;
  274. out:
  275. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  276. hash_algo, field_data);
  277. }
  278. static int ima_eventname_init_common(struct ima_event_data *event_data,
  279. struct ima_field_data *field_data,
  280. bool size_limit)
  281. {
  282. const char *cur_filename = NULL;
  283. u32 cur_filename_len = 0;
  284. BUG_ON(event_data->filename == NULL && event_data->file == NULL);
  285. if (event_data->filename) {
  286. cur_filename = event_data->filename;
  287. cur_filename_len = strlen(event_data->filename);
  288. if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
  289. goto out;
  290. }
  291. if (event_data->file) {
  292. cur_filename = event_data->file->f_path.dentry->d_name.name;
  293. cur_filename_len = strlen(cur_filename);
  294. } else
  295. /*
  296. * Truncate filename if the latter is too long and
  297. * the file descriptor is not available.
  298. */
  299. cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
  300. out:
  301. return ima_write_template_field_data(cur_filename, cur_filename_len,
  302. DATA_FMT_STRING, field_data);
  303. }
  304. /*
  305. * This function writes the name of an event (with size limit).
  306. */
  307. int ima_eventname_init(struct ima_event_data *event_data,
  308. struct ima_field_data *field_data)
  309. {
  310. return ima_eventname_init_common(event_data, field_data, true);
  311. }
  312. /*
  313. * This function writes the name of an event (without size limit).
  314. */
  315. int ima_eventname_ng_init(struct ima_event_data *event_data,
  316. struct ima_field_data *field_data)
  317. {
  318. return ima_eventname_init_common(event_data, field_data, false);
  319. }
  320. /*
  321. * ima_eventsig_init - include the file signature as part of the template data
  322. */
  323. int ima_eventsig_init(struct ima_event_data *event_data,
  324. struct ima_field_data *field_data)
  325. {
  326. struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
  327. if ((!xattr_value) || (xattr_value->type != EVM_IMA_XATTR_DIGSIG))
  328. return 0;
  329. return ima_write_template_field_data(xattr_value, event_data->xattr_len,
  330. DATA_FMT_HEX, field_data);
  331. }