Skip to content

Commit c9c6290

Browse files
borkmannintel-lab-lkp
authored andcommitted
tls: fix NULL pointer dereference on poll
While hacking on kTLS, I ran into the following panic from an unprivileged netserver / netperf TCP session: BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 PGD 800000037f378067 P4D 800000037f378067 PUD 3c0e61067 PMD 0 Oops: 0010 [#1] SMP KASAN PTI CPU: 1 PID: 2289 Comm: netserver Not tainted 4.17.0+ torvalds#139 Hardware name: LENOVO 20FBCTO1WW/20FBCTO1WW, BIOS N1FET47W (1.21 ) 11/28/2016 RIP: 0010: (null) Code: Bad RIP value. RSP: 0018:ffff88036abcf740 EFLAGS: 00010246 RAX: dffffc0000000000 RBX: ffff88036f5f6800 RCX: 1ffff1006debed26 RDX: ffff88036abcf920 RSI: ffff8803cb1a4f00 RDI: ffff8803c258c280 RBP: ffff8803c258c280 R08: ffff8803c258c280 R09: ffffed006f559d48 R10: ffff88037aacea43 R11: ffffed006f559d49 R12: ffff8803c258c280 R13: ffff8803cb1a4f20 R14: 00000000000000db R15: ffffffffc168a350 FS: 00007f7e631f4700(0000) GS:ffff8803d1c80000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffffffffffffffd6 CR3: 00000003ccf64005 CR4: 00000000003606e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: ? tls_sw_poll+0xa4/0x160 [tls] ? sock_poll+0x20a/0x680 ? do_select+0x77b/0x11a0 ? poll_schedule_timeout.constprop.12+0x130/0x130 ? pick_link+0xb00/0xb00 ? read_word_at_a_time+0x13/0x20 ? vfs_poll+0x270/0x270 ? deref_stack_reg+0xad/0xe0 ? __read_once_size_nocheck.constprop.6+0x10/0x10 [...] Debugging further, it turns out that calling into ctx->sk_poll() is invalid since sk_poll itself is NULL which was saved from the original TCP socket in order for tls_sw_poll() to invoke it. Looks like the recent conversion from poll to poll_mask callback started in 1525242 ("net: add support for ->poll_mask in proto_ops") missed to eventually convert kTLS, too: TCP's ->poll was converted over to the ->poll_mask in commit 2c7d3da ("net/tcp: convert to ->poll_mask") and therefore kTLS wrongly saved the ->poll old one which is now NULL. Convert kTLS over to use ->poll_mask instead. Also instead of POLLIN | POLLRDNORM use the proper EPOLLIN | EPOLLRDNORM bits as the case in tcp_poll_mask() as well that is mangled here. Fixes: 2c7d3da ("net/tcp: convert to ->poll_mask") Signed-off-by: Daniel Borkmann <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dave Watson <[email protected]>
1 parent 349b71d commit c9c6290

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

include/net/tls.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ struct tls_sw_context_rx {
109109

110110
struct strparser strp;
111111
void (*saved_data_ready)(struct sock *sk);
112-
unsigned int (*sk_poll)(struct file *file, struct socket *sock,
113-
struct poll_table_struct *wait);
112+
__poll_t (*sk_poll_mask)(struct socket *sock, __poll_t events);
114113
struct sk_buff *recv_pkt;
115114
u8 control;
116115
bool decrypted;
@@ -225,8 +224,7 @@ void tls_sw_free_resources_tx(struct sock *sk);
225224
void tls_sw_free_resources_rx(struct sock *sk);
226225
int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
227226
int nonblock, int flags, int *addr_len);
228-
unsigned int tls_sw_poll(struct file *file, struct socket *sock,
229-
struct poll_table_struct *wait);
227+
__poll_t tls_sw_poll_mask(struct socket *sock, __poll_t events);
230228
ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
231229
struct pipe_inode_info *pipe,
232230
size_t len, unsigned int flags);

net/tls/tls_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static int __init tls_register(void)
712712
build_protos(tls_prots[TLSV4], &tcp_prot);
713713

714714
tls_sw_proto_ops = inet_stream_ops;
715-
tls_sw_proto_ops.poll = tls_sw_poll;
715+
tls_sw_proto_ops.poll_mask = tls_sw_poll_mask;
716716
tls_sw_proto_ops.splice_read = tls_sw_splice_read;
717717

718718
#ifdef CONFIG_TLS_DEVICE

net/tls/tls_sw.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -915,23 +915,22 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
915915
return copied ? : err;
916916
}
917917

918-
unsigned int tls_sw_poll(struct file *file, struct socket *sock,
919-
struct poll_table_struct *wait)
918+
__poll_t tls_sw_poll_mask(struct socket *sock, __poll_t events)
920919
{
921-
unsigned int ret;
922920
struct sock *sk = sock->sk;
923921
struct tls_context *tls_ctx = tls_get_ctx(sk);
924922
struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
923+
__poll_t mask;
925924

926-
/* Grab POLLOUT and POLLHUP from the underlying socket */
927-
ret = ctx->sk_poll(file, sock, wait);
925+
/* Grab EPOLLOUT and EPOLLHUP from the underlying socket */
926+
mask = ctx->sk_poll_mask(sock, events);
928927

929-
/* Clear POLLIN bits, and set based on recv_pkt */
930-
ret &= ~(POLLIN | POLLRDNORM);
928+
/* Clear EPOLLIN bits, and set based on recv_pkt */
929+
mask &= ~(EPOLLIN | EPOLLRDNORM);
931930
if (ctx->recv_pkt)
932-
ret |= POLLIN | POLLRDNORM;
931+
mask |= EPOLLIN | EPOLLRDNORM;
933932

934-
return ret;
933+
return mask;
935934
}
936935

937936
static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
@@ -1188,7 +1187,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
11881187
sk->sk_data_ready = tls_data_ready;
11891188
write_unlock_bh(&sk->sk_callback_lock);
11901189

1191-
sw_ctx_rx->sk_poll = sk->sk_socket->ops->poll;
1190+
sw_ctx_rx->sk_poll_mask = sk->sk_socket->ops->poll_mask;
11921191

11931192
strp_check_rcv(&sw_ctx_rx->strp);
11941193
}

0 commit comments

Comments
 (0)