From patchwork Thu May 12 16:50:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12847914 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB3C1C433FE for ; Thu, 12 May 2022 16:51:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348145AbiELQvT (ORCPT ); Thu, 12 May 2022 12:51:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356694AbiELQvB (ORCPT ); Thu, 12 May 2022 12:51:01 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8AB12DA8E for ; Thu, 12 May 2022 09:50:59 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8E7DF6201B for ; Thu, 12 May 2022 16:50:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2F76C34117; Thu, 12 May 2022 16:50:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1652374259; bh=LMwb7sE6yW4zYLMvgtLTavWoydCEk72JSc/VUWzllZQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NsYzf3eFPfys/nn7e3knlM4cU9rr1tCYmO6knVFJLW5P3h3UOx+mH47R+na/O0HJ1 jFjwiky6dkRKC6S18RdMiYHnnRGqK8Gb732H9KMXRcSp4o4r4Uknv74Uk4N1svfkiy 9FAB+YCfzOnh+ZL8h1OvysfPgVjAWpGKzkABI19Wxni1tR0Rzf/qQOYEzKitPRfXuF ZOmhdyO9uQKrdMOGSJIOIdnaMWZrcylXb7qCOIAIFb7RXU/b2HmSOgzX6VD+98t+DM cr9sxrds6gta6AuUOSIz51Jzm7YA250J+qznLrOgku0g57NC8c5dNRmRVcLvpQKw4J jxYVqhMzBIeHQ== From: KP Singh To: bpf@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Subject: [PATCH bpf-next 2/2] bpf/selftests: Add a selftest for bpf_getxattr Date: Thu, 12 May 2022 16:50:51 +0000 Message-Id: <20220512165051.224772-3-kpsingh@kernel.org> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog In-Reply-To: <20220512165051.224772-1-kpsingh@kernel.org> References: <20220512165051.224772-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- .../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 +#include +#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 +#include + +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; +}