Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp475230ybv; Fri, 7 Feb 2020 02:57:58 -0800 (PST) X-Google-Smtp-Source: APXvYqyojBj4YQEoMdC7A83dkN9+ZpGq8ORf4avaRYnqR3lMx6WTDDthihajGV1JR+wf7JH4qULO X-Received: by 2002:aca:5150:: with SMTP id f77mr1639980oib.52.1581073078395; Fri, 07 Feb 2020 02:57:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581073078; cv=none; d=google.com; s=arc-20160816; b=NocqzStyMBZJYJL74yqGq5GNZRW6BFq5oTIsC2fK2K0LZ2mEvgvWeT+eWEJkI46Q+g ggqA3z1Uxxw3VscqSltyBQpq9o22oOGb/M5PxIk8zTbm48JgCBy6X2LC0dbBr+7u1aDo y8tEk6oEfktx93gtI9LiHrid0/3U0nnsO73pk1/YFeAK2ucToG/Zfyap7x46TsrXBzTb doD7TBOJUameeqRjKIc1TiqVUlToKrq3w7Sw6Il9wnRZV99Iw/vN3zHK/Qthld4w+DEl N+8uEoqK2rmUpoVjM+qKCHL4Jq9VF//pNi6cKpNSNe9LY3wTrX63kdvsf0qGrhrkRujl DUvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:in-reply-to :subject:cc:to:from:user-agent:references:dkim-signature; bh=Uixk1JfPRfZiskd4b5V0w8uIZZOxCYEiV7QUtBMbUWw=; b=Okr2gVuqyUEWJfPVfxhbtG4cCXGMxqdfIjAZyTvECe1PNZGuJZpaeGYVl7QvmxteMB xHxIzQql+Q9XKu5ACXqDIXVJZeYN3tyHUCgsxEdaRt0oXzcDZ0DXaB0s9XrQdmDtH6yf klsTkF1n48GVZE4q26jymxwDiJ/vzDCrWdUAurGCAHOII8MV6Xflpdrgk1m3J8RnX0Ci dCa2unLtVp5Hg1PsXeMd8RwdqET0Kug6o4U7cV7xnxkCWptDobW7ZBr+Uv2MSvuxpxy5 LqR12rHMXciKYLo9yRdwajZlTFiPUlVuUmS/zt5kciLUB2CM8pAisMhaVLp5AbLmwY/h 2u+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=RcYhavdZ; 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 k11si1519595otp.176.2020.02.07.02.57.45; Fri, 07 Feb 2020 02:57:58 -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=RcYhavdZ; 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 S1726942AbgBGK4m (ORCPT + 99 others); Fri, 7 Feb 2020 05:56:42 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:36853 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726798AbgBGK4m (ORCPT ); Fri, 7 Feb 2020 05:56:42 -0500 Received: by mail-wm1-f68.google.com with SMTP id p17so2237043wma.1 for ; Fri, 07 Feb 2020 02:56:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=Uixk1JfPRfZiskd4b5V0w8uIZZOxCYEiV7QUtBMbUWw=; b=RcYhavdZsLpqgP20VvYdLOC0mNiSX7lTL2sqhQ59iQIXWipEC6zKbuCeHoP4GY0t0B EZIx3Ipyt0LTHJziQIZ1vSHojZsnKA2EhadixIE+ES0+DXsiu4vG0jm9Bte5yyIaHdZk XYZfwMvMAtFU9q03VwDKV4aSHkIyO25r0r+mo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=Uixk1JfPRfZiskd4b5V0w8uIZZOxCYEiV7QUtBMbUWw=; b=eh9/Pvz279qaSWd+Gvr1WYWoSPKjAuZamL2UYxwITT/EY8xgxgqPFsYNFw2TsnYUGy tOguWV2a22NmTK5aK8CcN2ughEWlW3yyuQet26bAAm7wZ+EInNa/7pPdnoS0cQ8sA7+Y COvuzzGZXmAu8ZuEz1bbJwBWVhZtbOZn8CuAmJZ2S8mRw+yP0HT9jwWePe9MtMYVgIGI lO+38r7j8MNSNmmTs8mWoCzdUydNEyH+tJOZt64HynpgNS9BeucbMUM6pdn+Zz2/wDds h+WqYGkwK4PI4Q8DYQrMColHqmmJWXLhNtSGTznm/8nTPVxlsZkto9V+ziAiyVZtFB2n SZhw== X-Gm-Message-State: APjAAAXNE2MQfqBiH3md9YbkUj+Ln3ExUPbdVMrn9wTaLReTc8Os6JOJ TEBORYCNtXXBb6Jgk0yxwhh77Q== X-Received: by 2002:a7b:cb91:: with SMTP id m17mr3612169wmi.146.1581072999762; Fri, 07 Feb 2020 02:56:39 -0800 (PST) Received: from cloudflare.com ([176.221.114.230]) by smtp.gmail.com with ESMTPSA id a62sm2953727wmh.33.2020.02.07.02.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2020 02:56:39 -0800 (PST) References: <20200207103713.28175-1-lmb@cloudflare.com> User-agent: mu4e 1.1.0; emacs 26.3 From: Jakub Sitnicki To: Lorenz Bauer Cc: John Fastabend , Daniel Borkmann , "David S. Miller" , Jakub Kicinski , Alexei Starovoitov , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , kernel-team@cloudflare.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH bpf] bpf: sockmap: check update requirements after locking In-reply-to: <20200207103713.28175-1-lmb@cloudflare.com> Date: Fri, 07 Feb 2020 11:56:38 +0100 Message-ID: <87y2temzrt.fsf@cloudflare.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 07, 2020 at 11:37 AM CET, 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") > --- > net/core/sock_map.c | 16 ++++++++++------ > 1 file changed, 10 insertions(+), 6 deletions(-) > > diff --git a/net/core/sock_map.c b/net/core/sock_map.c > index 8998e356f423..36a2433e183f 100644 > --- a/net/core/sock_map.c > +++ b/net/core/sock_map.c > @@ -416,14 +416,16 @@ static int sock_map_update_elem(struct bpf_map *map, void *key, > ret = -EINVAL; > goto out; > } > - if (!sock_map_sk_is_suitable(sk) || > - sk->sk_state != TCP_ESTABLISHED) { > + if (!sock_map_sk_is_suitable(sk)) { > ret = -EOPNOTSUPP; > goto out; > } > > sock_map_sk_acquire(sk); > - ret = sock_map_update_common(map, idx, sk, flags); > + if (sk->sk_state != TCP_ESTABLISHED) > + ret = -EOPNOTSUPP; > + else > + ret = sock_map_update_common(map, idx, sk, flags); > sock_map_sk_release(sk); > out: > fput(sock->file); > @@ -739,14 +741,16 @@ static int sock_hash_update_elem(struct bpf_map *map, void *key, > ret = -EINVAL; > goto out; > } > - if (!sock_map_sk_is_suitable(sk) || > - sk->sk_state != TCP_ESTABLISHED) { > + if (!sock_map_sk_is_suitable(sk)) { > ret = -EOPNOTSUPP; > goto out; > } > > sock_map_sk_acquire(sk); > - ret = sock_hash_update_common(map, key, sk, flags); > + if (sk->sk_state != TCP_ESTABLISHED) > + ret = -EOPNOTSUPP; > + else > + ret = sock_hash_update_common(map, key, sk, flags); > sock_map_sk_release(sk); > out: > fput(sock->file); > -- > 2.20.1 Thanks for fixing this, Lorenz. I'll adapt socket state checks on update in "Extend SOCKMAP to store listening sockets" series accordingly. Reviewed-by: Jakub Sitnicki