From: KP Singh <kpsingh@kernel.org>
To: bpf@vger.kernel.org
Cc: KP Singh <kpsingh@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Subject: [PATCH bpf-next 2/2] bpf/selftests: Add a selftest for bpf_getxattr
Date: Thu, 12 May 2022 16:50:51 +0000 [thread overview]
Message-ID: <20220512165051.224772-3-kpsingh@kernel.org> (raw)
In-Reply-To: <20220512165051.224772-1-kpsingh@kernel.org>
A simple test that adds an xattr on a copied /bin/ls and reads it back
when the copied ls is executed.
Signed-off-by: KP Singh <kpsingh@kernel.org>
---
.../testing/selftests/bpf/prog_tests/xattr.c | 58 +++++++++++++++++++
tools/testing/selftests/bpf/progs/xattr.c | 34 +++++++++++
2 files changed, 92 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/xattr.c
create mode 100644 tools/testing/selftests/bpf/progs/xattr.c
diff --git a/tools/testing/selftests/bpf/prog_tests/xattr.c b/tools/testing/selftests/bpf/prog_tests/xattr.c
new file mode 100644
index 000000000000..442b6c1aed0e
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/xattr.c
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2022 Google LLC.
+ */
+
+#include <test_progs.h>
+#include <sys/xattr.h>
+#include "xattr.skel.h"
+
+#define XATTR_NAME "security.bpf"
+#define XATTR_VALUE "test_progs"
+
+static unsigned int duration;
+
+void test_xattr(void)
+{
+ struct xattr *skel = NULL;
+ char tmp_dir_path[] = "/tmp/xattrXXXXXX";
+ char tmp_exec_path[64];
+ char cmd[256];
+ int err;
+
+ if (CHECK(!mkdtemp(tmp_dir_path), "mkdtemp",
+ "unable to create tmpdir: %d\n", errno))
+ goto close_prog;
+
+ snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_ls",
+ tmp_dir_path);
+ snprintf(cmd, sizeof(cmd), "cp /bin/ls %s", tmp_exec_path);
+ if (CHECK_FAIL(system(cmd)))
+ goto close_prog_rmdir;
+
+ if (CHECK(setxattr(tmp_exec_path, XATTR_NAME, XATTR_VALUE,
+ sizeof(XATTR_VALUE), 0),
+ "setxattr", "unable to setxattr: %d", errno))
+ goto close_prog_rmdir;
+
+ skel = xattr__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_load"))
+ goto close_prog_rmdir;
+
+ err = xattr__attach(skel);
+ if (!ASSERT_OK(err, "xattr__attach failed"))
+ goto close_prog_rmdir;
+
+ snprintf(cmd, sizeof(cmd), "%s -l", tmp_exec_path);
+ if (CHECK_FAIL(system(cmd)))
+ goto close_prog_rmdir;
+
+ ASSERT_EQ(skel->bss->result, 1, "xattr result");
+
+close_prog_rmdir:
+ snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path);
+ system(cmd);
+close_prog:
+ xattr__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/xattr.c b/tools/testing/selftests/bpf/progs/xattr.c
new file mode 100644
index 000000000000..3bf6966a7900
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/xattr.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2022 Google LLC.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define XATTR_NAME "security.bpf"
+#define XATTR_VALUE "test_progs"
+
+__u64 result = 0;
+
+SEC("lsm.s/bprm_committed_creds")
+void BPF_PROG(bprm_cc, struct linux_binprm *bprm)
+{
+ struct task_struct *current = bpf_get_current_task_btf();
+ char dir_xattr_value[64];
+ int xattr_sz;
+
+ xattr_sz = bpf_getxattr(bprm->file->f_path.mnt->mnt_userns,
+ bprm->file->f_path.dentry, XATTR_NAME,
+ dir_xattr_value, 64);
+
+ if (xattr_sz <= 0)
+ return;
+
+ if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE))
+ result = 1;
+}
--
2.36.0.550.gb090851708-goog
prev parent reply other threads:[~2022-05-12 16:51 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-12 16:50 [PATCH bpf-next 0/2] Add bpf_getxattr KP Singh
2022-05-12 16:50 ` [PATCH bpf-next 1/2] bpf: Implement bpf_getxattr helper KP Singh
2022-05-12 17:29 ` Alexei Starovoitov
2022-05-13 0:20 ` KP Singh
2022-05-12 16:50 ` KP Singh [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220512165051.224772-3-kpsingh@kernel.org \
--to=kpsingh@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox