aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/binmiscctl/binmiscctl.c
Commit message (Collapse)AuthorAgeFilesLines
* MFC imgact_binmisc housecleaning: r367361, r367439, r367441-r367442,Kyle Evans2020-11-141-11/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | r367444, r367452, r367456, r367477 r367361: imgact_binmisc: fix up some minor nits - Removed a bunch of redundant headers - Don't explicitly initialize to 0 - The !error check prior to setting imgp->interpreter_name is redundant, all error paths should and do return or go to 'done'. We have larger problems otherwise. r367439: imgact_binmisc: minor re-organization of imgact_binmisc_exec exits Notably, streamline error paths through the existing 'done' label, making it easier to quickly verify correct cleanup. Future work might add a kernel-only flag to indicate that a interpreter uses #a. Currently, all executions via imgact_binmisc pay the penalty of constructing sname/fname, even if they will not use it. qemu-user-static doesn't need it, the stock rc script for qemu-user-static certainly doesn't use it, and I suspect these are the vast majority of (if not the only) current users. r367441: binmiscctl(8): miscellaneous cleanup - Bad whitespace in Makefile. - Reordered headers, sys/ first. - Annotated fatal/usage __dead2 to help `make analyze` out a little bit. - Spell a couple of sizeof constructs as "nitems" and "howmany" instead. r367442: imgact_binmisc: validate flags coming from userland We may want to reserve bits in the future for kernel-only use, so start rejecting any that aren't the two that we're currently expecting from userland. r367444: imgact_binmisc: abstract away the list lock (NFC) This module handles relatively few execs (initial qemu-user-static, then qemu-user-static handles exec'ing itself for binaries it's already running), but all execs pay the price of at least taking the relatively expensive sx/slock to check for a match when this module is loaded. Future work will almost certainly swap this out for another lock, perhaps an rmslock. The RLOCK/WLOCK phrasing was chosen based on what the callers are really wanting, rather than using the verbiage typically appropriate for an sx. r367452: imgact_binmisc: reorder members of struct imgact_binmisc_entry (NFC) This doesn't change anything at the moment since the out-of-order elements were a pair of uint32_t, but future additions may have caused unnecessary padding by following the existing precedent. r367456: imgact_binmisc: move some calculations out of the exec path The offset we need to account for in the interpreter string comes in two variants: 1. Fixed - macros other than #a that will not vary from invocation to invocation 2. Variable - #a, which is substitued with the argv0 that we're replacing Note that we don't have a mechanism to modify an existing entry. By recording both of these offset requirements when the interpreter is added, we can avoid some unnecessary calculations in the exec path. Most importantly, we can know up-front whether we need to grab calculate/grab the the filename for this interpreter. We also get to avoid walking the string a first time looking for macros. For most invocations, it's a swift exit as they won't have any, but there's no point entering a loop and searching for the macro indicator if we already know there will not be one. While we're here, go ahead and only calculate the argv0 name length once per invocation. While it's unlikely that we'll have more than one #a, there's no reason to recalculate it every time we encounter an #a when it will not change. I have not bothered trying to benchmark this at all, because it's arguably a minor and straightforward/obvious improvement. r367477: imgact_binmisc: limit the extent of match on incoming entries imgact_binmisc matches magic/mask from imgp->image_header, which is only a single page in size mapped from the first page of an image. One can specify an interpreter that matches on, e.g., --offset 4096 --size 256 to read up to 256 bytes past the mapped first page. The limitation is that we cannot specify a magic string that exceeds a single page, and we can't allow offset + size to exceed a single page either. A static assert has been added in case someone finds it useful to try and expand the size, but it does seem a little unlikely. While this looks kind of exploitable at a sideways squinty-glance, there are a couple of mitigating factors: 1.) imgact_binmisc is not enabled by default, 2.) entries may only be added by the superuser, 3.) trying to exploit this information to read what's mapped past the end would be worse than a root canal or some other relatably painful experience, and 4.) there's no way one could pull this off without it being completely obvious. The first page is mapped out of an sf_buf, the implementation of which (or lack thereof) depends on your platform. Notes: svn path=/stable/12/; revision=367657
* Don't leak resources on duplicate -m or -M arguments. Last one wins.Warner Losh2017-12-281-0/+2
| | | | | | | CID: 1204385, 1204384 Notes: svn path=/head/; revision=327262
* binmiscctl should use modfind instead of kldfindSean Bruno2017-07-281-1/+2
| | | | | | | | | | | | | | | | | kldfind() only matches kernel modules, so if you link imgact_binmisc directly into the kernel, binmiscctl can't find it, tries to load it, and errors out with: Can't load imgact_binmisc kernel module: File exists A quick search of other base commands shows that the correct procedure is to call modfind(), and then try kldload() if that fails. PR: 218593 Submitted by: Dan Nelson <dnelson_1901@yahoo.com> MFC after: 1 week Notes: svn path=/head/; revision=321658
* Another attempt at resolving CID 1305629. The test of cmd == -1Don Lewis2016-05-131-1/+1
| | | | | | | | | | | | may make Coverity think that other negative values of cmd (used as an index) are possible. Testing < 0 is a more common idiom in any case. Reported by: Coverity CID: 1305629 Notes: svn path=/head/; revision=299687
* Revert r299584:Don Lewis2016-05-131-1/+1
| | | | | | | | | | Mark usage() as __dead2 so that Coverity doesn't think that execution continues after the call and uses a negative array subscript. Requested by: bde Notes: svn path=/head/; revision=299588
* Mark usage() as __dead2 so that Coverity doesn't think that executionDon Lewis2016-05-131-1/+1
| | | | | | | | | | continues after the call and uses a negative array subscript. Reported by: Coverity CID: 1305629 Notes: svn path=/head/; revision=299584
* If no arguments are passed to a subcommand that requires arguments,Sean Bruno2015-02-151-1/+3
| | | | | | | | | | | error out before we deref a null pointer in the check for max length. Thanks to otis in IRC for the bug report. MFC after: 3 days Notes: svn path=/head/; revision=278827
* Check for invalid length or more than max length for the interpreter, insteadSean Bruno2015-01-281-1/+1
| | | | | | | | | | | of the validity of the string pointer holding the interpreter. Submitted by: sson Reviewed by: dim MFC after: 3 days Notes: svn path=/head/; revision=277853
* Add Stacey Son's binary activation patches that allow remapping ofSean Bruno2014-04-081-0/+508
execution to a emumation program via parsing of ELF header information. With this kernel module and userland tool, poudriere is able to build ports packages via the QEMU userland tools (or another emulator program) in a different architecture chroot, e.g. TARGET=mips TARGET_ARCH=mips I'm not connecting this to GENERIC for obvious reasons, but this should allow the kernel module to be built by default and enable the building of the userland tool (which automatically loads the kernel module). Submitted by: sson@ Reviewed by: jhb@ Notes: svn path=/head/; revision=264269