Skip to content

Commit faf2d88

Browse files
l0kodkees
authored andcommitted
samples/check-exec: Add set-exec
Add a simple tool to set SECBIT_EXEC_RESTRICT_FILE or SECBIT_EXEC_DENY_INTERACTIVE before executing a command. This is useful to easily test against enlighten script interpreters. Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Christian Brauner <brauner@kernel.org> Cc: Kees Cook <keescook@chromium.org> Cc: Paul Moore <paul@paul-moore.com> Cc: Serge Hallyn <serge@hallyn.com> Signed-off-by: Mickaël Salaün <mic@digikod.net> Link: https://lore.kernel.org/r/20241212174223.389435-6-mic@digikod.net Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 0e7f90f commit faf2d88

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

samples/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ config SAMPLE_CGROUP
291291
help
292292
Build samples that demonstrate the usage of the cgroup API.
293293

294+
config SAMPLE_CHECK_EXEC
295+
bool "Exec secure bits examples"
296+
depends on CC_CAN_LINK && HEADERS_INSTALL
297+
help
298+
Build a tool to easily configure SECBIT_EXEC_RESTRICT_FILE and
299+
SECBIT_EXEC_DENY_INTERACTIVE.
300+
294301
source "samples/rust/Kconfig"
295302

296303
endif # SAMPLES

samples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
subdir-$(CONFIG_SAMPLE_AUXDISPLAY) += auxdisplay
55
subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs
6+
subdir-$(CONFIG_SAMPLE_CHECK_EXEC) += check-exec
67
subdir-$(CONFIG_SAMPLE_CGROUP) += cgroup
78
obj-$(CONFIG_SAMPLE_CONFIGFS) += configfs/
89
obj-$(CONFIG_SAMPLE_CONNECTOR) += connector/

samples/check-exec/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/set-exec

samples/check-exec/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
3+
userprogs-always-y := \
4+
set-exec
5+
6+
userccflags += -I usr/include
7+
8+
.PHONY: all clean
9+
10+
all:
11+
$(MAKE) -C ../.. samples/check-exec/
12+
13+
clean:
14+
$(MAKE) -C ../.. M=samples/check-exec/ clean

samples/check-exec/set-exec.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/*
3+
* Simple tool to set SECBIT_EXEC_RESTRICT_FILE, SECBIT_EXEC_DENY_INTERACTIVE,
4+
* before executing a command.
5+
*
6+
* Copyright © 2024 Microsoft Corporation
7+
*/
8+
9+
#define _GNU_SOURCE
10+
#define __SANE_USERSPACE_TYPES__
11+
#include <errno.h>
12+
#include <linux/prctl.h>
13+
#include <linux/securebits.h>
14+
#include <stdbool.h>
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
#include <sys/prctl.h>
19+
#include <unistd.h>
20+
21+
static void print_usage(const char *argv0)
22+
{
23+
fprintf(stderr, "usage: %s -f|-i -- <cmd> [args]...\n\n", argv0);
24+
fprintf(stderr, "Execute a command with\n");
25+
fprintf(stderr, "- SECBIT_EXEC_RESTRICT_FILE set: -f\n");
26+
fprintf(stderr, "- SECBIT_EXEC_DENY_INTERACTIVE set: -i\n");
27+
}
28+
29+
int main(const int argc, char *const argv[], char *const *const envp)
30+
{
31+
const char *cmd_path;
32+
char *const *cmd_argv;
33+
int opt, secbits_cur, secbits_new;
34+
bool has_policy = false;
35+
36+
secbits_cur = prctl(PR_GET_SECUREBITS);
37+
if (secbits_cur == -1) {
38+
/*
39+
* This should never happen, except with a buggy seccomp
40+
* filter.
41+
*/
42+
perror("ERROR: Failed to get securebits");
43+
return 1;
44+
}
45+
46+
secbits_new = secbits_cur;
47+
while ((opt = getopt(argc, argv, "fi")) != -1) {
48+
switch (opt) {
49+
case 'f':
50+
secbits_new |= SECBIT_EXEC_RESTRICT_FILE |
51+
SECBIT_EXEC_RESTRICT_FILE_LOCKED;
52+
has_policy = true;
53+
break;
54+
case 'i':
55+
secbits_new |= SECBIT_EXEC_DENY_INTERACTIVE |
56+
SECBIT_EXEC_DENY_INTERACTIVE_LOCKED;
57+
has_policy = true;
58+
break;
59+
default:
60+
print_usage(argv[0]);
61+
return 1;
62+
}
63+
}
64+
65+
if (!argv[optind] || !has_policy) {
66+
print_usage(argv[0]);
67+
return 1;
68+
}
69+
70+
if (secbits_cur != secbits_new &&
71+
prctl(PR_SET_SECUREBITS, secbits_new)) {
72+
perror("Failed to set secure bit(s).");
73+
fprintf(stderr,
74+
"Hint: The running kernel may not support this feature.\n");
75+
return 1;
76+
}
77+
78+
cmd_path = argv[optind];
79+
cmd_argv = argv + optind;
80+
fprintf(stderr, "Executing command...\n");
81+
execvpe(cmd_path, cmd_argv, envp);
82+
fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
83+
strerror(errno));
84+
return 1;
85+
}

0 commit comments

Comments
 (0)