ima_template_lib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Politecnico di Torino, Italy
  4. * TORSEC group -- https://security.polito.it
  5. *
  6. * Author: Roberto Sassu <roberto.sassu@polito.it>
  7. *
  8. * File: ima_template_lib.c
  9. * Library of supported template fields.
  10. */
  11. #include "ima_template_lib.h"
  12. #include <linux/xattr.h>
  13. #include <linux/evm.h>
  14. static bool ima_template_hash_algo_allowed(u8 algo)
  15. {
  16. if (algo == HASH_ALGO_SHA1 || algo == HASH_ALGO_MD5)
  17. return true;
  18. return false;
  19. }
  20. enum data_formats {
  21. DATA_FMT_DIGEST = 0,
  22. DATA_FMT_DIGEST_WITH_ALGO,
  23. DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
  24. DATA_FMT_STRING,
  25. DATA_FMT_HEX,
  26. DATA_FMT_UINT
  27. };
  28. enum digest_type {
  29. DIGEST_TYPE_IMA,
  30. DIGEST_TYPE_VERITY,
  31. DIGEST_TYPE__LAST
  32. };
  33. #define DIGEST_TYPE_NAME_LEN_MAX 7 /* including NUL */
  34. static const char * const digest_type_name[DIGEST_TYPE__LAST] = {
  35. [DIGEST_TYPE_IMA] = "ima",
  36. [DIGEST_TYPE_VERITY] = "verity"
  37. };
  38. static int ima_write_template_field_data(const void *data, const u32 datalen,
  39. enum data_formats datafmt,
  40. struct ima_field_data *field_data)
  41. {
  42. u8 *buf, *buf_ptr;
  43. u32 buflen = datalen;
  44. if (datafmt == DATA_FMT_STRING)
  45. buflen = datalen + 1;
  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_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;
  72. u32 buflen = field_data->len;
  73. switch (datafmt) {
  74. case DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
  75. case DATA_FMT_DIGEST_WITH_ALGO:
  76. buf_ptr = strrchr(field_data->data, ':');
  77. if (buf_ptr != field_data->data)
  78. seq_printf(m, "%s", field_data->data);
  79. /* skip ':' and '\0' */
  80. buf_ptr += 2;
  81. buflen -= buf_ptr - field_data->data;
  82. fallthrough;
  83. case DATA_FMT_DIGEST:
  84. case DATA_FMT_HEX:
  85. if (!buflen)
  86. break;
  87. ima_print_digest(m, buf_ptr, buflen);
  88. break;
  89. case DATA_FMT_STRING:
  90. seq_printf(m, "%s", buf_ptr);
  91. break;
  92. case DATA_FMT_UINT:
  93. switch (field_data->len) {
  94. case sizeof(u8):
  95. seq_printf(m, "%u", *(u8 *)buf_ptr);
  96. break;
  97. case sizeof(u16):
  98. if (ima_canonical_fmt)
  99. seq_printf(m, "%u",
  100. le16_to_cpu(*(__le16 *)buf_ptr));
  101. else
  102. seq_printf(m, "%u", *(u16 *)buf_ptr);
  103. break;
  104. case sizeof(u32):
  105. if (ima_canonical_fmt)
  106. seq_printf(m, "%u",
  107. le32_to_cpu(*(__le32 *)buf_ptr));
  108. else
  109. seq_printf(m, "%u", *(u32 *)buf_ptr);
  110. break;
  111. case sizeof(u64):
  112. if (ima_canonical_fmt)
  113. seq_printf(m, "%llu",
  114. le64_to_cpu(*(__le64 *)buf_ptr));
  115. else
  116. seq_printf(m, "%llu", *(u64 *)buf_ptr);
  117. break;
  118. default:
  119. break;
  120. }
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. static void ima_show_template_data_binary(struct seq_file *m,
  127. enum ima_show_type show,
  128. enum data_formats datafmt,
  129. struct ima_field_data *field_data)
  130. {
  131. u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ?
  132. strlen(field_data->data) : field_data->len;
  133. if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
  134. u32 field_len = !ima_canonical_fmt ?
  135. len : (__force u32)cpu_to_le32(len);
  136. ima_putc(m, &field_len, sizeof(field_len));
  137. }
  138. if (!len)
  139. return;
  140. ima_putc(m, field_data->data, len);
  141. }
  142. static void ima_show_template_field_data(struct seq_file *m,
  143. enum ima_show_type show,
  144. enum data_formats datafmt,
  145. struct ima_field_data *field_data)
  146. {
  147. switch (show) {
  148. case IMA_SHOW_ASCII:
  149. ima_show_template_data_ascii(m, show, datafmt, field_data);
  150. break;
  151. case IMA_SHOW_BINARY:
  152. case IMA_SHOW_BINARY_NO_FIELD_LEN:
  153. case IMA_SHOW_BINARY_OLD_STRING_FMT:
  154. ima_show_template_data_binary(m, show, datafmt, field_data);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. void ima_show_template_digest(struct seq_file *m, enum ima_show_type show,
  161. struct ima_field_data *field_data)
  162. {
  163. ima_show_template_field_data(m, show, DATA_FMT_DIGEST, field_data);
  164. }
  165. void ima_show_template_digest_ng(struct seq_file *m, enum ima_show_type show,
  166. struct ima_field_data *field_data)
  167. {
  168. ima_show_template_field_data(m, show, DATA_FMT_DIGEST_WITH_ALGO,
  169. field_data);
  170. }
  171. void ima_show_template_digest_ngv2(struct seq_file *m, enum ima_show_type show,
  172. struct ima_field_data *field_data)
  173. {
  174. ima_show_template_field_data(m, show,
  175. DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO,
  176. field_data);
  177. }
  178. void ima_show_template_string(struct seq_file *m, enum ima_show_type show,
  179. struct ima_field_data *field_data)
  180. {
  181. ima_show_template_field_data(m, show, DATA_FMT_STRING, field_data);
  182. }
  183. void ima_show_template_sig(struct seq_file *m, enum ima_show_type show,
  184. struct ima_field_data *field_data)
  185. {
  186. ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
  187. }
  188. void ima_show_template_buf(struct seq_file *m, enum ima_show_type show,
  189. struct ima_field_data *field_data)
  190. {
  191. ima_show_template_field_data(m, show, DATA_FMT_HEX, field_data);
  192. }
  193. void ima_show_template_uint(struct seq_file *m, enum ima_show_type show,
  194. struct ima_field_data *field_data)
  195. {
  196. ima_show_template_field_data(m, show, DATA_FMT_UINT, field_data);
  197. }
  198. /**
  199. * ima_parse_buf() - Parses lengths and data from an input buffer
  200. * @bufstartp: Buffer start address.
  201. * @bufendp: Buffer end address.
  202. * @bufcurp: Pointer to remaining (non-parsed) data.
  203. * @maxfields: Length of fields array.
  204. * @fields: Array containing lengths and pointers of parsed data.
  205. * @curfields: Number of array items containing parsed data.
  206. * @len_mask: Bitmap (if bit is set, data length should not be parsed).
  207. * @enforce_mask: Check if curfields == maxfields and/or bufcurp == bufendp.
  208. * @bufname: String identifier of the input buffer.
  209. *
  210. * Return: 0 on success, -EINVAL on error.
  211. */
  212. int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp,
  213. int maxfields, struct ima_field_data *fields, int *curfields,
  214. unsigned long *len_mask, int enforce_mask, char *bufname)
  215. {
  216. void *bufp = bufstartp;
  217. int i;
  218. for (i = 0; i < maxfields; i++) {
  219. if (len_mask == NULL || !test_bit(i, len_mask)) {
  220. if (bufp > (bufendp - sizeof(u32)))
  221. break;
  222. if (ima_canonical_fmt)
  223. fields[i].len = le32_to_cpu(*(__le32 *)bufp);
  224. else
  225. fields[i].len = *(u32 *)bufp;
  226. bufp += sizeof(u32);
  227. }
  228. if (bufp > (bufendp - fields[i].len))
  229. break;
  230. fields[i].data = bufp;
  231. bufp += fields[i].len;
  232. }
  233. if ((enforce_mask & ENFORCE_FIELDS) && i != maxfields) {
  234. pr_err("%s: nr of fields mismatch: expected: %d, current: %d\n",
  235. bufname, maxfields, i);
  236. return -EINVAL;
  237. }
  238. if ((enforce_mask & ENFORCE_BUFEND) && bufp != bufendp) {
  239. pr_err("%s: buf end mismatch: expected: %p, current: %p\n",
  240. bufname, bufendp, bufp);
  241. return -EINVAL;
  242. }
  243. if (curfields)
  244. *curfields = i;
  245. if (bufcurp)
  246. *bufcurp = bufp;
  247. return 0;
  248. }
  249. static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize,
  250. u8 digest_type, u8 hash_algo,
  251. struct ima_field_data *field_data)
  252. {
  253. /*
  254. * digest formats:
  255. * - DATA_FMT_DIGEST: digest
  256. * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest,
  257. * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO:
  258. * <digest type> + ':' + <hash algo> + ':' + '\0' + digest,
  259. *
  260. * where 'DATA_FMT_DIGEST' is the original digest format ('d')
  261. * with a hash size limitation of 20 bytes,
  262. * where <digest type> is either "ima" or "verity",
  263. * where <hash algo> is the hash_algo_name[] string.
  264. */
  265. u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 +
  266. IMA_MAX_DIGEST_SIZE] = { 0 };
  267. enum data_formats fmt = DATA_FMT_DIGEST;
  268. u32 offset = 0;
  269. if (digest_type < DIGEST_TYPE__LAST && hash_algo < HASH_ALGO__LAST) {
  270. fmt = DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO;
  271. offset += 1 + sprintf(buffer, "%s:%s:",
  272. digest_type_name[digest_type],
  273. hash_algo_name[hash_algo]);
  274. } else if (hash_algo < HASH_ALGO__LAST) {
  275. fmt = DATA_FMT_DIGEST_WITH_ALGO;
  276. offset += 1 + sprintf(buffer, "%s:",
  277. hash_algo_name[hash_algo]);
  278. }
  279. if (digest) {
  280. memcpy(buffer + offset, digest, digestsize);
  281. } else {
  282. /*
  283. * If digest is NULL, the event being recorded is a violation.
  284. * Make room for the digest by increasing the offset by the
  285. * hash algorithm digest size. If the hash algorithm is not
  286. * specified increase the offset by IMA_DIGEST_SIZE which
  287. * fits SHA1 or MD5
  288. */
  289. if (hash_algo < HASH_ALGO__LAST)
  290. offset += hash_digest_size[hash_algo];
  291. else
  292. offset += IMA_DIGEST_SIZE;
  293. }
  294. return ima_write_template_field_data(buffer, offset + digestsize,
  295. fmt, field_data);
  296. }
  297. /*
  298. * This function writes the digest of an event (with size limit).
  299. */
  300. int ima_eventdigest_init(struct ima_event_data *event_data,
  301. struct ima_field_data *field_data)
  302. {
  303. struct ima_max_digest_data hash;
  304. struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
  305. struct ima_digest_data, hdr);
  306. u8 *cur_digest = NULL;
  307. u32 cur_digestsize = 0;
  308. struct inode *inode;
  309. int result;
  310. memset(&hash, 0, sizeof(hash));
  311. if (event_data->violation) /* recording a violation. */
  312. goto out;
  313. if (ima_template_hash_algo_allowed(event_data->iint->ima_hash->algo)) {
  314. cur_digest = event_data->iint->ima_hash->digest;
  315. cur_digestsize = event_data->iint->ima_hash->length;
  316. goto out;
  317. }
  318. if ((const char *)event_data->filename == boot_aggregate_name) {
  319. if (ima_tpm_chip) {
  320. hash.hdr.algo = HASH_ALGO_SHA1;
  321. result = ima_calc_boot_aggregate(hash_hdr);
  322. /* algo can change depending on available PCR banks */
  323. if (!result && hash.hdr.algo != HASH_ALGO_SHA1)
  324. result = -EINVAL;
  325. if (result < 0)
  326. memset(&hash, 0, sizeof(hash));
  327. }
  328. cur_digest = hash_hdr->digest;
  329. cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];
  330. goto out;
  331. }
  332. if (!event_data->file) /* missing info to re-calculate the digest */
  333. return -EINVAL;
  334. inode = file_inode(event_data->file);
  335. hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
  336. ima_hash_algo : HASH_ALGO_SHA1;
  337. result = ima_calc_file_hash(event_data->file, hash_hdr);
  338. if (result) {
  339. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
  340. event_data->filename, "collect_data",
  341. "failed", result, 0);
  342. return result;
  343. }
  344. cur_digest = hash_hdr->digest;
  345. cur_digestsize = hash.hdr.length;
  346. out:
  347. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  348. DIGEST_TYPE__LAST, HASH_ALGO__LAST,
  349. field_data);
  350. }
  351. /*
  352. * This function writes the digest of an event (without size limit).
  353. */
  354. int ima_eventdigest_ng_init(struct ima_event_data *event_data,
  355. struct ima_field_data *field_data)
  356. {
  357. u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
  358. u32 cur_digestsize = 0;
  359. if (event_data->violation) /* recording a violation. */
  360. goto out;
  361. cur_digest = event_data->iint->ima_hash->digest;
  362. cur_digestsize = event_data->iint->ima_hash->length;
  363. hash_algo = event_data->iint->ima_hash->algo;
  364. out:
  365. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  366. DIGEST_TYPE__LAST, hash_algo,
  367. field_data);
  368. }
  369. /*
  370. * This function writes the digest of an event (without size limit),
  371. * prefixed with both the digest type and hash algorithm.
  372. */
  373. int ima_eventdigest_ngv2_init(struct ima_event_data *event_data,
  374. struct ima_field_data *field_data)
  375. {
  376. u8 *cur_digest = NULL, hash_algo = ima_hash_algo;
  377. u32 cur_digestsize = 0;
  378. u8 digest_type = DIGEST_TYPE_IMA;
  379. if (event_data->violation) /* recording a violation. */
  380. goto out;
  381. cur_digest = event_data->iint->ima_hash->digest;
  382. cur_digestsize = event_data->iint->ima_hash->length;
  383. hash_algo = event_data->iint->ima_hash->algo;
  384. if (event_data->iint->flags & IMA_VERITY_REQUIRED)
  385. digest_type = DIGEST_TYPE_VERITY;
  386. out:
  387. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  388. digest_type, hash_algo,
  389. field_data);
  390. }
  391. /*
  392. * This function writes the digest of the file which is expected to match the
  393. * digest contained in the file's appended signature.
  394. */
  395. int ima_eventdigest_modsig_init(struct ima_event_data *event_data,
  396. struct ima_field_data *field_data)
  397. {
  398. enum hash_algo hash_algo;
  399. const u8 *cur_digest;
  400. u32 cur_digestsize;
  401. if (!event_data->modsig)
  402. return 0;
  403. if (event_data->violation) {
  404. /* Recording a violation. */
  405. hash_algo = HASH_ALGO_SHA1;
  406. cur_digest = NULL;
  407. cur_digestsize = 0;
  408. } else {
  409. int rc;
  410. rc = ima_get_modsig_digest(event_data->modsig, &hash_algo,
  411. &cur_digest, &cur_digestsize);
  412. if (rc)
  413. return rc;
  414. else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0)
  415. /* There was some error collecting the digest. */
  416. return -EINVAL;
  417. }
  418. return ima_eventdigest_init_common(cur_digest, cur_digestsize,
  419. DIGEST_TYPE__LAST, hash_algo,
  420. field_data);
  421. }
  422. static int ima_eventname_init_common(struct ima_event_data *event_data,
  423. struct ima_field_data *field_data,
  424. bool size_limit)
  425. {
  426. const char *cur_filename = NULL;
  427. struct name_snapshot filename;
  428. u32 cur_filename_len = 0;
  429. bool snapshot = false;
  430. int ret;
  431. BUG_ON(event_data->filename == NULL && event_data->file == NULL);
  432. if (event_data->filename) {
  433. cur_filename = event_data->filename;
  434. cur_filename_len = strlen(event_data->filename);
  435. if (!size_limit || cur_filename_len <= IMA_EVENT_NAME_LEN_MAX)
  436. goto out;
  437. }
  438. if (event_data->file) {
  439. take_dentry_name_snapshot(&filename,
  440. event_data->file->f_path.dentry);
  441. snapshot = true;
  442. cur_filename = filename.name.name;
  443. cur_filename_len = strlen(cur_filename);
  444. } else
  445. /*
  446. * Truncate filename if the latter is too long and
  447. * the file descriptor is not available.
  448. */
  449. cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
  450. out:
  451. ret = ima_write_template_field_data(cur_filename, cur_filename_len,
  452. DATA_FMT_STRING, field_data);
  453. if (snapshot)
  454. release_dentry_name_snapshot(&filename);
  455. return ret;
  456. }
  457. /*
  458. * This function writes the name of an event (with size limit).
  459. */
  460. int ima_eventname_init(struct ima_event_data *event_data,
  461. struct ima_field_data *field_data)
  462. {
  463. return ima_eventname_init_common(event_data, field_data, true);
  464. }
  465. /*
  466. * This function writes the name of an event (without size limit).
  467. */
  468. int ima_eventname_ng_init(struct ima_event_data *event_data,
  469. struct ima_field_data *field_data)
  470. {
  471. return ima_eventname_init_common(event_data, field_data, false);
  472. }
  473. /*
  474. * ima_eventsig_init - include the file signature as part of the template data
  475. */
  476. int ima_eventsig_init(struct ima_event_data *event_data,
  477. struct ima_field_data *field_data)
  478. {
  479. struct evm_ima_xattr_data *xattr_value = event_data->xattr_value;
  480. if (!xattr_value ||
  481. (xattr_value->type != EVM_IMA_XATTR_DIGSIG &&
  482. xattr_value->type != IMA_VERITY_DIGSIG))
  483. return ima_eventevmsig_init(event_data, field_data);
  484. return ima_write_template_field_data(xattr_value, event_data->xattr_len,
  485. DATA_FMT_HEX, field_data);
  486. }
  487. /*
  488. * ima_eventbuf_init - include the buffer(kexec-cmldine) as part of the
  489. * template data.
  490. */
  491. int ima_eventbuf_init(struct ima_event_data *event_data,
  492. struct ima_field_data *field_data)
  493. {
  494. if ((!event_data->buf) || (event_data->buf_len == 0))
  495. return 0;
  496. return ima_write_template_field_data(event_data->buf,
  497. event_data->buf_len, DATA_FMT_HEX,
  498. field_data);
  499. }
  500. /*
  501. * ima_eventmodsig_init - include the appended file signature as part of the
  502. * template data
  503. */
  504. int ima_eventmodsig_init(struct ima_event_data *event_data,
  505. struct ima_field_data *field_data)
  506. {
  507. const void *data;
  508. u32 data_len;
  509. int rc;
  510. if (!event_data->modsig)
  511. return 0;
  512. /*
  513. * modsig is a runtime structure containing pointers. Get its raw data
  514. * instead.
  515. */
  516. rc = ima_get_raw_modsig(event_data->modsig, &data, &data_len);
  517. if (rc)
  518. return rc;
  519. return ima_write_template_field_data(data, data_len, DATA_FMT_HEX,
  520. field_data);
  521. }
  522. /*
  523. * ima_eventevmsig_init - include the EVM portable signature as part of the
  524. * template data
  525. */
  526. int ima_eventevmsig_init(struct ima_event_data *event_data,
  527. struct ima_field_data *field_data)
  528. {
  529. struct evm_ima_xattr_data *xattr_data = NULL;
  530. int rc = 0;
  531. if (!event_data->file)
  532. return 0;
  533. rc = vfs_getxattr_alloc(&nop_mnt_idmap, file_dentry(event_data->file),
  534. XATTR_NAME_EVM, (char **)&xattr_data, 0,
  535. GFP_NOFS);
  536. if (rc <= 0 || xattr_data->type != EVM_XATTR_PORTABLE_DIGSIG) {
  537. rc = 0;
  538. goto out;
  539. }
  540. rc = ima_write_template_field_data((char *)xattr_data, rc, DATA_FMT_HEX,
  541. field_data);
  542. out:
  543. kfree(xattr_data);
  544. return rc;
  545. }
  546. static int ima_eventinodedac_init_common(struct ima_event_data *event_data,
  547. struct ima_field_data *field_data,
  548. bool get_uid)
  549. {
  550. unsigned int id;
  551. if (!event_data->file)
  552. return 0;
  553. if (get_uid)
  554. id = i_uid_read(file_inode(event_data->file));
  555. else
  556. id = i_gid_read(file_inode(event_data->file));
  557. if (ima_canonical_fmt) {
  558. if (sizeof(id) == sizeof(u16))
  559. id = (__force u16)cpu_to_le16(id);
  560. else
  561. id = (__force u32)cpu_to_le32(id);
  562. }
  563. return ima_write_template_field_data((void *)&id, sizeof(id),
  564. DATA_FMT_UINT, field_data);
  565. }
  566. /*
  567. * ima_eventinodeuid_init - include the inode UID as part of the template
  568. * data
  569. */
  570. int ima_eventinodeuid_init(struct ima_event_data *event_data,
  571. struct ima_field_data *field_data)
  572. {
  573. return ima_eventinodedac_init_common(event_data, field_data, true);
  574. }
  575. /*
  576. * ima_eventinodegid_init - include the inode GID as part of the template
  577. * data
  578. */
  579. int ima_eventinodegid_init(struct ima_event_data *event_data,
  580. struct ima_field_data *field_data)
  581. {
  582. return ima_eventinodedac_init_common(event_data, field_data, false);
  583. }
  584. /*
  585. * ima_eventinodemode_init - include the inode mode as part of the template
  586. * data
  587. */
  588. int ima_eventinodemode_init(struct ima_event_data *event_data,
  589. struct ima_field_data *field_data)
  590. {
  591. struct inode *inode;
  592. u16 mode;
  593. if (!event_data->file)
  594. return 0;
  595. inode = file_inode(event_data->file);
  596. mode = inode->i_mode;
  597. if (ima_canonical_fmt)
  598. mode = (__force u16)cpu_to_le16(mode);
  599. return ima_write_template_field_data((char *)&mode, sizeof(mode),
  600. DATA_FMT_UINT, field_data);
  601. }
  602. static int ima_eventinodexattrs_init_common(struct ima_event_data *event_data,
  603. struct ima_field_data *field_data,
  604. char type)
  605. {
  606. u8 *buffer = NULL;
  607. int rc;
  608. if (!event_data->file)
  609. return 0;
  610. rc = evm_read_protected_xattrs(file_dentry(event_data->file), NULL, 0,
  611. type, ima_canonical_fmt);
  612. if (rc < 0)
  613. return 0;
  614. buffer = kmalloc(rc, GFP_KERNEL);
  615. if (!buffer)
  616. return 0;
  617. rc = evm_read_protected_xattrs(file_dentry(event_data->file), buffer,
  618. rc, type, ima_canonical_fmt);
  619. if (rc < 0) {
  620. rc = 0;
  621. goto out;
  622. }
  623. rc = ima_write_template_field_data((char *)buffer, rc, DATA_FMT_HEX,
  624. field_data);
  625. out:
  626. kfree(buffer);
  627. return rc;
  628. }
  629. /*
  630. * ima_eventinodexattrnames_init - include a list of xattr names as part of the
  631. * template data
  632. */
  633. int ima_eventinodexattrnames_init(struct ima_event_data *event_data,
  634. struct ima_field_data *field_data)
  635. {
  636. return ima_eventinodexattrs_init_common(event_data, field_data, 'n');
  637. }
  638. /*
  639. * ima_eventinodexattrlengths_init - include a list of xattr lengths as part of
  640. * the template data
  641. */
  642. int ima_eventinodexattrlengths_init(struct ima_event_data *event_data,
  643. struct ima_field_data *field_data)
  644. {
  645. return ima_eventinodexattrs_init_common(event_data, field_data, 'l');
  646. }
  647. /*
  648. * ima_eventinodexattrvalues_init - include a list of xattr values as part of
  649. * the template data
  650. */
  651. int ima_eventinodexattrvalues_init(struct ima_event_data *event_data,
  652. struct ima_field_data *field_data)
  653. {
  654. return ima_eventinodexattrs_init_common(event_data, field_data, 'v');
  655. }