9 #include "../../memcheck.h"
12 assert_expected (int fd
, int expected_seals
)
14 int current_seals
= fcntl (fd
, F_GET_SEALS
);
15 assert (current_seals
== expected_seals
);
19 add_seal (int fd
, int *expected_seals
, int new_seal
)
21 int r
= fcntl (fd
, F_ADD_SEALS
, new_seal
);
24 *expected_seals
|= new_seal
;
26 // Make sure we get the result we expected.
27 assert_expected (fd
, *expected_seals
);
31 add_seal_expect_fail (int fd
, int new_seal
, int expected_errno
)
33 int r
= fcntl (fd
, F_ADD_SEALS
, new_seal
);
34 assert (r
== -1 && errno
== expected_errno
);
40 int expected_seals
= 0;
43 // Try with an fd that doesn't support sealing.
44 fd
= memfd_create ("xyz", 0);
47 // Not supported, nothing to test...
51 assert_expected (fd
, F_SEAL_SEAL
);
52 add_seal_expect_fail (fd
, F_SEAL_WRITE
, EPERM
);
53 assert_expected (fd
, F_SEAL_SEAL
); // ...should still be unset after failed attempt
56 // Now, try the successful case.
57 fd
= memfd_create ("xyz", MFD_ALLOW_SEALING
);
58 add_seal (fd
, &expected_seals
, F_SEAL_SHRINK
);
59 add_seal (fd
, &expected_seals
, F_SEAL_GROW
);
61 // Now prevent more sealing.
62 add_seal (fd
, &expected_seals
, F_SEAL_SEAL
);
64 // And make sure that it indeed fails.
65 add_seal_expect_fail (fd
, F_SEAL_WRITE
, EPERM
);
66 assert_expected (fd
, expected_seals
);
69 // Test the only memory failure possible: passing an undefined argument to F_ADD_SEALS
70 int undefined_seal
= 0;
71 VALGRIND_MAKE_MEM_UNDEFINED(&undefined_seal
, sizeof undefined_seal
);
72 fcntl (-1, F_ADD_SEALS
, undefined_seal
);