Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1256998pxb; Wed, 10 Feb 2021 04:15:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJypPDVWudeLm/9WtnbrJNRd8zbfLKJjeJiIIEltkOZvEnhysillsT/xLZKrC5QtQfXJaatX X-Received: by 2002:a17:906:3f8d:: with SMTP id b13mr2617899ejj.464.1612959321796; Wed, 10 Feb 2021 04:15:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612959321; cv=none; d=google.com; s=arc-20160816; b=MmaYSqulp2dSPb7URPir/MPiEUA/LGyrmLAMl7Be+Mvy4G3vUmOyLcByB87XuQxpsv 5UIzVIK1qa1fyk5el0HbrsVqMt4/WYeeBiYDR68yIRy2LqzvpJjT90wGNBPEY92QYy4e nesTLMc0RyVTN+2YATJkCo6td3wyePaH/Yr0MPLGuq0d7zc7lwEvzFz0wGanMTSvANfq Fh2FleLh+vdT4dkq9dedT1oKX8RoZ30kXqa19Jyztmw2Ghz2Da8m3EUL0RKsnlOR5AfJ ZC/3vQOUaVK0Z4uXt4FThjVjLq12KReOIdCSAro2vW0aWhwT9VzEp3m5rvKozP2nsa+K Uxiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=+9PeUG6v1r8aFGi6ORK3pCJUjeurz7TuWVSE9CMni0c=; b=Z8/YFcRBts2a9Bfyuk89SVToVyK/ip/uTh7OdcgYufTGwApDJM7tA497RTLzder5/E lZaoSDUuFO/F0YFSICOkTMMaY/mAGn81LkbxSdZmVs8hdAuP2dVuMpYtNIhILT3YN+5l GQUtSpit2FhOEQiQJCJcgeP7fzh5gChifJw8BgG8CtkN2KjhrGJcqFHR2pJyMtRFatCz RiJLxoNQq/MLpWD1XK+6N/a1gWDHextqbc/OUdDYEerp7lnbalxNL9O6F0mteWruQLkV +Fy4hHBMMcUq37xy8IdjxU518j7hXDGdIZu3C4t/wHUKpHih8TojFXYe0zLXGduIA8QS yaJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=vefKpoHH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d11si1102669eje.586.2021.02.10.04.14.57; Wed, 10 Feb 2021 04:15:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=vefKpoHH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231255AbhBJML7 (ORCPT + 99 others); Wed, 10 Feb 2021 07:11:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbhBJMIP (ORCPT ); Wed, 10 Feb 2021 07:08:15 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A90AC06121C for ; Wed, 10 Feb 2021 04:04:37 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id f16so1607099wmq.5 for ; Wed, 10 Feb 2021 04:04:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+9PeUG6v1r8aFGi6ORK3pCJUjeurz7TuWVSE9CMni0c=; b=vefKpoHHE9JY7wsXqdCVcvzKivbYRTXzF+0dgJaRh4tzwGHJlFAPcT6iyCPjtVk/QO 2nbSc5689PqMRWiH0mYBu5uUXuBmmxhvZbfVsxAu4AYbT9KkUXLQ8GUARAWnmp4QnaYC qrzOL4EoDzuwTZWyGC0rpjwwcObKmnDgs1a7A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+9PeUG6v1r8aFGi6ORK3pCJUjeurz7TuWVSE9CMni0c=; b=O0wZx5appvBdclhd5a+BTSbhvZZqaplQdmJpwrySRONvgQ1Y4N9OG3TnFsEXEDapLR byzKGxbEe5dDCgePlEsOuDP54futDKt5gUg8wC27RSLMxy7Mqqd2odX8AkSSLqEDKIDh 2V09M7xFXUf+Mm3BRjiCcY6WV4IxkqOF41D+1hTztw1oZqBoXeMMiFc8WqLpuAQWb7s1 UccHpMM0UK+2Aqm9chO7L/jOcBZ3ObtTThEaNSjOGtQ4ja08yCKDYrjTX55P6wkYHzTL ut4S98ahcRZBe0bCQtSgr9OYsKW3crf5pPB9oNImWm9hfMEi6aLwgR3VkRhm7e/yYyRm fUTg== X-Gm-Message-State: AOAM530lRzmUbR6038/50H8u8omok1luEOYzM1K5HRh5XcEYZET4qW0L ipy/6JWa8exQ6SDWe3ypf5+V+Q== X-Received: by 2002:a1c:9851:: with SMTP id a78mr2600203wme.66.1612958676276; Wed, 10 Feb 2021 04:04:36 -0800 (PST) Received: from antares.lan (c.3.c.9.d.d.c.e.0.a.6.8.a.9.e.c.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:ce9a:86a0:ecdd:9c3c]) by smtp.gmail.com with ESMTPSA id j7sm2837854wrp.72.2021.02.10.04.04.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:35 -0800 (PST) From: Lorenz Bauer To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel-team@cloudflare.com, Lorenz Bauer , bpf@vger.kernel.org, linux-alpha@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, netdev@vger.kernel.org, sparclinux@vger.kernel.org Subject: [PATCH bpf 0/4] Expose network namespace cookies to user space Date: Wed, 10 Feb 2021 12:04:21 +0000 Message-Id: <20210210120425.53438-1-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We're working on a user space control plane for the BPF sk_lookup hook [1]. The hook attaches to a network namespace and allows control over which socket receives a new connection / packet. Roughly, applications can give a socket to our user space component to participate in custom bind semantics. This creates an edge case where an application can provide us with a socket that lives in a different network namespace than our BPF sk_lookup program. We'd like to return an error in this case. Additionally, we have some user space state that is tied to the network namespace. We currently use the inode of the nsfs entry in a directory name, but this is suffers from inode reuse. I'm proposing to fix both of these issues by adding a new SO_NETNS_COOKIE socket option as well as a NS_GET_COOKIE ioctl. Using these we get a stable, unique identifier for a network namespace and check whether a socket belongs to the "correct" namespace. NS_GET_COOKIE could be renamed to NS_GET_NET_COOKIE. I kept the name generic because it seems like other namespace types could benefit from a cookie as well. I'm trying to land this via the bpf tree since this is where the netns cookie originated, please let me know if this isn't appropriate. 1: https://www.kernel.org/doc/html/latest/bpf/prog_sk_lookup.html Cc: bpf@vger.kernel.org Cc: linux-alpha@vger.kernel.org Cc: linux-api@vger.kernel.org Cc: linux-arch@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-kselftest@vger.kernel.org Cc: linux-mips@vger.kernel.org Cc: linux-parisc@vger.kernel.org Cc: netdev@vger.kernel.org Cc: sparclinux@vger.kernel.org Lorenz Bauer (4): net: add SO_NETNS_COOKIE socket option nsfs: add an ioctl to discover the network namespace cookie tools/testing: add test for NS_GET_COOKIE tools/testing: add a selftest for SO_NETNS_COOKIE arch/alpha/include/uapi/asm/socket.h | 2 + arch/mips/include/uapi/asm/socket.h | 2 + arch/parisc/include/uapi/asm/socket.h | 2 + arch/sparc/include/uapi/asm/socket.h | 2 + fs/nsfs.c | 9 +++ include/linux/sock_diag.h | 20 ++++++ include/net/net_namespace.h | 11 ++++ include/uapi/asm-generic/socket.h | 2 + include/uapi/linux/nsfs.h | 2 + net/core/filter.c | 9 ++- net/core/sock.c | 7 +++ tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 +- tools/testing/selftests/net/so_netns_cookie.c | 61 +++++++++++++++++++ tools/testing/selftests/nsfs/.gitignore | 1 + tools/testing/selftests/nsfs/Makefile | 2 +- tools/testing/selftests/nsfs/netns.c | 57 +++++++++++++++++ 17 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/net/so_netns_cookie.c create mode 100644 tools/testing/selftests/nsfs/netns.c -- 2.27.0