From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 390B0C2BA83 for ; Fri, 7 Feb 2020 21:50:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0E1B421775 for ; Fri, 7 Feb 2020 21:50:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727471AbgBGVuS (ORCPT ); Fri, 7 Feb 2020 16:50:18 -0500 Received: from www62.your-server.de ([213.133.104.62]:50278 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbgBGVuS (ORCPT ); Fri, 7 Feb 2020 16:50:18 -0500 Received: from sslproxy05.your-server.de ([78.46.172.2]) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1j0BVn-0006gr-L0; Fri, 07 Feb 2020 22:50:07 +0100 Received: from [85.7.42.192] (helo=pc-9.home) by sslproxy05.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j0BVn-000Qza-62; Fri, 07 Feb 2020 22:50:07 +0100 Subject: Re: [PATCH bpf] bpf: sockmap: check update requirements after locking To: Lorenz Bauer , John Fastabend , "David S. Miller" , Jakub Kicinski , Alexei Starovoitov , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko Cc: kernel-team@cloudflare.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org References: <20200207103713.28175-1-lmb@cloudflare.com> From: Daniel Borkmann Message-ID: <287e09b1-e510-4891-8d38-3a85e7a2efd4@iogearbox.net> Date: Fri, 7 Feb 2020 22:50:06 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20200207103713.28175-1-lmb@cloudflare.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.101.4/25717/Fri Feb 7 12:45:15 2020) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/7/20 11:37 AM, Lorenz Bauer wrote: > It's currently possible to insert sockets in unexpected states into > a sockmap, due to a TOCTTOU when updating the map from a syscall. > sock_map_update_elem checks that sk->sk_state == TCP_ESTABLISHED, > locks the socket and then calls sock_map_update_common. At this > point, the socket may have transitioned into another state, and > the earlier assumptions don't hold anymore. Crucially, it's > conceivable (though very unlikely) that a socket has become unhashed. > This breaks the sockmap's assumption that it will get a callback > via sk->sk_prot->unhash. > > Fix this by checking the (fixed) sk_type and sk_protocol without the > lock, followed by a locked check of sk_state. > > Unfortunately it's not possible to push the check down into > sock_(map|hash)_update_common, since BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB > run before the socket has transitioned from TCP_SYN_RECV into > TCP_ESTABLISHED. > > Signed-off-by: Lorenz Bauer > Fixes: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface") Applied, thanks!