|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (C) 2020 Google LLC. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <asm-generic/errno-base.h> |
| 8 | +#include <test_progs.h> |
| 9 | +#include <linux/limits.h> |
| 10 | + |
| 11 | +#include "bprm_opts.skel.h" |
| 12 | +#include "network_helpers.h" |
| 13 | + |
| 14 | +#ifndef __NR_pidfd_open |
| 15 | +#define __NR_pidfd_open 434 |
| 16 | +#endif |
| 17 | + |
| 18 | +static const char * const bash_envp[] = { "TMPDIR=shouldnotbeset", NULL }; |
| 19 | + |
| 20 | +static inline int sys_pidfd_open(pid_t pid, unsigned int flags) |
| 21 | +{ |
| 22 | + return syscall(__NR_pidfd_open, pid, flags); |
| 23 | +} |
| 24 | + |
| 25 | +static int update_storage(int map_fd, int secureexec) |
| 26 | +{ |
| 27 | + int task_fd, ret = 0; |
| 28 | + |
| 29 | + task_fd = sys_pidfd_open(getpid(), 0); |
| 30 | + if (task_fd < 0) |
| 31 | + return errno; |
| 32 | + |
| 33 | + ret = bpf_map_update_elem(map_fd, &task_fd, &secureexec, BPF_NOEXIST); |
| 34 | + if (ret) |
| 35 | + ret = errno; |
| 36 | + |
| 37 | + close(task_fd); |
| 38 | + return ret; |
| 39 | +} |
| 40 | + |
| 41 | +static int run_set_secureexec(int map_fd, int secureexec) |
| 42 | +{ |
| 43 | + |
| 44 | + int child_pid, child_status, ret, null_fd; |
| 45 | + |
| 46 | + child_pid = fork(); |
| 47 | + if (child_pid == 0) { |
| 48 | + null_fd = open("/dev/null", O_WRONLY); |
| 49 | + if (null_fd == -1) |
| 50 | + exit(errno); |
| 51 | + dup2(null_fd, STDOUT_FILENO); |
| 52 | + dup2(null_fd, STDERR_FILENO); |
| 53 | + close(null_fd); |
| 54 | + |
| 55 | + /* Ensure that all executions from hereon are |
| 56 | + * secure by setting a local storage which is read by |
| 57 | + * the bprm_creds_for_exec hook and sets bprm->secureexec. |
| 58 | + */ |
| 59 | + ret = update_storage(map_fd, secureexec); |
| 60 | + if (ret) |
| 61 | + exit(ret); |
| 62 | + |
| 63 | + /* If the binary is executed with securexec=1, the dynamic |
| 64 | + * loader ingores and unsets certain variables like LD_PRELOAD, |
| 65 | + * TMPDIR etc. TMPDIR is used here to simplify the example, as |
| 66 | + * LD_PRELOAD requires a real .so file. |
| 67 | + * |
| 68 | + * If the value of TMPDIR is set, the bash command returns 10 |
| 69 | + * and if the value is unset, it returns 20. |
| 70 | + */ |
| 71 | + execle("/bin/bash", "bash", "-c", |
| 72 | + "[[ -z \"${TMPDIR}\" ]] || exit 10 && exit 20", NULL, |
| 73 | + bash_envp); |
| 74 | + exit(errno); |
| 75 | + } else if (child_pid > 0) { |
| 76 | + waitpid(child_pid, &child_status, 0); |
| 77 | + ret = WEXITSTATUS(child_status); |
| 78 | + |
| 79 | + /* If a secureexec occured, the exit status should be 20. |
| 80 | + */ |
| 81 | + if (secureexec && ret == 20) |
| 82 | + return 0; |
| 83 | + |
| 84 | + /* If normal execution happened the exit code should be 10. |
| 85 | + */ |
| 86 | + if (!secureexec && ret == 10) |
| 87 | + return 0; |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + return -EINVAL; |
| 92 | +} |
| 93 | + |
| 94 | +void test_test_bprm_opts(void) |
| 95 | +{ |
| 96 | + int err, duration = 0; |
| 97 | + struct bprm_opts *skel = NULL; |
| 98 | + |
| 99 | + skel = bprm_opts__open_and_load(); |
| 100 | + if (CHECK(!skel, "skel_load", "skeleton failed\n")) |
| 101 | + goto close_prog; |
| 102 | + |
| 103 | + err = bprm_opts__attach(skel); |
| 104 | + if (CHECK(err, "attach", "attach failed: %d\n", err)) |
| 105 | + goto close_prog; |
| 106 | + |
| 107 | + /* Run the test with the secureexec bit unset */ |
| 108 | + err = run_set_secureexec(bpf_map__fd(skel->maps.secure_exec_task_map), |
| 109 | + 0 /* secureexec */); |
| 110 | + if (CHECK(err, "run_set_secureexec:0", "err = %d\n", err)) |
| 111 | + goto close_prog; |
| 112 | + |
| 113 | + /* Run the test with the secureexec bit set */ |
| 114 | + err = run_set_secureexec(bpf_map__fd(skel->maps.secure_exec_task_map), |
| 115 | + 1 /* secureexec */); |
| 116 | + if (CHECK(err, "run_set_secureexec:1", "err = %d\n", err)) |
| 117 | + goto close_prog; |
| 118 | + |
| 119 | +close_prog: |
| 120 | + bprm_opts__destroy(skel); |
| 121 | +} |
0 commit comments