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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/lsm_bdev.c
Extension
.c
Size
5712 bytes
Lines
222
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

struct verity_info {
	__u8  has_roothash;
	__u8  sig_valid;
	__u32 setintegrity_cnt;
};

#define DATA_SIZE_MB	8
#define HASH_SIZE_MB	1
#define DM_NAME		"bpf_test_verity"
#define DM_DEV_PATH	"/dev/mapper/" DM_NAME

/* Run a command and optionally capture the first line of stdout. */
static int run_cmd(const char *cmd, char *out, size_t out_sz)
{
	FILE *fp;
	int ret;

	fp = popen(cmd, "r");
	if (!fp)
		return -1;

	if (out && out_sz > 0) {
		if (!fgets(out, out_sz, fp))
			out[0] = '\0';
		/* strip trailing newline */
		out[strcspn(out, "\n")] = '\0';
	}

	ret = pclose(fp);
	return WIFEXITED(ret) ? WEXITSTATUS(ret) : -1;
}

static bool has_prerequisites(void)
{
	if (getuid() != 0) {
		printf("SKIP: must be root\n");
		return false;
	}

	if (run_cmd("modprobe loop 2>/dev/null", NULL, 0) &&
	    run_cmd("ls /dev/loop-control 2>/dev/null", NULL, 0)) {
		printf("SKIP: no loop device support\n");
		return false;
	}

	if (run_cmd("modprobe dm-verity 2>/dev/null", NULL, 0) &&
	    run_cmd("dmsetup targets 2>/dev/null | grep -q verity", NULL, 0)) {
		printf("SKIP: dm-verity module not available\n");
		return false;
	}

	if (run_cmd("which veritysetup >/dev/null 2>&1", NULL, 0)) {
		printf("SKIP: veritysetup not found\n");
		return false;
	}

	return true;
}

void test_lsm_bdev(void)
{
	char data_img[] = "/tmp/bpf_verity_data_XXXXXX";
	char hash_img[] = "/tmp/bpf_verity_hash_XXXXXX";
	char data_loop[64] = {};
	char hash_loop[64] = {};
	char roothash[256] = {};
	char cmd[512];
	int data_fd = -1, hash_fd = -1;
	struct lsm_bdev *skel = NULL;
	struct verity_info val;
	struct stat st;
	__u32 dev_key;
	int err;

	if (!has_prerequisites()) {
		test__skip();
		return;
	}

	/* Clean up any stale device from a previous crashed run. */
	snprintf(cmd, sizeof(cmd), "dmsetup remove %s 2>/dev/null", DM_NAME);
	run_cmd(cmd, NULL, 0);

	/* Create temporary image files. */
	data_fd = mkstemp(data_img);
	if (!ASSERT_OK_FD(data_fd, "mkstemp data"))
		return;

	hash_fd = mkstemp(hash_img);
	if (!ASSERT_OK_FD(hash_fd, "mkstemp hash"))

Annotation

Implementation Notes