Skip to contentSkip to conversation

xfrm: add x86 CONFIG_COMPAT support

PATCH In review Florian Westphal 4 participants 12 messages Feb 19, 2010 – Feb 26, 2010

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover xfrm: add x86 CONFIG_COMPAT support

2nd version of xfrm x86-compat patch set.

please refer to the individual patches for the list of changes since v1.

At the moment it is not possible to use the xfrm netlink interface on x86_64 with a 32bit userland.

The problem exists because a few structures, e.g. struct xfrm_usersa_info, have different sizes in user/kernelspace (3 byte padding on x86, 7 byte on x86_64) due to different alignment requirements of "u64".

First two patches add necessary CONFIG_COMPAT_NETLINK_MESSAGES infrastructure to netlink in/output path.

Patch 3 is a refactoring patch to split functionality (especially nlmsg allocation and adding data to the nlmsg) in order to re-use code and ease review.

Patch 4 adds CONFIG_COMPAT_FOR_U64_ALIGNMENT support to xfrm.

Patch 5 sets CMSG_MSG_COMPAT in sock_aio_write(), depending on is_compat_task(). If patch 5 is deemed too ugly, just pretend it doesn't exist; userspace can be patched to use sendmsg instead of write() to fix the problem, too.

 include/linux/netlink.h  |    1 
 net/netlink/af_netlink.c |   11 +
 net/socket.c             |    4 
 net/xfrm/Kconfig         |    1 
 net/xfrm/xfrm_user.c     |  505 +++++++++++++++++++++++++++++++++++++++++------
 5 files changed, 462 insertions(+), 60 deletions(-)
#1 lore raw

PATCH v2 1/5 netlink: append NLMSG_DONE to compatskb, too

modules using 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.

---
no changes since v1.

 net/netlink/af_netlink.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
net/netlink/af_netlink.c +8 -0
index 4c5972b..4910031 100644
@@ -1698,6 +1698,14 @@ static int netlink_dump(struct sock *sk)
 
 	memcpy(nlmsg_data(nlh), &len, sizeof(len));
 
