Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp4050932ybh; Tue, 17 Mar 2020 11:17:10 -0700 (PDT) X-Google-Smtp-Source: ADFU+vv8GrqbVQ7yF2D2v0fnx0t5PGHS1ynJcwcE01wkal5pECklgcRpOMis/ccmAFzCg8wYqPSU X-Received: by 2002:a9d:5381:: with SMTP id w1mr495561otg.220.1584469030521; Tue, 17 Mar 2020 11:17:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584469030; cv=none; d=google.com; s=arc-20160816; b=OXI1qynuJrXnHtAF1Qzhei1VLWW88/Z5NPoMjRNiSSWwSc+Pza+43s8tnvIZfzAgGU jDRwzCcqlKnK28WsH6FEiSWIh89hKeIL7aJgnlBFCV8KgDY1Zj1N+h60nhBBB8ICvse2 J9fwZh8WjIFGpKLMy7oUXAVbVm9UoVOM9QWj1YkDFC4duNIlbSOTWAovWvJLL1g4sGU1 Fgb2sRolrI8ollKT/b7BKP7abAYXfS5VGmV/ec/NVkuiAEjxm/WA5fmobvSLU4lqXQTH HgR4otpbJZ5s6WpTKwUSXv9yU8TzFccORn1Uxc6Jq2fdSe8u4R739jaOgnEd7Ag3yIaJ kSGg== 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 :subject:references:in-reply-to:message-id:cc:to:from:date :dkim-signature; bh=GPligt8SZ4vmyiqPXrOfMISNtA3jKedmIN0OJu/cq/k=; b=O75lXLLFIN8UWcvXUwZ8V1RkOyScHcV3npJKr2oOdqriSmvjaDyRmuVcERqjqpkrbo yRpX/uG4KVkgLmciZ/SDFVRyLvuKKvoeuL7uakhYYAUcVVsFs8UrqxUs8FLkF9os6ST3 aaAnbLopdDVvsEJogI7nZrJs45tNBQ7sDx8P35T5U1MHdCOq9qbxWOSjiMrW1Y2pY11c 1x/9o/cBFOS0p0K3sR1gaJhaC+xuRmPNAZDVw8jSPlHyUPSpdsCkyUacp+sLRJzFQDH0 PwmqJ9pkL0OyoOsjnYE6ANX4LEcTnR65IZ78Yc883fGBCiFjRA2SDvw5sj2957MBCYHy 5u9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="au03j/eH"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w31si1947762otb.59.2020.03.17.11.16.57; Tue, 17 Mar 2020 11:17:10 -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=@gmail.com header.s=20161025 header.b="au03j/eH"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726619AbgCQSQ2 (ORCPT + 99 others); Tue, 17 Mar 2020 14:16:28 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:33814 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726498AbgCQSQ2 (ORCPT ); Tue, 17 Mar 2020 14:16:28 -0400 Received: by mail-pf1-f194.google.com with SMTP id 23so12413064pfj.1; Tue, 17 Mar 2020 11:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:message-id:in-reply-to:references:subject :mime-version:content-transfer-encoding; bh=GPligt8SZ4vmyiqPXrOfMISNtA3jKedmIN0OJu/cq/k=; b=au03j/eHp4oDf86PBtb0jkX/mGTVoyM379kX+93nx3wKMkf+la9MBZCaQsFD54cBCA aCV55pzqQ+X0/Ci8qp7nIqhkrn+TRb1G0t63II+SEcabyPgZWJllk7LkdwHk08f3QNpk zKEHpCr5jQ0fwm5kwnqBJJrNWUEnqnH9rVnL4eVnVV+YnVynHZtE8aychr1mRpcR89fo n9b8BtyC743GyKu0TwZY+DA3KT8L9EOJm06FjmPMDjhdc0Egqsvvp8TphMYm/lXw1FOf ZM7/hsxq4kl2H5MfhS+sun8PifDdwzRL/kVv0N+qXUWucLCC92J3hfsHv3ajaTjnZPGY Nj5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to :references:subject:mime-version:content-transfer-encoding; bh=GPligt8SZ4vmyiqPXrOfMISNtA3jKedmIN0OJu/cq/k=; b=oPvp06daiJL6e4KChmo9J5Nn3xp6+MXxugdjOoYgsBjfNZdC2aI3qSvuGOWi2iJG2T 2VpBIjjKz35o//pmoO48Ua2KSi/XfI91BSrU9nw4lTrmYeTdbneQiAm8/albCi127Car UVEUD63lSryP/00UMg56O8oiYGQDSrjTKOXZ4IUWKwXjzAaTnYJ8JvCIx05iNkcHzrLw XJhhTeQszuFuI+MOn3R/3lsiXt7D/IO9sPy5T/CCABjGvaMGCkapry+YLqcAb16DOGLk dMxK7Gq9teGJjjMVIpUIAdNS+Mqxhom/GebuzkV+iHz6g+tchZObfHsQrl6aKxysRnrQ yX0Q== X-Gm-Message-State: ANhLgQ3kLTpPt3ZsMbJyInmOeDHtaIelqs7Xn+qFxUYdITfzz15E5+iQ yldgQJY+TDwHUM1UvYWWKwY= X-Received: by 2002:a63:4c5d:: with SMTP id m29mr480393pgl.376.1584468986648; Tue, 17 Mar 2020 11:16:26 -0700 (PDT) Received: from localhost ([184.63.162.180]) by smtp.gmail.com with ESMTPSA id x66sm3510220pgb.9.2020.03.17.11.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Mar 2020 11:16:25 -0700 (PDT) Date: Tue, 17 Mar 2020 11:16:17 -0700 From: John Fastabend To: Jakub Sitnicki , Lorenz Bauer Cc: John Fastabend , Daniel Borkmann , "David S. Miller" , Jakub Kicinski , Alexei Starovoitov , kernel-team@cloudflare.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <5e7113f16e7c6_278b2b1b264c65b445@john-XPS-13-9370.notmuch> In-Reply-To: <87imj3xb5t.fsf@cloudflare.com> References: <20200310174711.7490-1-lmb@cloudflare.com> <20200310174711.7490-5-lmb@cloudflare.com> <87imj3xb5t.fsf@cloudflare.com> Subject: Re: [PATCH 4/5] bpf: sockmap, sockhash: return file descriptors from privileged lookup Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Jakub Sitnicki wrote: > On Tue, Mar 10, 2020 at 06:47 PM CET, Lorenz Bauer wrote: > > Allow callers with CAP_NET_ADMIN to retrieve file descriptors from a > > sockmap and sockhash. O_CLOEXEC is enforced on all fds. > > > > Without this, it's difficult to resize or otherwise rebuild existing > > sockmap or sockhashes. > > > > Suggested-by: Jakub Sitnicki > > Signed-off-by: Lorenz Bauer > > --- > > net/core/sock_map.c | 19 +++++++++++++++++++ > > 1 file changed, 19 insertions(+) > > > > diff --git a/net/core/sock_map.c b/net/core/sock_map.c > > index 03e04426cd21..3228936aa31e 100644 > > --- a/net/core/sock_map.c > > +++ b/net/core/sock_map.c > > @@ -347,12 +347,31 @@ static void *sock_map_lookup(struct bpf_map *map, void *key) > > static int __sock_map_copy_value(struct bpf_map *map, struct sock *sk, > > void *value) > > { > > + struct file *file; > > + int fd; > > + > > switch (map->value_size) { > > case sizeof(u64): > > sock_gen_cookie(sk); > > *(u64 *)value = atomic64_read(&sk->sk_cookie); > > return 0; > > > > + case sizeof(u32): > > + if (!capable(CAP_NET_ADMIN)) > > + return -EPERM; > > + > > + fd = get_unused_fd_flags(O_CLOEXEC); > > + if (unlikely(fd < 0)) > > + return fd; > > + > > + read_lock_bh(&sk->sk_callback_lock); > > + file = get_file(sk->sk_socket->file); > > I think this deserves a second look. > > We don't lock the sock, so what if tcp_close orphans it before we enter > this critical section? Looks like sk->sk_socket might be NULL. > > I'd find a test that tries to trigger the race helpful, like: > > thread A: loop in lookup FD from map > thread B: loop in insert FD into map, close FD Agreed, this was essentially my question above as well. When the psock is created we call sock_hold() and will only do a sock_put() after an rcu grace period when its removed. So at least if you have the sock here it should have a sk_refcnt. (Note the user data is set to NULL so if you do reference psock you need to check its non-null.) Is that enough to ensure sk_socket? Seems not to me, tcp_close for example will still happen and call sock_orphan(sk) based on my admittddly quick look. Further, even if you do check sk->sk_socket is non-null what does it mean to return a file with a socket that is closed, deleted from the sock_map and psock removed? At this point is it just a dangling reference? Still a bit confused as well what would or should happen when the sock is closed after you have the file reference? I could probably dig up what exactly would happen but I think we need it in the commiit message so we understand it. I also didn't dig up the details here but if the receiver of the fd crashes or otherwise disappears this hopefully all get cleaned up? > > > + read_unlock_bh(&sk->sk_callback_lock); > > + > > + fd_install(fd, file); > > + *(u32 *)value = fd; > > + return 0; > > + > > default: > > return -ENOSPC; > > }