The Pedigree Project 0.1
power-contract-test/main.c
1/* Copyright (c) 2026, Pedigree Developers. */
2#include <errno.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <unistd.h>
6
7#include <sys/syscall.h>
8#include <sys/wait.h>
9
10static int failures;
11
12static void check(int condition, const char* description) {
13 printf("POWER-CONTRACT: %s %s\n", condition ? "PASS" : "FAIL", description);
14 if (!condition)
15 ++failures;
16}
17
18static int call(uint32_t magic1, uint32_t magic2, uint32_t command) {
19 errno = 0;
20 return syscall(SYS_reboot, magic1, magic2, command, 0);
21}
22
23int main(void) {
24 if (geteuid() != 0) {
25 fprintf(stderr, "power-contract-test must run as root\n");
26 return 1;
27 }
28 check(call(0, 672274793, 0x01234567) == -1 && errno == EINVAL, "invalid first magic");
29 check(call(0xfee1dead, 0, 0x01234567) == -1 && errno == EINVAL, "invalid second magic");
30 check(call(0xfee1dead, 672274793, 0x123) == -1 && errno == EINVAL, "unknown command");
31 check(call(0xfee1dead, 672274793, 0xa1b2c3d4) == -1 && errno == EINVAL, "unsupported restart2");
32 check(call(0xfee1dead, 672274793, 0xd000fce2) == -1 && errno == EINVAL, "unsupported suspend");
33 check(call(0xfee1dead, 672274793, 0x45584543) == -1 && errno == EINVAL, "unsupported kexec");
34 const uint32_t second[] = {672274793, 85072278, 369367448, 537993216};
35 for (unsigned i = 0; i < sizeof(second) / sizeof(second[0]); ++i) {
36 check(call(0xfee1dead, second[i], 0) == 0, "CAD_OFF is nonterminal");
37 check(call(0xfee1dead, second[i], 0x89abcdef) == 0, "CAD_ON is nonterminal");
38 }
39 fflush(stdout);
40 pid_t child = fork();
41 if (child == 0) {
42 if (setuid(65534) != 0)
43 _exit(1);
44 const uint32_t commands[] = {0x01234567, 0xcdef0123, 0x4321fedc, 0, 0x89abcdef};
45 for (unsigned i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i)
46 if (call(0xfee1dead, 672274793, commands[i]) != -1 || errno != EPERM)
47 _exit(1);
48 _exit(0);
49 }
50 int status = 0;
51 check(child > 0 && waitpid(child, &status, 0) == child && WIFEXITED(status) &&
52 WEXITSTATUS(status) == 0,
53 "unprivileged commands denied");
54 printf("POWER-CONTRACT: END failures=%d\n", failures);
55 return failures != 0;
56}