Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netlink_crypto.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
  3. * Copyright (c) 2017-2018 The strace developers.
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: LGPL-2.1-or-later
  7. */
  8. #include "defs.h"
  9. # include "netlink.h"
  10. # include "nlattr.h"
  11. # include "print_fields.h"
  12. # if HAVE_LINUX_CRYPTOUSER_H
  13. # include <linux/cryptouser.h>
  14. # endif
  15. # include "xlat/crypto_nl_attrs.h"
  16. # define XLAT_MACROS_ONLY
  17. # include "xlat/crypto_msgs.h"
  18. # undef XLAT_MACROS_ONLY
  19. # ifndef CRYPTO_MAX_NAME
  20. # define CRYPTO_MAX_NAME 64
  21. # endif
  22. typedef struct {
  23. char cru_name[CRYPTO_MAX_NAME];
  24. char cru_driver_name[CRYPTO_MAX_NAME];
  25. char cru_module_name[CRYPTO_MAX_NAME];
  26. uint32_t cru_type;
  27. uint32_t cru_mask;
  28. uint32_t cru_refcnt;
  29. uint32_t cru_flags;
  30. } struct_crypto_user_alg;
  31. typedef struct {
  32. char type[CRYPTO_MAX_NAME];
  33. uint32_t blocksize;
  34. uint32_t digestsize;
  35. } struct_crypto_report_hash;
  36. typedef struct {
  37. char type[CRYPTO_MAX_NAME];
  38. uint32_t blocksize;
  39. uint32_t min_keysize;
  40. uint32_t max_keysize;
  41. } struct_crypto_report_cipher;
  42. typedef struct {
  43. char type[CRYPTO_MAX_NAME];
  44. char geniv[CRYPTO_MAX_NAME];
  45. uint32_t blocksize;
  46. uint32_t min_keysize;
  47. uint32_t max_keysize;
  48. uint32_t ivsize;
  49. } struct_crypto_report_blkcipher;
  50. typedef struct {
  51. char type[CRYPTO_MAX_NAME];
  52. char geniv[CRYPTO_MAX_NAME];
  53. uint32_t blocksize;
  54. uint32_t maxauthsize;
  55. uint32_t ivsize;
  56. } struct_crypto_report_aead;
  57. typedef struct {
  58. char type[CRYPTO_MAX_NAME];
  59. uint32_t seedsize;
  60. } struct_crypto_report_rng;
  61. # ifdef HAVE_STRUCT_CRYPTO_USER_ALG
  62. static_assert(sizeof(struct_crypto_user_alg) == sizeof(struct crypto_user_alg),
  63. "struct crypto_user_alg mismatch, please update the decoder");
  64. # endif
  65. # ifdef HAVE_STRUCT_CRYPTO_REPORT_HASH
  66. static_assert(sizeof(struct_crypto_report_hash)
  67. == sizeof(struct crypto_report_hash),
  68. "struct crypto_report_hash mismatch, please update the decoder");
  69. # endif
  70. # ifdef HAVE_STRUCT_CRYPTO_REPORT_CIPHER
  71. static_assert(sizeof(struct_crypto_report_cipher)
  72. == sizeof(struct crypto_report_cipher),
  73. "struct crypto_report_cipher mismatch, please update the decoder");
  74. # endif
  75. # ifdef HAVE_STRUCT_CRYPTO_REPORT_BLKCIPHER
  76. static_assert(sizeof(struct_crypto_report_blkcipher)
  77. == sizeof(struct crypto_report_blkcipher),
  78. "struct crypto_report_blkcipher mismatch, please update the decoder");
  79. # endif
  80. # ifdef HAVE_STRUCT_CRYPTO_REPORT_AEAD
  81. static_assert(sizeof(struct_crypto_report_aead)
  82. == sizeof(struct crypto_report_aead),
  83. "struct crypto_report_aead mismatch, please update the decoder");
  84. # endif
  85. # ifdef HAVE_STRUCT_CRYPTO_REPORT_RNG
  86. static_assert(sizeof(struct_crypto_report_rng)
  87. == sizeof(struct crypto_report_rng),
  88. "struct crypto_report_rng mismatch, please update the decoder");
  89. # endif
  90. static bool
  91. decode_crypto_report_generic(struct tcb *const tcp,
  92. const kernel_ulong_t addr,
  93. const unsigned int len,
  94. const void *const opaque_data)
  95. {
  96. tprints("{type=");
  97. printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
  98. tprints("}");
  99. return true;
  100. }
  101. static bool
  102. decode_crypto_report_hash(struct tcb *const tcp,
  103. const kernel_ulong_t addr,
  104. const unsigned int len,
  105. const void *const opaque_data)
  106. {
  107. struct_crypto_report_hash rhash;
  108. if (len < sizeof(rhash))
  109. printstrn(tcp, addr, len);
  110. else if (!umove_or_printaddr(tcp, addr, &rhash)) {
  111. PRINT_FIELD_CSTRING("{", rhash, type);
  112. PRINT_FIELD_U(", ", rhash, blocksize);
  113. PRINT_FIELD_U(", ", rhash, digestsize);
  114. tprints("}");
  115. }
  116. return true;
  117. }
  118. static bool
  119. decode_crypto_report_blkcipher(struct tcb *const tcp,
  120. const kernel_ulong_t addr,
  121. const unsigned int len,
  122. const void *const opaque_data)
  123. {
  124. struct_crypto_report_blkcipher rblkcipher;
  125. if (len < sizeof(rblkcipher))
  126. printstrn(tcp, addr, len);
  127. else if (!umove_or_printaddr(tcp, addr, &rblkcipher)) {
  128. PRINT_FIELD_CSTRING("{", rblkcipher, type);
  129. PRINT_FIELD_CSTRING(", ", rblkcipher, geniv);
  130. PRINT_FIELD_U(", ", rblkcipher, blocksize);
  131. PRINT_FIELD_U(", ", rblkcipher, min_keysize);
  132. PRINT_FIELD_U(", ", rblkcipher, max_keysize);
  133. PRINT_FIELD_U(", ", rblkcipher, ivsize);
  134. tprints("}");
  135. }
  136. return true;
  137. }
  138. static bool
  139. decode_crypto_report_aead(struct tcb *const tcp,
  140. const kernel_ulong_t addr,
  141. const unsigned int len,
  142. const void *const opaque_data)
  143. {
  144. struct_crypto_report_aead raead;
  145. if (len < sizeof(raead))
  146. printstrn(tcp, addr, len);
  147. else if (!umove_or_printaddr(tcp, addr, &raead)) {
  148. PRINT_FIELD_CSTRING("{", raead, type);
  149. PRINT_FIELD_CSTRING(", ", raead, geniv);
  150. PRINT_FIELD_U(", ", raead, blocksize);
  151. PRINT_FIELD_U(", ", raead, maxauthsize);
  152. PRINT_FIELD_U(", ", raead, ivsize);
  153. tprints("}");
  154. }
  155. return true;
  156. }
  157. static bool
  158. decode_crypto_report_rng(struct tcb *const tcp,
  159. const kernel_ulong_t addr,
  160. const unsigned int len,
  161. const void *const opaque_data)
  162. {
  163. struct_crypto_report_rng rrng;
  164. if (len < sizeof(rrng))
  165. printstrn(tcp, addr, len);
  166. else if (!umove_or_printaddr(tcp, addr, &rrng)) {
  167. PRINT_FIELD_CSTRING("{", rrng, type);
  168. PRINT_FIELD_U(", ", rrng, seedsize);
  169. tprints("}");
  170. }
  171. return true;
  172. }
  173. static bool
  174. decode_crypto_report_cipher(struct tcb *const tcp,
  175. const kernel_ulong_t addr,
  176. const unsigned int len,
  177. const void *const opaque_data)
  178. {
  179. struct_crypto_report_cipher rcipher;
  180. if (len < sizeof(rcipher))
  181. printstrn(tcp, addr, len);
  182. else if (!umove_or_printaddr(tcp, addr, &rcipher)) {
  183. PRINT_FIELD_CSTRING("{", rcipher, type);
  184. PRINT_FIELD_U(", ", rcipher, blocksize);
  185. PRINT_FIELD_U(", ", rcipher, min_keysize);
  186. PRINT_FIELD_U(", ", rcipher, max_keysize);
  187. tprints("}");
  188. }
  189. return true;
  190. }
  191. static const nla_decoder_t crypto_user_alg_nla_decoders[] = {
  192. [CRYPTOCFGA_PRIORITY_VAL] = decode_nla_u32,
  193. [CRYPTOCFGA_REPORT_LARVAL] = decode_crypto_report_generic,
  194. [CRYPTOCFGA_REPORT_HASH] = decode_crypto_report_hash,
  195. [CRYPTOCFGA_REPORT_BLKCIPHER] = decode_crypto_report_blkcipher,
  196. [CRYPTOCFGA_REPORT_AEAD] = decode_crypto_report_aead,
  197. [CRYPTOCFGA_REPORT_COMPRESS] = decode_crypto_report_generic,
  198. [CRYPTOCFGA_REPORT_RNG] = decode_crypto_report_rng,
  199. [CRYPTOCFGA_REPORT_CIPHER] = decode_crypto_report_cipher,
  200. [CRYPTOCFGA_REPORT_AKCIPHER] = decode_crypto_report_generic,
  201. [CRYPTOCFGA_REPORT_KPP] = decode_crypto_report_generic,
  202. [CRYPTOCFGA_REPORT_ACOMP] = decode_crypto_report_generic
  203. };
  204. static void
  205. decode_crypto_user_alg(struct tcb *const tcp,
  206. const kernel_ulong_t addr,
  207. const unsigned int len)
  208. {
  209. struct_crypto_user_alg alg;
  210. if (len < sizeof(alg))
  211. printstrn(tcp, addr, len);
  212. else if (!umove_or_printaddr(tcp, addr, &alg)) {
  213. PRINT_FIELD_CSTRING("{", alg, cru_name);
  214. PRINT_FIELD_CSTRING(", ", alg, cru_driver_name);
  215. PRINT_FIELD_CSTRING(", ", alg, cru_module_name);
  216. PRINT_FIELD_X(", ", alg, cru_type);
  217. PRINT_FIELD_X(", ", alg, cru_mask);
  218. PRINT_FIELD_U(", ", alg, cru_refcnt);
  219. PRINT_FIELD_X(", ", alg, cru_flags);
  220. tprints("}");
  221. const size_t offset = NLMSG_ALIGN(sizeof(alg));
  222. if (len > offset) {
  223. tprints(", ");
  224. decode_nlattr(tcp, addr + offset, len - offset,
  225. crypto_nl_attrs, "CRYPTOCFGA_???",
  226. crypto_user_alg_nla_decoders,
  227. ARRAY_SIZE(crypto_user_alg_nla_decoders),
  228. NULL);
  229. }
  230. }
  231. }
  232. bool
  233. decode_netlink_crypto(struct tcb *const tcp,
  234. const struct nlmsghdr *const nlmsghdr,
  235. const kernel_ulong_t addr,
  236. const unsigned int len)
  237. {
  238. switch (nlmsghdr->nlmsg_type) {
  239. case CRYPTO_MSG_NEWALG:
  240. case CRYPTO_MSG_DELALG:
  241. case CRYPTO_MSG_UPDATEALG:
  242. case CRYPTO_MSG_GETALG:
  243. decode_crypto_user_alg(tcp, addr, len);
  244. break;
  245. default:
  246. return false;
  247. }
  248. return true;
  249. }