tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c
Extension
.c
Size
8443 bytes
Lines
292
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#include <stdlib.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <linux/fsverity.h>
#include <unistd.h>
#include <test_progs.h>
#include "test_get_xattr.skel.h"
#include "test_set_remove_xattr.skel.h"
#include "test_fsverity.skel.h"

static const char testfile[] = "/tmp/test_progs_fs_kfuncs";

static void test_get_xattr(const char *name, const char *value, bool allow_access)
{
	struct test_get_xattr *skel = NULL;
	int fd = -1, err;
	int v[32];

	fd = open(testfile, O_CREAT | O_RDONLY, 0644);
	if (!ASSERT_GE(fd, 0, "create_file"))
		return;

	close(fd);
	fd = -1;

	err = setxattr(testfile, name, value, strlen(value) + 1, 0);
	if (err && errno == EOPNOTSUPP) {
		printf("%s:SKIP:local fs doesn't support xattr (%d)\n"
		       "To run this test, make sure /tmp filesystem supports xattr.\n",
		       __func__, errno);
		test__skip();
		goto out;
	}

	if (!ASSERT_OK(err, "setxattr"))
		goto out;

	skel = test_get_xattr__open_and_load();
	if (!ASSERT_OK_PTR(skel, "test_get_xattr__open_and_load"))
		goto out;

	skel->bss->monitored_pid = getpid();
	err = test_get_xattr__attach(skel);

	if (!ASSERT_OK(err, "test_get_xattr__attach"))
		goto out;

	fd = open(testfile, O_RDONLY, 0644);

	if (!ASSERT_GE(fd, 0, "open_file"))
		goto out;

	/* Trigger security_inode_getxattr */
	err = getxattr(testfile, name, v, sizeof(v));

	if (allow_access) {
		ASSERT_EQ(err, -1, "getxattr_return");
		ASSERT_EQ(errno, EINVAL, "getxattr_errno");
		ASSERT_EQ(skel->bss->found_xattr_from_file, 1, "found_xattr_from_file");
		ASSERT_EQ(skel->bss->found_xattr_from_dentry, 1, "found_xattr_from_dentry");
	} else {
		ASSERT_EQ(err, strlen(value) + 1, "getxattr_return");
		ASSERT_EQ(skel->bss->found_xattr_from_file, 0, "found_xattr_from_file");
		ASSERT_EQ(skel->bss->found_xattr_from_dentry, 0, "found_xattr_from_dentry");
	}

out:
	close(fd);
	test_get_xattr__destroy(skel);
	remove(testfile);
}

/* xattr value we will set to security.bpf.foo */
static const char value_foo[] = "hello";

static void read_and_validate_foo(struct test_set_remove_xattr *skel)
{
	char value_out[32];
	int err;

	err = getxattr(testfile, skel->rodata->xattr_foo, value_out, sizeof(value_out));
	ASSERT_EQ(err, sizeof(value_foo), "getxattr size foo");
	ASSERT_EQ(strncmp(value_out, value_foo, sizeof(value_foo)), 0, "strncmp value_foo");
}

static void set_foo(struct test_set_remove_xattr *skel)
{

Annotation

Implementation Notes