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