Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1258229pxb; Wed, 10 Feb 2021 04:17:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcCoSvWXKLqwOYt7HAc5TffgZglrT3HAHcePw6dafBzbyyvwyNvte40qoix11gngCSzfTQ X-Received: by 2002:a17:906:b217:: with SMTP id p23mr2757678ejz.126.1612959425215; Wed, 10 Feb 2021 04:17:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612959425; cv=none; d=google.com; s=arc-20160816; b=Q+RdZFkVP/DwkyMVJJvXm+Tmu63UTkeF/f7k6R7Zvest7jJPbmsadAlt4LyNYKV5JK sHD9ceN/CaBCfkXWfcTeKWeL3L1p8eaHYZuiFRgEVaqhwQixV9yehcvPL83D4CTb/PGH oxXKdL1/UvC9Jc7vkRl7aHOp5EaEnw7hWx5/C++rq+ApnaP6ARCNxv12/Fw2YfV+UMZW zgfCMhyCS57/0u3Lh2zPYu9ZtgtNjy8OP0t2YZDKUdF25QOeswodm+TWSGmOZVThdQ9M ibLxao6cnb6OFYh7p/sSLUOwq2cct2A2iLevRG7C+5IDF9irQI7/Oov6Ld1FAmzCkk1p 8VFw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OSkPPwU1Tuk4mhkI6x3HPNgww1cxj5Q62PGKwYGTKNY=; b=zE5fzRYv76UPOXg/t8RiRtZLJFUxfydIa03BgZv99fZ8nybCLH3ybEEj9/KJKH68ic /iCtjZBcmYmHif50gfFU3b9obhOhwWIZgKf9KLFNrMs5AfxTt0mchsG5ZJlhb/45ijmo pcmxapx4+1Qc0s4sOEIXkAK8ye2j89bRMk8Nm7rkSWzehSFlbrrnpp+p/PkYUIEqux3Z qQxyfL8jR/XJ6oKxZKSPYpbKqJznmwBN4nC6CHsmBWE1c28vCt3e67+xd3Ri5bz+m112 UwehRJyahX1ONispsl+Tr4p+m0ouv5v/+amZSnUN+umFJjmYeF8ZvrpKfZ1T/1U0YSr1 IiMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=NLp2isIe; 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 w1si1455172edl.186.2021.02.10.04.16.42; Wed, 10 Feb 2021 04:17:05 -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=NLp2isIe; 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 S231486AbhBJMO1 (ORCPT + 99 others); Wed, 10 Feb 2021 07:14:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231566AbhBJMLQ (ORCPT ); Wed, 10 Feb 2021 07:11:16 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C42EC061A30 for ; Wed, 10 Feb 2021 04:04:42 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id j11so1613158wmi.3 for ; Wed, 10 Feb 2021 04:04:42 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=OSkPPwU1Tuk4mhkI6x3HPNgww1cxj5Q62PGKwYGTKNY=; b=NLp2isIeKm3p7bKBYZ+D2qSKM0rJhJ/2WySCYUP3OlU0+xBqJTZLX3aQPLYZSl0V+I H8xodj6XFViiqc3kwBVfr8HkmXkbuRDrSn1RFjycwaIFkUpeE5EO7bTho83PSLVbfaP3 fm0GNWYRYietbSVI6OcmUJOUriEgJFYy4gHUg= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=OSkPPwU1Tuk4mhkI6x3HPNgww1cxj5Q62PGKwYGTKNY=; b=joSRH7thChCxxa80k+Ik5yHSMUXpQDVCKFrE87Fglid1SUOA4aNUb/FJ7APvs2XZKu d9Z2W2AfBIteaJoVFFR3p+1q5RgGdWLruGEPdX7Esk+DuEdmmlJKhYOp2cctQ6bpQ9QJ JsBdi2xraMeHmdQbCsWJjYGkscpW3HvlmtZ3teE3rZonFK+CEzk7Y8C3Gbw/Rr/3TGZ3 OVNjrxaMMr2osYrjn3fLvHLn5I1eBiucphL5NBTwI9cWlhBBci5rVCK930g7HpAC0oP8 L8IjGZInXuECOzksA2vwh9sRfrLMrbriwPBN6u40D+qqjumLZGdPdvOThRNtFsv5uq3Z L3HQ== X-Gm-Message-State: AOAM532PFlnvtRHDlWbgLGSEQ2tDlBxkRw00sEZ2q3+QoGCvNz8sVwiT b+rHHA865jW6I3xdEwTQTSHqhQ== X-Received: by 2002:a1c:6487:: with SMTP id y129mr2680722wmb.106.1612958681086; Wed, 10 Feb 2021 04:04:41 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:40 -0800 (PST) From: Lorenz Bauer To: Shuah Khan , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf 3/4] tools/testing: add test for NS_GET_COOKIE Date: Wed, 10 Feb 2021 12:04:24 +0000 Message-Id: <20210210120425.53438-4-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210210120425.53438-1-lmb@cloudflare.com> References: <20210210120425.53438-1-lmb@cloudflare.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Check that NS_GET_COOKIE returns a non-zero value, and that distinct network namespaces have different cookies. Signed-off-by: Lorenz Bauer --- tools/testing/selftests/nsfs/.gitignore | 1 + tools/testing/selftests/nsfs/Makefile | 2 +- tools/testing/selftests/nsfs/netns.c | 57 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/nsfs/netns.c diff --git a/tools/testing/selftests/nsfs/.gitignore b/tools/testing/selftests/nsfs/.gitignore index ed79ebdf286e..ca31b216215b 100644 --- a/tools/testing/selftests/nsfs/.gitignore +++ b/tools/testing/selftests/nsfs/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only owner pidns +netns diff --git a/tools/testing/selftests/nsfs/Makefile b/tools/testing/selftests/nsfs/Makefile index dd9bd50b7b93..93793cdb5a7c 100644 --- a/tools/testing/selftests/nsfs/Makefile +++ b/tools/testing/selftests/nsfs/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -TEST_GEN_PROGS := owner pidns +TEST_GEN_PROGS := owner pidns netns CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/nsfs/netns.c b/tools/testing/selftests/nsfs/netns.c new file mode 100644 index 000000000000..8ab862667b45 --- /dev/null +++ b/tools/testing/selftests/nsfs/netns.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#define NSIO 0xb7 +#define NS_GET_COOKIE _IO(NSIO, 0x5) + +#define pr_err(fmt, ...) \ + ({ \ + fprintf(stderr, "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + 1; \ + }) + +int main(int argc, char *argvp[]) +{ + uint64_t cookie1, cookie2; + char path[128]; + int ns; + + snprintf(path, sizeof(path), "/proc/%d/ns/net", getpid()); + ns = open(path, O_RDONLY); + if (ns < 0) + return pr_err("Unable to open %s", path); + + if (ioctl(ns, NS_GET_COOKIE, &cookie1)) + return pr_err("Unable to get first namespace cookie"); + + if (!cookie1) + return pr_err("NS_GET_COOKIE returned zero first cookie"); + + close(ns); + if (unshare(CLONE_NEWNET)) + return pr_err("unshare"); + + ns = open(path, O_RDONLY); + if (ns < 0) + return pr_err("Unable to open %s", path); + + if (ioctl(ns, NS_GET_COOKIE, &cookie2)) + return pr_err("Unable to get second namespace cookie"); + + if (!cookie2) + return pr_err("NS_GET_COOKIE returned zero second cookie"); + + if (cookie1 == cookie2) + return pr_err("NS_GET_COOKIE returned identical cookies for distinct ns"); + + close(ns); + return 0; +} -- 2.27.0