xfrm_compat.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * XFRM compat layer
  4. * Author: Dmitry Safonov <[email protected]>
  5. * Based on code and translator idea by: Florian Westphal <[email protected]>
  6. */
  7. #include <linux/compat.h>
  8. #include <linux/nospec.h>
  9. #include <linux/xfrm.h>
  10. #include <net/xfrm.h>
  11. struct compat_xfrm_lifetime_cfg {
  12. compat_u64 soft_byte_limit, hard_byte_limit;
  13. compat_u64 soft_packet_limit, hard_packet_limit;
  14. compat_u64 soft_add_expires_seconds, hard_add_expires_seconds;
  15. compat_u64 soft_use_expires_seconds, hard_use_expires_seconds;
  16. }; /* same size on 32bit, but only 4 byte alignment required */
  17. struct compat_xfrm_lifetime_cur {
  18. compat_u64 bytes, packets, add_time, use_time;
  19. }; /* same size on 32bit, but only 4 byte alignment required */
  20. struct compat_xfrm_userpolicy_info {
  21. struct xfrm_selector sel;
  22. struct compat_xfrm_lifetime_cfg lft;
  23. struct compat_xfrm_lifetime_cur curlft;
  24. __u32 priority, index;
  25. u8 dir, action, flags, share;
  26. /* 4 bytes additional padding on 64bit */
  27. };
  28. struct compat_xfrm_usersa_info {
  29. struct xfrm_selector sel;
  30. struct xfrm_id id;
  31. xfrm_address_t saddr;
  32. struct compat_xfrm_lifetime_cfg lft;
  33. struct compat_xfrm_lifetime_cur curlft;
  34. struct xfrm_stats stats;
  35. __u32 seq, reqid;
  36. u16 family;
  37. u8 mode, replay_window, flags;
  38. /* 4 bytes additional padding on 64bit */
  39. };
  40. struct compat_xfrm_user_acquire {
  41. struct xfrm_id id;
  42. xfrm_address_t saddr;
  43. struct xfrm_selector sel;
  44. struct compat_xfrm_userpolicy_info policy;
  45. /* 4 bytes additional padding on 64bit */
  46. __u32 aalgos, ealgos, calgos, seq;
  47. };
  48. struct compat_xfrm_userspi_info {
  49. struct compat_xfrm_usersa_info info;
  50. /* 4 bytes additional padding on 64bit */
  51. __u32 min, max;
  52. };
  53. struct compat_xfrm_user_expire {
  54. struct compat_xfrm_usersa_info state;
  55. /* 8 bytes additional padding on 64bit */
  56. u8 hard;
  57. };
  58. struct compat_xfrm_user_polexpire {
  59. struct compat_xfrm_userpolicy_info pol;
  60. /* 8 bytes additional padding on 64bit */
  61. u8 hard;
  62. };
  63. #define XMSGSIZE(type) sizeof(struct type)
  64. static const int compat_msg_min[XFRM_NR_MSGTYPES] = {
  65. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_usersa_info),
  66. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  67. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  68. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userpolicy_info),
  69. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  70. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  71. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userspi_info),
  72. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_acquire),
  73. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_expire),
  74. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_userpolicy_info),
  75. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_usersa_info),
  76. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(compat_xfrm_user_polexpire),
  77. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  78. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  79. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  80. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  81. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  82. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  83. [XFRM_MSG_NEWSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  84. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  85. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  86. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  87. [XFRM_MSG_MAPPING - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_mapping)
  88. };
  89. static const struct nla_policy compat_policy[XFRMA_MAX+1] = {
  90. [XFRMA_SA] = { .len = XMSGSIZE(compat_xfrm_usersa_info)},
  91. [XFRMA_POLICY] = { .len = XMSGSIZE(compat_xfrm_userpolicy_info)},
  92. [XFRMA_LASTUSED] = { .type = NLA_U64},
  93. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  94. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  95. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  96. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  97. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  98. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  99. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  100. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
  101. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  102. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  103. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  104. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  105. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  106. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  107. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  108. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  109. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  110. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  111. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  112. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  113. [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
  114. [XFRMA_PROTO] = { .type = NLA_U8 },
  115. [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
  116. [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
  117. [XFRMA_SET_MARK] = { .type = NLA_U32 },
  118. [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
  119. [XFRMA_IF_ID] = { .type = NLA_U32 },
  120. [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
  121. };
  122. static struct nlmsghdr *xfrm_nlmsg_put_compat(struct sk_buff *skb,
  123. const struct nlmsghdr *nlh_src, u16 type)
  124. {
  125. int payload = compat_msg_min[type];
  126. int src_len = xfrm_msg_min[type];
  127. struct nlmsghdr *nlh_dst;
  128. /* Compat messages are shorter or equal to native (+padding) */
  129. if (WARN_ON_ONCE(src_len < payload))
  130. return ERR_PTR(-EMSGSIZE);
  131. nlh_dst = nlmsg_put(skb, nlh_src->nlmsg_pid, nlh_src->nlmsg_seq,
  132. nlh_src->nlmsg_type, payload, nlh_src->nlmsg_flags);
  133. if (!nlh_dst)
  134. return ERR_PTR(-EMSGSIZE);
  135. memset(nlmsg_data(nlh_dst), 0, payload);
  136. switch (nlh_src->nlmsg_type) {
  137. /* Compat message has the same layout as native */
  138. case XFRM_MSG_DELSA:
  139. case XFRM_MSG_DELPOLICY:
  140. case XFRM_MSG_FLUSHSA:
  141. case XFRM_MSG_FLUSHPOLICY:
  142. case XFRM_MSG_NEWAE:
  143. case XFRM_MSG_REPORT:
  144. case XFRM_MSG_MIGRATE:
  145. case XFRM_MSG_NEWSADINFO:
  146. case XFRM_MSG_NEWSPDINFO:
  147. case XFRM_MSG_MAPPING:
  148. WARN_ON_ONCE(src_len != payload);
  149. memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), src_len);
  150. break;
  151. /* 4 byte alignment for trailing u64 on native, but not on compat */
  152. case XFRM_MSG_NEWSA:
  153. case XFRM_MSG_NEWPOLICY:
  154. case XFRM_MSG_UPDSA:
  155. case XFRM_MSG_UPDPOLICY:
  156. WARN_ON_ONCE(src_len != payload + 4);
  157. memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), payload);
  158. break;
  159. case XFRM_MSG_EXPIRE: {
  160. const struct xfrm_user_expire *src_ue = nlmsg_data(nlh_src);
  161. struct compat_xfrm_user_expire *dst_ue = nlmsg_data(nlh_dst);
  162. /* compat_xfrm_user_expire has 4-byte smaller state */
  163. memcpy(dst_ue, src_ue, sizeof(dst_ue->state));
  164. dst_ue->hard = src_ue->hard;
  165. break;
  166. }
  167. case XFRM_MSG_ACQUIRE: {
  168. const struct xfrm_user_acquire *src_ua = nlmsg_data(nlh_src);
  169. struct compat_xfrm_user_acquire *dst_ua = nlmsg_data(nlh_dst);
  170. memcpy(dst_ua, src_ua, offsetof(struct compat_xfrm_user_acquire, aalgos));
  171. dst_ua->aalgos = src_ua->aalgos;
  172. dst_ua->ealgos = src_ua->ealgos;
  173. dst_ua->calgos = src_ua->calgos;
  174. dst_ua->seq = src_ua->seq;
  175. break;
  176. }
  177. case XFRM_MSG_POLEXPIRE: {
  178. const struct xfrm_user_polexpire *src_upe = nlmsg_data(nlh_src);
  179. struct compat_xfrm_user_polexpire *dst_upe = nlmsg_data(nlh_dst);
  180. /* compat_xfrm_user_polexpire has 4-byte smaller state */
  181. memcpy(dst_upe, src_upe, sizeof(dst_upe->pol));
  182. dst_upe->hard = src_upe->hard;
  183. break;
  184. }
  185. case XFRM_MSG_ALLOCSPI: {
  186. const struct xfrm_userspi_info *src_usi = nlmsg_data(nlh_src);
  187. struct compat_xfrm_userspi_info *dst_usi = nlmsg_data(nlh_dst);
  188. /* compat_xfrm_user_polexpire has 4-byte smaller state */
  189. memcpy(dst_usi, src_usi, sizeof(src_usi->info));
  190. dst_usi->min = src_usi->min;
  191. dst_usi->max = src_usi->max;
  192. break;
  193. }
  194. /* Not being sent by kernel */
  195. case XFRM_MSG_GETSA:
  196. case XFRM_MSG_GETPOLICY:
  197. case XFRM_MSG_GETAE:
  198. case XFRM_MSG_GETSADINFO:
  199. case XFRM_MSG_GETSPDINFO:
  200. default:
  201. pr_warn_once("unsupported nlmsg_type %d\n", nlh_src->nlmsg_type);
  202. return ERR_PTR(-EOPNOTSUPP);
  203. }
  204. return nlh_dst;
  205. }
  206. static int xfrm_nla_cpy(struct sk_buff *dst, const struct nlattr *src, int len)
  207. {
  208. return nla_put(dst, src->nla_type, len, nla_data(src));
  209. }
  210. static int xfrm_xlate64_attr(struct sk_buff *dst, const struct nlattr *src)
  211. {
  212. switch (src->nla_type) {
  213. case XFRMA_PAD:
  214. /* Ignore */
  215. return 0;
  216. case XFRMA_UNSPEC:
  217. case XFRMA_ALG_AUTH:
  218. case XFRMA_ALG_CRYPT:
  219. case XFRMA_ALG_COMP:
  220. case XFRMA_ENCAP:
  221. case XFRMA_TMPL:
  222. return xfrm_nla_cpy(dst, src, nla_len(src));
  223. case XFRMA_SA:
  224. return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_usersa_info));
  225. case XFRMA_POLICY:
  226. return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_userpolicy_info));
  227. case XFRMA_SEC_CTX:
  228. return xfrm_nla_cpy(dst, src, nla_len(src));
  229. case XFRMA_LTIME_VAL:
  230. return nla_put_64bit(dst, src->nla_type, nla_len(src),
  231. nla_data(src), XFRMA_PAD);
  232. case XFRMA_REPLAY_VAL:
  233. case XFRMA_REPLAY_THRESH:
  234. case XFRMA_ETIMER_THRESH:
  235. case XFRMA_SRCADDR:
  236. case XFRMA_COADDR:
  237. return xfrm_nla_cpy(dst, src, nla_len(src));
  238. case XFRMA_LASTUSED:
  239. return nla_put_64bit(dst, src->nla_type, nla_len(src),
  240. nla_data(src), XFRMA_PAD);
  241. case XFRMA_POLICY_TYPE:
  242. case XFRMA_MIGRATE:
  243. case XFRMA_ALG_AEAD:
  244. case XFRMA_KMADDRESS:
  245. case XFRMA_ALG_AUTH_TRUNC:
  246. case XFRMA_MARK:
  247. case XFRMA_TFCPAD:
  248. case XFRMA_REPLAY_ESN_VAL:
  249. case XFRMA_SA_EXTRA_FLAGS:
  250. case XFRMA_PROTO:
  251. case XFRMA_ADDRESS_FILTER:
  252. case XFRMA_OFFLOAD_DEV:
  253. case XFRMA_SET_MARK:
  254. case XFRMA_SET_MARK_MASK:
  255. case XFRMA_IF_ID:
  256. case XFRMA_MTIMER_THRESH:
  257. return xfrm_nla_cpy(dst, src, nla_len(src));
  258. default:
  259. BUILD_BUG_ON(XFRMA_MAX != XFRMA_MTIMER_THRESH);
  260. pr_warn_once("unsupported nla_type %d\n", src->nla_type);
  261. return -EOPNOTSUPP;
  262. }
  263. }
  264. /* Take kernel-built (64bit layout) and create 32bit layout for userspace */
  265. static int xfrm_xlate64(struct sk_buff *dst, const struct nlmsghdr *nlh_src)
  266. {
  267. u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE;
  268. const struct nlattr *nla, *attrs;
  269. struct nlmsghdr *nlh_dst;
  270. int len, remaining;
  271. nlh_dst = xfrm_nlmsg_put_compat(dst, nlh_src, type);
  272. if (IS_ERR(nlh_dst))
  273. return PTR_ERR(nlh_dst);
  274. attrs = nlmsg_attrdata(nlh_src, xfrm_msg_min[type]);
  275. len = nlmsg_attrlen(nlh_src, xfrm_msg_min[type]);
  276. nla_for_each_attr(nla, attrs, len, remaining) {
  277. int err;
  278. switch (nlh_src->nlmsg_type) {
  279. case XFRM_MSG_NEWSPDINFO:
  280. err = xfrm_nla_cpy(dst, nla, nla_len(nla));
  281. break;
  282. default:
  283. err = xfrm_xlate64_attr(dst, nla);
  284. break;
  285. }
  286. if (err)
  287. return err;
  288. }
  289. nlmsg_end(dst, nlh_dst);
  290. return 0;
  291. }
  292. static int xfrm_alloc_compat(struct sk_buff *skb, const struct nlmsghdr *nlh_src)
  293. {
  294. u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE;
  295. struct sk_buff *new = NULL;
  296. int err;
  297. if (type >= ARRAY_SIZE(xfrm_msg_min)) {
  298. pr_warn_once("unsupported nlmsg_type %d\n", nlh_src->nlmsg_type);
  299. return -EOPNOTSUPP;
  300. }
  301. if (skb_shinfo(skb)->frag_list == NULL) {
  302. new = alloc_skb(skb->len + skb_tailroom(skb), GFP_ATOMIC);
  303. if (!new)
  304. return -ENOMEM;
  305. skb_shinfo(skb)->frag_list = new;
  306. }
  307. err = xfrm_xlate64(skb_shinfo(skb)->frag_list, nlh_src);
  308. if (err) {
  309. if (new) {
  310. kfree_skb(new);
  311. skb_shinfo(skb)->frag_list = NULL;
  312. }
  313. return err;
  314. }
  315. return 0;
  316. }
  317. /* Calculates len of translated 64-bit message. */
  318. static size_t xfrm_user_rcv_calculate_len64(const struct nlmsghdr *src,
  319. struct nlattr *attrs[XFRMA_MAX + 1],
  320. int maxtype)
  321. {
  322. size_t len = nlmsg_len(src);
  323. switch (src->nlmsg_type) {
  324. case XFRM_MSG_NEWSA:
  325. case XFRM_MSG_NEWPOLICY:
  326. case XFRM_MSG_ALLOCSPI:
  327. case XFRM_MSG_ACQUIRE:
  328. case XFRM_MSG_UPDPOLICY:
  329. case XFRM_MSG_UPDSA:
  330. len += 4;
  331. break;
  332. case XFRM_MSG_EXPIRE:
  333. case XFRM_MSG_POLEXPIRE:
  334. len += 8;
  335. break;
  336. case XFRM_MSG_NEWSPDINFO:
  337. /* attirbutes are xfrm_spdattr_type_t, not xfrm_attr_type_t */
  338. return len;
  339. default:
  340. break;
  341. }
  342. /* Unexpected for anything, but XFRM_MSG_NEWSPDINFO, please
  343. * correct both 64=>32-bit and 32=>64-bit translators to copy
  344. * new attributes.
  345. */
  346. if (WARN_ON_ONCE(maxtype))
  347. return len;
  348. if (attrs[XFRMA_SA])
  349. len += 4;
  350. if (attrs[XFRMA_POLICY])
  351. len += 4;
  352. /* XXX: some attrs may need to be realigned
  353. * if !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  354. */
  355. return len;
  356. }
  357. static int xfrm_attr_cpy32(void *dst, size_t *pos, const struct nlattr *src,
  358. size_t size, int copy_len, int payload)
  359. {
  360. struct nlmsghdr *nlmsg = dst;
  361. struct nlattr *nla;
  362. /* xfrm_user_rcv_msg_compat() relies on fact that 32-bit messages
  363. * have the same len or shorted than 64-bit ones.
  364. * 32-bit translation that is bigger than 64-bit original is unexpected.
  365. */
  366. if (WARN_ON_ONCE(copy_len > payload))
  367. copy_len = payload;
  368. if (size - *pos < nla_attr_size(payload))
  369. return -ENOBUFS;
  370. nla = dst + *pos;
  371. memcpy(nla, src, nla_attr_size(copy_len));
  372. nla->nla_len = nla_attr_size(payload);
  373. *pos += nla_attr_size(copy_len);
  374. nlmsg->nlmsg_len += nla->nla_len;
  375. memset(dst + *pos, 0, payload - copy_len);
  376. *pos += payload - copy_len;
  377. return 0;
  378. }
  379. static int xfrm_xlate32_attr(void *dst, const struct nlattr *nla,
  380. size_t *pos, size_t size,
  381. struct netlink_ext_ack *extack)
  382. {
  383. int type = nla_type(nla);
  384. u16 pol_len32, pol_len64;
  385. int err;
  386. if (type > XFRMA_MAX) {
  387. BUILD_BUG_ON(XFRMA_MAX != XFRMA_MTIMER_THRESH);
  388. NL_SET_ERR_MSG(extack, "Bad attribute");
  389. return -EOPNOTSUPP;
  390. }
  391. type = array_index_nospec(type, XFRMA_MAX + 1);
  392. if (nla_len(nla) < compat_policy[type].len) {
  393. NL_SET_ERR_MSG(extack, "Attribute bad length");
  394. return -EOPNOTSUPP;
  395. }
  396. pol_len32 = compat_policy[type].len;
  397. pol_len64 = xfrma_policy[type].len;
  398. /* XFRMA_SA and XFRMA_POLICY - need to know how-to translate */
  399. if (pol_len32 != pol_len64) {
  400. if (nla_len(nla) != compat_policy[type].len) {
  401. NL_SET_ERR_MSG(extack, "Attribute bad length");
  402. return -EOPNOTSUPP;
  403. }
  404. err = xfrm_attr_cpy32(dst, pos, nla, size, pol_len32, pol_len64);
  405. if (err)
  406. return err;
  407. }
  408. return xfrm_attr_cpy32(dst, pos, nla, size, nla_len(nla), nla_len(nla));
  409. }
  410. static int xfrm_xlate32(struct nlmsghdr *dst, const struct nlmsghdr *src,
  411. struct nlattr *attrs[XFRMA_MAX+1],
  412. size_t size, u8 type, int maxtype,
  413. struct netlink_ext_ack *extack)
  414. {
  415. size_t pos;
  416. int i;
  417. memcpy(dst, src, NLMSG_HDRLEN);
  418. dst->nlmsg_len = NLMSG_HDRLEN + xfrm_msg_min[type];
  419. memset(nlmsg_data(dst), 0, xfrm_msg_min[type]);
  420. switch (src->nlmsg_type) {
  421. /* Compat message has the same layout as native */
  422. case XFRM_MSG_DELSA:
  423. case XFRM_MSG_GETSA:
  424. case XFRM_MSG_DELPOLICY:
  425. case XFRM_MSG_GETPOLICY:
  426. case XFRM_MSG_FLUSHSA:
  427. case XFRM_MSG_FLUSHPOLICY:
  428. case XFRM_MSG_NEWAE:
  429. case XFRM_MSG_GETAE:
  430. case XFRM_MSG_REPORT:
  431. case XFRM_MSG_MIGRATE:
  432. case XFRM_MSG_NEWSADINFO:
  433. case XFRM_MSG_GETSADINFO:
  434. case XFRM_MSG_NEWSPDINFO:
  435. case XFRM_MSG_GETSPDINFO:
  436. case XFRM_MSG_MAPPING:
  437. memcpy(nlmsg_data(dst), nlmsg_data(src), compat_msg_min[type]);
  438. break;
  439. /* 4 byte alignment for trailing u64 on native, but not on compat */
  440. case XFRM_MSG_NEWSA:
  441. case XFRM_MSG_NEWPOLICY:
  442. case XFRM_MSG_UPDSA:
  443. case XFRM_MSG_UPDPOLICY:
  444. memcpy(nlmsg_data(dst), nlmsg_data(src), compat_msg_min[type]);
  445. break;
  446. case XFRM_MSG_EXPIRE: {
  447. const struct compat_xfrm_user_expire *src_ue = nlmsg_data(src);
  448. struct xfrm_user_expire *dst_ue = nlmsg_data(dst);
  449. /* compat_xfrm_user_expire has 4-byte smaller state */
  450. memcpy(dst_ue, src_ue, sizeof(src_ue->state));
  451. dst_ue->hard = src_ue->hard;
  452. break;
  453. }
  454. case XFRM_MSG_ACQUIRE: {
  455. const struct compat_xfrm_user_acquire *src_ua = nlmsg_data(src);
  456. struct xfrm_user_acquire *dst_ua = nlmsg_data(dst);
  457. memcpy(dst_ua, src_ua, offsetof(struct compat_xfrm_user_acquire, aalgos));
  458. dst_ua->aalgos = src_ua->aalgos;
  459. dst_ua->ealgos = src_ua->ealgos;
  460. dst_ua->calgos = src_ua->calgos;
  461. dst_ua->seq = src_ua->seq;
  462. break;
  463. }
  464. case XFRM_MSG_POLEXPIRE: {
  465. const struct compat_xfrm_user_polexpire *src_upe = nlmsg_data(src);
  466. struct xfrm_user_polexpire *dst_upe = nlmsg_data(dst);
  467. /* compat_xfrm_user_polexpire has 4-byte smaller state */
  468. memcpy(dst_upe, src_upe, sizeof(src_upe->pol));
  469. dst_upe->hard = src_upe->hard;
  470. break;
  471. }
  472. case XFRM_MSG_ALLOCSPI: {
  473. const struct compat_xfrm_userspi_info *src_usi = nlmsg_data(src);
  474. struct xfrm_userspi_info *dst_usi = nlmsg_data(dst);
  475. /* compat_xfrm_user_polexpire has 4-byte smaller state */
  476. memcpy(dst_usi, src_usi, sizeof(src_usi->info));
  477. dst_usi->min = src_usi->min;
  478. dst_usi->max = src_usi->max;
  479. break;
  480. }
  481. default:
  482. NL_SET_ERR_MSG(extack, "Unsupported message type");
  483. return -EOPNOTSUPP;
  484. }
  485. pos = dst->nlmsg_len;
  486. if (maxtype) {
  487. /* attirbutes are xfrm_spdattr_type_t, not xfrm_attr_type_t */
  488. WARN_ON_ONCE(src->nlmsg_type != XFRM_MSG_NEWSPDINFO);
  489. for (i = 1; i <= maxtype; i++) {
  490. int err;
  491. if (!attrs[i])
  492. continue;
  493. /* just copy - no need for translation */
  494. err = xfrm_attr_cpy32(dst, &pos, attrs[i], size,
  495. nla_len(attrs[i]), nla_len(attrs[i]));
  496. if (err)
  497. return err;
  498. }
  499. return 0;
  500. }
  501. for (i = 1; i < XFRMA_MAX + 1; i++) {
  502. int err;
  503. if (i == XFRMA_PAD)
  504. continue;
  505. if (!attrs[i])
  506. continue;
  507. err = xfrm_xlate32_attr(dst, attrs[i], &pos, size, extack);
  508. if (err)
  509. return err;
  510. }
  511. return 0;
  512. }
  513. static struct nlmsghdr *xfrm_user_rcv_msg_compat(const struct nlmsghdr *h32,
  514. int maxtype, const struct nla_policy *policy,
  515. struct netlink_ext_ack *extack)
  516. {
  517. /* netlink_rcv_skb() checks if a message has full (struct nlmsghdr) */
  518. u16 type = h32->nlmsg_type - XFRM_MSG_BASE;
  519. struct nlattr *attrs[XFRMA_MAX+1];
  520. struct nlmsghdr *h64;
  521. size_t len;
  522. int err;
  523. BUILD_BUG_ON(ARRAY_SIZE(xfrm_msg_min) != ARRAY_SIZE(compat_msg_min));
  524. if (type >= ARRAY_SIZE(xfrm_msg_min))
  525. return ERR_PTR(-EINVAL);
  526. /* Don't call parse: the message might have only nlmsg header */
  527. if ((h32->nlmsg_type == XFRM_MSG_GETSA ||
  528. h32->nlmsg_type == XFRM_MSG_GETPOLICY) &&
  529. (h32->nlmsg_flags & NLM_F_DUMP))
  530. return NULL;
  531. err = nlmsg_parse_deprecated(h32, compat_msg_min[type], attrs,
  532. maxtype ? : XFRMA_MAX, policy ? : compat_policy, extack);
  533. if (err < 0)
  534. return ERR_PTR(err);
  535. len = xfrm_user_rcv_calculate_len64(h32, attrs, maxtype);
  536. /* The message doesn't need translation */
  537. if (len == nlmsg_len(h32))
  538. return NULL;
  539. len += NLMSG_HDRLEN;
  540. h64 = kvmalloc(len, GFP_KERNEL);
  541. if (!h64)
  542. return ERR_PTR(-ENOMEM);
  543. err = xfrm_xlate32(h64, h32, attrs, len, type, maxtype, extack);
  544. if (err < 0) {
  545. kvfree(h64);
  546. return ERR_PTR(err);
  547. }
  548. return h64;
  549. }
  550. static int xfrm_user_policy_compat(u8 **pdata32, int optlen)
  551. {
  552. struct compat_xfrm_userpolicy_info *p = (void *)*pdata32;
  553. u8 *src_templates, *dst_templates;
  554. u8 *data64;
  555. if (optlen < sizeof(*p))
  556. return -EINVAL;
  557. data64 = kmalloc_track_caller(optlen + 4, GFP_USER | __GFP_NOWARN);
  558. if (!data64)
  559. return -ENOMEM;
  560. memcpy(data64, *pdata32, sizeof(*p));
  561. memset(data64 + sizeof(*p), 0, 4);
  562. src_templates = *pdata32 + sizeof(*p);
  563. dst_templates = data64 + sizeof(*p) + 4;
  564. memcpy(dst_templates, src_templates, optlen - sizeof(*p));
  565. kfree(*pdata32);
  566. *pdata32 = data64;
  567. return 0;
  568. }
  569. static struct xfrm_translator xfrm_translator = {
  570. .owner = THIS_MODULE,
  571. .alloc_compat = xfrm_alloc_compat,
  572. .rcv_msg_compat = xfrm_user_rcv_msg_compat,
  573. .xlate_user_policy_sockptr = xfrm_user_policy_compat,
  574. };
  575. static int __init xfrm_compat_init(void)
  576. {
  577. return xfrm_register_translator(&xfrm_translator);
  578. }
  579. static void __exit xfrm_compat_exit(void)
  580. {
  581. xfrm_unregister_translator(&xfrm_translator);
  582. }
  583. module_init(xfrm_compat_init);
  584. module_exit(xfrm_compat_exit);
  585. MODULE_LICENSE("GPL");
  586. MODULE_AUTHOR("Dmitry Safonov");
  587. MODULE_DESCRIPTION("XFRM 32-bit compatibility layer");