summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_fsverity.c
blob: 9e0f73e8189ce3bb0c6aa3b8faa15eb60906e8ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_kfuncs.h"

char _license[] SEC("license") = "GPL";

#ifndef SHA256_DIGEST_SIZE
#define SHA256_DIGEST_SIZE      32
#endif

#define SIZEOF_STRUCT_FSVERITY_DIGEST 4  /* sizeof(struct fsverity_digest) */

char expected_digest[SIZEOF_STRUCT_FSVERITY_DIGEST + SHA256_DIGEST_SIZE];
char digest[SIZEOF_STRUCT_FSVERITY_DIGEST + SHA256_DIGEST_SIZE];
__u32 monitored_pid;
__u32 got_fsverity;
__u32 digest_matches;

SEC("lsm.s/file_open")
int BPF_PROG(test_file_open, struct file *f)
{
	struct bpf_dynptr digest_ptr;
	__u32 pid;
	int ret;
	int i;

	pid = bpf_get_current_pid_tgid() >> 32;
	if (pid != monitored_pid)
		return 0;

	bpf_dynptr_from_mem(digest, sizeof(digest), 0, &digest_ptr);
	ret = bpf_get_fsverity_digest(f, &digest_ptr);
	if (ret < 0)
		return 0;
	got_fsverity = 1;

	for (i = 0; i < (int)sizeof(digest); i++) {
		if (digest[i] != expected_digest[i])
			return 0;
	}

	digest_matches = 1;
	return 0;
}