xfrm_compat.c 20 KB

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