From 2f809d29e85527acd75d4d2e46aaa14adc2f09dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 22 Mar 2023 13:42:40 +0100 Subject: [PATCH] test-coredump-util: add tests for parse_aux() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test files are /proc//auxv files copies from various architecutres signified by the file name suffix. Those tests are fairly simple, but when we run them on n architectures, we do ~n² cross-arch tests. --- src/test/test-coredump-util.c | 63 ++++++++++++++++++++ test/auxv/.gitattributes | 3 + test/auxv/bash.riscv64 | Bin 0 -> 384 bytes test/auxv/cat.s390x | Bin 0 -> 304 bytes test/auxv/dbus-broker-launch.aarch64 | Bin 0 -> 336 bytes test/auxv/dbus-broker-launch.amd64 | Bin 0 -> 336 bytes test/auxv/polkitd.aarch64 | Bin 0 -> 336 bytes test/auxv/resolved.arm32 | Bin 0 -> 160 bytes test/auxv/sleep.i686 | Bin 0 -> 192 bytes test/auxv/sleep32.i686 | Bin 0 -> 192 bytes test/auxv/sleep64.amd64 | Bin 0 -> 336 bytes test/auxv/sudo.aarch64 | Bin 0 -> 336 bytes test/auxv/sudo.amd64 | Bin 0 -> 336 bytes test/meson.build | 83 +++++++++------------------ 14 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 test/auxv/.gitattributes create mode 100644 test/auxv/bash.riscv64 create mode 100644 test/auxv/cat.s390x create mode 100644 test/auxv/dbus-broker-launch.aarch64 create mode 100644 test/auxv/dbus-broker-launch.amd64 create mode 100644 test/auxv/polkitd.aarch64 create mode 100644 test/auxv/resolved.arm32 create mode 100644 test/auxv/sleep.i686 create mode 100644 test/auxv/sleep32.i686 create mode 100644 test/auxv/sleep64.amd64 create mode 100644 test/auxv/sudo.aarch64 create mode 100644 test/auxv/sudo.amd64 diff --git a/src/test/test-coredump-util.c b/src/test/test-coredump-util.c index 40b68df9f4..755cb49dae 100644 --- a/src/test/test-coredump-util.c +++ b/src/test/test-coredump-util.c @@ -1,7 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #include "alloc-util.h" #include "coredump-util.h" +#include "fileio.h" +#include "fd-util.h" +#include "format-util.h" #include "macro.h" #include "tests.h" @@ -64,4 +69,62 @@ TEST(coredump_filter_mask_from_string) { 1 << COREDUMP_FILTER_SHARED_DAX))); } +static void test_parse_auxv_one( + uint8_t elf_class, + int dir_fd, + const char *filename, + int expect_at_secure, + uid_t expect_uid, + uid_t expect_euid, + gid_t expect_gid, + gid_t expect_egid) { + + _cleanup_free_ char *data; + size_t data_size; + log_info("Parsing %s…", filename); + assert_se(read_full_file_at(dir_fd, filename, &data, &data_size) >= 0); + + int at_secure; + uid_t uid, euid; + gid_t gid, egid; + assert_se(parse_auxv(LOG_ERR, elf_class, data, data_size, + &at_secure, &uid, &euid, &gid, &egid) == 0); + + log_info("at_secure=%d, uid="UID_FMT", euid="UID_FMT", gid="GID_FMT", egid="GID_FMT, + at_secure, uid, euid, gid, egid); + + assert_se(uid == expect_uid); + assert_se(euid == expect_euid); + assert_se(gid == expect_gid); + assert_se(egid == expect_egid); +} + +TEST(test_parse_auxv) { + _cleanup_free_ char *dir = NULL; + _cleanup_close_ int dir_fd = -EBADF; + + assert_se(get_testdata_dir("auxv", &dir) >= 0); + dir_fd = open(dir, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH); + assert_se(dir_fd >= 0); + + if (__BYTE_ORDER == __LITTLE_ENDIAN) { + test_parse_auxv_one(ELFCLASS32, dir_fd, "resolved.arm32", 0, 193, 193, 193, 193); + test_parse_auxv_one(ELFCLASS64, dir_fd, "bash.riscv64", 0, 1001, 1001, 1001, 1001); + test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep.i686", 0, 1000, 1000, 1000, 1000); + /* after chgrp and chmod g+s */ + test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep32.i686", 1, 1000, 1000, 1000, 10); + test_parse_auxv_one(ELFCLASS64, dir_fd, "sleep64.amd64", 1, 1000, 1000, 1000, 10); + + test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.aarch64", 1, 1494200408, 0, 1494200408, 1494200408); + test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.amd64", 1, 1000, 0, 1000, 1000); + + /* Those run unprivileged, but start as root. */ + test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.amd64", 0, 0, 0, 0, 0); + test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.aarch64", 0, 0, 0, 0, 0); + test_parse_auxv_one(ELFCLASS64, dir_fd, "polkitd.aarch64", 0, 0, 0, 0, 0); + } else { + test_parse_auxv_one(ELFCLASS64, dir_fd, "cat.s390x", 0, 3481, 3481, 3481, 3481); + } +} + DEFINE_TEST_MAIN(LOG_INFO); diff --git a/test/auxv/.gitattributes b/test/auxv/.gitattributes new file mode 100644 index 0000000000..58e3ff4c98 --- /dev/null +++ b/test/auxv/.gitattributes @@ -0,0 +1,3 @@ +/*.* -whitespace +/*.* binary +/*.* generated diff --git a/test/auxv/bash.riscv64 b/test/auxv/bash.riscv64 new file mode 100644 index 0000000000000000000000000000000000000000..273a468b300ba75532527ff4746f31dca3d0d9ea GIT binary patch literal 384 zcmY#nfP#h<^XwTI7&M?@6D9r<<*`YMU1c-ki zu7(LRFmOVd4$2i^^SGh>mr#pf=D_&8X!3k$d~qlrMoB^${{O;$gThlD%76DC!iRF< F6aes?7GVGY literal 0 HcmV?d00001 diff --git a/test/auxv/cat.s390x b/test/auxv/cat.s390x new file mode 100644 index 0000000000000000000000000000000000000000..aa76441fed60eef4598e972a30db9d5cca5cb8bf GIT binary patch literal 304 zcmZR807V7{=Kp>D4b zKxqpo%?hPCp)@<#z78mlfq?_gf-pEC=4niTa2dEEH1A9Z&Ah literal 0 HcmV?d00001 diff --git a/test/auxv/dbus-broker-launch.aarch64 b/test/auxv/dbus-broker-launch.aarch64 new file mode 100644 index 0000000000000000000000000000000000000000..3a05e3cabb0d25a6ec8b16bc908e89c0b6263e17 GIT binary patch literal 336 zcmY#nfP#d;ZU6r>Fc?F*1ws&p0EA-rKVJ~cWP|bQVj1Q$bp>%^U#5|Zjs4xQ$i~*&2p)`!*gE64AIFyD_ il28T*cl$k%|D~XO7$py7+~J4F^F#R(Q1wtQoB{xSqZ|zY literal 0 HcmV?d00001 diff --git a/test/auxv/dbus-broker-launch.amd64 b/test/auxv/dbus-broker-launch.amd64 new file mode 100644 index 0000000000000000000000000000000000000000..21965e85e318fd0ea065b7b36b170f2e71e52114 GIT binary patch literal 336 zcmY#nfPx9TzW=XhU@(Sq4fr4o0SLwL|M%or^nW3};!-EIh zVGIl`P`(9}W`)wcP?{Y|GbFe)+14{KaKQLbniEPZBtp#NhVo$)4~zk&Vdg@)3{Y>t qxD4WO7K9-Qp%{LC-U{}=6qL^drRAaYo&OMdekh;w3xp5l!YKgA&>qbI literal 0 HcmV?d00001 diff --git a/test/auxv/polkitd.aarch64 b/test/auxv/polkitd.aarch64 new file mode 100644 index 0000000000000000000000000000000000000000..ff1ea9f1409bcaaa3578eb5a73299afd89192c01 GIT binary patch literal 336 zcmY#nfP#jgY5)H-Fc?F*1ws&p0EA-rKVJ~cWP|bs literal 0 HcmV?d00001 diff --git a/test/auxv/resolved.arm32 b/test/auxv/resolved.arm32 new file mode 100644 index 0000000000000000000000000000000000000000..7d05f3b20b8f961b92f7aa1de3794d1af0f98f52 GIT binary patch literal 160 zcmY#nU|?V<__t4hfq~)L273lJ5MO|SL6CugA%%f~fti7U!GytqfrWvAL4kpRft7)Q zfs=uOfgPl-;ny|}5DgV;SnI&R&A`BLkb!}L2TJooX+9_|4plG7z`$Vr@9RD(1_lOs WD8BXo>pp%41_tkcU-vvbgz`)GF zz+lqA!okA8zyQ+A%D}+D!@$764pMjE^LGvq&A`CG$-uynFoA`Gn}LDh1v5kqh~|az v`Jl8oRJ|kv14H-43IC-S7#Nrs7#QRk7#QBYpYWfbfq`N6#R>nR$}lJZ$kQCV literal 0 HcmV?d00001 diff --git a/test/auxv/sleep32.i686 b/test/auxv/sleep32.i686 new file mode 100644 index 0000000000000000000000000000000000000000..f52f512436de7eba5847aade98baabb3cd3f4521 GIT binary patch literal 192 zcmY#jU|e41_mYu1_pTs28MU`|NrwdFfgoE{r?|oECvMt D)zcb5 literal 0 HcmV?d00001 diff --git a/test/auxv/sleep64.amd64 b/test/auxv/sleep64.amd64 new file mode 100644 index 0000000000000000000000000000000000000000..c3c7ed472fa964bd48bb16107f631f2643a24fb9 GIT binary patch literal 336 zcmY#nfC7WH^8f1@7>r?DJ_thqLNWaR{dzx$$p#`o7%CwMVlyx>q(EtADDA-D0A{g( zNd^WBD9s9`d7(5rlxAR9``@6Rfq?_ghcGxH6oZ2R#5`^&{{_?{9yC78T$nu68w^}f ob>dK(5lTx!=}fKVVE;=&`Akq+9!kIShsg6o`IS)hP%fMT0PegO*8l(j literal 0 HcmV?d00001 diff --git a/test/auxv/sudo.aarch64 b/test/auxv/sudo.aarch64 new file mode 100644 index 0000000000000000000000000000000000000000..e49ce6a9acae909d38af44c87b280ca8a760b52a GIT binary patch literal 336 zcmY#nfC7ix?f?HXFc?F*1ws&p0EA-rKVJ~cWP|bL;#VO5OF{WCN*>C1bsi$m59LQd)kC>(3IIAPBwzpl literal 0 HcmV?d00001 diff --git a/test/auxv/sudo.amd64 b/test/auxv/sudo.amd64 new file mode 100644 index 0000000000000000000000000000000000000000..91e46466154133d87ae0f87f1c3231ac6043aba8 GIT binary patch literal 336 zcmY#nfPxRw;s5Iy7>uD@13m~t075bR|NVMDh{*;bKo}|^2x2oZFr+|fW+?6OAX05j zC<6lvly3p0S)nv9lxBz03<)|L9O@YuIADAz%?YI^)I@>J