1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Google LLC.
11 #include <test_progs.h>
12 #include <linux/ring_buffer.h>
18 static int _run_measured_process(const char *measured_dir, u32 *monitored_pid,
21 int child_pid, child_status;
25 *monitored_pid = getpid();
26 execlp("./ima_setup.sh", "./ima_setup.sh", cmd, measured_dir,
30 } else if (child_pid > 0) {
31 waitpid(child_pid, &child_status, 0);
32 return WEXITSTATUS(child_status);
38 static int run_measured_process(const char *measured_dir, u32 *monitored_pid)
40 return _run_measured_process(measured_dir, monitored_pid, "run");
43 static u64 ima_hash_from_bpf[MAX_SAMPLES];
44 static int ima_hash_from_bpf_idx;
46 static int process_sample(void *ctx, void *data, size_t len)
48 if (ima_hash_from_bpf_idx >= MAX_SAMPLES)
51 ima_hash_from_bpf[ima_hash_from_bpf_idx++] = *((u64 *)data);
55 static void test_init(struct ima__bss *bss)
57 ima_hash_from_bpf_idx = 0;
59 bss->use_ima_file_hash = false;
60 bss->enable_bprm_creds_for_exec = false;
61 bss->enable_kernel_read_file = false;
62 bss->test_deny = false;
65 void test_test_ima(void)
67 char measured_dir_template[] = "/tmp/ima_measuredXXXXXX";
68 struct ring_buffer *ringbuf = NULL;
69 const char *measured_dir;
73 int err, duration = 0, fresh_digest_idx = 0;
74 struct ima *skel = NULL;
76 skel = ima__open_and_load();
77 if (CHECK(!skel, "skel_load", "skeleton failed\n"))
80 ringbuf = ring_buffer__new(bpf_map__fd(skel->maps.ringbuf),
81 process_sample, NULL, NULL);
82 if (!ASSERT_OK_PTR(ringbuf, "ringbuf"))
85 err = ima__attach(skel);
86 if (CHECK(err, "attach", "attach failed: %d\n", err))
89 measured_dir = mkdtemp(measured_dir_template);
90 if (CHECK(measured_dir == NULL, "mkdtemp", "err %d\n", errno))
93 snprintf(cmd, sizeof(cmd), "./ima_setup.sh setup %s", measured_dir);
95 if (CHECK(err, "failed to run command", "%s, errno = %d\n", cmd, errno))
100 * - Goal: obtain a sample with the bpf_ima_inode_hash() helper
101 * - Expected result: 1 sample (/bin/true)
103 test_init(skel->bss);
104 err = run_measured_process(measured_dir, &skel->bss->monitored_pid);
105 if (CHECK(err, "run_measured_process #1", "err = %d\n", err))
108 err = ring_buffer__consume(ringbuf);
109 ASSERT_EQ(err, 1, "num_samples_or_err");
110 ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
114 * - Goal: obtain samples with the bpf_ima_file_hash() helper
115 * - Expected result: 2 samples (./ima_setup.sh, /bin/true)
117 test_init(skel->bss);
118 skel->bss->use_ima_file_hash = true;
119 err = run_measured_process(measured_dir, &skel->bss->monitored_pid);
120 if (CHECK(err, "run_measured_process #2", "err = %d\n", err))
123 err = ring_buffer__consume(ringbuf);
124 ASSERT_EQ(err, 2, "num_samples_or_err");
125 ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
126 ASSERT_NEQ(ima_hash_from_bpf[1], 0, "ima_hash");
127 bin_true_sample = ima_hash_from_bpf[1];
131 * - Goal: confirm that bpf_ima_inode_hash() returns a non-fresh digest
133 * 1 sample (/bin/true: fresh) if commit 62622dab0a28 applied
134 * 2 samples (/bin/true: non-fresh, fresh) if commit 62622dab0a28 is
137 * If commit 62622dab0a28 ("ima: return IMA digest value only when
138 * IMA_COLLECTED flag is set") is applied, bpf_ima_inode_hash() refuses
139 * to give a non-fresh digest, hence the correct result is 1 instead of
142 test_init(skel->bss);
144 err = _run_measured_process(measured_dir, &skel->bss->monitored_pid,
146 if (CHECK(err, "modify-bin #3", "err = %d\n", err))
149 skel->bss->enable_bprm_creds_for_exec = true;
150 err = run_measured_process(measured_dir, &skel->bss->monitored_pid);
151 if (CHECK(err, "run_measured_process #3", "err = %d\n", err))
154 err = ring_buffer__consume(ringbuf);
155 ASSERT_GE(err, 1, "num_samples_or_err");
157 ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
158 ASSERT_EQ(ima_hash_from_bpf[0], bin_true_sample,
159 "sample_equal_or_err");
160 fresh_digest_idx = 1;
163 ASSERT_NEQ(ima_hash_from_bpf[fresh_digest_idx], 0, "ima_hash");
164 /* IMA refreshed the digest. */
165 ASSERT_NEQ(ima_hash_from_bpf[fresh_digest_idx], bin_true_sample,
166 "sample_equal_or_err");
170 * - Goal: verify that bpf_ima_file_hash() returns a fresh digest
171 * - Expected result: 4 samples (./ima_setup.sh: fresh, fresh;
172 * /bin/true: fresh, fresh)
174 test_init(skel->bss);
175 skel->bss->use_ima_file_hash = true;
176 skel->bss->enable_bprm_creds_for_exec = true;
177 err = run_measured_process(measured_dir, &skel->bss->monitored_pid);
178 if (CHECK(err, "run_measured_process #4", "err = %d\n", err))
181 err = ring_buffer__consume(ringbuf);
182 ASSERT_EQ(err, 4, "num_samples_or_err");
183 ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
184 ASSERT_NEQ(ima_hash_from_bpf[1], 0, "ima_hash");
185 ASSERT_NEQ(ima_hash_from_bpf[2], 0, "ima_hash");
186 ASSERT_NEQ(ima_hash_from_bpf[3], 0, "ima_hash");
187 ASSERT_NEQ(ima_hash_from_bpf[2], bin_true_sample,
188 "sample_different_or_err");
189 ASSERT_EQ(ima_hash_from_bpf[3], ima_hash_from_bpf[2],
190 "sample_equal_or_err");
192 skel->bss->use_ima_file_hash = false;
193 skel->bss->enable_bprm_creds_for_exec = false;
194 err = _run_measured_process(measured_dir, &skel->bss->monitored_pid,
196 if (CHECK(err, "restore-bin #3", "err = %d\n", err))
201 * - Goal: obtain a sample from the kernel_read_file hook
202 * - Expected result: 2 samples (./ima_setup.sh, policy_test)
204 test_init(skel->bss);
205 skel->bss->use_ima_file_hash = true;
206 skel->bss->enable_kernel_read_file = true;
207 err = _run_measured_process(measured_dir, &skel->bss->monitored_pid,
209 if (CHECK(err, "run_measured_process #5", "err = %d\n", err))
212 err = ring_buffer__consume(ringbuf);
213 ASSERT_EQ(err, 2, "num_samples_or_err");
214 ASSERT_NEQ(ima_hash_from_bpf[0], 0, "ima_hash");
215 ASSERT_NEQ(ima_hash_from_bpf[1], 0, "ima_hash");
219 * - Goal: ensure that the kernel_read_file hook denies an operation
220 * - Expected result: 0 samples
222 test_init(skel->bss);
223 skel->bss->enable_kernel_read_file = true;
224 skel->bss->test_deny = true;
225 err = _run_measured_process(measured_dir, &skel->bss->monitored_pid,
227 if (CHECK(!err, "run_measured_process #6", "err = %d\n", err))
230 err = ring_buffer__consume(ringbuf);
231 ASSERT_EQ(err, 0, "num_samples_or_err");
234 snprintf(cmd, sizeof(cmd), "./ima_setup.sh cleanup %s", measured_dir);
236 CHECK(err, "failed to run command", "%s, errno = %d\n", cmd, errno);
238 ring_buffer__free(ringbuf);