Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3926289imm; Mon, 8 Oct 2018 11:48:27 -0700 (PDT) X-Google-Smtp-Source: ACcGV62mzmhTWEApUXBnRIvMvqX/WjWKy82RcnWsmPiYU7N0m38Gz9RFZYICzmakHn5NQox80i/D X-Received: by 2002:a17:902:ba8b:: with SMTP id k11-v6mr25342736pls.12.1539024507331; Mon, 08 Oct 2018 11:48:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539024507; cv=none; d=google.com; s=arc-20160816; b=M3NaCfUprNKCiiSNGe/vLCRoTz/FImhpokVreyakUioMORCXbgL+mx6+rAplgRALUl dDyjtfC4vrwKDhkoF6bHPM8RiJU3S+3cwj3Mlp4ztuoIdDzpolKbc446WJuipEpEBFsA FH+um+u8g7zbgRgCIETRpa5rQwib8/iYP8VBulPWgj9VGbyh57B9SmPEdVQj7n4oFzlF aeY6bmtzCmJGbhtQ7FtclO94/1l1asKLvmcBcdBhomoCm2pGhYAykORRQi4kkRyleMvt 8BWAh8NtLwK7aymPOuzbiObt5cf8uIX+646EV++2XltcBbIU74DhfYA4cGLoBRmNuFYt rz0g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MtJE8W22AcAFE3WTwKnAUm7n2KRAifB/dIhQu4AL/pM=; b=P+jsWQQ7O1jjoOFN0c1MuHX+mOodxhe/BYeikpQYf+m3pdLlj8biZiODT6VqBHINaJ CigbHvORq2r1q1y1ox8vcQ7byJIFUuqDhMPXsWPuuayGrA+Y2OeKIDR7D0kOAppjGAVR oCIhC7VBLU2CLGEwDNgr4XJyqISx0ACG3M9W2QzPRBmx0/MGZw6vyht8mdRRnkDAeS/x svQqUrdt0/rQMZG2F6TG5BygZW4Qig1ZQQjeilM7EkR/ma3g2kIcRWXrJX/JcBhhGv9u hWA6bgfLJDK6gYblJixxRCPZaNWo0JU+qwxgr7tk0voVJrfh5g6lH6JcUuLn/rjEbjpk zS6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MOZgMGjw; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w6-v6si18150591pgb.180.2018.10.08.11.48.12; Mon, 08 Oct 2018 11:48:27 -0700 (PDT) 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=@kernel.org header.s=default header.b=MOZgMGjw; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731553AbeJICBD (ORCPT + 99 others); Mon, 8 Oct 2018 22:01:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:49930 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728239AbeJICBC (ORCPT ); Mon, 8 Oct 2018 22:01:02 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 156DC214C2; Mon, 8 Oct 2018 18:47:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539024475; bh=oLfD5pePvazR8pMVlAWlYLXI6itcODynxHkSj9/0aNE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MOZgMGjwIxCzF9GWu7xz+ibnlMj2SPiUWOD+h7Vz+ei74o2cVcLzI6Z2FIjMdj++w pbqhOvQmxmN3DYv8pgOsorNhcYEny4gy9LGDL8HDsS10hKRk7aHSrjUxBHx0Suefjr 5XfWmSNGJQyNKcaxFLXXbrh0khNYwXT+QhUnusA4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Fastabend , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.18 052/168] bpf: avoid misuse of psock when TCP_ULP_BPF collides with another ULP Date: Mon, 8 Oct 2018 20:30:32 +0200 Message-Id: <20181008175622.029416852@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181008175620.043587728@linuxfoundation.org> References: <20181008175620.043587728@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: John Fastabend [ Upstream commit 597222f72a94118f593e4f32bf58ae7e049a0df1 ] Currently we check sk_user_data is non NULL to determine if the sk exists in a map. However, this is not sufficient to ensure the psock or the ULP ops are not in use by another user, such as kcm or TLS. To avoid this when adding a sock to a map also verify it is of the correct ULP type. Additionally, when releasing a psock verify that it is the TCP_ULP_BPF type before releasing the ULP. The error case where we abort an update due to ULP collision can cause this error path. For example, __sock_map_ctx_update_elem() [...] err = tcp_set_ulp_id(sock, TCP_ULP_BPF) <- collides with TLS if (err) <- so err out here goto out_free [...] out_free: smap_release_sock() <- calling tcp_cleanup_ulp releases the TLS ULP incorrectly. Fixes: 2f857d04601a ("bpf: sockmap, remove STRPARSER map_flags and add multi-map support") Signed-off-by: John Fastabend Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/sockmap.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) --- a/kernel/bpf/sockmap.c +++ b/kernel/bpf/sockmap.c @@ -1465,10 +1465,16 @@ static void smap_destroy_psock(struct rc schedule_work(&psock->gc_work); } +static bool psock_is_smap_sk(struct sock *sk) +{ + return inet_csk(sk)->icsk_ulp_ops == &bpf_tcp_ulp_ops; +} + static void smap_release_sock(struct smap_psock *psock, struct sock *sock) { if (refcount_dec_and_test(&psock->refcnt)) { - tcp_cleanup_ulp(sock); + if (psock_is_smap_sk(sock)) + tcp_cleanup_ulp(sock); write_lock_bh(&sock->sk_callback_lock); smap_stop_sock(psock, sock); write_unlock_bh(&sock->sk_callback_lock); @@ -1895,6 +1901,10 @@ static int __sock_map_ctx_update_elem(st * doesn't update user data. */ if (psock) { + if (!psock_is_smap_sk(sock)) { + err = -EBUSY; + goto out_progs; + } if (READ_ONCE(psock->bpf_parse) && parse) { err = -EBUSY; goto out_progs;