Skip to content

Commit d5bee73

Browse files
jsitnickidavem330
authored andcommitted
net/tls: Annotate access to sk_prot with READ_ONCE/WRITE_ONCE
sockmap performs lockless writes to sk->sk_prot on the following paths: tcp_bpf_{recvmsg|sendmsg} / sock_map_unref sk_psock_put sk_psock_drop sk_psock_restore_proto WRITE_ONCE(sk->sk_prot, proto) To prevent load/store tearing [1], and to make tooling aware of intentional shared access [2], we need to annotate other sites that access sk_prot with READ_ONCE/WRITE_ONCE macros. Change done with Coccinelle with following semantic patch: @@ expression E; identifier I; struct sock *sk; identifier sk_prot =~ "^sk_prot$"; @@ ( E = -sk->sk_prot +READ_ONCE(sk->sk_prot) | -sk->sk_prot = E +WRITE_ONCE(sk->sk_prot, E) | -sk->sk_prot +READ_ONCE(sk->sk_prot) ->I ) Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5bb4c45 commit d5bee73

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

net/tls/tls_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static int tls_do_allocation(struct sock *sk,
366366
if (!offload_ctx->open_record) {
367367
if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
368368
sk->sk_allocation))) {
369-
sk->sk_prot->enter_memory_pressure(sk);
369+
READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk);
370370
sk_stream_moderate_sndbuf(sk);
371371
return -ENOMEM;
372372
}

net/tls/tls_main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ void update_sk_prot(struct sock *sk, struct tls_context *ctx)
6969
{
7070
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
7171

72-
sk->sk_prot = &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf];
72+
WRITE_ONCE(sk->sk_prot,
73+
&tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
7374
}
7475

7576
int wait_on_pending_writer(struct sock *sk, long *timeo)
@@ -312,7 +313,7 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
312313
write_lock_bh(&sk->sk_callback_lock);
313314
if (free_ctx)
314315
rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
315-
sk->sk_prot = ctx->sk_proto;
316+
WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
316317
if (sk->sk_write_space == tls_write_space)
317318
sk->sk_write_space = ctx->sk_write_space;
318319
write_unlock_bh(&sk->sk_callback_lock);
@@ -621,14 +622,14 @@ struct tls_context *tls_ctx_create(struct sock *sk)
621622

622623
mutex_init(&ctx->tx_lock);
623624
rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
624-
ctx->sk_proto = sk->sk_prot;
625+
ctx->sk_proto = READ_ONCE(sk->sk_prot);
625626
return ctx;
626627
}
627628

628629
static void tls_build_proto(struct sock *sk)
629630
{
630631
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
631-
const struct proto *prot = sk->sk_prot;
632+
const struct proto *prot = READ_ONCE(sk->sk_prot);
632633

633634
/* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
634635
if (ip_ver == TLSV6 &&

0 commit comments

Comments
 (0)