1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BPF-backed binary type handlers for binfmt_misc. 4 * 5 * A handler is a struct binfmt_misc_ops struct_ops map. Loading and 6 * registering it makes the handler available under its name in the user 7 * namespace it was registered in. A binfmt_misc 'B' entry activates it: 8 * 9 * echo ':entry:B::::<handler-name>:' > <binfmt_misc>/register 10 * 11 * The entry can bind the interpreters the handler may run its binaries 12 * with, each opened by the write that binds it and selected by name per 13 * exec. An entry registered with 'D' is not matchable yet, which is what 14 * leaves it open to being given them: 15 * 16 * echo ':entry:B::::<handler-name>:D' > <binfmt_misc>/register 17 * echo '+<name> <path>' > <binfmt_misc>/entry 18 * echo 1 > <binfmt_misc>/entry 19 */ 20 21 #include <linux/binfmt_misc.h> 22 #include <linux/binfmts.h> 23 #include <linux/bpf.h> 24 #include <linux/bpf_verifier.h> 25 #include <linux/btf.h> 26 #include <linux/btf_ids.h> 27 #include <linux/cred.h> 28 #include <linux/file.h> 29 #include <linux/fs.h> 30 #include <linux/init.h> 31 #include <linux/limits.h> 32 #include <linux/slab.h> 33 #include <linux/spinlock.h> 34 #include <linux/string.h> 35 #include <linux/user_namespace.h> 36 37 struct bm_bpf_ops_reg { 38 struct list_head list; 39 const struct binfmt_misc_ops *ops; 40 struct bpf_link *link; 41 struct user_namespace *user_ns; 42 }; 43 44 static DEFINE_SPINLOCK(bm_bpf_ops_lock); 45 static LIST_HEAD(bm_bpf_ops_list); 46 47 static struct bpf_struct_ops bpf_binfmt_misc_ops; 48 49 static struct bm_bpf_ops_reg *bm_bpf_ops_find(const struct user_namespace *user_ns, 50 const char *name) 51 { 52 struct bm_bpf_ops_reg *reg; 53 54 lockdep_assert_held(&bm_bpf_ops_lock); 55 56 list_for_each_entry(reg, &bm_bpf_ops_list, list) { 57 if (reg->user_ns == user_ns && !strcmp(reg->ops->name, name)) 58 return reg; 59 } 60 return NULL; 61 } 62 63 /** 64 * binfmt_misc_get_ops - look up a bpf binary type handler by name 65 * @user_ns: user namespace of the binfmt_misc instance 66 * @name: name the handler was registered under 67 * 68 * Look for a handler named @name registered in @user_ns. A handler is not 69 * inherited from ancestor user namespaces: an entry can only name a handler 70 * registered in the same user namespace as its instance. The returned handler 71 * stays callable until binfmt_misc_put_ops() even if the backing struct_ops 72 * map is detached or deleted in the meantime. 73 * 74 * Return: the handler on success, NULL on failure 75 */ 76 const struct binfmt_misc_ops *binfmt_misc_get_ops(struct user_namespace *user_ns, 77 const char *name) 78 { 79 struct bm_bpf_ops_reg *reg; 80 81 guard(spinlock)(&bm_bpf_ops_lock); 82 83 reg = bm_bpf_ops_find(user_ns, name); 84 if (!reg) 85 return NULL; 86 if (!bpf_struct_ops_get(reg->ops)) 87 return NULL; 88 return reg->ops; 89 } 90 91 void binfmt_misc_put_ops(const struct binfmt_misc_ops *ops) 92 { 93 bpf_struct_ops_put(ops); 94 } 95 96 bool bpf_prog_is_binfmt_misc_ops(const struct bpf_prog *prog) 97 { 98 return prog->type == BPF_PROG_TYPE_STRUCT_OPS && 99 prog->aux->st_ops == &bpf_binfmt_misc_ops; 100 } 101 102 /* 103 * Replace the staged interpreter selection: naming a path drops a bound 104 * file, selecting a bound interpreter carries its file along. 105 */ 106 static void bm_bpf_stage_selection(struct linux_binprm *bprm, char *path, 107 struct file *f) 108 { 109 if (bprm->bpf_interp_file) 110 fput(bprm->bpf_interp_file); 111 kfree(bprm->bpf_interp); 112 bprm->bpf_interp = path; 113 bprm->bpf_interp_file = f; 114 } 115 116 __bpf_kfunc_start_defs(); 117 118 /** 119 * bpf_binprm_set_interp - select the interpreter for the current exec 120 * @bprm: binary that is being executed 121 * @path: absolute path to the interpreter 122 * @path__sz: size of the @path buffer, including the terminating NUL 123 * 124 * To be called from the load program of a struct binfmt_misc_ops handler 125 * before returning zero; the verifier rejects the call from any other 126 * program, including the handler's own match program. The path is opened 127 * with the credentials of the task doing the exec after the program 128 * returns. Calling it again replaces the selection, as does selecting an 129 * interpreter the entry bound with bpf_binprm_select_interp(). 130 * 131 * Return: 0 on success, a negative errno on failure 132 */ 133 __bpf_kfunc int bpf_binprm_set_interp(struct linux_binprm *bprm, 134 const char *path, size_t path__sz) 135 { 136 size_t len; 137 char *interp; 138 139 if (!path__sz) 140 return -EINVAL; 141 len = strnlen(path, path__sz); 142 if (len == path__sz) 143 return -EINVAL; 144 if (path[0] != '/') 145 return -EINVAL; 146 if (len >= PATH_MAX) 147 return -ENAMETOOLONG; 148 149 interp = kmemdup_nul(path, len, GFP_KERNEL); 150 if (!interp) 151 return -ENOMEM; 152 153 bm_bpf_stage_selection(bprm, interp, NULL); 154 return 0; 155 } 156 157 /** 158 * bpf_binprm_select_interp - run this exec under an interpreter the entry bound 159 * @bprm: binary that is being executed 160 * @name: name the interpreter was registered under 161 * @name__sz: size of the @name buffer, including the terminating NUL 162 * 163 * To be called from the load program of a struct binfmt_misc_ops handler 164 * instead of bpf_binprm_set_interp(). It selects one of the interpreters 165 * the matched entry was registered with, each of which was opened once when 166 * the entry was registered. Nothing is resolved at exec time, so no 167 * filesystem view can redirect the interpreter. 168 * 169 * The interpreter runs under the path the entry registered it under. 170 * Calling it again replaces the selection. 171 * 172 * Return: 0 on success, -ENOENT if the matched entry bound no interpreter 173 * of that name, a negative errno on failure 174 */ 175 __bpf_kfunc int bpf_binprm_select_interp(struct linux_binprm *bprm, 176 const char *name, size_t name__sz) 177 { 178 const struct binfmt_misc_interp *interp; 179 size_t len; 180 char *path; 181 182 if (!name__sz) 183 return -EINVAL; 184 len = strnlen(name, name__sz); 185 if (len == name__sz || !len) 186 return -EINVAL; 187 188 interp = binfmt_misc_find_interp(bprm->bpf_interps, name); 189 if (!interp) 190 return -ENOENT; 191 192 path = kstrdup(interp->path, GFP_KERNEL); 193 if (!path) 194 return -ENOMEM; 195 196 bm_bpf_stage_selection(bprm, path, get_file(interp->file)); 197 return 0; 198 } 199 200 /** 201 * bpf_binprm_set_interp_arg - set a single argument for the interpreter 202 * @bprm: binary that is being executed 203 * @arg: argument to pass to the interpreter 204 * @arg__sz: size of the @arg buffer, including the terminating NUL 205 * 206 * To be called from the load program of a struct binfmt_misc_ops handler. The 207 * argument is passed to the interpreter ahead of the binary, mirroring the 208 * single optional argument of a #! interpreter line. Calling it again 209 * replaces the argument. 210 * 211 * Return: 0 on success, a negative errno on failure 212 */ 213 __bpf_kfunc int bpf_binprm_set_interp_arg(struct linux_binprm *bprm, 214 const char *arg, size_t arg__sz) 215 { 216 size_t len; 217 char *val; 218 219 if (!arg__sz) 220 return -EINVAL; 221 len = strnlen(arg, arg__sz); 222 if (len == arg__sz) 223 return -EINVAL; 224 if (!len) 225 return -EINVAL; 226 227 val = kmemdup_nul(arg, len, GFP_KERNEL); 228 if (!val) 229 return -ENOMEM; 230 231 kfree(bprm->bpf_interp_arg); 232 bprm->bpf_interp_arg = val; 233 return 0; 234 } 235 236 /** 237 * bpf_binprm_set_flags - choose the interpreter invocation flags for this exec 238 * @bprm: binary that is being executed 239 * @flags: an OR of enum bpf_binprm_flags values 240 * 241 * To be called from the load program of a struct binfmt_misc_ops handler. It 242 * decides per exec what a static entry fixes at registration with the P, C, 243 * O, T and L flags: BPF_BINPRM_PRESERVE_ARGV0 keeps the caller's argv[0], 244 * BPF_BINPRM_CREDENTIALS computes credentials from the binary, and 245 * BPF_BINPRM_EXECFD hands the binary to the interpreter through AT_EXECFD. 246 * BPF_BINPRM_TRANSPARENT additionally leaves the argument vector untouched, 247 * making the exec look like a direct execution of the binary. 248 * BPF_BINPRM_LOADER substitutes the interpreter for the binary's PT_INTERP 249 * and runs the binary as a native exec; it excludes every other flag. 250 * Calling it again replaces the flags, passing zero clears them again. 251 * 252 * Return: 0 on success, -EINVAL if @flags contains an unknown bit or an 253 * invalid combination 254 */ 255 __bpf_kfunc int bpf_binprm_set_flags(struct linux_binprm *bprm, 256 enum bpf_binprm_flags flags) 257 { 258 if (flags & ~(BPF_BINPRM_PRESERVE_ARGV0 | BPF_BINPRM_CREDENTIALS | 259 BPF_BINPRM_EXECFD | BPF_BINPRM_TRANSPARENT | 260 BPF_BINPRM_LOADER)) 261 return -EINVAL; 262 263 /* Loader substitution is a native exec: no splice, execfd or creds work. */ 264 if ((flags & BPF_BINPRM_LOADER) && (flags & ~BPF_BINPRM_LOADER)) 265 return -EINVAL; 266 267 /* Transparency preserves the whole argv, argv[0] included. */ 268 if ((flags & BPF_BINPRM_TRANSPARENT) && (flags & BPF_BINPRM_PRESERVE_ARGV0)) 269 return -EINVAL; 270 271 bprm->bpf_flags = flags; 272 return 0; 273 } 274 275 __bpf_kfunc_end_defs(); 276 277 BTF_KFUNCS_START(bm_bpf_kfunc_ids) 278 BTF_ID_FLAGS(func, bpf_binprm_set_interp, KF_SLEEPABLE) 279 BTF_ID_FLAGS(func, bpf_binprm_select_interp, KF_SLEEPABLE) 280 BTF_ID_FLAGS(func, bpf_binprm_set_interp_arg, KF_SLEEPABLE) 281 BTF_ID_FLAGS(func, bpf_binprm_set_flags, KF_SLEEPABLE) 282 BTF_KFUNCS_END(bm_bpf_kfunc_ids) 283 284 static int bm_bpf_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id) 285 { 286 if (!btf_id_set8_contains(&bm_bpf_kfunc_ids, kfunc_id)) 287 return 0; 288 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) 289 return -EACCES; 290 /* ->st_ops is unset during the cfg pass; enforced once it is set. */ 291 if (!prog->aux->st_ops) 292 return 0; 293 /* Only the load program decides how a binary is run. */ 294 if (bpf_prog_is_binfmt_misc_ops(prog) && 295 prog->aux->attach_st_ops_member_off == offsetof(struct binfmt_misc_ops, load)) 296 return 0; 297 return -EACCES; 298 } 299 300 static const struct btf_kfunc_id_set bm_bpf_kfunc_set = { 301 .owner = THIS_MODULE, 302 .set = &bm_bpf_kfunc_ids, 303 .filter = bm_bpf_kfunc_filter, 304 }; 305 306 static bool bm_bpf_ops__match(struct linux_binprm *bprm) 307 { 308 return false; 309 } 310 311 static int bm_bpf_ops__load(struct linux_binprm *bprm) 312 { 313 return 0; 314 } 315 316 static struct binfmt_misc_ops bm_bpf_ops_stubs = { 317 .match = bm_bpf_ops__match, 318 .load = bm_bpf_ops__load, 319 }; 320 321 static int bm_bpf_init(struct btf *btf) 322 { 323 return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, 324 &bm_bpf_kfunc_set); 325 } 326 327 static int bm_bpf_check_member(const struct btf_type *t, 328 const struct btf_member *member, 329 const struct bpf_prog *prog) 330 { 331 u32 moff = __btf_member_bit_offset(t, member) / 8; 332 333 switch (moff) { 334 case offsetof(struct binfmt_misc_ops, match): 335 case offsetof(struct binfmt_misc_ops, load): 336 /* Reliable file reads at exec time require sleeping. */ 337 if (!prog->sleepable) 338 return -EINVAL; 339 break; 340 } 341 return 0; 342 } 343 344 static int bm_bpf_init_member(const struct btf_type *t, 345 const struct btf_member *member, 346 void *kdata, const void *udata) 347 { 348 const struct binfmt_misc_ops *uops = udata; 349 struct binfmt_misc_ops *ops = kdata; 350 u32 moff = __btf_member_bit_offset(t, member) / 8; 351 352 switch (moff) { 353 case offsetof(struct binfmt_misc_ops, name): 354 if (bpf_obj_name_cpy(ops->name, uops->name, 355 sizeof(ops->name)) <= 0) 356 return -EINVAL; 357 return 1; 358 } 359 return 0; 360 } 361 362 static int bm_bpf_validate(void *kdata) 363 { 364 struct binfmt_misc_ops *ops = kdata; 365 366 if (!ops->match || !ops->load) 367 return -EINVAL; 368 return 0; 369 } 370 371 static int bm_bpf_reg(void *kdata, struct bpf_link *link) 372 { 373 struct binfmt_misc_ops *ops = kdata; 374 struct bm_bpf_ops_reg *reg; 375 376 reg = kzalloc_obj(*reg, GFP_KERNEL_ACCOUNT); 377 if (!reg) 378 return -ENOMEM; 379 380 reg->ops = ops; 381 reg->link = link; 382 reg->user_ns = get_user_ns(current_user_ns()); 383 384 guard(spinlock)(&bm_bpf_ops_lock); 385 386 if (bm_bpf_ops_find(reg->user_ns, ops->name)) { 387 put_user_ns(reg->user_ns); 388 kfree(reg); 389 return -EEXIST; 390 } 391 392 list_add(®->list, &bm_bpf_ops_list); 393 return 0; 394 } 395 396 static void bm_bpf_unreg(void *kdata, struct bpf_link *link) 397 { 398 struct bm_bpf_ops_reg *reg; 399 400 guard(spinlock)(&bm_bpf_ops_lock); 401 402 list_for_each_entry(reg, &bm_bpf_ops_list, list) { 403 if (reg->ops == kdata && reg->link == link) { 404 list_del(®->list); 405 put_user_ns(reg->user_ns); 406 kfree(reg); 407 return; 408 } 409 } 410 } 411 412 static const struct bpf_verifier_ops bm_bpf_verifier_ops = { 413 .get_func_proto = bpf_base_func_proto, 414 .is_valid_access = bpf_tracing_btf_ctx_access, 415 }; 416 417 static struct bpf_struct_ops bpf_binfmt_misc_ops = { 418 .verifier_ops = &bm_bpf_verifier_ops, 419 .init = bm_bpf_init, 420 .check_member = bm_bpf_check_member, 421 .init_member = bm_bpf_init_member, 422 .validate = bm_bpf_validate, 423 .reg = bm_bpf_reg, 424 .unreg = bm_bpf_unreg, 425 .cfi_stubs = &bm_bpf_ops_stubs, 426 .name = "binfmt_misc_ops", 427 .owner = THIS_MODULE, 428 }; 429 430 static int __init bm_bpf_struct_ops_init(void) 431 { 432 return register_bpf_struct_ops(&bpf_binfmt_misc_ops, binfmt_misc_ops); 433 } 434 late_initcall(bm_bpf_struct_ops_init); 435