Netdev List
 help / color / mirror / Atom feed
* [net PATCH 0/2] sockmap fixes
@ 2017-10-27 16:45 John Fastabend
  2017-10-27 16:45 ` [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure John Fastabend
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: John Fastabend @ 2017-10-27 16:45 UTC (permalink / raw)
  To: alexei.starovoitov, davem; +Cc: netdev, edumazet, daniel

Last two fixes (as far as I know) for sockmap code this round.

First, we are using the qdisc cb structure when making the data end
calculation. This is really just wrong so, store it with the other
metadata in the correct tcp_skb_cb sturct to avoid breaking things.

Next, with recent work to attach multiple programs to a cgroup a
specific enumeration of return codes was agreed upon. However,
I wrote the sk_skb program types before seeing this work and used
a different convention. Patch 2 in the series aligns the return
codes to avoid breaking with this infrastructure and also aligns
with other programming conventions to avoid being the odd duck out
forcing programs to remember SK_SKB programs are different. Pusing
to net because its a user visible change. With this SK_SKB program
return codes are the same as other cgroup program types.

Thanks!
John

---

John Fastabend (2):
      bpf: bpf_compute_data uses incorrect cb structure
      bpf: rename sk_actions to align with bpf infrastructure


 include/net/tcp.h              |    1 +
 include/uapi/linux/bpf.h       |    6 +++---
 kernel/bpf/sockmap.c           |   15 ++++++++++++---
 net/core/filter.c              |   32 +++++++++++++++++++++++++++++---
 tools/include/uapi/linux/bpf.h |    4 ++--
 5 files changed, 47 insertions(+), 11 deletions(-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure
  2017-10-27 16:45 [net PATCH 0/2] sockmap fixes John Fastabend
@ 2017-10-27 16:45 ` John Fastabend
  2017-10-27 19:43   ` Alexei Starovoitov
  2017-10-27 16:45 ` [net PATCH 2/2] bpf: rename sk_actions to align with bpf infrastructure John Fastabend
  2017-10-29  2:19 ` [net PATCH 0/2] sockmap fixes David Miller
  2 siblings, 1 reply; 6+ messages in thread
From: John Fastabend @ 2017-10-27 16:45 UTC (permalink / raw)
  To: alexei.starovoitov, davem; +Cc: netdev, edumazet, daniel

SK_SKB program types use bpf_compute_data to store the end of the
packet data. However, bpf_compute_data assumes the cb is stored in the
qdisc layer format. But, for SK_SKB this is the wrong layer of the
stack for this type.

It happens to work (sort of!) because in most cases nothing happens
to be overwritten today. This is very fragile and error prone.
Fortunately, we have another hole in tcp_skb_cb we can use so lets
put the data_end value there.

Note, SK_SKB program types do not use data_meta, they are failed by
sk_skb_is_valid_access().

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 include/net/tcp.h    |    1 +
 kernel/bpf/sockmap.c |   12 ++++++++++--
 net/core/filter.c    |   27 ++++++++++++++++++++++++++-
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index b1ef98e..33599d17 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -844,6 +844,7 @@ struct tcp_skb_cb {
 			__u32 key;
 			__u32 flags;
 			struct bpf_map *map;
+			void *data_end;
 		} bpf;
 	};
 };
diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c
index 2b6eb35..6778fb7 100644
--- a/kernel/bpf/sockmap.c
+++ b/kernel/bpf/sockmap.c
@@ -93,6 +93,14 @@ static inline struct smap_psock *smap_psock_sk(const struct sock *sk)
 	return rcu_dereference_sk_user_data(sk);
 }
 
+/* compute the linear packet data range [data, data_end) for skb when
+ * sk_skb type programs are in use.
+ */
+static inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
+{
+	TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
+}
+
 static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
 {
 	struct bpf_prog *prog = READ_ONCE(psock->bpf_verdict);
@@ -108,7 +116,7 @@ static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
 	 */
 	TCP_SKB_CB(skb)->bpf.map = NULL;
 	skb->sk = psock->sock;
-	bpf_compute_data_end(skb);
+	bpf_compute_data_end_sk_skb(skb);
 	preempt_disable();
 	rc = (*prog->bpf_func)(skb, prog->insnsi);
 	preempt_enable();
@@ -368,7 +376,7 @@ static int smap_parse_func_strparser(struct strparser *strp,
 	 * any socket yet.
 	 */
 	skb->sk = psock->sock;
-	bpf_compute_data_end(skb);
+	bpf_compute_data_end_sk_skb(skb);
 	rc = (*prog->bpf_func)(skb, prog->insnsi);
 	skb->sk = NULL;
 	rcu_read_unlock();
diff --git a/net/core/filter.c b/net/core/filter.c
index aa02659..68eaa2f 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4243,6 +4243,31 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
 	return insn - insn_buf;
 }
 
+static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
+				     const struct bpf_insn *si,
+				     struct bpf_insn *insn_buf,
+				     struct bpf_prog *prog, u32 *target_size)
+{
+	struct bpf_insn *insn = insn_buf;
+	int off;
+
+	switch (si->off) {
+	case offsetof(struct __sk_buff, data_end):
+		off  = si->off;
+		off -= offsetof(struct __sk_buff, data_end);
+		off += offsetof(struct sk_buff, cb);
+		off += offsetof(struct tcp_skb_cb, bpf.data_end);
+		*insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
+				      si->src_reg, off);
+		break;
+	default:
+		return bpf_convert_ctx_access(type, si, insn_buf, prog,
+					      target_size);
+	}
+
+	return insn - insn_buf;
+}
+
 const struct bpf_verifier_ops sk_filter_prog_ops = {
 	.get_func_proto		= sk_filter_func_proto,
 	.is_valid_access	= sk_filter_is_valid_access,
@@ -4301,7 +4326,7 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
 const struct bpf_verifier_ops sk_skb_prog_ops = {
 	.get_func_proto		= sk_skb_func_proto,
 	.is_valid_access	= sk_skb_is_valid_access,
-	.convert_ctx_access	= bpf_convert_ctx_access,
+	.convert_ctx_access	= sk_skb_convert_ctx_access,
 	.gen_prologue		= sk_skb_prologue,
 };
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [net PATCH 2/2] bpf: rename sk_actions to align with bpf infrastructure
  2017-10-27 16:45 [net PATCH 0/2] sockmap fixes John Fastabend
  2017-10-27 16:45 ` [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure John Fastabend
@ 2017-10-27 16:45 ` John Fastabend
  2017-10-27 19:44   ` Alexei Starovoitov
  2017-10-29  2:19 ` [net PATCH 0/2] sockmap fixes David Miller
  2 siblings, 1 reply; 6+ messages in thread
From: John Fastabend @ 2017-10-27 16:45 UTC (permalink / raw)
  To: alexei.starovoitov, davem; +Cc: netdev, edumazet, daniel

Recent additions to support multiple programs in cgroups impose
a strict requirement, "all yes is yes, any no is no". To enforce
this the infrastructure requires the 'no' return code, SK_DROP in
this case, to be 0.

To apply these rules to SK_SKB program types the sk_actions return
codes need to be adjusted.

This fix adds SK_PASS and makes 'SK_DROP = 0'. Finally, remove
SK_ABORTED to remove any chance that the API may allow aborted
program flows to be passed up the stack. This would be incorrect
behavior and allow programs to break existing policies.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 include/uapi/linux/bpf.h       |    6 +++---
 kernel/bpf/sockmap.c           |    3 ++-
 net/core/filter.c              |    5 +++--
 tools/include/uapi/linux/bpf.h |    4 ++--
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index f90860d..0d7948c 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -575,7 +575,7 @@ enum bpf_attach_type {
  *     @map: pointer to sockmap
  *     @key: key to lookup sock in map
  *     @flags: reserved for future use
- *     Return: SK_REDIRECT
+ *     Return: SK_PASS
  *
  * int bpf_sock_map_update(skops, map, key, flags)
  *	@skops: pointer to bpf_sock_ops
@@ -786,8 +786,8 @@ struct xdp_md {
 };
 
 enum sk_action {
-	SK_ABORTED = 0,
-	SK_DROP,
+	SK_DROP = 0,
+	SK_PASS,
 	SK_REDIRECT,
 };
 
diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c
index 6778fb7..66f00a2 100644
--- a/kernel/bpf/sockmap.c
+++ b/kernel/bpf/sockmap.c
@@ -122,7 +122,8 @@ static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
 	preempt_enable();
 	skb->sk = NULL;
 
-	return rc;
+	return rc == SK_PASS ?
+		(TCP_SKB_CB(skb)->bpf.map ? SK_REDIRECT : SK_PASS) : SK_DROP;
 }
 
 static void smap_do_verdict(struct smap_psock *psock, struct sk_buff *skb)
diff --git a/net/core/filter.c b/net/core/filter.c
index 68eaa2f..6ae94f8 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -1844,14 +1844,15 @@ int skb_do_redirect(struct sk_buff *skb)
 {
 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
 
+	/* If user passes invalid input drop the packet. */
 	if (unlikely(flags))
-		return SK_ABORTED;
+		return SK_DROP;
 
 	tcb->bpf.key = key;
 	tcb->bpf.flags = flags;
 	tcb->bpf.map = map;
 
-	return SK_REDIRECT;
+	return SK_PASS;
 }
 
 struct sock *do_sk_redirect_map(struct sk_buff *skb)
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 24b35a1..c174971 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -787,8 +787,8 @@ struct xdp_md {
 };
 
 enum sk_action {
-	SK_ABORTED = 0,
-	SK_DROP,
+	SK_DROP = 0,
+	SK_PASS,
 	SK_REDIRECT,
 };
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure
  2017-10-27 16:45 ` [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure John Fastabend
@ 2017-10-27 19:43   ` Alexei Starovoitov
  0 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2017-10-27 19:43 UTC (permalink / raw)
  To: John Fastabend; +Cc: davem, netdev, edumazet, daniel

On Fri, Oct 27, 2017 at 09:45:34AM -0700, John Fastabend wrote:
> SK_SKB program types use bpf_compute_data to store the end of the
> packet data. However, bpf_compute_data assumes the cb is stored in the
> qdisc layer format. But, for SK_SKB this is the wrong layer of the
> stack for this type.
> 
> It happens to work (sort of!) because in most cases nothing happens
> to be overwritten today. This is very fragile and error prone.
> Fortunately, we have another hole in tcp_skb_cb we can use so lets
> put the data_end value there.
> 
> Note, SK_SKB program types do not use data_meta, they are failed by
> sk_skb_is_valid_access().
> 
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>

Acked-by: Alexei Starovoitov <ast@kernel.org>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [net PATCH 2/2] bpf: rename sk_actions to align with bpf infrastructure
  2017-10-27 16:45 ` [net PATCH 2/2] bpf: rename sk_actions to align with bpf infrastructure John Fastabend
@ 2017-10-27 19:44   ` Alexei Starovoitov
  0 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2017-10-27 19:44 UTC (permalink / raw)
  To: John Fastabend; +Cc: davem, netdev, edumazet, daniel

On Fri, Oct 27, 2017 at 09:45:53AM -0700, John Fastabend wrote:
> Recent additions to support multiple programs in cgroups impose
> a strict requirement, "all yes is yes, any no is no". To enforce
> this the infrastructure requires the 'no' return code, SK_DROP in
> this case, to be 0.
> 
> To apply these rules to SK_SKB program types the sk_actions return
> codes need to be adjusted.
> 
> This fix adds SK_PASS and makes 'SK_DROP = 0'. Finally, remove
> SK_ABORTED to remove any chance that the API may allow aborted
> program flows to be passed up the stack. This would be incorrect
> behavior and allow programs to break existing policies.
> 
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>

Acked-by: Alexei Starovoitov <ast@kernel.org>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [net PATCH 0/2] sockmap fixes
  2017-10-27 16:45 [net PATCH 0/2] sockmap fixes John Fastabend
  2017-10-27 16:45 ` [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure John Fastabend
  2017-10-27 16:45 ` [net PATCH 2/2] bpf: rename sk_actions to align with bpf infrastructure John Fastabend
@ 2017-10-29  2:19 ` David Miller
  2 siblings, 0 replies; 6+ messages in thread
From: David Miller @ 2017-10-29  2:19 UTC (permalink / raw)
  To: john.fastabend; +Cc: alexei.starovoitov, netdev, edumazet, daniel

From: John Fastabend <john.fastabend@gmail.com>
Date: Fri, 27 Oct 2017 09:45:16 -0700

> Last two fixes (as far as I know) for sockmap code this round.
> 
> First, we are using the qdisc cb structure when making the data end
> calculation. This is really just wrong so, store it with the other
> metadata in the correct tcp_skb_cb sturct to avoid breaking things.
> 
> Next, with recent work to attach multiple programs to a cgroup a
> specific enumeration of return codes was agreed upon. However,
> I wrote the sk_skb program types before seeing this work and used
> a different convention. Patch 2 in the series aligns the return
> codes to avoid breaking with this infrastructure and also aligns
> with other programming conventions to avoid being the odd duck out
> forcing programs to remember SK_SKB programs are different. Pusing
> to net because its a user visible change. With this SK_SKB program
> return codes are the same as other cgroup program types.

Series applied, thanks a lot John.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-10-29  2:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-27 16:45 [net PATCH 0/2] sockmap fixes John Fastabend
2017-10-27 16:45 ` [net PATCH 1/2] bpf: bpf_compute_data uses incorrect cb structure John Fastabend
2017-10-27 19:43   ` Alexei Starovoitov
2017-10-27 16:45 ` [net PATCH 2/2] bpf: rename sk_actions to align with bpf infrastructure John Fastabend
2017-10-27 19:44   ` Alexei Starovoitov
2017-10-29  2:19 ` [net PATCH 0/2] sockmap fixes David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox