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.

keyctl.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
  3. * Copyright (c) 2014-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 "keyctl_kdf_params.h"
  10. #include "print_fields.h"
  11. typedef int32_t key_serial_t;
  12. #include "xlat/key_spec.h"
  13. struct keyctl_dh_params {
  14. int32_t private;
  15. int32_t prime;
  16. int32_t base;
  17. };
  18. static void
  19. print_keyring_serial_number(key_serial_t id)
  20. {
  21. printxval_d(key_spec, id, NULL);
  22. }
  23. SYS_FUNC(add_key)
  24. {
  25. /* type */
  26. printstr(tcp, tcp->u_arg[0]);
  27. /* description */
  28. tprints(", ");
  29. printstr(tcp, tcp->u_arg[1]);
  30. /* payload */
  31. tprints(", ");
  32. printstrn(tcp, tcp->u_arg[2], tcp->u_arg[3]);
  33. /* payload length */
  34. tprintf(", %" PRI_klu ", ", tcp->u_arg[3]);
  35. /* keyring serial number */
  36. print_keyring_serial_number(tcp->u_arg[4]);
  37. return RVAL_DECODED;
  38. }
  39. SYS_FUNC(request_key)
  40. {
  41. /* type */
  42. printstr(tcp, tcp->u_arg[0]);
  43. /* description */
  44. tprints(", ");
  45. printstr(tcp, tcp->u_arg[1]);
  46. /* callout_info */
  47. tprints(", ");
  48. printstr(tcp, tcp->u_arg[2]);
  49. /* keyring serial number */
  50. tprints(", ");
  51. print_keyring_serial_number(tcp->u_arg[3]);
  52. return RVAL_DECODED;
  53. }
  54. static void
  55. keyctl_get_keyring_id(struct tcb *tcp, key_serial_t id, int create)
  56. {
  57. print_keyring_serial_number(id);
  58. tprintf(", %d", create);
  59. }
  60. static void
  61. keyctl_update_key(struct tcb *tcp, key_serial_t id, kernel_ulong_t addr,
  62. kernel_ulong_t len)
  63. {
  64. print_keyring_serial_number(id);
  65. tprints(", ");
  66. printstrn(tcp, addr, len);
  67. tprintf(", %llu", zero_extend_signed_to_ull(len));
  68. }
  69. static void
  70. keyctl_handle_key_key(struct tcb *tcp, key_serial_t id1, key_serial_t id2)
  71. {
  72. print_keyring_serial_number(id1);
  73. tprints(", ");
  74. print_keyring_serial_number(id2);
  75. }
  76. static void
  77. keyctl_read_key(struct tcb *tcp, key_serial_t id, kernel_ulong_t addr,
  78. kernel_ulong_t len, bool has_nul)
  79. {
  80. if (entering(tcp)) {
  81. print_keyring_serial_number(id);
  82. tprints(", ");
  83. } else {
  84. if (syserror(tcp))
  85. printaddr(addr);
  86. else {
  87. kernel_ulong_t rval = (tcp->u_rval >= 0) &&
  88. ((kernel_ulong_t) tcp->u_rval > len) ? len :
  89. (kernel_ulong_t) tcp->u_rval;
  90. printstr_ex(tcp, addr, rval, has_nul ?
  91. QUOTE_OMIT_TRAILING_0 : 0);
  92. }
  93. tprintf(", %llu", zero_extend_signed_to_ull(len));
  94. }
  95. }
  96. static void
  97. keyctl_keyring_search(struct tcb *tcp, key_serial_t id1, kernel_ulong_t addr1,
  98. kernel_ulong_t addr2, key_serial_t id2)
  99. {
  100. print_keyring_serial_number(id1);
  101. tprints(", ");
  102. printstr(tcp, addr1);
  103. tprints(", ");
  104. printstr(tcp, addr2);
  105. tprints(", ");
  106. print_keyring_serial_number(id2);
  107. }
  108. static void
  109. keyctl_chown_key(struct tcb *tcp, key_serial_t id, unsigned user,
  110. unsigned group)
  111. {
  112. print_keyring_serial_number(id);
  113. printuid(", ", user);
  114. printuid(", ", group);
  115. }
  116. static void
  117. keyctl_instantiate_key(struct tcb *tcp, key_serial_t id1, kernel_ulong_t addr,
  118. kernel_ulong_t len, key_serial_t id2)
  119. {
  120. print_keyring_serial_number(id1);
  121. tprints(", ");
  122. printstrn(tcp, addr, len);
  123. tprintf(", %llu, ", zero_extend_signed_to_ull(len));
  124. print_keyring_serial_number(id2);
  125. }
  126. static void
  127. keyctl_instantiate_key_iov(struct tcb *tcp, key_serial_t id1,
  128. kernel_ulong_t addr, kernel_ulong_t len,
  129. key_serial_t id2)
  130. {
  131. print_keyring_serial_number(id1);
  132. tprints(", ");
  133. tprint_iov(tcp, len, addr, IOV_DECODE_STR);
  134. tprintf(", %llu, ", zero_extend_signed_to_ull(len));
  135. print_keyring_serial_number(id2);
  136. }
  137. static void
  138. keyctl_negate_key(struct tcb *tcp, key_serial_t id1, unsigned timeout,
  139. key_serial_t id2)
  140. {
  141. print_keyring_serial_number(id1);
  142. tprintf(", %u, ", timeout);
  143. print_keyring_serial_number(id2);
  144. }
  145. static void
  146. keyctl_reject_key(struct tcb *tcp, key_serial_t id1, unsigned timeout,
  147. unsigned error, key_serial_t id2)
  148. {
  149. print_keyring_serial_number(id1);
  150. tprintf(", %u, ", timeout);
  151. print_err(error, false);
  152. tprints(", ");
  153. print_keyring_serial_number(id2);
  154. }
  155. static void
  156. keyctl_set_timeout(struct tcb *tcp, key_serial_t id, unsigned timeout)
  157. {
  158. print_keyring_serial_number(id);
  159. tprintf(", %u", timeout);
  160. }
  161. static void
  162. keyctl_get_persistent(struct tcb *tcp, unsigned uid, key_serial_t id)
  163. {
  164. printuid("", uid);
  165. tprints(", ");
  166. print_keyring_serial_number(id);
  167. }
  168. #include "xlat/key_perms.h"
  169. static void
  170. keyctl_setperm_key(struct tcb *tcp, key_serial_t id, uint32_t perm)
  171. {
  172. print_keyring_serial_number(id);
  173. tprints(", ");
  174. printflags(key_perms, perm, "KEY_???");
  175. }
  176. static void
  177. print_dh_params(struct tcb *tcp, kernel_ulong_t addr)
  178. {
  179. struct keyctl_dh_params params;
  180. if (umove_or_printaddr(tcp, addr, &params))
  181. return;
  182. tprints("{private=");
  183. print_keyring_serial_number(params.private);
  184. tprints(", prime=");
  185. print_keyring_serial_number(params.prime);
  186. tprints(", base=");
  187. print_keyring_serial_number(params.base);
  188. tprints("}");
  189. }
  190. static void
  191. keyctl_dh_compute(struct tcb *tcp, kernel_ulong_t params, kernel_ulong_t buf,
  192. kernel_ulong_t len, kernel_ulong_t kdf_addr)
  193. {
  194. if (entering(tcp)) {
  195. print_dh_params(tcp, params);
  196. tprints(", ");
  197. } else {
  198. struct strace_keyctl_kdf_params kdf;
  199. if (syserror(tcp)) {
  200. printaddr(buf);
  201. } else {
  202. kernel_ulong_t rval = (tcp->u_rval >= 0) &&
  203. ((kernel_ulong_t) tcp->u_rval > len) ? len :
  204. (kernel_ulong_t) tcp->u_rval;
  205. printstrn(tcp, buf, rval);
  206. }
  207. tprintf(", %llu, ", zero_extend_signed_to_ull(len));
  208. if (fetch_keyctl_kdf_params(tcp, kdf_addr, &kdf)) {
  209. printaddr(kdf_addr);
  210. } else {
  211. size_t i;
  212. PRINT_FIELD_STR("{", kdf, hashname, tcp);
  213. /*
  214. * Kernel doesn't touch otherinfo
  215. * if otherinfolen is zero.
  216. */
  217. if (kdf.otherinfolen)
  218. PRINT_FIELD_STRN(", ", kdf, otherinfo,
  219. kdf.otherinfolen, tcp);
  220. else
  221. PRINT_FIELD_PTR(", ", kdf, otherinfo);
  222. PRINT_FIELD_U(", ", kdf, otherinfolen);
  223. /* Some future-proofing */
  224. for (i = 0; i < ARRAY_SIZE(kdf.__spare); i++) {
  225. if (kdf.__spare[i])
  226. break;
  227. }
  228. if (i < ARRAY_SIZE(kdf.__spare)) {
  229. tprints(", __spare=[");
  230. for (i = 0; i < ARRAY_SIZE(kdf.__spare); i++) {
  231. if (i)
  232. tprints(", ");
  233. tprintf("%#x", kdf.__spare[i]);
  234. }
  235. tprints("]");
  236. }
  237. tprints("}");
  238. }
  239. }
  240. }
  241. static void
  242. keyctl_restrict_keyring(struct tcb *const tcp,
  243. const key_serial_t id,
  244. const kernel_ulong_t addr1,
  245. const kernel_ulong_t addr2)
  246. {
  247. print_keyring_serial_number(id);
  248. tprints(", ");
  249. printstr(tcp, addr1);
  250. tprints(", ");
  251. printstr(tcp, addr2);
  252. }
  253. #include "xlat/key_reqkeys.h"
  254. #include "xlat/keyctl_commands.h"
  255. SYS_FUNC(keyctl)
  256. {
  257. int cmd = tcp->u_arg[0];
  258. kernel_ulong_t arg2 = tcp->u_arg[1];
  259. kernel_ulong_t arg3 = tcp->u_arg[2];
  260. kernel_ulong_t arg4 = tcp->u_arg[3];
  261. kernel_ulong_t arg5 = tcp->u_arg[4];
  262. if (entering(tcp)) {
  263. printxval(keyctl_commands, cmd, "KEYCTL_???");
  264. /*
  265. * For now, KEYCTL_SESSION_TO_PARENT is the only cmd without
  266. * arguments.
  267. */
  268. if (cmd != KEYCTL_SESSION_TO_PARENT)
  269. tprints(", ");
  270. }
  271. switch (cmd) {
  272. case KEYCTL_GET_KEYRING_ID:
  273. keyctl_get_keyring_id(tcp, arg2, arg3);
  274. break;
  275. case KEYCTL_JOIN_SESSION_KEYRING:
  276. printstr(tcp, arg2);
  277. break;
  278. case KEYCTL_UPDATE:
  279. keyctl_update_key(tcp, arg2, arg3, arg4);
  280. break;
  281. case KEYCTL_REVOKE:
  282. case KEYCTL_CLEAR:
  283. case KEYCTL_INVALIDATE:
  284. case KEYCTL_ASSUME_AUTHORITY:
  285. print_keyring_serial_number(arg2);
  286. break;
  287. case KEYCTL_LINK:
  288. case KEYCTL_UNLINK:
  289. keyctl_handle_key_key(tcp, arg2, arg3);
  290. break;
  291. case KEYCTL_DESCRIBE:
  292. case KEYCTL_READ:
  293. case KEYCTL_GET_SECURITY:
  294. keyctl_read_key(tcp, arg2, arg3, arg4, cmd != KEYCTL_READ);
  295. return 0;
  296. case KEYCTL_SEARCH:
  297. keyctl_keyring_search(tcp, arg2, arg3, arg4, arg5);
  298. break;
  299. case KEYCTL_CHOWN:
  300. keyctl_chown_key(tcp, arg2, arg3, arg4);
  301. break;
  302. case KEYCTL_SETPERM:
  303. keyctl_setperm_key(tcp, arg2, arg3);
  304. break;
  305. case KEYCTL_INSTANTIATE:
  306. keyctl_instantiate_key(tcp, arg2, arg3, arg4, arg5);
  307. break;
  308. case KEYCTL_NEGATE:
  309. keyctl_negate_key(tcp, arg2, arg3, arg4);
  310. break;
  311. case KEYCTL_SET_REQKEY_KEYRING:
  312. printxvals_ex((int) arg2, "KEY_REQKEY_DEFL_???",
  313. XLAT_STYLE_FMT_D, key_reqkeys, NULL);
  314. break;
  315. case KEYCTL_SET_TIMEOUT:
  316. keyctl_set_timeout(tcp, arg2, arg3);
  317. break;
  318. case KEYCTL_SESSION_TO_PARENT:
  319. break;
  320. case KEYCTL_REJECT:
  321. keyctl_reject_key(tcp, arg2, arg3, arg4, arg5);
  322. break;
  323. case KEYCTL_INSTANTIATE_IOV:
  324. keyctl_instantiate_key_iov(tcp, arg2, arg3, arg4, arg5);
  325. break;
  326. case KEYCTL_GET_PERSISTENT:
  327. keyctl_get_persistent(tcp, arg2, arg3);
  328. break;
  329. case KEYCTL_DH_COMPUTE:
  330. keyctl_dh_compute(tcp, arg2, arg3, arg4, arg5);
  331. return 0;
  332. case KEYCTL_RESTRICT_KEYRING:
  333. keyctl_restrict_keyring(tcp, arg2, arg3, arg4);
  334. break;
  335. default:
  336. tprintf("%#" PRI_klx ", %#" PRI_klx
  337. ", %#" PRI_klx ", %#" PRI_klx,
  338. arg2, arg3, arg4, arg5);
  339. break;
  340. }
  341. return RVAL_DECODED;
  342. }