Merge pull request #25608 from poettering/dissect-moar
dissect: add dissection policies
This commit is contained in:
@@ -95,6 +95,7 @@ simple_tests += files(
|
||||
'test-hostname-setup.c',
|
||||
'test-hostname-util.c',
|
||||
'test-id128.c',
|
||||
'test-image-policy.c',
|
||||
'test-import-util.c',
|
||||
'test-in-addr-prefix-util.c',
|
||||
'test-in-addr-util.c',
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "image-policy.h"
|
||||
#include "pretty-print.h"
|
||||
#include "string-util.h"
|
||||
#include "tests.h"
|
||||
#include "pager.h"
|
||||
|
||||
static void test_policy(const ImagePolicy *p, const char *name) {
|
||||
_cleanup_free_ char *as_string = NULL, *as_string_simplified = NULL;
|
||||
_cleanup_free_ ImagePolicy *parsed = NULL;
|
||||
|
||||
assert_se(image_policy_to_string(p, /* simplified= */ false, &as_string) >= 0);
|
||||
assert_se(image_policy_to_string(p, /* simplified= */ true, &as_string_simplified) >= 0);
|
||||
|
||||
printf("%s%s", ansi_underline(), name);
|
||||
|
||||
if (!streq(as_string_simplified, name)) {
|
||||
printf(" → %s", as_string_simplified);
|
||||
|
||||
if (!streq(as_string, as_string_simplified))
|
||||
printf(" (aka %s)", as_string);
|
||||
}
|
||||
|
||||
printf("%s\n", ansi_normal());
|
||||
|
||||
assert_se(image_policy_from_string(as_string, &parsed) >= 0);
|
||||
assert_se(image_policy_equal(p, parsed));
|
||||
parsed = image_policy_free(parsed);
|
||||
|
||||
assert_se(image_policy_from_string(as_string_simplified, &parsed) >= 0);
|
||||
assert_se(image_policy_equivalent(p, parsed));
|
||||
parsed = image_policy_free(parsed);
|
||||
|
||||
for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
|
||||
_cleanup_free_ char *k = NULL;
|
||||
PartitionPolicyFlags f;
|
||||
|
||||
f = image_policy_get(p, d);
|
||||
if (f < 0) {
|
||||
f = image_policy_get_exhaustively(p, d);
|
||||
assert_se(f >= 0);
|
||||
assert_se(partition_policy_flags_to_string(f, /* simplified= */ true, &k) >= 0);
|
||||
|
||||
printf("%s\t%s → n/a (exhaustively: %s)%s\n", ansi_grey(), partition_designator_to_string(d), k, ansi_normal());
|
||||
} else {
|
||||
assert_se(partition_policy_flags_to_string(f, /* simplified= */ true, &k) >= 0);
|
||||
printf("\t%s → %s\n", partition_designator_to_string(d), k);
|
||||
}
|
||||
}
|
||||
|
||||
_cleanup_free_ char *w = NULL;
|
||||
assert_se(partition_policy_flags_to_string(image_policy_default(p), /* simplified= */ true, &w) >= 0);
|
||||
printf("\tdefault → %s\n", w);
|
||||
}
|
||||
|
||||
static void test_policy_string(const char *t) {
|
||||
_cleanup_free_ ImagePolicy *parsed = NULL;
|
||||
|
||||
assert_se(image_policy_from_string(t, &parsed) >= 0);
|
||||
test_policy(parsed, t);
|
||||
}
|
||||
|
||||
static void test_policy_equiv(const char *s, bool (*func)(const ImagePolicy *p)) {
|
||||
_cleanup_(image_policy_freep) ImagePolicy *p = NULL;
|
||||
|
||||
assert_se(image_policy_from_string(s, &p) >= 0);
|
||||
|
||||
assert_se(func(p));
|
||||
assert_se(func == image_policy_equiv_ignore || !image_policy_equiv_ignore(p));
|
||||
assert_se(func == image_policy_equiv_allow || !image_policy_equiv_allow(p));
|
||||
assert_se(func == image_policy_equiv_deny || !image_policy_equiv_deny(p));
|
||||
}
|
||||
|
||||
TEST_RET(test_image_policy_to_string) {
|
||||
test_policy(&image_policy_allow, "*");
|
||||
test_policy(&image_policy_ignore, "-");
|
||||
test_policy(&image_policy_deny, "~");
|
||||
test_policy(&image_policy_sysext, "sysext");
|
||||
test_policy(&image_policy_sysext_strict, "sysext-strict");
|
||||
test_policy(&image_policy_container, "container");
|
||||
test_policy(&image_policy_host, "host");
|
||||
test_policy(&image_policy_service, "service");
|
||||
test_policy(NULL, "null");
|
||||
|
||||
test_policy_string("");
|
||||
test_policy_string("-");
|
||||
test_policy_string("*");
|
||||
test_policy_string("~");
|
||||
test_policy_string("swap=open");
|
||||
test_policy_string("swap=open:root=signed");
|
||||
test_policy_string("swap=open:root=signed+read-only-on+growfs-off:=absent");
|
||||
test_policy_string("=-");
|
||||
test_policy_string("=");
|
||||
|
||||
test_policy_equiv("", image_policy_equiv_ignore);
|
||||
test_policy_equiv("-", image_policy_equiv_ignore);
|
||||
test_policy_equiv("*", image_policy_equiv_allow);
|
||||
test_policy_equiv("~", image_policy_equiv_deny);
|
||||
test_policy_equiv("=absent", image_policy_equiv_deny);
|
||||
test_policy_equiv("=open", image_policy_equiv_allow);
|
||||
test_policy_equiv("=verity+signed+encrypted+unprotected+unused+absent", image_policy_equiv_allow);
|
||||
test_policy_equiv("=signed+verity+encrypted+unused+unprotected+absent", image_policy_equiv_allow);
|
||||
test_policy_equiv("=ignore", image_policy_equiv_ignore);
|
||||
test_policy_equiv("=absent+unused", image_policy_equiv_ignore);
|
||||
test_policy_equiv("=unused+absent", image_policy_equiv_ignore);
|
||||
test_policy_equiv("root=ignore:=ignore", image_policy_equiv_ignore);
|
||||
|
||||
assert_se(image_policy_from_string("pfft", NULL) == -EINVAL);
|
||||
assert_se(image_policy_from_string("öäüß", NULL) == -EINVAL);
|
||||
assert_se(image_policy_from_string(":", NULL) == -EINVAL);
|
||||
assert_se(image_policy_from_string("a=", NULL) == -EBADSLT);
|
||||
assert_se(image_policy_from_string("=a", NULL) == -EBADRQC);
|
||||
assert_se(image_policy_from_string("==", NULL) == -EBADRQC);
|
||||
assert_se(image_policy_from_string("root=verity:root=encrypted", NULL) == -ENOTUNIQ);
|
||||
assert_se(image_policy_from_string("root=grbl", NULL) == -EBADRQC);
|
||||
assert_se(image_policy_from_string("wowza=grbl", NULL) == -EBADSLT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_INFO);
|
||||
@@ -82,7 +82,7 @@ static void* thread_func(void *ptr) {
|
||||
|
||||
log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted);
|
||||
|
||||
r = dissect_loop_device(loop, NULL, NULL, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected);
|
||||
r = dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected);
|
||||
if (r < 0)
|
||||
log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node);
|
||||
assert_se(r >= 0);
|
||||
@@ -213,7 +213,7 @@ static int run(int argc, char *argv[]) {
|
||||
sfdisk = NULL;
|
||||
|
||||
#if HAVE_BLKID
|
||||
assert_se(dissect_image_file(p, NULL, NULL, 0, &dissected) >= 0);
|
||||
assert_se(dissect_image_file(p, NULL, NULL, NULL, 0, &dissected) >= 0);
|
||||
verify_dissected_image(dissected);
|
||||
dissected = dissected_image_unref(dissected);
|
||||
#endif
|
||||
@@ -231,7 +231,7 @@ static int run(int argc, char *argv[]) {
|
||||
assert_se(loop_device_make(fd, O_RDWR, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_EX, &loop) >= 0);
|
||||
|
||||
#if HAVE_BLKID
|
||||
assert_se(dissect_loop_device(loop, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
|
||||
assert_se(dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
|
||||
verify_dissected_image(dissected);
|
||||
|
||||
FOREACH_STRING(fs, "vfat", "ext4") {
|
||||
@@ -267,12 +267,12 @@ static int run(int argc, char *argv[]) {
|
||||
|
||||
/* Try to read once, without pinning or adding partitions, i.e. by only accessing the whole block
|
||||
* device. */
|
||||
assert_se(dissect_loop_device(loop, NULL, NULL, 0, &dissected) >= 0);
|
||||
assert_se(dissect_loop_device(loop, NULL, NULL, NULL, 0, &dissected) >= 0);
|
||||
verify_dissected_image_harder(dissected);
|
||||
dissected = dissected_image_unref(dissected);
|
||||
|
||||
/* Now go via the loopback device after all, but this time add/pin, because now we want to mount it. */
|
||||
assert_se(dissect_loop_device(loop, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
|
||||
assert_se(dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
|
||||
verify_dissected_image_harder(dissected);
|
||||
|
||||
assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
|
||||
|
||||
@@ -176,6 +176,7 @@ TEST(protect_kernel_logs) {
|
||||
assert_se(fd > 0);
|
||||
|
||||
r = setup_namespace(NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
&ns_info,
|
||||
@@ -193,6 +194,7 @@ TEST(protect_kernel_logs) {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
@@ -208,6 +210,7 @@ TEST(protect_kernel_logs) {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL);
|
||||
assert_se(r == 0);
|
||||
|
||||
|
||||
@@ -77,6 +77,7 @@ int main(int argc, char *argv[]) {
|
||||
log_info("Not chrooted");
|
||||
|
||||
r = setup_namespace(root_directory,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
&ns_info,
|
||||
@@ -91,6 +92,7 @@ int main(int argc, char *argv[]) {
|
||||
&(TemporaryFileSystem) { .path = (char*) "/var", .options = (char*) "ro" }, 1,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
tmp_dir,
|
||||
var_tmp_dir,
|
||||
NULL,
|
||||
@@ -110,6 +112,7 @@ int main(int argc, char *argv[]) {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to set up namespace: %m");
|
||||
|
||||
Reference in New Issue
Block a user