Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1068113ybv; Fri, 7 Feb 2020 13:51:33 -0800 (PST) X-Google-Smtp-Source: APXvYqzRqQP+L8Qh2y5SArf6q+rmqf7/3XbK6nplkcGtVDosuRh283lOoNwH43SNc6+89WphA98R X-Received: by 2002:a05:6830:2110:: with SMTP id i16mr1093979otc.337.1581112293606; Fri, 07 Feb 2020 13:51:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581112293; cv=none; d=google.com; s=arc-20160816; b=rMJxeW4SI7ULGghDnjKBBZz+4mK3NjzMye5/OLyaWYn3laxHbjAiCoRZvfYlbUWAD7 RK11vYcGb51A93MRyxiJ1dDkYn2ZbK4cqymrqtjhuG5Raw1jki1AaTu+62/afoPpNleX 4+vMc3+MtjBo64YiQQPnioOKdm8MxDE704pauVxd/SeV3l6CL+RSAFZzY2rZUkjFYNBS NKwYL9PAR+OnLeaRDLhzOdsdtqtx9Q6Jqw16tRbL0qTcTf25EDWYrQxbSgvS8Odt8abp 8dkMEmPXG4gTIL/wY2vxG2Et7ZSiLPfnNWqOwuSGLoivqVvkHkjQcjREmXyoB713MjMy 2Hdw== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=mnI2uHR4urfPdHklhcne82Xva8UtUFqgWmle6G1P2yU=; b=osALwSi7JjC8pM3p8ndTYr0GdIkqK0i1tsm8S9+wNzXuWhY/D/Hu+YpHTSNdTxOd0m by2nvJbSJM2WEWmB2lyvW6xFXNEHfT2EMtkCjDoVoTify0QNT780tIYpAgBFvy82UZbf 8M8pzlqa3EAvI6vqZztCh5xK52PQPDbBQH2dF602fkmUwG9T5kYKA0Qrcc6bbOBjZPjq yX1BJ5tDjboY1D7MN69W7jgN30ZS9QrsqvoFN/tkPqamAOny+ZY2pqkaaIKrSHFEq1ql w0BI9d4o84N4GV1QYa7ikLd/PXwPN6aRk6jSDQnji4/Y3lYI5TfoJZ6jQVercXeW0g5y gdZQ== ARC-Authentication-Results: i=1; mx.google.com; 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 g17si324255otr.261.2020.02.07.13.51.21; Fri, 07 Feb 2020 13:51:33 -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; 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 S1727456AbgBGVuS (ORCPT + 99 others); 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!