Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1047796ybh; Wed, 11 Mar 2020 16:28:25 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuU9lnH6l1qWmb4qltW42bDdDES+TZRPyyUKQuitBHxtRv2uDAPJAroUV82Nm7liYByZ0Pt X-Received: by 2002:aca:4e08:: with SMTP id c8mr737176oib.143.1583969305831; Wed, 11 Mar 2020 16:28:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583969305; cv=none; d=google.com; s=arc-20160816; b=n+4fWXx2llQUyTFGCGd7X0ecB/qJ72ja1w9FGUcoFqStA0i8ZfR1ANE76qe1XzDaDr cp40Q9j2YMWUU8v9P67JC/agW5KWrtuTaZh6kMl5CWy9jfFqcGkyxffJuGI2A5X+vYt+ xueVvvplxP2n8VecHDKFFP0m2hooPVu+xtlTKunNl/DBqSWgZFzUynQ8cQgV6XC2JzlB GD4sD7qexdS5L99KD+qdchc8mYX2C1Y6Zy3dorNEeGbHnPTr6nxr/uBrkqJsI5M7sIWA 14MqavJcdCHldmQ30z39uXEDDoGOw7IxO2VXDGoDR/QixxeVUJNQbFdTuBYaZJ4Hbjcl nXhw== 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=5R3NN9uod3puJ0jWV50H8GmRPXUpheE41lirnX7p648=; b=T3L7BRyEhL1gdotDlqtDcKdPwpJoZHv406rx56MQfttZ5xbCkLDOiFpjxpcdN4J3QQ kvCD8cOrIl259G2H6tbOnneQ+8Mef/RZn/2/2yGK/lNfxGZKteAg8KOZduw3FCnq628A JkdQnzR3FP/85FY82tqUwYW7OKfOFPhl5neybQux/WgIlpxkMhR99PFW9Xq2wvxKe9gE uaytgEY6alfaz7+nf71sXYiLlHW3HqvW+vXAUzdbPTsbpnwoISx7gs/Pixscq9YGIE/o TpFdEcO4jBfnO0gj/4wdsuYrPFFu3XTBuA55v4NvgXJ2wIFLZyhbOS5zri9d/00JZ2DR mCvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=pUb+BoV8; 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 r17si1839072ota.160.2020.03.11.16.28.13; Wed, 11 Mar 2020 16:28:25 -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=pUb+BoV8; 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 S1731534AbgCKX1x (ORCPT + 99 others); Wed, 11 Mar 2020 19:27:53 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:32989 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731392AbgCKX1w (ORCPT ); Wed, 11 Mar 2020 19:27:52 -0400 Received: by mail-pg1-f194.google.com with SMTP id m5so2053012pgg.0; Wed, 11 Mar 2020 16:27:51 -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=5R3NN9uod3puJ0jWV50H8GmRPXUpheE41lirnX7p648=; b=pUb+BoV8jLVjeU16S4PEOg3tBCYrl5lqKfkvCxyc+ZfjKZgAaINNZ8KJ/1oTikFe8s mPDwEAOF5NDA0lxRp36m7kCFnwbrHMHhiYJxbiwcJ5Qljsv/chgvvzxjYblRFDayLY3s 0RRjmrXiN+op5rNo9h1txoctx3RBu1uVQM3899eVOktFKtV+MZx4WEVNkCj8TNFZasE+ oO7Uya6N8CwDWFMJaL9PzIkXIrQa+9nceOo2fRZ8IrJOdnKvZUtxb/2h8gEW2ynMfFew LdP3kLU3b2xGOILVhG0SzNi4c+/3oBFEp+ovA7zCLYW23q22bP2UgA3HI+tb3g2Gv/Lg Eh5Q== 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=5R3NN9uod3puJ0jWV50H8GmRPXUpheE41lirnX7p648=; b=k06GApmRV5i0kT8YZSSESuezdXZwFRvOW3Xlf7wjghVpfGJqMFgBZhikbR0axHmHq0 NgriyldMbjLooZjJKY82ae1u+UWel6hQtGfr3N4vRLtYXwGbx8e7VADeGTaWUNoxlxS6 Y9DrwSUsmtjAxyybRz4Mz3RQmmHJuGDyoxA5kKYPhc0oh43fxeI6mdeONqbTLbGBOtXg QBWWpfj2KDB+3a3gyyjsNYPTOo9OHdFPYPpknUi5KeBY86+yOV8os/6zDWBqma8iWw/e SvfUkn8aSs6HXNhpTka1hmxS9qIH9r0fO5ldzL/YYBH9r6qRZEBOPVywtM+B9edSBw2M 8ExQ== X-Gm-Message-State: ANhLgQ08mWuqKd9xq5SRPs8byTuxGOK5y7FVUifZDnuoPz3ClHqvJ8CK Op8rv679zPGvE8vpOusR2KQ= X-Received: by 2002:a62:cdcc:: with SMTP id o195mr3095617pfg.323.1583969271400; Wed, 11 Mar 2020 16:27:51 -0700 (PDT) Received: from localhost ([184.63.162.180]) by smtp.gmail.com with ESMTPSA id s18sm6479240pjp.24.2020.03.11.16.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 16:27:50 -0700 (PDT) Date: Wed, 11 Mar 2020 16:27:41 -0700 From: John Fastabend To: Lorenz Bauer , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer , "David S. Miller" , Jakub Kicinski , Alexei Starovoitov Cc: kernel-team@cloudflare.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <5e6973ed90f8d_20552ab9153405b4ca@john-XPS-13-9370.notmuch> In-Reply-To: <20200310174711.7490-5-lmb@cloudflare.com> References: <20200310174711.7490-1-lmb@cloudflare.com> <20200310174711.7490-5-lmb@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 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); > + read_unlock_bh(&sk->sk_callback_lock); > + > + fd_install(fd, file); > + *(u32 *)value = fd; > + return 0; > + Hi Lorenz, Can you say something about what happens if the sk is deleted from the map or the sock is closed/unhashed ideally in the commit message so we have it for later reference. I guess because we are in an rcu block here the sk will be OK and psock reference will exist until after the rcu block at least because of call_rcu(). If the psock is destroyed from another path then the fd will still point at the sock. correct? Thanks.