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