xfrm: Add compat layer
Subsystems (4)
v2 cover xfrm: Add compat layer
Changes since v1:
- reworked patches set to use translator
- separated the compat layer into xfrm_compat.c, compiled under XFRM_USER_COMPAT config
- 32-bit messages now being sent in frag_list (like wext-core does)
- instead of __packed add compat_u64 members in compat structures
- selftest reworked to kselftest lib API
- added netlink dump testing to the selftest
XFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit 19d7df69fdb2 ("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit 74005991b78a ("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").
This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by xfrm_user, which resulted in over-complicated code [1].
Florian Westphal provided the idea of translator and some draft patches in the discussion. In these patches, his idea is reused and some of his initial code is also present.
There were a couple of attempts to solve xfrm compat problem:
https://lkml.org/lkml/2017/1/20/733
https://patchwork.ozlabs.org/patch/44600/
http://netdev.vger.kernel.narkive.com/2Gesykj6/patch-net-next-xfrm-correctly-parse-netlink-msg-from-32bits-ip-command-on-64bits-host
All the discussions end in the conclusion that xfrm should have a full compatible layer to correctly work with 32-bit applications on 64-bit kernels:
https://lkml.org/lkml/2017/1/23/413
https://patchwork.ozlabs.org/patch/433279/
In some recent lkml discussion, Linus said that it's worth to fix this problem and not giving people an excuse to stay on 32-bit kernel:
https://lkml.org/lkml/2018/2/13/752
There is also an selftest for ipsec tunnels.
It doesn't depend on any library and compat version can be easy build with: make CFLAGS=-m32 net/ipsec
Patches as a .git branch:
https://github.com/0x7f454c46/linux/tree/xfrm-compat-v2
[1]: https://lkml.kernel.org/r/[email protected]
Dmitry Safonov (6):
xfrm/compat: Add 64=>32-bit messages translator xfrm/compat: Attach xfrm dumps to 64=>32 bit translator netlink/compat: Append NLMSG_DONE/extack to frag_list xfrm/compat: Add 32=>64-bit messages translator xfrm/compat: Translate 32-bit user_policy from sockptr selftest/net/xfrm: Add test for ipsec tunnel
MAINTAINERS | 1 + include/net/xfrm.h | 32 + net/netlink/af_netlink.c | 48 +- net/xfrm/Kconfig | 11 + net/xfrm/Makefile | 1 + net/xfrm/xfrm_compat.c | 609 +++++++ net/xfrm/xfrm_state.c | 11 +- net/xfrm/xfrm_user.c | 79 +- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/ipsec.c | 2195 ++++++++++++++++++++++++ 11 files changed, 2953 insertions(+), 36 deletions(-) create mode 100644 net/xfrm/xfrm_compat.c create mode 100644 tools/testing/selftests/net/ipsec.c
Signature · 1 line
2.27.0
PATCH v2 1/6 xfrm/compat: Add 64=>32-bit messages translator
XFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit 19d7df69fdb2 ("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit 74005991b78a ("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").
This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by xfrm_user, which resulted in over-complicated code [1].
Florian Westphal provided the idea of translator and some draft patches in the discussion. In these patches, his idea is reused and some of his initial code is also present.
Provide the kernel-to-user translator under XFRM_USER_COMPAT, that creates for 64-bit xfrm-user message a 32-bit translation and puts it in skb's frag_list. net/compat.c layer provides MSG_CMSG_COMPAT to decide if the message should be taken from skb or frag_list. (used by wext-core which has also an ABI difference)
Kernel sends 64-bit xfrm messages to the userspace for:
- multicast (monitor events)
- netlink dumps
Wire up the translator to xfrm_nlmsg_multicast().
[1]: https://lkml.kernel.org/r/[email protected]
--- include/net/xfrm.h | 10 ++ net/xfrm/Kconfig | 10 ++ net/xfrm/Makefile | 1 + net/xfrm/xfrm_compat.c | 302 +++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_user.c | 9 +- 5 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 net/xfrm/xfrm_compat.c
include/net/xfrm.h +10 -0
@@ -2000,6 +2000,16 @@ static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,return 0 ; } +#ifdef CONFIG_XFRM_USER_COMPAT +extern int xfrm_alloc_compat (struct sk_buff *skb); +extern const int xfrm_msg_min[XFRM_NR_MSGTYPES ]; +#else +static inline int xfrm_alloc_compat (struct sk_buff *skb) +{ +return 0 ; +} +#endif +#if IS_ENABLED (CONFIG_IPV6 )static inline bool xfrm6_local_dontfrag (const struct sock *sk) {
net/xfrm/Kconfig +10 -0
@@ -28,6 +28,16 @@ config XFRM_USER If unsure, say Y. +config XFRM_USER_COMPAT + tristate "Compatible ABI support" + depends on XFRM_USER && COMPAT_FOR_U64_ALIGNMENT + select WANT_COMPAT_NETLINK_MESSAGES + help + Transformation(XFRM) user configuration interface like IPsec + used by compatible Linux applications. + + If unsure, say N. + config XFRM_INTERFACE tristate "Transformation virtual interface" depends on XFRM && IPV6
net/xfrm/Makefile +1 -0
@@ -9,6 +9,7 @@ obj-$(CONFIG_XFRM) := xfrm_policy.o xfrm_state.o xfrm_hash.o \ obj-$(CONFIG_XFRM_STATISTICS) += xfrm_proc.o obj-$(CONFIG_XFRM_ALGO) += xfrm_algo.o obj-$(CONFIG_XFRM_USER) += xfrm_user.o +obj-$(CONFIG_XFRM_USER_COMPAT) += xfrm_compat.o obj-$(CONFIG_XFRM_IPCOMP) += xfrm_ipcomp.o obj-$(CONFIG_XFRM_INTERFACE) += xfrm_interface.o obj-$(CONFIG_XFRM_ESPINTCP) += espintcp.o
net/xfrm/xfrm_compat.c new file +302 -0
302 lines not on this page · raw message
net/xfrm/xfrm_user.c +8 -1
@@ -1083,12 +1083,19 @@ static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, u32 pid, unsigned int group) {struct sock *nlsk =rcu_dereference (net->xfrm.nlsk); +int err;if (!nlsk) {kfree_skb (skb);return -EPIPE ; } + err =xfrm_alloc_compat (skb); +if (err) { +kfree_skb (skb); +return err; + } +return nlmsg_multicast (nlsk, skb, pid, group,GFP_ATOMIC ); }
@@ -2533,7 +2540,7 @@ static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,#define XMSGSIZE (type) sizeof(struct type) -static const int xfrm_msg_min[XFRM_NR_MSGTYPES ] = { +const int xfrm_msg_min[XFRM_NR_MSGTYPES ] = { [XFRM_MSG_NEWSA -XFRM_MSG_BASE ] =XMSGSIZE (xfrm_usersa_info), [XFRM_MSG_DELSA -XFRM_MSG_BASE ] =XMSGSIZE (xfrm_usersa_id), [XFRM_MSG_GETSA -XFRM_MSG_BASE ] =XMSGSIZE (xfrm_usersa_id),
Signature · 1 line
2.27.0
PATCH v2 2/6 xfrm/compat: Attach xfrm dumps to 64=>32 bit translator
In reply to Dmitry SafonovXFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit 19d7df69fdb2 ("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit 74005991b78a ("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").
This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by xfrm_user, which resulted in over-complicated code [1].
Florian Westphal provided the idea of translator and some draft patches in the discussion. In these patches, his idea is reused and some of his initial code is also present.
Currently nlmsg_unicast() is used by functions that dump structures that can be different in size for compat tasks, see dump_one_state() and dump_one_policy().
The following nlmsg_unicast() users exist today in xfrm:
Function | Message can be different
| in size on compat
-------------------------------------------|------------------------------
xfrm_get_spdinfo() | N
xfrm_get_sadinfo() | N
xfrm_get_sa() | Y
xfrm_alloc_userspi() | Y
xfrm_get_policy() | Y
xfrm_get_ae() | NBesides, dump_one_state() and dump_one_policy() can be used by filtered netlink dump for XFRM_MSG_GETSA, XFRM_MSG_GETPOLICY.
Just as for xfrm multicast, allocate frag_list for compat skb journey down to recvmsg() which will give user the desired skb according to syscall bitness.
[1]: https://lkml.kernel.org/r/[email protected]
--- include/net/xfrm.h | 6 ++++++ net/xfrm/xfrm_compat.c | 10 ++++++++-- net/xfrm/xfrm_user.c | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-)
include/net/xfrm.h +6 -0
@@ -2002,12 +2002,18 @@ static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,#ifdef CONFIG_XFRM_USER_COMPAT extern int xfrm_alloc_compat (struct sk_buff *skb); +extern int __xfrm_alloc_compat (struct sk_buff *skb,const struct nlmsghdr *nlh);extern const int xfrm_msg_min[XFRM_NR_MSGTYPES ];#else static inline int xfrm_alloc_compat (struct sk_buff *skb) {return 0 ; } +static inline int __xfrm_alloc_compat (struct sk_buff *skb, +const struct nlmsghdr *nlh) +{ +return 0 ; +}#endif #if IS_ENABLED (CONFIG_IPV6 )
net/xfrm/xfrm_compat.c +8 -2
@@ -272,9 +272,8 @@ static int xfrm_xlate64(struct sk_buff *dst, const struct nlmsghdr *nlh_src)return 0 ; } -int xfrm_alloc_compat (struct sk_buff *skb) +int __xfrm_alloc_compat (struct sk_buff *skb,const struct nlmsghdr *nlh_src) { -const struct nlmsghdr *nlh_src =nlmsg_hdr (skb);u16 type = nlh_src->nlmsg_type -XFRM_MSG_BASE ;struct sk_buff *new =NULL ;int err;
@@ -300,3 +299,10 @@ int xfrm_alloc_compat(struct sk_buff *skb)return 0 ; } + +int xfrm_alloc_compat (struct sk_buff *skb) +{ +const struct nlmsghdr *nlh_src =nlmsg_hdr (skb); + +return __xfrm_alloc_compat (skb, nlh_src); +}
net/xfrm/xfrm_user.c +20 -0
@@ -992,6 +992,13 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)return err; }nlmsg_end (skb, nlh); + + err =__xfrm_alloc_compat (skb, nlh); +if (err) { +nlmsg_cancel (skb, nlh); +return err; + } +return 0 ; }
@@ -1365,6 +1372,12 @@ static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, goto out; } + err =xfrm_alloc_compat (skb); +if (err) { +kfree_skb (resp_skb); + goto out; + } + err =nlmsg_unicast (net->xfrm.nlsk, resp_skb,NETLINK_CB (skb).portid); out:
@@ -1795,6 +1808,13 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptrreturn err; }nlmsg_end (skb, nlh); + + err =__xfrm_alloc_compat (skb, nlh); +if (err) { +nlmsg_cancel (skb, nlh); +return err; + } +return 0 ; }
Signature · 1 line
2.27.0
PATCH v2 4/6 xfrm/compat: Add 32=>64-bit messages translator
In reply to Dmitry SafonovXFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit 19d7df69fdb2 ("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit 74005991b78a ("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").
This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by xfrm_user, which resulted in over-complicated code [1].
Florian Westphal provided the idea of translator and some draft patches in the discussion. Here his idea is reused and some of his initial code is also present.
Provide the user-to-kernel translator under XFRM_USER_COMPAT, that creates for 32-bit xfrm-user message a 64-bit translation. The translation is afterwards reused by xfrm_user code just as if userspace had sent 64-bit message.
[1]: https://lkml.kernel.org/r/[email protected]
--- include/net/xfrm.h | 11 ++ net/xfrm/Kconfig | 3 +- net/xfrm/xfrm_compat.c | 276 +++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_user.c | 50 +++++--- 4 files changed, 321 insertions(+), 19 deletions(-)
include/net/xfrm.h +11 -0
@@ -2000,10 +2000,15 @@ static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,return 0 ; } +extern const struct nla_policy xfrma_policy[XFRMA_MAX +1 ]; +#ifdef CONFIG_XFRM_USER_COMPAT extern int xfrm_alloc_compat (struct sk_buff *skb);extern int __xfrm_alloc_compat (struct sk_buff *skb,const struct nlmsghdr *nlh);extern const int xfrm_msg_min[XFRM_NR_MSGTYPES ]; +extern struct nlmsghdr *xfrm_user_rcv_msg_compat (const struct nlmsghdr *nlh, +int maxtype,const struct nla_policy *policy, +struct netlink_ext_ack *extack);#else static inline int xfrm_alloc_compat (struct sk_buff *skb) {
@@ -2014,6 +2019,12 @@ static inline int __xfrm_alloc_compat(struct sk_buff *skb, {return 0 ; } +static inline struct nlmsghdr *xfrm_user_rcv_msg_compat (const struct nlmsghdr *nlh, +int maxtype,const struct nla_policy *policy, +struct netlink_ext_ack *extack) +{ +return ERR_PTR (-EOPNOTSUPP ); +}#endif #if IS_ENABLED (CONFIG_IPV6 )
net/xfrm/Kconfig +2 -1
@@ -30,7 +30,8 @@ config XFRM_USER config XFRM_USER_COMPAT tristate "Compatible ABI support" - depends on XFRM_USER && COMPAT_FOR_U64_ALIGNMENT + depends on XFRM_USER && COMPAT_FOR_U64_ALIGNMENT && \ + HAVE_EFFICIENT_UNALIGNED_ACCESS select WANT_COMPAT_NETLINK_MESSAGES help Transformation(XFRM) user configuration interface like IPsec
net/xfrm/xfrm_compat.c +276 -0
291 lines not on this page · raw message
net/xfrm/xfrm_user.c +32 -18
89 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v2 5/6 xfrm/compat: Translate 32-bit user_policy from sockptr
In reply to Dmitry SafonovXFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit 19d7df69fdb2 ("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit 74005991b78a ("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").
This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by xfrm_user, which resulted in over-complicated code [1].
Florian Westphal provided the idea of translator and some draft patches in the discussion. In these patches, his idea is reused and some of his initial code is also present.
Provide compat_xfrm_userpolicy_info translation for xfrm setsocketopt(). Reallocate buffer and put the missing padding for 64-bit message.
[1]: https://lkml.kernel.org/r/[email protected]
--- include/net/xfrm.h | 5 +++++ net/xfrm/xfrm_compat.c | 25 +++++++++++++++++++++++++ net/xfrm/xfrm_state.c | 11 ++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-)
include/net/xfrm.h +5 -0
@@ -2009,6 +2009,7 @@ extern const int xfrm_msg_min[XFRM_NR_MSGTYPES];extern struct nlmsghdr *xfrm_user_rcv_msg_compat (const struct nlmsghdr *nlh,int maxtype,const struct nla_policy *policy,struct netlink_ext_ack *extack); +extern int xfrm_user_policy_compat (u8 **pdata32,int optlen);#else static inline int xfrm_alloc_compat (struct sk_buff *skb) {
@@ -2025,6 +2026,10 @@ static inline struct nlmsghdr *xfrm_user_rcv_msg_compat(const struct nlmsghdr *n {return ERR_PTR (-EOPNOTSUPP ); } +static inline int xfrm_user_policy_compat (u8 **pdata32,int optlen) +{ +return -EOPNOTSUPP ; +}#endif #if IS_ENABLED (CONFIG_IPV6 )
net/xfrm/xfrm_compat.c +25 -0
@@ -582,3 +582,28 @@ struct nlmsghdr *xfrm_user_rcv_msg_compat(const struct nlmsghdr *h32,return h64; } + +int xfrm_user_policy_compat (u8 **pdata32,int optlen) +{ +struct compat_xfrm_userpolicy_info *p = (void *)*pdata32; +u8 *src_templates, *dst_templates; +u8 *data64; + +if (optlen <sizeof (*p)) +return -EINVAL ; + + data64 =kmalloc_track_caller (optlen +4 ,GFP_USER | __GFP_NOWARN); +if (!data64) +return -ENOMEM ; + +memcpy (data64, *pdata32,sizeof (*p)); +memset (data64 +sizeof (*p),0 ,4 ); + + src_templates = *pdata32 +sizeof (*p); + dst_templates = data64 +sizeof (*p) +4 ; +memcpy (dst_templates, src_templates, optlen -sizeof (*p)); + +kfree (*pdata32); + *pdata32 = data64; +return 0 ; +}
net/xfrm/xfrm_state.c +8 -3
@@ -2271,9 +2271,6 @@ int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)struct xfrm_mgr *km;struct xfrm_policy *pol =NULL ; -if (in_compat_syscall ()) -return -EOPNOTSUPP ; -if (sockptr_is_null (optval) && !optlen) {xfrm_sk_policy_insert (sk,XFRM_POLICY_IN ,NULL );xfrm_sk_policy_insert (sk,XFRM_POLICY_OUT ,NULL );
@@ -2288,6 +2285,14 @@ int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)if (IS_ERR (data))return PTR_ERR (data); +if (in_compat_syscall ()) { + err =xfrm_user_policy_compat (&data, optlen); +if (err) { +kfree (data); +return err; + } + } + err = -EINVAL ;rcu_read_lock ();list_for_each_entry_rcu (km, &xfrm_km_list, list) {
Signature · 1 line
2.27.0
PATCH v2 6/6 selftest/net/xfrm: Add test for ipsec tunnel
In reply to Dmitry SafonovIt's an exhaustive testing for ipsec: covering all encryption/ authentication/compression algorithms. The tests are run in two network namespaces, connected by veth interfaces. To make exhaustive testing less time-consuming, the tests are run in parallel tasks, specified by parameter to the selftest.
As the patches set adds support for xfrm in compatible tasks, there are tests to check structures that differ in size between 64-bit and 32-bit applications.
The selftest doesn't use libnl so that it can be easily compiled as compatible application and don't require compatible .so.
Here is a diagram of the selftest:
---------------
| selftest |
| (parent) |
---------------
| |
| (pipe) |
----------
/ | | \
/------------- / \ -------------\
| /----- -----\ |
---------|----------|----------------|----------|---------
| --------- --------- --------- --------- |
| | child | | child | NS A | child | | child | |
| --------- --------- --------- --------- |
-------|------------|----------------|-------------|------
veth0 veth1 veth2 vethN
---------|------------|----------------|-------------|----------
| ------------ ------------ ------------ ------------ |
| | gr.child | | gr.child | NS B | gr.child | | gr.child | |
| ------------ ------------ ------------ ------------ |
----------------------------------------------------------------The parent sends the description of a test (xfrm parameters) to the child, the child and grand child setup a tunnel over veth interface and test it by sending udp packets.
--- MAINTAINERS | 1 + tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/ipsec.c | 2195 ++++++++++++++++++++++++ 4 files changed, 2198 insertions(+) create mode 100644 tools/testing/selftests/net/ipsec.c
MAINTAINERS +1 -0
@@ -12110,6 +12110,7 @@ F: net/ipv6/ipcomp6.c F: net/ipv6/xfrm* F: net/key/ F: net/xfrm/ +F: tools/testing/selftests/net/ipsec.c NETWORKING [IPv4/IPv6] M: "David S. Miller" <[email protected]>
tools/testing/selftests/net/.gitignore +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +ipsec msg_zerocopy socket psock_fanout
tools/testing/selftests/net/Makefile +1 -0
@@ -29,6 +29,7 @@ TEST_GEN_FILES += tcp_fastopen_backup_key TEST_GEN_FILES += fin_ack_lat TEST_GEN_FILES += reuseaddr_ports_exhausted TEST_GEN_FILES += hwtstamp_config rxtimestamp timestamping txtimestamp +TEST_GEN_FILES += ipsec TEST_GEN_PROGS = reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa TEST_GEN_PROGS += reuseport_dualstack reuseaddr_conflict tls
tools/testing/selftests/net/ipsec.c new file +2195 -0
2,195 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v2 3/6 netlink/compat: Append NLMSG_DONE/extack to frag_list
In reply to Dmitry SafonovModules those use netlink may supply a 2nd skb, (via frag_list) that contains an alternative data set meant for applications using 32bit compatibility mode.
In such a case, netlink_recvmsg will use this 2nd skb instead of the original one.
Without this patch, such compat applications will retrieve all netlink dump data, but will then get an unexpected EOF.
--- net/netlink/af_netlink.c | 48 ++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 14 deletions(-)
net/netlink/af_netlink.c +34 -14
@@ -2186,13 +2186,36 @@ EXPORT_SYMBOL(__nlmsg_put);* It would be better to create kernel thread. */ +static int netlink_dump_done (struct netlink_sock *nlk,struct sk_buff *skb, +struct netlink_callback *cb, +struct netlink_ext_ack *extack) +{ +struct nlmsghdr *nlh; + + nlh =nlmsg_put_answer (skb, cb,NLMSG_DONE ,sizeof (nlk->dump_done_errno), +NLM_F_MULTI | cb->answer_flags); +if (WARN_ON (!nlh)) +return -ENOBUFS ; + +nl_dump_check_consistent (cb, nlh); +memcpy (nlmsg_data (nlh), &nlk->dump_done_errno, +sizeof (nlk->dump_done_errno)); + +if (extack->_msg && nlk->flags &NETLINK_F_EXT_ACK ) { + nlh->nlmsg_flags |=NLM_F_ACK_TLVS ; +if (!nla_put_string (skb,NLMSGERR_ATTR_MSG , extack->_msg)) +nlmsg_end (skb, nlh); + } + +return 0 ; +} +static int netlink_dump (struct sock *sk) {struct netlink_sock *nlk =nlk_sk (sk);struct netlink_ext_ack extack = {};struct netlink_callback *cb;struct sk_buff *skb =NULL ; -struct nlmsghdr *nlh;struct module *module;int err = -ENOBUFS ;int alloc_min_size;
@@ -2258,22 +2281,19 @@ static int netlink_dump(struct sock *sk)return 0 ; } - nlh =nlmsg_put_answer (skb, cb,NLMSG_DONE , -sizeof (nlk->dump_done_errno), -NLM_F_MULTI | cb->answer_flags); -if (WARN_ON (!nlh)) +if (netlink_dump_done (nlk, skb, cb, &extack)) goto errout_skb; -nl_dump_check_consistent (cb, nlh); - -memcpy (nlmsg_data (nlh), &nlk->dump_done_errno, -sizeof (nlk->dump_done_errno)); - -if (extack._msg && nlk->flags &NETLINK_F_EXT_ACK ) { - nlh->nlmsg_flags |=NLM_F_ACK_TLVS ; -if (!nla_put_string (skb,NLMSGERR_ATTR_MSG , extack._msg)) -nlmsg_end (skb, nlh); +#ifdef CONFIG_COMPAT_NETLINK_MESSAGES +/* frag_list skb's data is used for compat tasks +* and the regular skb's data for normal (non-compat) tasks. +* See netlink_recvmsg(). +*/ +if (unlikely (skb_shinfo (skb)->frag_list)) { +if (netlink_dump_done (nlk,skb_shinfo (skb)->frag_list, cb, &extack)) + goto errout_skb; } +#endif if (sk_filter (sk, skb))kfree_skb (skb);
Signature · 1 line
2.27.0
kernel test robot
build botRe: [PATCH v2 1/6] xfrm/compat: Add 64=>32-bit messages translator
In reply to Dmitry SafonovThank you for the patch! Yet something to improve:
Hi Dmitry,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on ipsec/master]
[also build test ERROR on kselftest/next linus/master v5.9-rc2 next-20200825] [cannot apply to ipsec-next/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Dmitry-Safonov/xfrm-Add-compat-layer/20200826-095240 base: https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git master config: x86_64-allmodconfig (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
net/xfrm/xfrm_compat.c: In function 'xfrm_nlmsg_put_compat':
>> net/xfrm/xfrm_compat.c:103:16: error: 'xfrm_msg_min' undeclared (first use in this function); did you mean 'xfrm_alg_len'?
103 | int src_len = xfrm_msg_min[type];
| ^~~~~~~~~~~~
| xfrm_alg_len
net/xfrm/xfrm_compat.c:103:16: note: each undeclared identifier is reported only once for each function it appears in
net/xfrm/xfrm_compat.c: In function 'xfrm_xlate64':
net/xfrm/xfrm_compat.c:260:34: error: 'xfrm_msg_min' undeclared (first use in this function); did you mean 'xfrm_alg_len'?
260 | attrs = nlmsg_attrdata(nlh_src, xfrm_msg_min[type]);
| ^~~~~~~~~~~~
| xfrm_alg_len
net/xfrm/xfrm_compat.c: At top level:
>> net/xfrm/xfrm_compat.c:275:5: error: redefinition of 'xfrm_alloc_compat'
275 | int xfrm_alloc_compat(struct sk_buff *skb)
| ^~~~~~~~~~~~~~~~~
In file included from net/xfrm/xfrm_compat.c:9:
include/net/xfrm.h:2007:19: note: previous definition of 'xfrm_alloc_compat' was here
2007 | static inline int xfrm_alloc_compat(struct sk_buff *skb)
| ^~~~~~~~~~~~~~~~~
In file included from arch/x86/include/asm/bug.h:93,
from include/linux/bug.h:5,
from include/linux/thread_info.h:12,
from arch/x86/include/asm/preempt.h:7,
from include/linux/preempt.h:78,
from include/linux/spinlock.h:51,
from include/linux/seqlock.h:15,
from include/linux/time.h:6,
from include/linux/compat.h:10,
from net/xfrm/xfrm_compat.c:7:
net/xfrm/xfrm_compat.c: In function 'xfrm_alloc_compat':
net/xfrm/xfrm_compat.c:282:38: error: 'xfrm_msg_min' undeclared (first use in this function); did you mean 'xfrm_alg_len'?
282 | if (WARN_ON_ONCE(type >= ARRAY_SIZE(xfrm_msg_min)))
| ^~~~~~~~~~~~
include/asm-generic/bug.h:102:25: note: in definition of macro 'WARN_ON_ONCE'
102 | int __ret_warn_on = !!(condition); \
| ^~~~~~~~~
net/xfrm/xfrm_compat.c:282:27: note: in expansion of macro 'ARRAY_SIZE'
282 | if (WARN_ON_ONCE(type >= ARRAY_SIZE(xfrm_msg_min)))
| ^~~~~~~~~~
>> include/linux/build_bug.h:16:51: error: bit-field '<anonymous>' width not an integer constant
16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
| ^
include/asm-generic/bug.h:102:25: note: in definition of macro 'WARN_ON_ONCE'
102 | int __ret_warn_on = !!(condition); \
| ^~~~~~~~~
include/linux/compiler.h:224:28: note: in expansion of macro 'BUILD_BUG_ON_ZERO'
224 | #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
| ^~~~~~~~~~~~~~~~~
include/linux/kernel.h:47:59: note: in expansion of macro '__must_be_array'
47 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~~~~~~~~~~~~~
net/xfrm/xfrm_compat.c:282:27: note: in expansion of macro 'ARRAY_SIZE'
282 | if (WARN_ON_ONCE(type >= ARRAY_SIZE(xfrm_msg_min)))
| ^~~~~~~~~~# https://github.com/0day-ci/linux/commit/fa198f6763bf103396e06e12549e1dc00941a3d0
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Dmitry-Safonov/xfrm-Add-compat-layer/20200826-095240 git checkout fa198f6763bf103396e06e12549e1dc00941a3d0
vim +103 net/xfrm/xfrm_compat.c
98
99 static struct nlmsghdr *xfrm_nlmsg_put_compat(struct sk_buff *skb,
100 const struct nlmsghdr *nlh_src, u16 type)
101 {
102 int payload = compat_msg_min[type];
> 103 int src_len = xfrm_msg_min[type];
104 struct nlmsghdr *nlh_dst;
105
106 /* Compat messages are shorter or equal to native (+padding) */
107 if (WARN_ON_ONCE(src_len < payload))
108 return ERR_PTR(-EMSGSIZE);
109
110 nlh_dst = nlmsg_put(skb, nlh_src->nlmsg_pid, nlh_src->nlmsg_seq,
111 nlh_src->nlmsg_type, payload, nlh_src->nlmsg_flags);
112 if (!nlh_dst)
113 return ERR_PTR(-EMSGSIZE);
114
115 memset(nlmsg_data(nlh_dst), 0, payload);
116
117 switch (nlh_src->nlmsg_type) {
118 /* Compat message has the same layout as native */
119 case XFRM_MSG_DELSA:
120 case XFRM_MSG_DELPOLICY:
121 case XFRM_MSG_FLUSHSA:
122 case XFRM_MSG_FLUSHPOLICY:
123 case XFRM_MSG_NEWAE:
124 case XFRM_MSG_REPORT:
125 case XFRM_MSG_MIGRATE:
126 case XFRM_MSG_NEWSADINFO:
127 case XFRM_MSG_NEWSPDINFO:
128 case XFRM_MSG_MAPPING:
129 WARN_ON_ONCE(src_len != payload);
130 memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), src_len);
131 break;
132 /* 4 byte alignment for trailing u64 on native, but not on compat */
133 case XFRM_MSG_NEWSA:
134 case XFRM_MSG_NEWPOLICY:
135 case XFRM_MSG_UPDSA:
136 case XFRM_MSG_UPDPOLICY:
137 WARN_ON_ONCE(src_len != payload + 4);
138 memcpy(nlmsg_data(nlh_dst), nlmsg_data(nlh_src), payload);
139 break;
140 case XFRM_MSG_EXPIRE: {
141 const struct xfrm_user_expire *src_ue = nlmsg_data(nlh_src);
142 struct compat_xfrm_user_expire *dst_ue = nlmsg_data(nlh_dst);
143
144 /* compat_xfrm_user_expire has 4-byte smaller state */
145 memcpy(dst_ue, src_ue, sizeof(dst_ue->state));
146 dst_ue->hard = src_ue->hard;
147 break;
148 }
149 case XFRM_MSG_ACQUIRE: {
150 const struct xfrm_user_acquire *src_ua = nlmsg_data(nlh_src);
151 struct compat_xfrm_user_acquire *dst_ua = nlmsg_data(nlh_dst);
152
153 memcpy(dst_ua, src_ua, offsetof(struct compat_xfrm_user_acquire, aalgos));
154 dst_ua->aalgos = src_ua->aalgos;
155 dst_ua->ealgos = src_ua->ealgos;
156 dst_ua->calgos = src_ua->calgos;
157 dst_ua->seq = src_ua->seq;
158 break;
159 }
160 case XFRM_MSG_POLEXPIRE: {
161 const struct xfrm_user_polexpire *src_upe = nlmsg_data(nlh_src);
162 struct compat_xfrm_user_polexpire *dst_upe = nlmsg_data(nlh_dst);
163
164 /* compat_xfrm_user_polexpire has 4-byte smaller state */
165 memcpy(dst_upe, src_upe, sizeof(dst_upe->pol));
166 dst_upe->hard = src_upe->hard;
167 break;
168 }
169 case XFRM_MSG_ALLOCSPI: {
170 const struct xfrm_userspi_info *src_usi = nlmsg_data(nlh_src);
171 struct compat_xfrm_userspi_info *dst_usi = nlmsg_data(nlh_dst);
172
173 /* compat_xfrm_user_polexpire has 4-byte smaller state */
174 memcpy(dst_usi, src_usi, sizeof(src_usi->info));
175 dst_usi->min = src_usi->min;
176 dst_usi->max = src_usi->max;
177 break;
178 }
179 /* Not being sent by kernel */
180 case XFRM_MSG_GETSA:
181 case XFRM_MSG_GETPOLICY:
182 case XFRM_MSG_GETAE:
183 case XFRM_MSG_GETSADINFO:
184 case XFRM_MSG_GETSPDINFO:
185 default:
186 WARN_ONCE(1, "unsupported nlmsg_type %d", nlh_src->nlmsg_type);
187 return ERR_PTR(-EOPNOTSUPP);
188 }
189
190 return nlh_dst;
191 }
192
193 static int xfrm_nla_cpy(struct sk_buff *dst, const struct nlattr *src, int len)
194 {
195 return nla_put(dst, src->nla_type, len, nla_data(src));
196 }
197
198 static int xfrm_xlate64_attr(struct sk_buff *dst, const struct nlattr *src)
199 {
200 switch (src->nla_type) {
201 case XFRMA_ALG_AUTH:
202 case XFRMA_ALG_CRYPT:
203 case XFRMA_ALG_COMP:
204 case XFRMA_ENCAP:
205 case XFRMA_TMPL:
206 return xfrm_nla_cpy(dst, src, nla_len(src));
207 case XFRMA_SA:
208 return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_usersa_info));
209 case XFRMA_POLICY:
210 return xfrm_nla_cpy(dst, src, XMSGSIZE(compat_xfrm_userpolicy_info));
211 case XFRMA_SEC_CTX:
212 return xfrm_nla_cpy(dst, src, nla_len(src));
213 case XFRMA_LTIME_VAL:
214 return nla_put_64bit(dst, src->nla_type, nla_len(src),
215 nla_data(src), XFRMA_PAD);
216 case XFRMA_REPLAY_VAL:
217 case XFRMA_REPLAY_THRESH:
218 case XFRMA_ETIMER_THRESH:
219 case XFRMA_SRCADDR:
220 case XFRMA_COADDR:
221 return xfrm_nla_cpy(dst, src, nla_len(src));
222 case XFRMA_LASTUSED:
223 return nla_put_64bit(dst, src->nla_type, nla_len(src),
224 nla_data(src), XFRMA_PAD);
225 case XFRMA_POLICY_TYPE:
226 case XFRMA_MIGRATE:
227 case XFRMA_ALG_AEAD:
228 case XFRMA_KMADDRESS:
229 case XFRMA_ALG_AUTH_TRUNC:
230 case XFRMA_MARK:
231 case XFRMA_TFCPAD:
232 case XFRMA_REPLAY_ESN_VAL:
233 case XFRMA_SA_EXTRA_FLAGS:
234 case XFRMA_PROTO:
235 case XFRMA_ADDRESS_FILTER:
236 case XFRMA_OFFLOAD_DEV:
237 case XFRMA_SET_MARK:
238 case XFRMA_SET_MARK_MASK:
239 case XFRMA_IF_ID:
240 return xfrm_nla_cpy(dst, src, nla_len(src));
241 default:
242 BUILD_BUG_ON(XFRMA_MAX != XFRMA_IF_ID);
243 WARN_ONCE(1, "unsupported nla_type %d", src->nla_type);
244 return -EOPNOTSUPP;
245 }
246 }
247
248 /* Take kernel-built (64bit layout) and create 32bit layout for userspace */
249 static int xfrm_xlate64(struct sk_buff *dst, const struct nlmsghdr *nlh_src)
250 {
251 u16 type = nlh_src->nlmsg_type - XFRM_MSG_BASE;
252 const struct nlattr *nla, *attrs;
253 struct nlmsghdr *nlh_dst;
254 int len, remaining;
255
256 nlh_dst = xfrm_nlmsg_put_compat(dst, nlh_src, type);
257 if (IS_ERR(nlh_dst))
258 return PTR_ERR(nlh_dst);
259
260 attrs = nlmsg_attrdata(nlh_src, xfrm_msg_min[type]);
261 len = nlmsg_attrlen(nlh_src, xfrm_msg_min[type]);
262
263 nla_for_each_attr(nla, attrs, len, remaining) {
264 int err = xfrm_xlate64_attr(dst, nla);
265
266 if (err)
267 return err;
268 }
269
270 nlmsg_end(dst, nlh_dst);
271
272 return 0;
273 }
274
> 275 int xfrm_alloc_compat(struct sk_buff *skb)---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH v2 3/6] netlink/compat: Append NLMSG_DONE/extack to frag_list
In reply to Dmitry SafonovQuoted 3 lines · Dmitry Safonov
Modules those use netlink may supply a 2nd skb, (via
frag_list) that contains an alternative data set meant for applications using 32bit compatibility mode.
Do note, however, that until this day the facility here was only used by (and originally intended for) wireless extensions, where it exclusively applies to *event* messages. Hence, we really didn't worry about dump or any other kind of netlink usage.
That said, it's really just a historic note explaining why it didn't work for you out of the box :)
Quoted 25 lines
In such a case,
netlink_recvmsgwill use this 2nd skb instead of the original one.Without this patch, such compat applications will retrieve all netlink dump data, but will then get an unexpected EOF.
Cc Johannes Berg <[email protected]>Signed-off-by Florian Westphal <[email protected]>Signed-off-by Dmitry Safonov <[email protected]>--- net/netlink/af_netlink.c | 48 ++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 14 deletions(-)net/netlink/af_netlink.c +4
-0@@ -2186,13 +2186,36 @@ EXPORT_SYMBOL(__nlmsg_put);* It would be better to create kernel thread. */ +static int netlink_dump_done (struct netlink_sock *nlk,struct sk_buff *skb, +struct netlink_callback *cb, +struct netlink_ext_ack *extack) +{
net/netlink/af_netlink.c EXPORT_SYMBOL(__nlmsg_put); · line 2202
Quoted 10 lines
net/netlink/af_netlink.c +10
-0+struct nlmsghdr *nlh; + + nlh =nlmsg_put_answer (skb, cb,NLMSG_DONE ,sizeof (nlk->dump_done_errno), +NLM_F_MULTI | cb->answer_flags); +if (WARN_ON (!nlh)) +return -ENOBUFS ; + +nl_dump_check_consistent (cb, nlh); +memcpy (nlmsg_data (nlh), &nlk->dump_done_errno, +sizeof (nlk->dump_done_errno));
nit: indentation here looks odd.
Other than that, looks reasonable to me.
johannes
Steffen Klassert
maintainerQuoted 43 lines · Dmitry Safonov
Changes since v1:
- reworked patches set to use translator
- separated the compat layer intoxfrm_compat.c, compiled underXFRM_USER_COMPATconfig
- 32-bit messages now being sent infrag_list(like wext-core does)
- instead of__packedaddcompat_u64members in compat structures
- selftest reworked to kselftest lib API
- added netlink dump testing to the selftestXFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit
19d7df69fdb2("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit74005991b78a("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by
xfrm_user, which resulted in over-complicated code [1].Florian Westphal provided the idea of translator and some draft patches in the discussion. In these patches, his idea is reused and some of his initial code is also present.
There were a couple of attempts to solve xfrm compat problem:
https://lkml.org/lkml/2017/1/20/733
https://patchwork.ozlabs.org/patch/44600/
http://netdev.vger.kernel.narkive.com/2Gesykj6/patch-net-next-xfrm-correctly-parse-netlink-msg-from-32bits-ip-command-on-64bits-hostAll the discussions end in the conclusion that xfrm should have a full compatible layer to correctly work with 32-bit applications on 64-bit kernels:
https://lkml.org/lkml/2017/1/23/413
https://patchwork.ozlabs.org/patch/433279/In some recent lkml discussion, Linus said that it's worth to fix this problem and not giving people an excuse to stay on 32-bit kernel:
https://lkml.org/lkml/2018/2/13/752
Quoted 8 lines
There is also an selftest for ipsec tunnels.
It doesn't depend on any library and compat version can be easy build with: make CFLAGS=-m32 net/ipsecPatches as a .git branch:
https://github.com/0x7f454c46/linux/tree/xfrm-compat-v2
Thanks for the patches, looks good!
Please fix the issue reported from 'kernel test robot' and resend.
Thanks!
Steffen Klassert
maintainerRe: [PATCH v2 1/6] xfrm/compat: Add 64=>32-bit messages translator
In reply to Dmitry SafonovQuoted 15 lines · Dmitry Safonov
XFRM is disabled for compatible users because of the UABI difference. The difference is in structures paddings and in the result the size of netlink messages differ.
Possibility for compatible application to manage xfrm tunnels was disabled by: the commmit
19d7df69fdb2("xfrm: Refuse to insert 32 bit userspace socket policies on 64 bit systems") and the commit74005991b78a("xfrm: Do not parse 32bits compiled xfrm netlink msg on 64bits host").This is my second attempt to resolve the xfrm/compat problem by adding the 64=>32 and 32=>64 bit translators those non-visibly to a user provide translation between compatible user and kernel. Previous attempt was to interrupt the message ABI according to a syscall by
xfrm_user, which resulted in over-complicated code [1].
Quoted 3 lines
Florian Westphal provided the idea of translator and some draft patches in the discussion. In these patches, his idea is reused and some of his initial code is also present.
One comment on this. Looks like the above is the same in all commit messages. Please provide that generic information with the patch 0/n and remove it from the other patches.
Re: [PATCH v2 3/6] netlink/compat: Append NLMSG_DONE/extack to frag_list
In reply to Johannes BergQuoted 1 line · Johannes Berg
On Wed, 2020-08-26 at 02:49 +0100, Dmitry Safonov wrote:
[..]
Quoted 9 lines
net/netlink/af_netlink.c EXPORT_SYMBOL(__nlmsg_put); · line 2202
Quoted 3 lines
net/netlink/af_netlink.c +3
-0+nl_dump_check_consistent (cb, nlh); +memcpy (nlmsg_data (nlh), &nlk->dump_done_errno, +sizeof (nlk->dump_done_errno));nit: indentation here looks odd.
Other than that, looks reasonable to me.
Reviewed-by Johannes Berg <[email protected]>
Thank you for the review!
Signature · 1 line
Dmitry
Quoted 1 line · Steffen Klassert
On Wed, Aug 26, 2020 at 02:49:43AM +0100, Dmitry Safonov wrote:
[..]
Quoted 4 lines
Thanks for the patches, looks good!
Please fix the issue reported from 'kernel test robot' and resend.
Thanks, will do!
Signature · 1 line
Dmitry
Re: [PATCH v2 1/6] xfrm/compat: Add 64=>32-bit messages translator
In reply to Steffen KlassertOn 9/7/20 12:24 PM, Steffen Klassert wrote:
[..]
Quoted 3 lines
One comment on this. Looks like the above is the same in all commit messages. Please provide that generic information with the patch 0/n and remove it from the other patches.
Yeah, I think I've used to that from x86/core submissions - they prefer having general information copied from cover-letter to every patch, that way commits in git log or git show preserve it. Probably, one of small differences in style between contributions to different subsystems. Will do, no problem.
Thanks,
Dmitry