imgact_gzip.c revision 7090
1/* 2 * ---------------------------------------------------------------------------- 3 * "THE BEER-WARE LICENSE" (Revision 42): 4 * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you think 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7 * ---------------------------------------------------------------------------- 8 * 9 * $Id: imgact_gzip.c,v 1.12 1995/02/20 22:23:07 davidg Exp $ 10 * 11 * This module handles execution of a.out files which have been run through 12 * "gzip". This saves diskspace, but wastes cpu-cycles and VM. 13 * 14 * TODO: 15 * text-segments should be made R/O after being filled 16 * is the vm-stuff safe ? 17 * should handle the entire header of gzip'ed stuff. 18 * inflate isn't quite reentrant yet... 19 * error-handling is a mess... 20 * so is the rest... 21 * tidy up unnecesary includes 22 */ 23 24#include <sys/param.h> 25#include <sys/exec.h> 26#include <sys/imgact.h> 27#include <sys/imgact_aout.h> 28#include <sys/kernel.h> 29#include <sys/mman.h> 30#include <sys/resourcevar.h> 31#include <sys/sysent.h> 32#include <sys/systm.h> 33#include <sys/inflate.h> 34 35#include <vm/vm.h> 36#include <vm/vm_kern.h> 37 38struct imgact_gzip { 39 struct image_params *ip; 40 struct exec a_out; 41 int error; 42 int where; 43 u_char *inbuf; 44 u_long offset; 45 u_long output; 46 u_long len; 47 int idx; 48 u_long virtual_offset, file_offset, file_end, bss_size; 49}; 50 51static int NextByte __P((void *vp)); 52static int do_aout_hdr __P((struct imgact_gzip *)); 53static int Flush __P((void *vp, u_char *, u_long siz)); 54 55int 56exec_gzip_imgact(iparams) 57 struct image_params *iparams; 58{ 59 int error, error2 = 0; 60 u_char *p = (u_char *) iparams->image_header; 61 struct imgact_gzip igz; 62 struct inflate infl; 63 64 /* If these four are not OK, it isn't a gzip file */ 65 if (p[0] != 0x1f) 66 return -1; /* 0 Simply magic */ 67 if (p[1] != 0x8b) 68 return -1; /* 1 Simply magic */ 69 if (p[2] != 0x08) 70 return -1; /* 2 Compression method */ 71 if (p[9] != 0x03) 72 return -1; /* 9 OS compressed on */ 73 74 /* 75 * If this one contains anything but a comment or a filename marker, 76 * we don't want to chew on it 77 */ 78 if (p[3] & ~(0x18)) 79 return ENOEXEC; /* 3 Flags */ 80 81 /* These are of no use to us */ 82 /* 4-7 Timestamp */ 83 /* 8 Extra flags */ 84 85 bzero(&igz, sizeof igz); 86 bzero(&infl, sizeof infl); 87 infl.gz_private = (void *) &igz; 88 infl.gz_input = NextByte; 89 infl.gz_output = Flush; 90 91 igz.ip = iparams; 92 igz.idx = 10; 93 94 if (p[3] & 0x08) { /* skip a filename */ 95 while (p[igz.idx++]) 96 if (igz.idx >= PAGE_SIZE) 97 return ENOEXEC; 98 } 99 if (p[3] & 0x10) { /* skip a comment */ 100 while (p[igz.idx++]) 101 if (igz.idx >= PAGE_SIZE) 102 return ENOEXEC; 103 } 104 igz.len = igz.ip->attr->va_size; 105 106 error = inflate(&infl); 107 108 if (igz.inbuf) { 109 error2 = 110 vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf, 111 (vm_offset_t) igz.inbuf + PAGE_SIZE); 112 } 113 if (igz.error || error || error2) { 114 printf("Output=%lu ", igz.output); 115 printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n", 116 error, igz.error, error2, igz.where); 117 } 118 if (igz.error) 119 return igz.error; 120 if (error) 121 return ENOEXEC; 122 if (error2) 123 return error2; 124 return 0; 125} 126 127static int 128do_aout_hdr(struct imgact_gzip * gz) 129{ 130 int error; 131 struct vmspace *vmspace = gz->ip->proc->p_vmspace; 132 u_long vmaddr; 133 134 /* 135 * Set file/virtual offset based on a.out variant. We do two cases: 136 * host byte order and network byte order (for NetBSD compatibility) 137 */ 138 switch ((int) (gz->a_out.a_magic & 0xffff)) { 139 case ZMAGIC: 140 gz->virtual_offset = 0; 141 if (gz->a_out.a_text) { 142 gz->file_offset = NBPG; 143 } else { 144 /* Bill's "screwball mode" */ 145 gz->file_offset = 0; 146 } 147 break; 148 case QMAGIC: 149 gz->virtual_offset = NBPG; 150 gz->file_offset = 0; 151 break; 152 default: 153 /* NetBSD compatibility */ 154 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) { 155 case ZMAGIC: 156 case QMAGIC: 157 gz->virtual_offset = NBPG; 158 gz->file_offset = 0; 159 break; 160 default: 161 gz->where = __LINE__; 162 return (-1); 163 } 164 } 165 166 gz->bss_size = roundup(gz->a_out.a_bss, NBPG); 167 168 /* 169 * Check various fields in header for validity/bounds. 170 */ 171 if ( /* entry point must lay with text region */ 172 gz->a_out.a_entry < gz->virtual_offset || 173 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text || 174 175 /* text and data size must each be page rounded */ 176 gz->a_out.a_text % NBPG || 177 gz->a_out.a_data % NBPG) { 178 gz->where = __LINE__; 179 return (-1); 180 } 181 /* 182 * text/data/bss must not exceed limits 183 */ 184 if ( /* text can't exceed maximum text size */ 185 gz->a_out.a_text > MAXTSIZ || 186 187 /* data + bss can't exceed maximum data size */ 188 gz->a_out.a_data + gz->bss_size > MAXDSIZ || 189 190 /* data + bss can't exceed rlimit */ 191 gz->a_out.a_data + gz->bss_size > 192 gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) { 193 gz->where = __LINE__; 194 return (ENOMEM); 195 } 196 /* Find out how far we should go */ 197 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data; 198 199 /* copy in arguments and/or environment from old process */ 200 error = exec_extract_strings(gz->ip); 201 if (error) { 202 gz->where = __LINE__; 203 return (error); 204 } 205 /* 206 * Destroy old process VM and create a new one (with a new stack) 207 */ 208 exec_new_vmspace(gz->ip); 209 210 vmaddr = gz->virtual_offset; 211 212 error = vm_mmap(&vmspace->vm_map, /* map */ 213 &vmaddr,/* address */ 214 gz->a_out.a_text, /* size */ 215 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, /* protection */ 216 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, 217 MAP_ANON | MAP_FIXED, /* flags */ 218 0, /* vnode */ 219 0); /* offset */ 220 221 if (error) { 222 gz->where = __LINE__; 223 return (error); 224 } 225 vmaddr = gz->virtual_offset + gz->a_out.a_text; 226 227 /* 228 * Map data read/write (if text is 0, assume text is in data area 229 * [Bill's screwball mode]) 230 */ 231 232 error = vm_mmap(&vmspace->vm_map, 233 &vmaddr, 234 gz->a_out.a_data, 235 VM_PROT_READ | VM_PROT_WRITE | (gz->a_out.a_text ? 0 : VM_PROT_EXECUTE), 236 VM_PROT_ALL, MAP_ANON | MAP_FIXED, 237 0, 238 0); 239 240 if (error) { 241 gz->where = __LINE__; 242 return (error); 243 } 244 if (gz->bss_size != 0) { 245 /* 246 * Allocate demand-zeroed area for uninitialized data "bss" = 'block 247 * started by symbol' - named after the IBM 7090 instruction of the 248 * same name. 249 */ 250 vmaddr = gz->virtual_offset + gz->a_out.a_text + gz->a_out.a_data; 251 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, gz->bss_size, FALSE); 252 if (error) { 253 gz->where = __LINE__; 254 return (error); 255 } 256 } 257 /* Fill in process VM information */ 258 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT; 259 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT; 260 vmspace->vm_taddr = (caddr_t) gz->virtual_offset; 261 vmspace->vm_daddr = (caddr_t) gz->virtual_offset + gz->a_out.a_text; 262 263 /* Fill in image_params */ 264 gz->ip->interpreted = 0; 265 gz->ip->entry_addr = gz->a_out.a_entry; 266 267 gz->ip->proc->p_sysent = &aout_sysvec; 268 269 return 0; 270} 271 272static int 273NextByte(void *vp) 274{ 275 int error; 276 struct imgact_gzip *igz = (struct imgact_gzip *) vp; 277 278 if (igz->idx >= igz->len) { 279 igz->where = __LINE__; 280 return GZ_EOF; 281 } 282 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) { 283 return igz->inbuf[(igz->idx++) - igz->offset]; 284 } 285 if (igz->inbuf) { 286 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf, 287 (vm_offset_t) igz->inbuf + PAGE_SIZE); 288 if (error) { 289 igz->where = __LINE__; 290 igz->error = error; 291 return GZ_EOF; 292 } 293 } 294 igz->offset = igz->idx & ~PAGE_MASK; 295 296 error = vm_mmap(kernel_map, /* map */ 297 (vm_offset_t *) & igz->inbuf, /* address */ 298 PAGE_SIZE, /* size */ 299 VM_PROT_READ, /* protection */ 300 VM_PROT_READ, /* max protection */ 301 0, /* flags */ 302 (caddr_t) igz->ip->vnodep, /* vnode */ 303 igz->offset); /* offset */ 304 if (error) { 305 igz->where = __LINE__; 306 igz->error = error; 307 return GZ_EOF; 308 } 309 return igz->inbuf[(igz->idx++) - igz->offset]; 310} 311 312static int 313Flush(void *vp, u_char * ptr, u_long siz) 314{ 315 struct imgact_gzip *gz = (struct imgact_gzip *) vp; 316 u_char *p = ptr, *q; 317 int i; 318 319 /* First, find a a.out-header */ 320 if (gz->output < sizeof gz->a_out) { 321 q = (u_char *) & gz->a_out; 322 i = min(siz, sizeof gz->a_out - gz->output); 323 bcopy(p, q + gz->output, i); 324 gz->output += i; 325 p += i; 326 siz -= i; 327 if (gz->output == sizeof gz->a_out) { 328 i = do_aout_hdr(gz); 329 if (i == -1) { 330 if (!gz->where) 331 gz->where = __LINE__; 332 gz->error = ENOEXEC; 333 return ENOEXEC; 334 } else if (i) { 335 gz->where = __LINE__; 336 gz->error = i; 337 return ENOEXEC; 338 } 339 if (gz->file_offset < sizeof gz->a_out) { 340 q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset; 341 bcopy(&gz->a_out, q, sizeof gz->a_out - gz->file_offset); 342 } 343 } 344 } 345 /* Skip over zero-padded first PAGE if needed */ 346 if (gz->output < gz->file_offset && (gz->output + siz) > gz->file_offset) { 347 i = min(siz, gz->file_offset - gz->output); 348 gz->output += i; 349 p += i; 350 siz -= i; 351 } 352 if (gz->output >= gz->file_offset && gz->output < gz->file_end) { 353 i = min(siz, gz->file_end - gz->output); 354 q = (u_char *) gz->virtual_offset + gz->output - gz->file_offset; 355 bcopy(p, q, i); 356 gz->output += i; 357 p += i; 358 siz -= i; 359 } 360 gz->output += siz; 361 return 0; 362} 363 364 365/* 366 * Tell kern_execve.c about it, with a little help from the linker. 367 * Since `const' objects end up in the text segment, TEXT_SET is the 368 * correct directive to use. 369 */ 370 371static const struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"}; 372TEXT_SET(execsw_set, gzip_execsw); 373