Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Subsystems (6)
v3 cover Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
This set addresses issue about accessing invalid ptr returned from bpf_tcp_sock() and bpf_sk_fullsock() after bpf_sk_release().
v3:
- reset reg->refcount_id for the is_null case in mark_ptr_or_null_reg()
v2:
- Remove refcount_id arg from release_reference() because id == refcount_id
- Add a WARN_ON_ONCE to mark_ptr_or_null_regs() to catch an internal verifier bug.
Martin KaFai Lau (2):
bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to
bpf_sk_release
bpf: Test ref release issue in bpf_tcp_sock and bpf_sk_fullsock.
include/linux/bpf_verifier.h | 9 ++ kernel/bpf/verifier.c | 107 +++++++++++------- .../selftests/bpf/verifier/ref_tracking.c | 73 ++++++++++++ 3 files changed, 150 insertions(+), 39 deletions(-)
Signature · 1 line
2.17.1
PATCH v3 1/2 bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Lorenz Bauer [thanks!] reported that a ptr returned by bpf_tcp_sock(sk) can still be accessed after bpf_sk_release(sk).
Both bpf_tcp_sock() and bpf_sk_fullsock() have the same issue. This patch addresses them together.
A simple reproducer looks like this:
sk = bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp = bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd = tp->snd_cwnd; /* oops! The verifier does not complain. */
The problem is the verifier did not scrub the register's states of the tcp_sock ptr (tp) after bpf_sk_release(sk).
[ Note that when calling bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g. bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]
Currently, the verifier does not track if a helper's return ptr (in REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, the reg1->id needs to be stored in reg0. The reg0->id has already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "struct bpf_reg_state".
With refcount_id, when bpf_sk_release(sk) is called, the verifier can scrub all reg states which has a refcount_id match. It is done with the changes in release_reg_references().
When acquiring and releasing a refcount, the reg->id is still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.
Misc change notes:
- With the new refcount_id, reg_is_refcounted() test can now be done with "reg->refcount_id && reg->id == reg->refcount_id" instead of testing the ptr type.
The type_is_refcounted() and type_is_refcounted_or_null() are no longer needed, so removed.
- An anonymous struct is added to bpf_call_arg_meta to store the reg->id and reg->refcount_id of the arg. Otherwise, they will be unavailable after check_helper_call() has cleared all CALLER_SAVED_REGS.
- The check_func_arg() can only allow one refcount-ed arg. It is guaranteed by check_refcount_ok() which ensures at most one arg can be refcount-ed. Hence, it is a verifier internal error if >1 refcount arg found in check_func_arg().
- The check_func_arg() also complains if a "is_acquire_function(func_id)" helper is having a refcount-ed arg. No func_id is doing this now and should have been rejected earlier, so it is treated as verifier internal error also.
- In check_func_arg(), the "!reg->id" check is removed under the ARG_PTR_TO_SOCKET case. It is because a PTR_TO_SOCKET can be obtained from bpf_sk_fullsock() and it does not take a refcount. The verifier will still complain during release_reference() but it does not treat it as a verifier internal error anymore.
- In release_reference(), release_reference_state() is called first to ensure a match on "reg->id" can be found before scrubbing the reg states with release_reg_references().
--- include/linux/bpf_verifier.h | 9 +++ kernel/bpf/verifier.c | 107 ++++++++++++++++++++++------------- 2 files changed, 77 insertions(+), 39 deletions(-)
include/linux/bpf_verifier.h +9 -0
@@ -66,6 +66,15 @@ struct bpf_reg_state {* same reference to the socket, to determine proper reference freeing. */ u32 id; +/* For PTR_TO_SOCKET and PTR_TO_TCP_SOCK, this ptr may not actually +* hold a refcount of a socket but instead it is a ptr +* returned from a helper which is based on its refcount-ed +* ptr argument (e.g. bpf_tcp_sock()). +* "refcount_id" stores which refcount-ed argument it +* originally derived from. When this original argument's +* refcount is released, this ptr will also be invalidated. +*/ +u32 refcount_id;/* For scalar types (SCALAR_VALUE), this represents our knowledge of * the actual value. * For pointer types, this represents the variable part of the offset
kernel/bpf/verifier.c +68 -39
206 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".
Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.
I think the choice of returning listener full sock from req sock
in sk_to_full_sk() was a wrong one.
It seems better to make semantics of bpf_tcp_sock() and bpf_sk_fullsock() as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.
Then it will be ok to call bpf_sk_release(tp) when tp came from bpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be in acquired_refs.
The additional refcount_id won't be necessary. bpf_sk_fullsock() and bpf_tcp_sock() will not call sk_to_full_sk and the verifier will be copying reg1->id into reg0->id.
In release_reference() the verifier will do
if (regs[i].id == id)
mark_reg_unknown(env, regs, i);
for all socket types.
release_reference_state() will stay as-is.
imo such logic will be easier to follow.
This implicit sk_to_full_sk() makes the whole thing much harder for the verifier and for the bpf program writers.
The new bpf_get_listener_sock(sk) doesn't have to copy ID from reg1 to reg0 since req socket will not be returned from bpf_sk_lookup_tcp and its ID will not be stored in acuired_refs.
Does it make sense ?
Re: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 58 lines · Alexei Starovoitov
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.I think the choice of returning listener full sock from req sock
insk_to_full_sk()was a wrong one.
It seems better to make semantics ofbpf_tcp_sock()andbpf_sk_fullsock()as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.Then it will be ok to call
bpf_sk_release(tp)when tp came frombpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be inacquired_refs.The additional
refcount_idwon't be necessary.bpf_sk_fullsock()andbpf_tcp_sock()will not callsk_to_full_skand the verifier will be copyingreg1->idintoreg0->id.In
release_reference()the verifier will do
if (regs[i].id == id)mark_reg_unknown(env, regs, i);for all socket types.
release_reference_state()will stay as-is.imo such logic will be easier to follow.
Quoted 8 lines
This implicit
sk_to_full_sk()makes the whole thing much harder for the verifier and for the bpf program writers.The new
bpf_get_listener_sock(sk)doesn't have to copy ID from reg1 to reg0 since req socket will not be returned frombpf_sk_lookup_tcpand its ID will not be stored inacuired_refs.Does it make sense ?
I like this idea. Many thanks for thinking it through!
Allowing bpf_sk_release(tp), no need to call bpf_sk_release() on ptr returned from bpf_get_listener_sock(sk) and keep one reg->id.
I think it should work. I will rework the patches.
Daniel Borkmann
maintainerRe: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 66 lines · Martin Lau
Quoted 65 lines · Alexei Starovoitov
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.I think the choice of returning listener full sock from req sock
insk_to_full_sk()was a wrong one.
It seems better to make semantics ofbpf_tcp_sock()andbpf_sk_fullsock()as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.Then it will be ok to call
bpf_sk_release(tp)when tp came frombpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be inacquired_refs.The additional
refcount_idwon't be necessary.bpf_sk_fullsock()andbpf_tcp_sock()will not callsk_to_full_skand the verifier will be copyingreg1->idintoreg0->id.In
release_reference()the verifier will do
if (regs[i].id == id)mark_reg_unknown(env, regs, i);for all socket types.
release_reference_state()will stay as-is.imo such logic will be easier to follow.
This implicit
sk_to_full_sk()makes the whole thing much harder for the verifier and for the bpf program writers.The new
bpf_get_listener_sock(sk)doesn't have to copy ID from reg1 to reg0 since req socket will not be returned frombpf_sk_lookup_tcpand its ID will not be stored inacuired_refs.
Quoted 7 lines
Quoted 1 line
Does it make sense ?
I like this idea. Many thanks for thinking it through!
Allowing
bpf_sk_release(tp), no need to callbpf_sk_release()on ptr returned frombpf_get_listener_sock(sk)and keep onereg->id.I think it should work. I will rework the patches.
Agree, makes sense, that seems much better fix.
Thanks,
Daniel
Re: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 68 lines · Daniel Borkmann
Quoted 67 lines · Martin Lau
Quoted 66 lines · Alexei Starovoitov
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.I think the choice of returning listener full sock from req sock
insk_to_full_sk()was a wrong one.
It seems better to make semantics ofbpf_tcp_sock()andbpf_sk_fullsock()as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.Then it will be ok to call
bpf_sk_release(tp)when tp came frombpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be inacquired_refs.The additional
refcount_idwon't be necessary.bpf_sk_fullsock()andbpf_tcp_sock()will not callsk_to_full_skand the verifier will be copyingreg1->idintoreg0->id.In
release_reference()the verifier will do
if (regs[i].id == id)mark_reg_unknown(env, regs, i);for all socket types.
release_reference_state()will stay as-is.imo such logic will be easier to follow.
This implicit
sk_to_full_sk()makes the whole thing much harder for the verifier and for the bpf program writers.The new
bpf_get_listener_sock(sk)doesn't have to copy ID from reg1 to reg0 since req socket will not be returned frombpf_sk_lookup_tcpand its ID will not be stored inacuired_refs.Does it make sense ?
Quoted 8 lines
Quoted 6 lines
I like this idea. Many thanks for thinking it through!
Allowing
bpf_sk_release(tp), no need to callbpf_sk_release()on ptr returned frombpf_get_listener_sock(sk)and keep onereg->id.I think it should work. I will rework the patches.
Agree, makes sense, that seems much better fix.
While I was working on this change, based on the code, one issue I saw is:
if the bpf prog does this:
sk = bpf_sk_lookup_tcp();
/* if (!sk) ... */
fullsock = bpf_sk_fullsock(sk);
if (!fullsock) {
bpf_sk_release(sk); /* Fail. sk_reg->id not found in ref state */
return 0;
}The bpf_sk_release(sk) failed because the reference state has already been released by "release_reference_state(state, fullsock_reg->id)" during "if (!fullsock) /* handled by mark_ptr_or_null_regs(is_null == true) */" Logically, I think bpf_sk_release(sk) should not fail regardless of bpf_sk_fullsock() doing sk_to_full_sk() or not.
bpf_sk_fullsock() could disallow PTR_TO_SOCKET or PTR_TO_TCP_SOCK but that would be weird.
I think we still need two id. May be rename the refcount_id proposed in this patch to ref_obj_id which is the original refcounted object id.
If the sk_to_full_sk() is removed from bpf_sk_fullsock() and bpf_tcp_sock(), these two helpers become a simple cast (i.e. either return the same pointer or NULL). Then bpf_sk_release(fullsock) and bpf_sk_release(tp) could work:
- When is_null == true, release_reference_state(state, reg->id) is called.
- During bpf_sk_release(fullsock), release_reference(env, reg->ref_obj_id) is called so that sk, fullsock and tp with the same ref_obj_id will be mark_reg_unknown().
Re: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 99 lines · Martin Lau
Quoted 68 lines · Daniel Borkmann
Quoted 67 lines · Martin Lau
Quoted 66 lines · Alexei Starovoitov
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.I think the choice of returning listener full sock from req sock
insk_to_full_sk()was a wrong one.
It seems better to make semantics ofbpf_tcp_sock()andbpf_sk_fullsock()as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.Then it will be ok to call
bpf_sk_release(tp)when tp came frombpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be inacquired_refs.The additional
refcount_idwon't be necessary.bpf_sk_fullsock()andbpf_tcp_sock()will not callsk_to_full_skand the verifier will be copyingreg1->idintoreg0->id.In
release_reference()the verifier will do
if (regs[i].id == id)mark_reg_unknown(env, regs, i);for all socket types.
release_reference_state()will stay as-is.imo such logic will be easier to follow.
This implicit
sk_to_full_sk()makes the whole thing much harder for the verifier and for the bpf program writers.The new
bpf_get_listener_sock(sk)doesn't have to copy ID from reg1 to reg0 since req socket will not be returned frombpf_sk_lookup_tcpand its ID will not be stored inacuired_refs.Does it make sense ?
Quoted 8 lines
Quoted 6 lines
I like this idea. Many thanks for thinking it through!
Allowing
bpf_sk_release(tp), no need to callbpf_sk_release()on ptr returned frombpf_get_listener_sock(sk)and keep onereg->id.I think it should work. I will rework the patches.
Agree, makes sense, that seems much better fix.
While I was working on this change, based on the code, one issue I saw is:
if the bpf prog does this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
fullsock =bpf_sk_fullsock(sk);if (!fullsock) { bpf_sk_release(sk); /* Fail. sk_reg->id not found in ref state */ return 0; }The
bpf_sk_release(sk)failed because the reference state has already been released by "release_reference_state(state, fullsock_reg->id)" during "if (!fullsock) /* handled bymark_ptr_or_null_regs(is_null == true)*/" Logically, I thinkbpf_sk_release(sk)should not fail regardless ofbpf_sk_fullsock()doingsk_to_full_sk()or not.
bpf_sk_fullsock()could disallowPTR_TO_SOCKETorPTR_TO_TCP_SOCKbut that would be weird.
Quoted 8 lines
I think we still need two id. May be rename the
refcount_idproposed in this patch toref_obj_idwhich is the original refcounted object id.If the
sk_to_full_sk()is removed frombpf_sk_fullsock()andbpf_tcp_sock(), these two helpers become a simple cast (i.e. either return the same pointer or NULL). Thenbpf_sk_release(fullsock)andbpf_sk_release(tp)could work:- When
is_null== true,release_reference_state(state, reg->id)is called.
If I understand correctly, this works because we never acquire_reference() for tp/ fullsock,
making this a no-op?
Quoted 3 lines
- During
bpf_sk_release(fullsock),release_reference(env, reg->ref_obj_id)is called so that sk, fullsock and tp with the sameref_obj_idwill bemark_reg_unknown().
To clarify, the following states are possible:
* id == 0, ref_obj_id == 0: not a pointer / reference
* id != 0, reg_obj_id == 0: a reference which didn't have acquire_reference() called
* id != 0, reg_obj_id != 0: a reference which had acquire_reference() called
* id == 0, reg_obj_id: illegal
Sounds reasonable to me.
Signature · 4 lines
Lorenz Bauer | Systems Engineer 25 Lavington St., London SE1 0NZ www.cloudflare.com
Re: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 107 lines · Lorenz Bauer
Quoted 106 lines · Martin Lau
Quoted 68 lines · Daniel Borkmann
Quoted 67 lines · Martin Lau
Quoted 66 lines · Alexei Starovoitov
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.I think the choice of returning listener full sock from req sock
insk_to_full_sk()was a wrong one.
It seems better to make semantics ofbpf_tcp_sock()andbpf_sk_fullsock()as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.Then it will be ok to call
bpf_sk_release(tp)when tp came frombpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be inacquired_refs.The additional
refcount_idwon't be necessary.bpf_sk_fullsock()andbpf_tcp_sock()will not callsk_to_full_skand the verifier will be copyingreg1->idintoreg0->id.In
release_reference()the verifier will do
if (regs[i].id == id)mark_reg_unknown(env, regs, i);for all socket types.
release_reference_state()will stay as-is.imo such logic will be easier to follow.
This implicit
sk_to_full_sk()makes the whole thing much harder for the verifier and for the bpf program writers.The new
bpf_get_listener_sock(sk)doesn't have to copy ID from reg1 to reg0 since req socket will not be returned frombpf_sk_lookup_tcpand its ID will not be stored inacuired_refs.Does it make sense ?
Quoted 8 lines
Quoted 6 lines
I like this idea. Many thanks for thinking it through!
Allowing
bpf_sk_release(tp), no need to callbpf_sk_release()on ptr returned frombpf_get_listener_sock(sk)and keep onereg->id.I think it should work. I will rework the patches.
Agree, makes sense, that seems much better fix.
While I was working on this change, based on the code, one issue I saw is:
if the bpf prog does this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
fullsock =bpf_sk_fullsock(sk);if (!fullsock) { bpf_sk_release(sk); /* Fail. sk_reg->id not found in ref state */ return 0; }The
bpf_sk_release(sk)failed because the reference state has already been released by "release_reference_state(state, fullsock_reg->id)" during "if (!fullsock) /* handled bymark_ptr_or_null_regs(is_null == true)*/" Logically, I thinkbpf_sk_release(sk)should not fail regardless ofbpf_sk_fullsock()doingsk_to_full_sk()or not.
bpf_sk_fullsock()could disallowPTR_TO_SOCKETorPTR_TO_TCP_SOCKbut that would be weird.I think we still need two id. May be rename the
refcount_idproposed in this patch toref_obj_idwhich is the original refcounted object id.If the
sk_to_full_sk()is removed frombpf_sk_fullsock()andbpf_tcp_sock(), these two helpers become a simple cast (i.e. either return the same pointer or NULL). Thenbpf_sk_release(fullsock)andbpf_sk_release(tp)could work:
Quoted 5 lines
Quoted 1 line
- When
is_null== true,release_reference_state(state, reg->id)is called.If I understand correctly, this works because we never
acquire_reference()for tp/ fullsock,
making this a no-op?
Sorry for the late reply.
Correct. Those two helpers do not take ref, so release_reference_state() will not be called.
Quoted 11 lines
Quoted 4 lines
- During
bpf_sk_release(fullsock),release_reference(env, reg->ref_obj_id)is called so that sk, fullsock and tp with the sameref_obj_idwill bemark_reg_unknown().To clarify, the following states are possible:
* id == 0,ref_obj_id== 0: not a pointer / reference
* id != 0,reg_obj_id== 0: a reference which didn't haveacquire_reference()called
* id != 0,reg_obj_id!= 0: a reference which hadacquire_reference()called
* id == 0,reg_obj_id: illegal
In this 2 id(s) approach, I would think of it in this way. id and ref_obj_id are for two different purposes. One for null checking and one for reference tracking. Whenever its own purpose is served, it can be set to 0.
Regardless, I am working on another idea that does not require two id(s) in bpf_reg_state. I will give an update on this.
Re: [PATCH v3 bpf-next 1/2] bpf: Fix bpf_tcp_sock and bpf_sk_fullsock issue related to bpf_sk_release
Quoted 130 lines · Martin Lau
Quoted 107 lines · Lorenz Bauer
Quoted 106 lines · Martin Lau
Quoted 68 lines · Daniel Borkmann
Quoted 67 lines · Martin Lau
Quoted 66 lines · Alexei Starovoitov
Quoted 27 lines · Martin KaFai Lau
Lorenz Bauer [thanks!] reported that a ptr returned by
bpf_tcp_sock(sk)can still be accessed afterbpf_sk_release(sk).
Bothbpf_tcp_sock()andbpf_sk_fullsock()have the same issue. This patch addresses them together.A simple reproducer looks like this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
tp =bpf_tcp_sock(sk);
/* if (!tp) ... */bpf_sk_release(sk);snd_cwnd=tp->snd_cwnd; /* oops! The verifier does not complain. */The problem is the verifier did not scrub the register's states of the
tcp_sockptr (tp) afterbpf_sk_release(sk).[ Note that when calling
bpf_tcp_sock(sk), the sk is not always
refcount-acquired. e.g.bpf_tcp_sock(skb->sk). The verifier works fine for this case. ]Currently, the verifier does not track if a helper's return ptr (in
REG_0) is "carry"-ing one of its argument's refcount status. To carry this info, thereg1->idneeds to be stored in reg0. Thereg0->idhas already been used for NULL checking purpose. Hence, a new "refcount_id" is needed in "structbpf_reg_state".Quoted 7 lines
With
refcount_id, whenbpf_sk_release(sk)is called, the verifier can scrub all reg states which has arefcount_idmatch. It is done with the changes inrelease_reg_references().When acquiring and releasing a refcount, the
reg->idis still used. Hence, we cannot do "bpf_sk_release(tp)" in the above reproducer example.I think the choice of returning listener full sock from req sock
insk_to_full_sk()was a wrong one.
It seems better to make semantics ofbpf_tcp_sock()andbpf_sk_fullsock()as always type cast or null.
And have a separate helper for req socket that returns inet_reqsk(sk)->rsk_listener.Then it will be ok to call
bpf_sk_release(tp)when tp came frombpf_sk_lookup_tcp. The verifier will know that it's the case because its ID will be inacquired_refs.The additional
refcount_idwon't be necessary.bpf_sk_fullsock()andbpf_tcp_sock()will not callsk_to_full_skand the verifier will be copyingreg1->idintoreg0->id.In
release_reference()the verifier will do
if (regs[i].id == id)mark_reg_unknown(env, regs, i);for all socket types.
release_reference_state()will stay as-is.imo such logic will be easier to follow.
This implicit
sk_to_full_sk()makes the whole thing much harder for the verifier and for the bpf program writers.The new
bpf_get_listener_sock(sk)doesn't have to copy ID from reg1 to reg0 since req socket will not be returned frombpf_sk_lookup_tcpand its ID will not be stored inacuired_refs.Does it make sense ?
Quoted 8 lines
Quoted 6 lines
I like this idea. Many thanks for thinking it through!
Allowing
bpf_sk_release(tp), no need to callbpf_sk_release()on ptr returned frombpf_get_listener_sock(sk)and keep onereg->id.I think it should work. I will rework the patches.
Agree, makes sense, that seems much better fix.
While I was working on this change, based on the code, one issue I saw is:
if the bpf prog does this:
sk =
bpf_sk_lookup_tcp();
/* if (!sk) ... */
fullsock =bpf_sk_fullsock(sk);if (!fullsock) { bpf_sk_release(sk); /* Fail. sk_reg->id not found in ref state */ return 0; }The
bpf_sk_release(sk)failed because the reference state has already been released by "release_reference_state(state, fullsock_reg->id)" during "if (!fullsock) /* handled bymark_ptr_or_null_regs(is_null == true)*/" Logically, I thinkbpf_sk_release(sk)should not fail regardless ofbpf_sk_fullsock()doingsk_to_full_sk()or not.
bpf_sk_fullsock()could disallowPTR_TO_SOCKETorPTR_TO_TCP_SOCKbut that would be weird.I think we still need two id. May be rename the
refcount_idproposed in this patch toref_obj_idwhich is the original refcounted object id.If the
sk_to_full_sk()is removed frombpf_sk_fullsock()andbpf_tcp_sock(), these two helpers become a simple cast (i.e. either return the same pointer or NULL). Thenbpf_sk_release(fullsock)andbpf_sk_release(tp)could work:Quoted 5 lines
Quoted 1 line
- When
is_null== true,release_reference_state(state, reg->id)is called.If I understand correctly, this works because we never
acquire_reference()for tp/ fullsock,
making this a no-op?Sorry for the late reply.
Correct. Those two helpers do not take ref, so
release_reference_state()will not be called.Quoted 11 lines
Quoted 4 lines
- During
bpf_sk_release(fullsock),release_reference(env, reg->ref_obj_id)is called so that sk, fullsock and tp with the sameref_obj_idwill bemark_reg_unknown().To clarify, the following states are possible:
* id == 0,ref_obj_id== 0: not a pointer / reference
* id != 0,reg_obj_id== 0: a reference which didn't haveacquire_reference()called
* id != 0,reg_obj_id!= 0: a reference which hadacquire_reference()called
* id == 0,reg_obj_id: illegal
Quoted 8 lines
In this 2 id(s) approach, I would think of it in this way. id and
ref_obj_idare for two different purposes. One for null checking and one for reference tracking. Whenever its own purpose is served, it can be set to 0.Regardless, I am working on another idea that does not require two id(s) in
bpf_reg_state. I will give an update on this.
Ok, I'm holding off re-submitting the syn cookie helper until you've landed your fix, otherwise
I run into problems when making sk_release accept PTR_TO_SOCK_COMMON.
Signature · 4 lines
Lorenz Bauer | Systems Engineer 25 Lavington St., London SE1 0NZ www.cloudflare.com
PATCH v3 2/2 bpf: Test ref release issue in bpf_tcp_sock and bpf_sk_fullsock.
In reply to Martin KaFai LauAdding verifier tests to ensure the ptr returned from bpf_tcp_sock() and bpf_sk_fullsock() cannot be accessed after bpf_sk_release() is called. It is derived from a reproducer test from Lorenz Bauer.
--- .../selftests/bpf/verifier/ref_tracking.c | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+)
tools/testing/selftests/bpf/verifier/ref_tracking.c +73 -0
@@ -605,3 +605,76 @@ .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = ACCEPT, }, +{ + "reference tracking: use ptr from bpf_tcp_sock() after release", + .insns = { +BPF_SK_LOOKUP , +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_tcp_sock), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,3 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ), +BPF_EMIT_CALL (BPF_FUNC_sk_release), +BPF_EXIT_INSN (), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ), +BPF_EMIT_CALL (BPF_FUNC_sk_release), +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_7 , offsetof(struct bpf_tcp_sock , snd_cwnd)), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SCHED_CLS , + .result = REJECT, + .errstr = "invalid mem access", +}, +{ +"reference tracking: use ptr from bpf_sk_fullsock() after release" , + .insns = { +BPF_SK_LOOKUP , +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_sk_fullsock), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,3 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ), +BPF_EMIT_CALL (BPF_FUNC_sk_release), +BPF_EXIT_INSN (), +BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ), +BPF_EMIT_CALL (BPF_FUNC_sk_release), +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_7 , offsetof(struct bpf_sock , type)), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SCHED_CLS , + .result = REJECT, + .errstr = "invalid mem access", +}, +{ +"reference tracking: use ptr from bpf_sk_fullsock(tp) after release" , + .insns = { +BPF_SK_LOOKUP , +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_0 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_tcp_sock), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_0 ,0 ,3 ), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ), +BPF_EMIT_CALL (BPF_FUNC_sk_release), +BPF_EXIT_INSN (), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_sk_fullsock), +BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ), +BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_0 ), +BPF_EMIT_CALL (BPF_FUNC_sk_release), +BPF_JMP_IMM (BPF_JNE ,BPF_REG_6 ,0 ,1 ), +BPF_EXIT_INSN (), +BPF_LDX_MEM (BPF_W ,BPF_REG_0 ,BPF_REG_6 , offsetof(struct bpf_sock , type)), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_SCHED_CLS , + .result = REJECT, + .errstr = "invalid mem access", +},
Signature · 1 line
2.17.1