1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2013 Stacey D. Son 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30#ifndef _IMGACT_BINMISC_H_ 31#define _IMGACT_BINMISC_H_ 32 33/** 34 * Miscellaneous binary interpreter image activator. 35 */ 36 37#include <sys/param.h> /* for MAXPATHLEN */ 38 39/* 40 * Imgact bin misc parameters. 41 */ 42#define IBE_VERSION 1 /* struct ximgact_binmisc_entry version. */ 43#define IBE_NAME_MAX 32 /* Max size for entry name. */ 44#define IBE_MAGIC_MAX 256 /* Max size for header magic and mask. */ 45#define IBE_ARG_LEN_MAX 256 /* Max space for optional interpreter command- 46 line arguments separated by white space */ 47#define IBE_INTERP_LEN_MAX (MAXPATHLEN + IBE_ARG_LEN_MAX) 48#define IBE_MAX_ENTRIES 64 /* Max number of interpreter entries. */ 49 50/* We only map the first page for identification purposes. */ 51#define IBE_MATCH_MAX PAGE_SIZE 52_Static_assert(IBE_MAGIC_MAX <= IBE_MATCH_MAX, 53 "Cannot identify binaries past the first page."); 54 55/* 56 * Imgact bin misc interpreter entry flags. 57 */ 58#define IBF_ENABLED 0x0001 /* Entry is active. */ 59#define IBF_USE_MASK 0x0002 /* Use mask on header magic field. */ 60 61#define IBF_VALID_UFLAGS 0x0003 /* Bits allowed from userland. */ 62 63/* 64 * Used with sysctlbyname() to pass imgact bin misc entries in and out of the 65 * kernel. 66 */ 67typedef struct ximgact_binmisc_entry { 68 uint32_t xbe_version; /* Struct version(IBE_VERSION) */ 69 uint32_t xbe_flags; /* Entry flags (IBF_*) */ 70 uint32_t xbe_moffset; /* Magic offset in header */ 71 uint32_t xbe_msize; /* Magic size */ 72 uint32_t spare[3]; /* Spare fields for future use */ 73 char xbe_name[IBE_NAME_MAX]; /* Unique interpreter name */ 74 char xbe_interpreter[IBE_INTERP_LEN_MAX]; /* Interpreter path + args */ 75 uint8_t xbe_magic[IBE_MAGIC_MAX]; /* Header Magic */ 76 uint8_t xbe_mask[IBE_MAGIC_MAX]; /* Magic Mask */ 77} ximgact_binmisc_entry_t; 78 79/* 80 * sysctl() command names. 81 */ 82#define IBE_SYSCTL_NAME "kern.binmisc" 83 84#define IBE_SYSCTL_NAME_ADD IBE_SYSCTL_NAME ".add" 85#define IBE_SYSCTL_NAME_REMOVE IBE_SYSCTL_NAME ".remove" 86#define IBE_SYSCTL_NAME_DISABLE IBE_SYSCTL_NAME ".disable" 87#define IBE_SYSCTL_NAME_ENABLE IBE_SYSCTL_NAME ".enable" 88#define IBE_SYSCTL_NAME_LOOKUP IBE_SYSCTL_NAME ".lookup" 89#define IBE_SYSCTL_NAME_LIST IBE_SYSCTL_NAME ".list" 90 91#define KMOD_NAME "imgact_binmisc" 92 93/* 94 * Examples of manipulating the interpreter table using sysctlbyname(3): 95 * 96 * #include <sys/imgact_binmisc.h> 97 * 98 * #define LLVM_MAGIC "BC\xc0\xde" 99 * 100 * #define MIPS64_ELF_MAGIC "\x7f\x45\x4c\x46\x02\x02\x01\x00\x00\x00" \ 101 * "\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08" 102 * #define MIPS64_ELF_MASK "\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff" \ 103 * "\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff" 104 * 105 * ximgact_binmisc_entry_t xbe, *xbep, out_xbe; 106 * size_t size = 0, osize; 107 * int error, i; 108 * 109 * // Add image activator for LLVM byte code 110 * bzero(&xbe, sizeof(xbe)); 111 * xbe.xbe_version = IBE_VERSION; 112 * xbe.xbe_flags = IBF_ENABLED; 113 * strlcpy(xbe.xbe_name, "llvm_bc", IBE_NAME_MAX); 114 * strlcpy(xbe.xbe_interpreter, "/usr/bin/lli --fake-arg0=#a", 115 * IBE_INTERP_LEN_MAX); 116 * xbe.xbe_moffset = 0; 117 * xbe.xbe_msize = 4; 118 * memcpy(xbe.xbe_magic, LLVM_MAGIC, xbe.xbe_msize); 119 * error = sysctlbyname(IBE_SYSCTL_NAME_ADD, NULL, NULL, &xbe, sizeof(xbe)); 120 * 121 * // Add image activator for mips64 ELF binaries to use qemu user mode 122 * bzero(&xbe, sizeof(xbe)); 123 * xbe.xbe_version = IBE_VERSION; 124 * xbe.xbe_flags = IBF_ENABLED | IBF_USE_MASK; 125 * strlcpy(xbe.xbe_name, "mips64elf", IBE_NAME_MAX); 126 * strlcpy(xbe.xbe_interpreter, "/usr/local/bin/qemu-mips64", 127 * IBE_INTERP_LEN_MAX); 128 * xbe.xbe_moffset = 0; 129 * xbe.xbe_msize = 20; 130 * memcpy(xbe.xbe_magic, MIPS64_ELF_MAGIC, xbe.xbe_msize); 131 * memcpy(xbe.xbe_mask, MIPS64_ELF_MASK, xbe.xbe_msize); 132 * sysctlbyname(IBE_SYSCTL_NAME_ADD, NULL, NULL, &xbe, sizeof(xbe)); 133 * 134 * // Disable (OR Enable OR Remove) image activator for LLVM byte code 135 * bzero(&xbe, sizeof(xbe)); 136 * xbe.xbe_version = IBE_VERSION; 137 * strlcpy(xbe.xbe_name, "llvm_bc", IBE_NAME_MAX); 138 * error = sysctlbyname(IBE_SYSCTL_NAME_DISABLE, NULL, NULL, &xbe, sizeof(xbe)); 139 * // OR sysctlbyname(IBE_SYSCTL_NAME_ENABLE, NULL, NULL, &xbe, sizeof(xbe)); 140 * // OR sysctlbyname(IBE_SYSCTL_NAME_REMOVE, NULL, NULL, &xbe, sizeof(xbe)); 141 * 142 * // Lookup image activator "llvm_bc" 143 * bzero(&xbe, sizeof(xbe)); 144 * xbe.xbe_version = IBE_VERSION; 145 * strlcpy(xbe.xbe_name, "llvm_bc", IBE_NAME_MAX); 146 * size = sizeof(out_xbe); 147 * error = sysctlbyname(IBE_SYSCTL_NAME_LOOKUP, &out_xbe, &size, &xbe, 148 * sizeof(xbe)); 149 * 150 * // Get all the currently configured image activators and report 151 * error = sysctlbyname(IBE_SYSCTL_NAME_LIST, NULL, &size, NULL, 0); 152 * if (0 == error && size > 0) { 153 * xbep = malloc(size); 154 * while(1) { 155 * osize = size; 156 * error = sysctlbyname("kern.binmisc.list", xbep, &size, NULL, 0); 157 * if (-1 == error && ENOMEM == errno && size == osize) { 158 * // The buffer too small and needs to grow 159 * size += sizeof(xbe); 160 * xbep = realloc(xbep, size); 161 * } else 162 * break; 163 * } 164 * } 165 * for(i = 0; i < (size / sizeof(xbe)); i++, xbep++) 166 * printf("name: %s interpreter: %s flags: %s %s\n", xbep->xbe_name, 167 * xbep->xbe_interpreter, (xbep->xbe_flags & IBF_ENABLED) ? 168 * "ENABLED" : "", (xbep->xbe_flags & IBF_ENABLED) ? "USE_MASK" : ""); 169 * 170 * The sysctlbyname() calls above may return the following errors in addition 171 * to the standard ones: 172 * 173 * [EINVAL] Invalid argument in the input ximgact_binmisc_entry_t structure. 174 * [EEXIST] Interpreter entry for given name already exist in kernel list. 175 * [ENOMEM] Allocating memory in the kernel failed or, in the case of 176 * kern.binmisc.list, the user buffer is too small. 177 * [ENOENT] Interpreter entry for given name is not found. 178 * [ENOSPC] Attempted to exceed maximum number of entries (IBE_MAX_ENTRIES). 179 */ 180 181#endif /* !_IMGACT_BINMISC_H_ */ 182