+#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
+	if (unlikely(skb_shinfo(skb)->frag_list)) {
+		nlh = nlmsg_put_answer(skb_shinfo(skb)->frag_list, cb,
+					NLMSG_DONE, sizeof(len), NLM_F_MULTI);
+		if (nlh)
+			memcpy(nlmsg_data(nlh), &len, sizeof(len));
+	}
+#endif
 	if (sk_filter(sk, skb))
 		kfree_skb(skb);
 	else {
Signature · 1 line
1.6.3.3
#2 lore raw

PATCH v2 2/5 netlink: store MSG_CMSG_COMPAT flag in netlink_skb_parms

In reply to Florian Westphal

From: Florian Westphal <[email protected]>

This allows the netlink processing context to determine if the data needs any 32 bit fixups.

---
changes since v1: change msg_compat to bool.

 include/linux/netlink.h  |    1 +
 net/netlink/af_netlink.c |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
include/linux/netlink.h +1 -0
index fde27c0..c094694 100644
@@ -164,6 +164,7 @@ struct netlink_skb_parms {
 	__u32			loginuid;	/* Login (audit) uid */
 	__u32			sessionid;	/* Session id (audit) */
 	__u32			sid;		/* SELinux security id */
+	bool			msg_compat;	/* Message needs 32bit fixups */
 };
 
 #define NETLINK_CB(skb)		(*(struct netlink_skb_parms*)&((skb)->cb))
net/netlink/af_netlink.c +3 -0
index 4910031..5ff97cf 100644
@@ -1328,6 +1328,9 @@ static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
 	NETLINK_CB(skb).dst_group = dst_group;
 	NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
 	NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
+#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
+	NETLINK_CB(skb).msg_compat = !!(msg->msg_flags & MSG_CMSG_COMPAT);
+#endif
 	security_task_getsecid(current, &(NETLINK_CB(skb).sid));
 	memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
 
Signature · 1 line
1.6.3.3
#3 lore raw

Re: [PATCH 2/5] netlink: store MSG_CMSG_COMPAT flag in netlink_skb_parms

Quoted 31 lines · Florian Westphal

From: Florian Westphal <[email protected]>

This allows the netlink processing context to determine if the data needs any 32 bit fixups.

---
changes since v1: change msg_compat to bool.

 include/linux/netlink.h  |    1 +
 net/netlink/af_netlink.c |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
include/linux/netlink.h +1 -0
index fde27c0..c094694 100644
@@ -164,6 +164,7 @@ struct netlink_skb_parms {
 	__u32			loginuid;	/* Login (audit) uid */
 	__u32			sessionid;	/* Session id (audit) */
 	__u32			sid;		/* SELinux security id */
+	bool			msg_compat;	/* Message needs 32bit fixups */
 };
 
 #define NETLINK_CB(skb)		(*(struct netlink_skb_parms*)&((skb)->cb))
net/netlink/af_netlink.c +0 -0
index 4910031..5ff97cf 100644

net/netlink/af_netlink.c static int netlink_sendmsg(struct kiocb *kiocb, · line 1334

Quoted 7 lines
net/netlink/af_netlink.c +3 -0
@@ -1328,6 +1328,9 @@ static int netlink_sendmsg(struct kiocb *kiocb,
 struct socket *sock,
 	NETLINK_CB(skb).dst_group = dst_group;
 	NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
 	NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
+#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
+	NETLINK_CB(skb).msg_compat = !!(msg->msg_flags & MSG_CMSG_COMPAT);
+#endif

Can't really decide ... does that ifdef make sense? Or should the variable be ifdef'ed as well?

johannes

#4 lore raw

Re: [PATCH 2/5] netlink: store MSG_CMSG_COMPAT flag in netlink_skb_parms

Quoted 26 lines · Johannes Berg
Quoted 25 lines · Florian Westphal

From: Florian Westphal <[email protected]>

This allows the netlink processing context to determine if the data needs any 32 bit fixups.

---
changes since v1: change msg_compat to bool.

 include/linux/netlink.h  |    1 +
 net/netlink/af_netlink.c |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
include/linux/netlink.h +1 -0
index fde27c0..c094694 100644
@@ -164,6 +164,7 @@ struct netlink_skb_parms {
 	__u32			loginuid;	/* Login (audit) uid */
 	__u32			sessionid;	/* Session id (audit) */
 	__u32			sid;		/* SELinux security id */
+	bool			msg_compat;	/* Message needs 32bit fixups */
 };
 
Quoted 16 lines

net/netlink/af_netlink.c static int netlink_sendmsg(struct kiocb *kiocb, · line 1334

Quoted 13 lines
include/linux/netlink.h +0 -0
 #define NETLINK_CB(skb)		(*(struct netlink_skb_parms*)&((skb)->cb))
net/netlink/af_netlink.c +3 -0
index 4910031..5ff97cf 100644
@@ -1328,6 +1328,9 @@ static int netlink_sendmsg(struct kiocb *kiocb,
 struct socket *sock,
 	NETLINK_CB(skb).dst_group = dst_group;
 	NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
 	NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
+#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
+	NETLINK_CB(skb).msg_compat = !!(msg->msg_flags & MSG_CMSG_COMPAT);
+#endif

Can't really decide ... does that ifdef make sense? Or should the variable be ifdef'ed as well?

The define is there because the assignment is only relevant for COMPAT_NETLINK_MESSAGES=y.

I did not add the define to netlink.h because I don't see any advantage.

But I do not mind changing this.

#5 lore raw

PATCH v2 3/5 xfrm: split nlmsg allocation and data copying

In reply to Florian Westphal

From: Florian Westphal <[email protected]>

To support 32bit userland with different u64 alignment requirements than a 64bit kernel (COMPAT_FOR_U64_ALIGNMENT), it is necessary to prepare messages containing affected structures
twice: once in the format expected by 64bit listeners, one in the format expected by 32bit applications.

In order to minimize copy & pasting and re-use existing code where possible, split nlmsg allocation and data copying.

Also, replace foo(..., sizeof(*structure)) with

len = sizeof(*structure);
foo(..., len);

so len can be made conditional if we are preparing a compat message. This will be done in a followup-patch.

---
changes since v1:
renamed sizeof_xfrm_usersa_info variable (dropped _xfrm) to avoid long line.

 net/xfrm/xfrm_user.c |  161 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 119 insertions(+), 42 deletions(-)
net/xfrm/xfrm_user.c +119 -42
index 943c871..a36dbc9 100644
@@ -692,17 +692,17 @@ nla_put_failure:
 	return -EMSGSIZE;
 }
 
-static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
+static int copy_one_state(struct sk_buff *skb, struct xfrm_state *x,
+			  struct xfrm_dump_info *sp)
 {
-	struct xfrm_dump_info *sp = ptr;
 	struct sk_buff *in_skb = sp->in_skb;
-	struct sk_buff *skb = sp->out_skb;
 	struct xfrm_usersa_info *p;
 	struct nlmsghdr *nlh;
+	size_t len = sizeof(*p);
 	int err;
 
 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
-			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
+			XFRM_MSG_NEWSA, len, sp->nlmsg_flags);
 	if (nlh == NULL)
 		return -EMSGSIZE;
 
@@ -720,6 +720,14 @@ nla_put_failure:
 	return err;
 }
 
+static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
+{
+	struct xfrm_dump_info *sp = ptr;
+	struct sk_buff *skb = sp->out_skb;
+	int ret = copy_one_state(skb, x, sp);
+	return ret;
+}
+
 static int xfrm_dump_sa_done(struct netlink_callback *cb)
 {
 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
@@ -1345,16 +1353,16 @@ static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
 }
 #endif
 
-static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
+static int copy_one_policy(struct sk_buff *skb, struct xfrm_policy *xp,
+			   int dir, struct xfrm_dump_info *sp)
 {
-	struct xfrm_dump_info *sp = ptr;
 	struct xfrm_userpolicy_info *p;
 	struct sk_buff *in_skb = sp->in_skb;
-	struct sk_buff *skb = sp->out_skb;
 	struct nlmsghdr *nlh;
+	size_t len = sizeof(*p);
 
 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
-			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
+			XFRM_MSG_NEWPOLICY, len, sp->nlmsg_flags);
 	if (nlh == NULL)
 		return -EMSGSIZE;
 
@@ -1375,6 +1383,15 @@ nlmsg_failure:
 	return -EMSGSIZE;
 }
 
+static int dump_one_policy(struct xfrm_policy *xp, int dir,
+			   int count, void *ptr)
+{
+	struct xfrm_dump_info *sp = ptr;
+	struct sk_buff *skb = sp->out_skb;
+	int ret = copy_one_policy(skb, xp, dir, sp);
+	return ret;
+}
+
 static int xfrm_dump_policy_done(struct netlink_callback *cb)
 {
 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
@@ -1695,7 +1712,7 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
 	struct xfrm_userpolicy_info *p = &up->pol;
 	u8 type = XFRM_POLICY_TYPE_MAIN;
-	int err = -ENOENT;
+	int hard, err = -ENOENT;
 
 	err = copy_from_user_policy_type(&type, attrs);
 	if (err)
@@ -1733,7 +1750,8 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	read_unlock(&xp->lock);
 	err = 0;
-	if (up->hard) {
+	hard = up->hard;
+	if (hard) {
 		uid_t loginuid = NETLINK_CB(skb).loginuid;
 		uid_t sessionid = NETLINK_CB(skb).sessionid;
 		u32 sid = NETLINK_CB(skb).sid;
@@ -1744,7 +1762,7 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
 		// reset the timers here?
 		printk("Dont know what to do with soft policy expire\n");
 	}
-	km_policy_expired(xp, p->dir, up->hard, current->pid);
+	km_policy_expired(xp, p->dir, hard, current->pid);
 
 out:
 	xfrm_pol_put(xp);
@@ -1756,7 +1774,7 @@ static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
 {
 	struct net *net = sock_net(skb->sk);
 	struct xfrm_state *x;
-	int err;
+	int hard, err;
 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
 	struct xfrm_usersa_info *p = &ue->state;
 
@@ -1770,9 +1788,10 @@ static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
 	err = -EINVAL;
 	if (x->km.state != XFRM_STATE_VALID)
 		goto out;
-	km_state_expired(x, ue->hard, current->pid);
+	hard = ue->hard;
+	km_state_expired(x, hard, current->pid);
 
-	if (ue->hard) {
+	if (hard) {
 		uid_t loginuid = NETLINK_CB(skb).loginuid;
 		uid_t sessionid = NETLINK_CB(skb).sessionid;
 		u32 sid = NETLINK_CB(skb).sid;
@@ -2256,26 +2275,35 @@ static inline size_t xfrm_sa_len(struct xfrm_state *x)
 	return l;
 }
 
-static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
+static int xfrm_notify_sa_len(struct xfrm_state *x, const struct km_event *c)
 {
-	struct net *net = xs_net(x);
-	struct xfrm_usersa_info *p;
-	struct xfrm_usersa_id *id;
-	struct nlmsghdr *nlh;
-	struct sk_buff *skb;
 	int len = xfrm_sa_len(x);
-	int headlen;
+	int headlen = sizeof(struct xfrm_usersa_info);
 
-	headlen = sizeof(*p);
 	if (c->event == XFRM_MSG_DELSA) {
 		len += nla_total_size(headlen);
-		headlen = sizeof(*id);
+		headlen = sizeof(struct xfrm_usersa_id);
 	}
 	len += NLMSG_ALIGN(headlen);
 
-	skb = nlmsg_new(len, GFP_ATOMIC);
-	if (skb == NULL)
-		return -ENOMEM;
+	return len;
+}
+
+static int xfrm_notify_sa_headlen(const struct km_event *c)
+{
+	if (c->event == XFRM_MSG_DELSA)
+		return sizeof(struct xfrm_usersa_id);
+	return sizeof(struct xfrm_usersa_info);
+}
+
+static int copy_to_user_xfrm_notify_sa(struct sk_buff *skb,
+				       struct xfrm_state *x, struct km_event *c)
+{
+	struct xfrm_usersa_info *p;
+	struct xfrm_usersa_id *id;
+	struct nlmsghdr *nlh;
+	int sizeof_usersa_info = sizeof(*p);
+	int headlen = xfrm_notify_sa_headlen(c);
 
 	nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
 	if (nlh == NULL)
@@ -2291,7 +2319,7 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
 		id->family = x->props.family;
 		id->proto = x->id.proto;
 
-		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
+		attr = nla_reserve(skb, XFRMA_SA, sizeof_usersa_info);
 		if (attr == NULL)
 			goto nla_put_failure;
 
@@ -2302,6 +2330,25 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
 		goto nla_put_failure;
 
 	nlmsg_end(skb, nlh);
+	return 0;
+nla_put_failure:
+	/* Somebody screwed up with xfrm_sa_len! */
+	WARN_ON(1);
+	return -1;
+}
+
+static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
+{
+	struct sk_buff *skb;
+	struct net *net = xs_net(x);
+	int len = xfrm_notify_sa_len(x, c);
+
+	skb = nlmsg_new(len, GFP_ATOMIC);
+	if (skb == NULL)
+		return -ENOMEM;
+
+	if (copy_to_user_xfrm_notify_sa(skb, x, c))
+		goto nla_put_failure;
 
 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
 
@@ -2349,10 +2396,11 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
 			 int dir)
 {
 	struct xfrm_user_acquire *ua;
+	size_t len = sizeof(*ua);
 	struct nlmsghdr *nlh;
 	__u32 seq = xfrm_get_acqseq();
 
-	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
+	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, len, 0);
 	if (nlh == NULL)
 		return -EMSGSIZE;
 
@@ -2468,10 +2516,11 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
 			   int dir, struct km_event *c)
 {
 	struct xfrm_user_polexpire *upe;
+	size_t len = sizeof(*upe);
 	struct nlmsghdr *nlh;
 	int hard = c->data.hard;
 
-	nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
+	nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, len, 0);
 	if (nlh == NULL)
 		return -EMSGSIZE;
 
@@ -2507,27 +2556,36 @@ static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_eve
 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
 }
 
-static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
+static int xfrm_notify_policy_len(struct xfrm_policy *xp, struct km_event *c)
 {
-	struct net *net = xp_net(xp);
-	struct xfrm_userpolicy_info *p;
-	struct xfrm_userpolicy_id *id;
-	struct nlmsghdr *nlh;
-	struct sk_buff *skb;
 	int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
-	int headlen;
+	int headlen = sizeof(struct xfrm_userpolicy_info);
 
-	headlen = sizeof(*p);
 	if (c->event == XFRM_MSG_DELPOLICY) {
 		len += nla_total_size(headlen);
-		headlen = sizeof(*id);
+		headlen = sizeof(struct xfrm_userpolicy_id);
 	}
 	len += userpolicy_type_attrsize();
 	len += NLMSG_ALIGN(headlen);
+	return len;
+}
 
-	skb = nlmsg_new(len, GFP_ATOMIC);
-	if (skb == NULL)
-		return -ENOMEM;
+static int xfrm_notify_policy_headlen(const struct km_event *c)
+{
+	if (c->event == XFRM_MSG_DELPOLICY)
+		return sizeof(struct xfrm_userpolicy_id);
+	return sizeof(struct xfrm_userpolicy_info);
+}
+
+static int copy_to_user_xfrm_notify_policy(struct sk_buff *skb, int dir,
+					   struct xfrm_policy *xp,
+					   struct km_event *c)
+{
+	struct xfrm_userpolicy_info *p;
+	struct xfrm_userpolicy_id *id;
+	struct nlmsghdr *nlh;
+	int sizeof_userpol_info = sizeof(*p);
+	int headlen = xfrm_notify_policy_headlen(c);
 
 	nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
 	if (nlh == NULL)
@@ -2545,7 +2603,7 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
 		else
 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
 
-		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
+		attr = nla_reserve(skb, XFRMA_POLICY, sizeof_userpol_info);
 		if (attr == NULL)
 			goto nlmsg_failure;
 
@@ -2560,6 +2618,25 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
 
 	nlmsg_end(skb, nlh);
 
+	return 0;
+
+nlmsg_failure:
+	return -1;
+}
+
+static int xfrm_notify_policy(struct xfrm_policy *xp, int dir,
+			      struct km_event *c)
+{
+	struct net *net = xp_net(xp);
+	struct sk_buff *skb;
+	int len = xfrm_notify_policy_len(xp, c);
+
+	skb = nlmsg_new(len, GFP_ATOMIC);
+	if (skb == NULL)
+		return -ENOMEM;
+	if (copy_to_user_xfrm_notify_policy(skb, dir, xp, c))
+		goto nlmsg_failure;
+
 	return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
 
 nlmsg_failure:
Signature · 1 line
1.6.3.3
#6 lore raw

PATCH v2 4/5 xfrm: CONFIG_COMPAT support for x86 architecture

In reply to Florian Westphal

on x86_64, struct xfrm_userpolicy_info/usersa_info have four additional bytes of padding at the end compared to x86.

Thus, when calling nlmsg_parse(.., sizeof(struct xfrm_userpolicy_info), trailing attributes are not parsed correctly by the kernel when the message was sent from an x86 32bit task.

Furthermore, those structures are contained inside a few other structures, e.g. struct xfrm_user_acquire.

When dealing with incoming data from userland,
those structures need special treatment in the "userland is 32bit" case.

When sending data to userland, it is sent in both 32bit and native format.

Errors when building the compat message are not visisble to user space; data will then be sent without the compat payload.

refer to 1dacc76d0014a034b8aca14237c127d7c19d7726 (net/compat/wext: send different messages to compat tasks) for more information on netlink compat handling.

---
changes since v1:
- reformat to avoid overly long lines
- restrict xfrm_msg_compat() use to input path
- xfrm_add_compatskb() now returns pointer to compatskb
- xfrm_msg_min_compat_pad table no longer hardcodes value "4"
- add missing compatskb alloc in xfrm_state_netlink and xfrm_policy_netlink.
- removed compat_xfrm_acquire_msgsize() etc. and use normal msgsize helper for compatskb, too. This means that we'll allocate 4 bytes more memory for the compatskb than needed, but this saves about 30 LOC.

I did not remove the CONFIG_COMPAT_FOR_U64_ALIGNMENT-ifdef from dump_one_state/_policy -- without it .o size increases by about 150 bytes with COMPAT_FOR_U64_ALIGNMENT=n, as the compiler cannot know that skb_shinfo(skb)->frag_list will always be NULL (and therefore the 2nd call to copy_() is not removed).

 net/xfrm/Kconfig     |    1 +
 net/xfrm/xfrm_user.c |  368 +++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 339 insertions(+), 30 deletions(-)
net/xfrm/Kconfig +1 -0
index 6d08167..0b30357 100644
@@ -9,6 +9,7 @@ config XFRM
 config XFRM_USER
 	tristate "Transformation user configuration interface"
 	depends on INET && XFRM
+	select WANT_COMPAT_NETLINK_MESSAGES if COMPAT_FOR_U64_ALIGNMENT
 	---help---
 	  Support for Transformation(XFRM) user configuration interface
 	  like IPsec used by native Linux tools.
net/xfrm/xfrm_user.c +338 -30

624 lines not on this page · raw message

Signature · 1 line
1.6.3.3
#7 lore raw

PATCH v2 5/5 net: sock_aio_write: set CMSG_MSG_COMPAT flag if is_compat_task

In reply to Florian Westphal

some programs (e.g. pluto ike daemon), send netlink data to the kernel via write().

So far, the CMSG_MSG_COMPAT flag is not set in this case, which results in the kernel interpreting the netlink data the wrong way.

An alternative solution is to switch userspace to sendmsg() when using netlink sockets.

---
no changes since v1.

 net/socket.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
net/socket.c +4 -0
index 769c386..e6a6a67 100644
@@ -837,6 +837,10 @@ static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 	msg->msg_iov = (struct iovec *)iov;
 	msg->msg_iovlen = nr_segs;
 	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
+#ifdef CONFIG_COMPAT
+	if (is_compat_task())
+		msg->msg_flags |= MSG_CMSG_COMPAT;
+#endif
 	if (sock->type == SOCK_SEQPACKET)
 		msg->msg_flags |= MSG_EOR;
 
Signature · 1 line
1.6.3.3
maintainer
#8 lore raw

Re: [PATCH 5/5] net: sock_aio_write: set CMSG_MSG_COMPAT flag if is_compat_task

From: Florian Westphal <[email protected]>
Date: Fri, 19 Feb 2010 13:41:47 +0100

Quoted 11 lines

some programs (e.g. pluto ike daemon), send netlink data to the kernel via write().

So far, the CMSG_MSG_COMPAT flag is not set in this case, which results in the kernel interpreting the netlink data the wrong way.

An alternative solution is to switch userspace to sendmsg() when using netlink sockets.

I was about to apply this entire patch set, but thinking about this one patch we have to do it differently:

net/socket.c static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, · line 846

Quoted 11 lines
net/socket.c +4 -0
@@ -837,6 +837,10 @@ static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 	msg->msg_iov = (struct iovec *)iov;
 	msg->msg_iovlen = nr_segs;
 	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
+#ifdef CONFIG_COMPAT
+	if (is_compat_task())
+		msg->msg_flags |= MSG_CMSG_COMPAT;
+#endif
 	if (sock->type == SOCK_SEQPACKET)
 		msg->msg_flags |= MSG_EOR;
 

What if the kernel itself does a socket write, say from a software interrupt or to generate events in response to a user event, and the current process happens to be a compat task?

I think this could even occur for NFS perhaps?

Anyways, that isn't going to work properly.

We might need to add ->compat_write to file operations to do it properly.

maintainer
#9 lore raw

Re: [PATCH 5/5] net: sock_aio_write: set CMSG_MSG_COMPAT flag if is_compat_task

From: David Miller <[email protected]>
Date: Fri, 26 Feb 2010 01:16:46 -0800 (PST)

Quoted 18 lines

From: Florian Westphal <[email protected]>
Date: Fri, 19 Feb 2010 13:41:47 +0100

net/socket.c static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, · line 846

Quoted 11 lines
net/socket.c +4 -0
@@ -837,6 +837,10 @@ static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 	msg->msg_iov = (struct iovec *)iov;
 	msg->msg_iovlen = nr_segs;
 	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
+#ifdef CONFIG_COMPAT
+	if (is_compat_task())
+		msg->msg_flags |= MSG_CMSG_COMPAT;
+#endif
 	if (sock->type == SOCK_SEQPACKET)
 		msg->msg_flags |= MSG_EOR;
 

What if the kernel itself does a socket write, say from a software interrupt or to generate events in response to a user event, and the current process happens to be a compat task?

BTW, it's again another example of why is_compat_task() is almost always the wrong thing to use.

#10 lore raw

Re: [PATCH 5/5] net: sock_aio_write: set CMSG_MSG_COMPAT flag if is_compat_task

In reply to David Miller
Quoted 34 lines · David Miller

From: Florian Westphal <[email protected]>
Date: Fri, 19 Feb 2010 13:41:47 +0100

Quoted 11 lines

some programs (e.g. pluto ike daemon), send netlink data to the kernel via write().

So far, the CMSG_MSG_COMPAT flag is not set in this case, which results in the kernel interpreting the netlink data the wrong way.

An alternative solution is to switch userspace to sendmsg() when using netlink sockets.

I was about to apply this entire patch set, but thinking about this one patch we have to do it differently:

net/socket.c static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, · line 846

Quoted 11 lines
net/socket.c +4 -0
@@ -837,6 +837,10 @@ static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
 	msg->msg_iov = (struct iovec *)iov;
 	msg->msg_iovlen = nr_segs;
 	msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
+#ifdef CONFIG_COMPAT
+	if (is_compat_task())
+		msg->msg_flags |= MSG_CMSG_COMPAT;
+#endif
 	if (sock->type == SOCK_SEQPACKET)
 		msg->msg_flags |= MSG_EOR;
 

What if the kernel itself does a socket write, say from a software interrupt or to generate events in response to a user event, and the current process happens to be a compat task?

Quoted 1 line

I think this could even occur for NFS perhaps?

I do not know. But it certainly shows that this patch should not be applied.

Quoted 4 lines

Anyways, that isn't going to work properly.

We might need to add ->compat_write to file operations to do it properly.

Fair enough.

If its necessary to set MSG_CMSG_COMPAT properly for plain write() as well to have the patch set merged then I'll see about adding such a ->compat_write and will resubmit the entire set again once net-next opens again for 2.6.35.

Thanks,
Florian

maintainer
#11 lore raw

Re: [PATCH 5/5] net: sock_aio_write: set CMSG_MSG_COMPAT flag if is_compat_task

From: Florian Westphal <[email protected]>
Date: Fri, 26 Feb 2010 11:03:05 +0100

Quoted 4 lines

If its necessary to set MSG_CMSG_COMPAT properly for plain write() as well to have the patch set merged then I'll see about adding such a ->compat_write and will resubmit the entire set again once net-next opens again for 2.6.35.

If you can get the VFS folks to take the addition of the ->compat_write hook quickly, I can still merge your patch series for 2.6.34