interpreter line, e.g. for a handler that resolves ``$ORIGIN`` in a script's
``#!`` path and needs to preserve the argument that followed it.
+The invocation flags a static entry fixes at registration - ``P``, ``C``
+and ``O`` - are per-exec choices for a bpf handler, made by the ``load``
+program with the ``bpf_binprm_set_flags()`` kfunc, so a single handler can
+decide them differently for each binary it handles:
+
+- ``BPF_BINPRM_PRESERVE_ARGV0`` keeps the caller's ``argv[0]`` (the ``P``
+ flag).
+- ``BPF_BINPRM_CREDENTIALS`` computes credentials from the binary (the ``C``
+ flag), bounded to user namespaces that map the binary's owner just like
+ any other setuid exec.
+- ``BPF_BINPRM_EXECFD`` opens the binary on the interpreter's behalf and
+ passes it through the ``AT_EXECFD`` aux vector entry (the ``O`` flag), so
+ the interpreter can run binaries it could not open by path.
+
+Because these are program choices, a ``B`` entry carries no flags in the
+register string; ``F`` (pre-open a fixed interpreter) has no meaning for it.
+
A handler is looked up only in the user namespace the struct_ops map was
registered in. Handlers are not inherited, so an entry can only reference a
handler registered in the same user namespace as its binfmt_misc instance.
The entry keeps the handler alive; deleting the struct_ops map only prevents
new activations.
-The ``F`` flag cannot be combined with ``B`` entries: it pre-opens a fixed
-interpreter at registration time and a ``B`` entry has none. The ``C`` flag
-works as it does for a static entry: the interpreter runs with the matched
-binary's credentials, bounded to user namespaces that map the binary's owner
-just like any other setuid exec.
-
To use binfmt_misc you have to mount it first. You can mount it with
``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add
a line ``none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your
/* A failing load leaves nothing behind for later entries. */
kfree(bprm->bpf_interp_arg);
bprm->bpf_interp_arg = NULL;
+ bprm->bpf_flags = 0;
return ERR_PTR(retval);
}
const char *interpreter;
struct file *interp_file;
struct binfmt_misc *misc;
+ bool preserve_argv0, want_execfd, want_creds;
int retval;
misc = current_binfmt_misc();
if (IS_ERR(interpreter))
return PTR_ERR(interpreter);
- if (fmt->flags & MISC_FMT_PRESERVE_ARGV0) {
+ /*
+ * The invocation flags are fixed at registration for a static handler
+ * and chosen per exec by the load program, via bpf_binprm_set_flags(),
+ * for a bpf one.
+ */
+ if (test_bit(MISC_FMT_BPF_BIT, &fmt->flags)) {
+ u64 f = bprm->bpf_flags;
+
+ /* Clear so it can't accumulate into a nested interpreter level. */
+ bprm->bpf_flags = 0;
+
+ preserve_argv0 = f & BPF_BINPRM_PRESERVE_ARGV0;
+ want_creds = f & BPF_BINPRM_CREDENTIALS;
+ want_execfd = f & (BPF_BINPRM_CREDENTIALS | BPF_BINPRM_EXECFD);
+ } else {
+ preserve_argv0 = fmt->flags & MISC_FMT_PRESERVE_ARGV0;
+ want_creds = fmt->flags & MISC_FMT_CREDENTIALS;
+ want_execfd = fmt->flags & MISC_FMT_OPEN_BINARY;
+ }
+
+ /* The entry's own choice - not one accumulated from an earlier level. */
+ if (preserve_argv0) {
bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
} else {
retval = remove_arg_zero(bprm);
return PTR_ERR(interp_file);
bprm->interpreter = interp_file;
- if (fmt->flags & MISC_FMT_OPEN_BINARY)
+ if (want_execfd)
bprm->have_execfd = 1;
- if (fmt->flags & MISC_FMT_CREDENTIALS)
+ if (want_creds)
bprm->execfd_creds = 1;
return 0;
}
return ERR_PTR(-EINVAL);
/*
- * 'F' pre-opens a fixed interpreter at registration time which is
- * meaningless for a per-exec computed path. 'C' is fine: it honors the
- * suid bits of the matched binary exactly like a static entry, gated by
- * the same vfsuid_has_mapping() check in bprm_fill_uid() that keeps the
- * transition to uids mapped in the caller's user namespace.
+ * A bpf handler decides the invocation flags per exec with
+ * bpf_binprm_set_flags() rather than fixing them at registration, so a
+ * 'B' entry carries no flags: 'P', 'C' and 'O' become per-exec choices
+ * and 'F' (pre-open a fixed interpreter) is meaningless for it.
*/
if (test_bit(MISC_FMT_BPF_BIT, &e->flags) &&
- (e->flags & MISC_FMT_OPEN_FILE))
+ (e->flags & (MISC_FMT_PRESERVE_ARGV0 | MISC_FMT_OPEN_BINARY |
+ MISC_FMT_CREDENTIALS | MISC_FMT_OPEN_FILE)))
return ERR_PTR(-EINVAL);
return no_free_ptr(e);