Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp704635ybl; Fri, 10 Jan 2020 05:27:29 -0800 (PST) X-Google-Smtp-Source: APXvYqwTw+MRCIAIia6SrXeH6mE+Z/+XSJHVIlrSCXKSMzeRwwrH050HSTkJD9cOS7vPuAKeeyMC X-Received: by 2002:aca:5793:: with SMTP id l141mr2071578oib.38.1578662849564; Fri, 10 Jan 2020 05:27:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578662849; cv=none; d=google.com; s=arc-20160816; b=jRTd0inA77J0jQhAYfdpODRA/nom9h+GCHrgGP61QhPKzpS4p6W2XgyHRLRiHE+ARy KE3+T7oTP4lIodC8o7hn4WxyvGv+uXlr3sfTHpOkmk306gh3u45ryf5xeQbaKGi/7DD3 FnTRMRqXT/LCYI3Vfx4kGDWTYentBtHvx+ejvb4LcrVKzomg6+slGqBcDA2iKnmOWfIB D2L7eg7bO5tCfbKpgHfFRmOQQhDB7Jr2Y0BHpA5grwkFMPuZ7ezLzZ/uOMqS/KsXZFPf deZXbckOAHBRkcLO8Ek8/KQWMQLuctq6L84wr9AoKa3fiJznkV8WByM/vEwmZM2xkQ1x XAXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pPkkayMqoY404hwMIWti3s+tl9smeoH8DKQs1LSa8V4=; b=RGcATGA26Hze4PJDa5sAxi2aw8FsNzGO7cvRql8Z0PF8wx98bMN2vGx7hr3ByyT6GF Ija+BO//uKs6laipGId8fGf2H1WbPme4T4bu//3zjhirilwGh/Hxy/TPIxP9UsVMDVsC Qn/4YSnXrusOqdeMtw7cR8jmUsBVGeEyW82fwCX6Xgj3XoYRln4ujdDaWyoyYTI3jL/X wsbsuSjUaKj0WwS4qcNAFwKT/Nw56JerBO/pHzXZ41MiHWGuNZYsryXH2LOgIrMwaSiO yLYPanrCC8ObBaEc86DsiBabHb/oKPuNQOAqEAjOD3Q/ry+Mng1/sLBlOKR6urgKETVQ rCcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=Vh0GCGNe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 73si1054652oii.60.2020.01.10.05.27.18; Fri, 10 Jan 2020 05:27:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=Vh0GCGNe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727867AbgAJNZw (ORCPT + 99 others); Fri, 10 Jan 2020 08:25:52 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:50511 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727553AbgAJNZv (ORCPT ); Fri, 10 Jan 2020 08:25:51 -0500 Received: by mail-wm1-f66.google.com with SMTP id a5so2009665wmb.0 for ; Fri, 10 Jan 2020 05:25:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pPkkayMqoY404hwMIWti3s+tl9smeoH8DKQs1LSa8V4=; b=Vh0GCGNeW4bRhhYlzpky0Y1SiaDf4HTWvITnqtsIimZNbZd61XZNuTT7JdXyszOYFY nlJMifjThgw+w7+kHxFAGI9rsKGHFZiapxKPIT9UgVfmIY7Z1W4SfmRcJ0f949TUTHjY kJ7J6GTkKlxSiM3vvdzlv8EjThPKbQMKMYMzo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pPkkayMqoY404hwMIWti3s+tl9smeoH8DKQs1LSa8V4=; b=bFNp9pSC0i1xhKXRaQUg26pTCC6zQoEMC0xLNpYOACRs8FZHIyoVqOFygx5tzwTFG4 qabSp2vhpSYKyQx9gb0AGjV8oFOkDw2CR1N7vB9rD1CjG00vG0mcuCidZqm/M4+OJUpq jSlRIFsokpa83TpUes41PNKa0K6XbyDHh6MNsqz0GZmU5UbQ2iqyXRgc8RzWWTGCQvER Z/c9Xz0YsufZMKG6Tiz/0wWo1Fk+33TMljB0RpeXbaYNWSnrOH3K2rDOkGBgV1ghuEJG eo0gPjP3o+YYctScjgn6uIZdiY+OI1HBM1R5UwTOLmtBGfW5OBnWyXrqeGqAfeTGdFQA iBYA== X-Gm-Message-State: APjAAAX+4BOdobtuWbpd3POQeRpPShkX2g6EoGUWHlZI1QrB4sHciXED 0IWiby9Ww5cCbz+0bTJKr5XA5g== X-Received: by 2002:a7b:c93a:: with SMTP id h26mr4290073wml.83.1578662749896; Fri, 10 Jan 2020 05:25:49 -0800 (PST) Received: from localhost.localdomain ([2a06:98c0:1000:8250:51ce:3bcb:24c3:4be9]) by smtp.gmail.com with ESMTPSA id m10sm2204033wrx.19.2020.01.10.05.25.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2020 05:25:49 -0800 (PST) From: Lorenz Bauer To: kafai@fb.com, Alexei Starovoitov , Daniel Borkmann , "David S. Miller" , Lorenz Bauer , Joe Stringer , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: kernel-team@cloudflare.com Subject: [PATCH bpf v2] net: bpf: don't leak time wait and request sockets Date: Fri, 10 Jan 2020 13:23:36 +0000 Message-Id: <20200110132336.26099-1-lmb@cloudflare.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200109115749.12283-1-lmb@cloudflare.com> References: <20200109115749.12283-1-lmb@cloudflare.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It's possible to leak time wait and request sockets via the following BPF pseudo code:   sk = bpf_skc_lookup_tcp(...) if (sk) bpf_sk_release(sk) If sk->sk_state is TCP_NEW_SYN_RECV or TCP_TIME_WAIT the refcount taken by bpf_skc_lookup_tcp is not undone by bpf_sk_release. This is because sk_flags is re-used for other data in both kinds of sockets. The check !sock_flag(sk, SOCK_RCU_FREE) therefore returns a bogus result. Check that sk_flags is valid by calling sk_fullsock. Skip checking SOCK_RCU_FREE if we already know that sk is not a full socket. Fixes: edbf8c01de5a ("bpf: add skc_lookup_tcp helper") Fixes: f7355a6c0497 ("bpf: Check sk_fullsock() before returning from bpf_sk_lookup()") Signed-off-by: Lorenz Bauer --- net/core/filter.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 42fd17c48c5f..41820ba0774c 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5277,8 +5277,7 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, if (sk) { sk = sk_to_full_sk(sk); if (!sk_fullsock(sk)) { - if (!sock_flag(sk, SOCK_RCU_FREE)) - sock_gen_put(sk); + sock_gen_put(sk); return NULL; } } @@ -5315,8 +5314,7 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, if (sk) { sk = sk_to_full_sk(sk); if (!sk_fullsock(sk)) { - if (!sock_flag(sk, SOCK_RCU_FREE)) - sock_gen_put(sk); + sock_gen_put(sk); return NULL; } } @@ -5383,7 +5381,8 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { BPF_CALL_1(bpf_sk_release, struct sock *, sk) { - if (!sock_flag(sk, SOCK_RCU_FREE)) + /* Only full sockets have sk->sk_flags. */ + if (!sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE)) sock_gen_put(sk); return 0; } -- 2.20.1