Do better range checking on the LDT. FreeBSD-SA-05:07.ldt
[dragonfly.git] / sys / i386 / i386 / sys_machdep.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      from: @(#)sys_machdep.c 5.5 (Berkeley) 1/19/91
34  * $FreeBSD: src/sys/i386/i386/sys_machdep.c,v 1.47.2.3 2002/10/07 17:20:00 jhb Exp $
35  * $DragonFly: src/sys/i386/i386/Attic/sys_machdep.c,v 1.17 2005/05/23 18:23:29 dillon Exp $
36  *
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/malloc.h>
43 #include <sys/thread.h>
44 #include <sys/proc.h>
45 #include <sys/thread.h>
46
47 #include <vm/vm.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
52
53 #include <sys/user.h>
54
55 #include <machine/cpu.h>
56 #include <machine/ipl.h>
57 #include <machine/pcb_ext.h>    /* pcb.h included by sys/user.h */
58 #include <machine/sysarch.h>
59 #ifdef SMP
60 #include <machine/smp.h>
61 #endif
62 #include <machine/globaldata.h> /* mdcpu */
63
64 #include <vm/vm_kern.h>         /* for kernel_map */
65 #include <sys/thread2.h>
66
67 #define MAX_LD 8192
68 #define LD_PER_PAGE 512
69 #define NEW_MAX_LD(num)  ((num + LD_PER_PAGE) & ~(LD_PER_PAGE-1))
70 #define SIZE_FROM_LARGEST_LD(num) (NEW_MAX_LD(num) << 3)
71
72
73
74 static int i386_get_ldt (struct proc *, char *, int *);
75 static int i386_set_ldt (struct proc *, char *, int *);
76 static int i386_get_ioperm      (struct proc *, char *);
77 static int i386_set_ioperm      (struct proc *, char *);
78 static int check_descs(union descriptor *, int);
79 int i386_extend_pcb     (struct proc *);
80
81 /*
82  * sysarch_args(int op, char *params)
83  */
84
85 int
86 sysarch(struct sysarch_args *uap)
87 {
88         struct proc *p = curproc;
89         int error = 0;
90
91         switch(uap->op) {
92         case I386_GET_LDT:
93                 error = i386_get_ldt(p, uap->parms, &uap->sysmsg_result);
94                 break;
95         case I386_SET_LDT:
96                 error = i386_set_ldt(p, uap->parms, &uap->sysmsg_result);
97                 break;
98         case I386_GET_IOPERM:
99                 error = i386_get_ioperm(p, uap->parms);
100                 break;
101         case I386_SET_IOPERM:
102                 error = i386_set_ioperm(p, uap->parms);
103                 break;
104         case I386_VM86:
105                 error = vm86_sysarch(p, uap->parms);
106                 break;
107         default:
108                 error = EOPNOTSUPP;
109                 break;
110         }
111         return (error);
112 }
113
114 int
115 i386_extend_pcb(struct proc *p)
116 {
117         int i, offset;
118         u_long *addr;
119         struct pcb_ext *ext;
120         struct soft_segment_descriptor ssd = {
121                 0,                      /* segment base address (overwritten) */
122                 ctob(IOPAGES + 1) - 1,  /* length */
123                 SDT_SYS386TSS,          /* segment type */
124                 0,                      /* priority level */
125                 1,                      /* descriptor present */
126                 0, 0,
127                 0,                      /* default 32 size */
128                 0                       /* granularity */
129         };
130
131         ext = (struct pcb_ext *)kmem_alloc(kernel_map, ctob(IOPAGES+1));
132         if (ext == 0)
133                 return (ENOMEM);
134         p->p_thread->td_pcb->pcb_ext = ext;
135         bzero(ext, sizeof(struct pcb_ext)); 
136         ext->ext_tss.tss_esp0 = (unsigned)((char *)p->p_thread->td_pcb - 16);
137         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
138         /*
139          * The last byte of the i/o map must be followed by an 0xff byte.
140          * We arbitrarily allocate 16 bytes here, to keep the starting
141          * address on a doubleword boundary.
142          */
143         offset = PAGE_SIZE - 16;
144         ext->ext_tss.tss_ioopt = 
145             (offset - ((unsigned)&ext->ext_tss - (unsigned)ext)) << 16;
146         ext->ext_iomap = (caddr_t)ext + offset;
147         ext->ext_vm86.vm86_intmap = (caddr_t)ext + offset - 32;
148
149         addr = (u_long *)ext->ext_vm86.vm86_intmap;
150         for (i = 0; i < (ctob(IOPAGES) + 32 + 16) / sizeof(u_long); i++)
151                 *addr++ = ~0;
152
153         ssd.ssd_base = (unsigned)&ext->ext_tss;
154         ssd.ssd_limit -= ((unsigned)&ext->ext_tss - (unsigned)ext);
155         ssdtosd(&ssd, &ext->ext_tssd);
156         
157         /* switch to the new TSS after syscall completes */
158         need_user_resched();
159
160         return 0;
161 }
162
163 static int
164 i386_set_ioperm(struct proc *p, char *args)
165 {
166         int i, error;
167         struct i386_ioperm_args ua;
168         char *iomap;
169
170         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
171                 return (error);
172
173         if ((error = suser_cred(p->p_ucred, 0)) != 0)
174                 return (error);
175         if (securelevel > 0)
176                 return (EPERM);
177         /*
178          * XXX 
179          * While this is restricted to root, we should probably figure out
180          * whether any other driver is using this i/o address, as so not to
181          * cause confusion.  This probably requires a global 'usage registry'.
182          */
183
184         if (p->p_thread->td_pcb->pcb_ext == 0)
185                 if ((error = i386_extend_pcb(p)) != 0)
186                         return (error);
187         iomap = (char *)p->p_thread->td_pcb->pcb_ext->ext_iomap;
188
189         if (ua.start + ua.length > IOPAGES * PAGE_SIZE * NBBY)
190                 return (EINVAL);
191
192         for (i = ua.start; i < ua.start + ua.length; i++) {
193                 if (ua.enable) 
194                         iomap[i >> 3] &= ~(1 << (i & 7));
195                 else
196                         iomap[i >> 3] |= (1 << (i & 7));
197         }
198         return (error);
199 }
200
201 static int
202 i386_get_ioperm(struct proc *p, char *args)
203 {
204         int i, state, error;
205         struct i386_ioperm_args ua;
206         char *iomap;
207
208         if ((error = copyin(args, &ua, sizeof(struct i386_ioperm_args))) != 0)
209                 return (error);
210         if (ua.start >= IOPAGES * PAGE_SIZE * NBBY)
211                 return (EINVAL);
212
213         if (p->p_thread->td_pcb->pcb_ext == 0) {
214                 ua.length = 0;
215                 goto done;
216         }
217
218         iomap = (char *)p->p_thread->td_pcb->pcb_ext->ext_iomap;
219
220         i = ua.start;
221         state = (iomap[i >> 3] >> (i & 7)) & 1;
222         ua.enable = !state;
223         ua.length = 1;
224
225         for (i = ua.start + 1; i < IOPAGES * PAGE_SIZE * NBBY; i++) {
226                 if (state != ((iomap[i >> 3] >> (i & 7)) & 1))
227                         break;
228                 ua.length++;
229         }
230                         
231 done:
232         error = copyout(&ua, args, sizeof(struct i386_ioperm_args));
233         return (error);
234 }
235
236 /*
237  * Update the TLS entries for the process.  Used by assembly, do not staticize.
238  *
239  * Must be called from a critical section (else an interrupt thread preemption
240  * may cause %gs to fault).  Normally called from the low level swtch.s code.
241  */
242 void
243 set_user_TLS(void)
244 {
245         struct thread *td = curthread;
246         int i;
247 #ifdef SMP
248         int off = GTLS_START + mycpu->gd_cpuid * NGDT;
249 #else
250         const int off = GTLS_START;
251 #endif
252         for (i = 0; i < NGTLS; ++i)
253                 gdt[off + i].sd = td->td_tls[i];
254 }
255
256 /*
257  * Update the GDT entry pointing to the LDT to point to the LDT of the
258  * current process.  Used by assembly, do not staticize.
259  *
260  * Must be called from a critical section (else an interrupt thread preemption
261  * may cause %gs to fault).  Normally called from the low level swtch.s code.
262  */   
263 void
264 set_user_ldt(struct pcb *pcb)
265 {
266         struct pcb_ldt *pcb_ldt;
267
268         if (pcb != curthread->td_pcb)
269                 return;
270
271         pcb_ldt = pcb->pcb_ldt;
272 #ifdef SMP
273         gdt[mycpu->gd_cpuid * NGDT + GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
274 #else
275         gdt[GUSERLDT_SEL].sd = pcb_ldt->ldt_sd;
276 #endif
277         lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
278         mdcpu->gd_currentldt = GSEL(GUSERLDT_SEL, SEL_KPL);
279 }
280
281 struct pcb_ldt *
282 user_ldt_alloc(struct pcb *pcb, int len)
283 {
284         struct pcb_ldt *pcb_ldt, *new_ldt;
285
286         MALLOC(new_ldt, struct pcb_ldt *, sizeof(struct pcb_ldt),
287                 M_SUBPROC, M_WAITOK);
288         if (new_ldt == NULL)
289                 return NULL;
290
291         new_ldt->ldt_len = len = NEW_MAX_LD(len);
292         new_ldt->ldt_base = (caddr_t)kmem_alloc(kernel_map,
293                 len * sizeof(union descriptor));
294         if (new_ldt->ldt_base == NULL) {
295                 FREE(new_ldt, M_SUBPROC);
296                 return NULL;
297         }
298         new_ldt->ldt_refcnt = 1;
299         new_ldt->ldt_active = 0;
300
301         gdt_segs[GUSERLDT_SEL].ssd_base = (unsigned)new_ldt->ldt_base;
302         gdt_segs[GUSERLDT_SEL].ssd_limit = len * sizeof(union descriptor) - 1;
303         ssdtosd(&gdt_segs[GUSERLDT_SEL], &new_ldt->ldt_sd);
304
305         if ((pcb_ldt = pcb->pcb_ldt)) {
306                 if (len > pcb_ldt->ldt_len)
307                         len = pcb_ldt->ldt_len;
308                 bcopy(pcb_ldt->ldt_base, new_ldt->ldt_base,
309                         len * sizeof(union descriptor));
310         } else {
311                 bcopy(ldt, new_ldt->ldt_base, sizeof(ldt));
312         }
313         return new_ldt;
314 }
315
316 void
317 user_ldt_free(struct pcb *pcb)
318 {
319         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
320
321         if (pcb_ldt == NULL)
322                 return;
323
324         crit_enter();
325         if (pcb == curthread->td_pcb) {
326                 lldt(_default_ldt);
327                 mdcpu->gd_currentldt = _default_ldt;
328         }
329         pcb->pcb_ldt = NULL;
330         crit_exit();
331
332         if (--pcb_ldt->ldt_refcnt == 0) {
333                 kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
334                         pcb_ldt->ldt_len * sizeof(union descriptor));
335                 FREE(pcb_ldt, M_SUBPROC);
336         }
337 }
338
339 static int
340 i386_get_ldt(struct proc *p, char *args, int *res)
341 {
342         int error = 0;
343         struct pcb *pcb = p->p_thread->td_pcb;
344         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
345         unsigned int nldt, num;
346         union descriptor *lp;
347         int s;
348         struct i386_ldt_args ua, *uap = &ua;
349
350         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
351                 return(error);
352
353 #ifdef  DEBUG
354         printf("i386_get_ldt: start=%d num=%d descs=%p\n",
355             uap->start, uap->num, (void *)uap->descs);
356 #endif
357
358         s = splhigh();
359
360         if (pcb_ldt) {
361                 nldt = (unsigned int)pcb_ldt->ldt_len;
362                 num = min(uap->num, nldt);
363                 lp = &((union descriptor *)(pcb_ldt->ldt_base))[uap->start];
364         } else {
365                 nldt = (unsigned int)(sizeof(ldt) / sizeof(ldt[0]));
366                 num = min(uap->num, nldt);
367                 lp = &ldt[uap->start];
368         }
369
370         /*
371          * note: uap->(args), num, and nldt are unsigned.  nldt and num
372          * are limited in scope, but uap->start can be anything.
373          */
374         if (uap->start > nldt || uap->start + num > nldt) {
375                 splx(s);
376                 return(EINVAL);
377         }
378
379         error = copyout(lp, uap->descs, num * sizeof(union descriptor));
380         if (!error)
381                 *res = num;
382         splx(s);
383         return(error);
384 }
385
386 static int
387 i386_set_ldt(struct proc *p, char *args, int *res)
388 {
389         int error = 0;
390         int largest_ld;
391         struct pcb *pcb = p->p_thread->td_pcb;
392         struct pcb_ldt *pcb_ldt = pcb->pcb_ldt;
393         union descriptor *descs;
394         int descs_size;
395         struct i386_ldt_args ua, *uap = &ua;
396
397         if ((error = copyin(args, uap, sizeof(struct i386_ldt_args))) < 0)
398                 return(error);
399
400 #ifdef  DEBUG
401         printf("i386_set_ldt: start=%d num=%d descs=%p\n",
402             uap->start, uap->num, (void *)uap->descs);
403 #endif
404
405         /* verify range of descriptors to modify */
406         if ((uap->start < 0) || (uap->start >= MAX_LD) || (uap->num < 0) ||
407                 (uap->num > MAX_LD))
408         {
409                 return(EINVAL);
410         }
411         largest_ld = uap->start + uap->num - 1;
412         if (largest_ld >= MAX_LD)
413                 return(EINVAL);
414
415         /* allocate user ldt */
416         if (!pcb_ldt || largest_ld >= pcb_ldt->ldt_len) {
417                 struct pcb_ldt *new_ldt = user_ldt_alloc(pcb, largest_ld);
418                 if (new_ldt == NULL)
419                         return ENOMEM;
420                 if (pcb_ldt) {
421                         pcb_ldt->ldt_sd = new_ldt->ldt_sd;
422                         kmem_free(kernel_map, (vm_offset_t)pcb_ldt->ldt_base,
423                                 pcb_ldt->ldt_len * sizeof(union descriptor));
424                         pcb_ldt->ldt_base = new_ldt->ldt_base;
425                         pcb_ldt->ldt_len = new_ldt->ldt_len;
426                         FREE(new_ldt, M_SUBPROC);
427                 } else {
428                         pcb->pcb_ldt = pcb_ldt = new_ldt;
429                 }
430                 /*
431                  * Since the LDT may be shared, we must signal other cpus to
432                  * reload it.  XXX we need to track which cpus might be
433                  * using the shared ldt and only signal those.
434                  */
435 #ifdef SMP
436                 smp_rendezvous(NULL, (void (*)(void *))set_user_ldt, NULL, pcb);
437 #else
438                 set_user_ldt(pcb);
439 #endif
440         }
441
442         descs_size = uap->num * sizeof(union descriptor);
443         descs = (union descriptor *)kmem_alloc(kernel_map, descs_size);
444         if (descs == NULL)
445                 return (ENOMEM);
446         error = copyin(&uap->descs[0], descs, descs_size);
447         if (error) {
448                 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
449                 return (error);
450         }
451         /* Check descriptors for access violations */
452         error = check_descs(descs, uap->num);
453         if (error) {
454                 kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
455                 return (error);
456         }
457
458         /*
459          * Fill in the actual ldt entries.  Since %fs might point to one of
460          * these entries a critical section is required to prevent an
461          * interrupt thread from preempting us, switch back, and faulting
462          * on the load of %fs due to a half-formed descriptor.
463          */
464         crit_enter();
465         bcopy(descs, 
466                  &((union descriptor *)(pcb_ldt->ldt_base))[uap->start],
467                 uap->num * sizeof(union descriptor));
468         *res = uap->start;
469
470         crit_exit();
471         kmem_free(kernel_map, (vm_offset_t)descs, descs_size);
472         return (0);
473 }
474
475 static int
476 check_descs(union descriptor *descs, int num)
477 {
478         int i;
479
480         /* Check descriptors for access violations */
481         for (i = 0; i < num; i++) {
482                 union descriptor *dp;
483                 dp = &descs[i];
484
485                 switch (dp->sd.sd_type) {
486                 case SDT_SYSNULL:       /* system null */ 
487                         dp->sd.sd_p = 0;
488                         break;
489                 case SDT_SYS286TSS: /* system 286 TSS available */
490                 case SDT_SYSLDT:    /* system local descriptor table */
491                 case SDT_SYS286BSY: /* system 286 TSS busy */
492                 case SDT_SYSTASKGT: /* system task gate */
493                 case SDT_SYS286IGT: /* system 286 interrupt gate */
494                 case SDT_SYS286TGT: /* system 286 trap gate */
495                 case SDT_SYSNULL2:  /* undefined by Intel */ 
496                 case SDT_SYS386TSS: /* system 386 TSS available */
497                 case SDT_SYSNULL3:  /* undefined by Intel */
498                 case SDT_SYS386BSY: /* system 386 TSS busy */
499                 case SDT_SYSNULL4:  /* undefined by Intel */ 
500                 case SDT_SYS386IGT: /* system 386 interrupt gate */
501                 case SDT_SYS386TGT: /* system 386 trap gate */
502                 case SDT_SYS286CGT: /* system 286 call gate */ 
503                 case SDT_SYS386CGT: /* system 386 call gate */
504                         /* I can't think of any reason to allow a user proc
505                          * to create a segment of these types.  They are
506                          * for OS use only.
507                          */
508                         return EACCES;
509
510                 /* memory segment types */
511                 case SDT_MEMEC:   /* memory execute only conforming */
512                 case SDT_MEMEAC:  /* memory execute only accessed conforming */
513                 case SDT_MEMERC:  /* memory execute read conforming */
514                 case SDT_MEMERAC: /* memory execute read accessed conforming */
515                         /* Must be "present" if executable and conforming. */
516                         if (dp->sd.sd_p == 0)
517                                 return (EACCES);
518                         break;
519                 case SDT_MEMRO:   /* memory read only */
520                 case SDT_MEMROA:  /* memory read only accessed */
521                 case SDT_MEMRW:   /* memory read write */
522                 case SDT_MEMRWA:  /* memory read write accessed */
523                 case SDT_MEMROD:  /* memory read only expand dwn limit */
524                 case SDT_MEMRODA: /* memory read only expand dwn lim accessed */
525                 case SDT_MEMRWD:  /* memory read write expand dwn limit */  
526                 case SDT_MEMRWDA: /* memory read write expand dwn lim acessed */
527                 case SDT_MEME:    /* memory execute only */ 
528                 case SDT_MEMEA:   /* memory execute only accessed */
529                 case SDT_MEMER:   /* memory execute read */
530                 case SDT_MEMERA:  /* memory execute read accessed */
531                         break;
532                 default:
533                         return(EINVAL);
534                         /*NOTREACHED*/
535                 }
536
537                 /* Only user (ring-3) descriptors may be present. */
538                 if ((dp->sd.sd_p != 0) && (dp->sd.sd_dpl != SEL_UPL))
539                         return (EACCES);
540         }
541         return (0);
542 }