e90a533a22ae2042849d393f6a4aee5362e7f255
[dragonfly.git] / sys / kern / kern_varsym.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/kern/kern_varsym.c,v 1.9 2007/04/30 07:18:54 dillon Exp $
35  */
36
37 /*
38  * This module implements variable storage and management for variant
39  * symlinks.  These variables may also be used for general purposes.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/ucred.h>
46 #include <sys/resourcevar.h>
47 #include <sys/proc.h>
48 #include <sys/priv.h>
49 #include <sys/jail.h>
50 #include <sys/queue.h>
51 #include <sys/sysctl.h>
52 #include <sys/malloc.h>
53 #include <sys/varsym.h>
54 #include <sys/sysproto.h>
55
56 MALLOC_DEFINE(M_VARSYM, "varsym", "variable sets for variant symlinks");
57
58 struct varsymset        varsymset_sys;
59
60 /*
61  * Initialize the variant symlink subsystem
62  */
63 static void
64 varsym_sysinit(void *dummy)
65 {
66     varsymset_init(&varsymset_sys, NULL);
67 }
68 SYSINIT(announce, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, varsym_sysinit, NULL);
69
70 /*
71  * varsymreplace() - called from namei
72  *
73  *      Do variant symlink variable substitution
74  */
75 int
76 varsymreplace(char *cp, int linklen, int maxlen)
77 {
78     int rlen;
79     int xlen;
80     int nlen;
81     int i;
82     varsym_t var;
83
84     rlen = linklen;
85     while (linklen > 1) {
86         if (cp[0] == '$' && cp[1] == '{') {
87             for (i = 2; i < linklen; ++i) {
88                 if (cp[i] == '}')
89                     break;
90             }
91             if (i < linklen && 
92                 (var = varsymfind(VARSYM_ALL_MASK, cp + 2, i - 2)) != NULL
93             ) {
94                 xlen = i + 1;                   /* bytes to strike */
95                 nlen = strlen(var->vs_data);    /* bytes to add */
96                 if (linklen + nlen - xlen >= maxlen) {
97                     varsymdrop(var);
98                     return(-1);
99                 }
100                 KKASSERT(linklen >= xlen);
101                 if (linklen != xlen)
102                     bcopy(cp + xlen, cp + nlen, linklen - xlen);
103                 bcopy(var->vs_data, cp, nlen);
104                 linklen += nlen - xlen; /* new relative length */
105                 rlen += nlen - xlen;    /* returned total length */
106                 cp += nlen;             /* adjust past replacement */
107                 linklen -= nlen;        /* adjust past replacement */
108                 maxlen -= nlen;         /* adjust past replacement */
109             } else {
110                 /*
111                  * It's ok if i points to the '}', it will simply be
112                  * skipped.  i could also have hit linklen.
113                  */
114                 cp += i;
115                 linklen -= i;
116                 maxlen -= i;
117             }
118         } else {
119             ++cp;
120             --linklen;
121             --maxlen;
122         }
123     }
124     return(rlen);
125 }
126
127 /*
128  * varsym_set() system call
129  *
130  * (int level, const char *name, const char *data)
131  *
132  * MPALMOSTSAFE
133  */
134 int
135 sys_varsym_set(struct varsym_set_args *uap)
136 {
137     char name[MAXVARSYM_NAME];
138     char *buf;
139     struct thread *td;
140     struct proc *p;
141     struct lwp *lp;
142     int error;
143
144     td = curthread;
145     lp = td->td_lwp;
146     p = lp ? lp->lwp_proc : NULL;
147
148     if ((error = copyinstr(uap->name, name, sizeof(name), NULL)) != 0)
149         goto done2;
150     buf = kmalloc(MAXVARSYM_DATA, M_TEMP, M_WAITOK);
151     if (uap->data && 
152         (error = copyinstr(uap->data, buf, MAXVARSYM_DATA, NULL)) != 0)
153     {
154         goto done1;
155     }
156
157     get_mplock();
158
159     switch(uap->level) {
160     case VARSYM_SYS:
161         if (lp != NULL && td->td_ucred->cr_prison != NULL)
162             uap->level = VARSYM_PRISON;
163     case VARSYM_PRISON:
164         if (lp != NULL &&
165             (error = priv_check_cred(td->td_ucred, PRIV_VARSYM_SYS, 0)) != 0)
166             break;
167         /* fall through */
168     case VARSYM_USER:
169         /* XXX check jail / implement per-jail user */
170         /* fall through */
171     case VARSYM_PROC:
172         if (uap->data) {
173             (void)varsymmake(uap->level, name, NULL);
174             error = varsymmake(uap->level, name, buf);
175         } else {
176             error = varsymmake(uap->level, name, NULL);
177         }
178         break;
179     }
180     rel_mplock();
181 done1:
182     kfree(buf, M_TEMP);
183 done2:
184     return(error);
185 }
186
187 /*
188  * varsym_get() system call
189  *
190  * (int mask, const char *wild, char *buf, int bufsize)
191  *
192  * MPALMOSTSAFE
193  */
194 int
195 sys_varsym_get(struct varsym_get_args *uap)
196 {
197     char wild[MAXVARSYM_NAME];
198     varsym_t sym;
199     int error;
200     int dlen;
201
202     get_mplock();
203     if ((error = copyinstr(uap->wild, wild, sizeof(wild), NULL)) != 0)
204         goto done;
205     sym = varsymfind(uap->mask, wild, strlen(wild));
206     if (sym == NULL) {
207         error = ENOENT;
208         goto done;
209     }
210     dlen = strlen(sym->vs_data);
211     if (dlen < uap->bufsize) {
212         copyout(sym->vs_data, uap->buf, dlen + 1);
213     } else if (uap->bufsize) {
214         copyout("", uap->buf, 1);
215     }
216     uap->sysmsg_result = dlen + 1;
217     varsymdrop(sym);
218 done:
219     rel_mplock();
220     return(error);
221 }
222
223 /*
224  * varsym_list() system call
225  *
226  * (int level, char *buf, int maxsize, int *marker)
227  *
228  * MPALMOSTSAFE
229  */
230 int
231 sys_varsym_list(struct varsym_list_args *uap)
232 {
233         struct varsymset *vss;
234         struct varsyment *ve;
235         struct thread *td;
236         struct proc *p;
237         struct lwp *lp;
238         int i;
239         int error;
240         int bytes;
241         int earlyterm;
242         int marker;
243
244         /*
245          * Get the marker from userspace.
246          */
247         get_mplock();
248         if ((error = copyin(uap->marker, &marker, sizeof(marker))) != 0)
249                 goto done;
250
251         /*
252          * Figure out the varsym set.
253          */
254         td = curthread;
255         lp = td->td_lwp;
256         p = lp ? lp->lwp_proc : NULL;
257
258         vss = NULL;
259
260         switch (uap->level) {
261         case VARSYM_PROC:
262                 if (p)
263                         vss = &p->p_varsymset;
264                 break;
265         case VARSYM_USER:
266                 if (lp)
267                         vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
268                 break;
269         case VARSYM_SYS:
270                 vss = &varsymset_sys;
271                 break;
272         case VARSYM_PRISON:
273                 if (lp && td->td_ucred->cr_prison)
274                         vss = &td->td_ucred->cr_prison->pr_varsymset;
275                 break;
276         }
277         if (vss == NULL) {
278                 error = EINVAL;
279                 goto done;
280         }
281
282         /*
283          * Loop through the variables and dump them to uap->buf
284          */
285         i = 0;
286         bytes = 0;
287         earlyterm = 0;
288
289         lockmgr(&vss->vx_lock, LK_SHARED);
290         TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
291                 varsym_t sym = ve->ve_sym;
292                 int namelen = strlen(sym->vs_name);
293                 int datalen = strlen(sym->vs_data);
294                 int totlen = namelen + datalen + 2;
295
296                 /*
297                  * Skip to our index point
298                  */
299                 if (i < marker) {
300                         ++i;
301                         continue;
302                 }
303
304                 /*
305                  * Stop if there is insufficient space in the user buffer.
306                  * If we haven't stored anything yet return EOVERFLOW. 
307                  * Note that the marker index (i) does not change.
308                  */
309                 if (bytes + totlen > uap->maxsize) {
310                         if (bytes == 0)
311                                 error = EOVERFLOW;
312                         earlyterm = 1;
313                         break;
314                 }
315
316                 error = copyout(sym->vs_name, uap->buf + bytes, namelen + 1);
317                 if (error == 0) {
318                         bytes += namelen + 1;
319                         error = copyout(sym->vs_data, uap->buf + bytes, datalen + 1);
320                         if (error == 0)
321                                 bytes += datalen + 1;
322                         else
323                                 bytes -= namelen + 1;   /* revert if error */
324                 }
325                 if (error) {
326                         earlyterm = 1;
327                         break;
328                 }
329                 ++i;
330         }
331         lockmgr(&vss->vx_lock, LK_RELEASE);
332
333         /*
334          * Save the marker back.  If no error occured and earlyterm is clear
335          * the marker is set to -1 indicating that the variable list has been
336          * exhausted.  If no error occured the number of bytes loaded into
337          * the buffer will be returned, otherwise the syscall code returns -1.
338          */
339         if (error == 0 && earlyterm == 0)
340                 marker = -1;
341         else
342                 marker = i;
343         if (error == 0)
344                 error = copyout(&marker, uap->marker, sizeof(marker));
345         uap->sysmsg_result = bytes;
346 done:
347         rel_mplock();
348         return(error);
349 }
350
351 /*
352  * Lookup a variant symlink.  XXX use a hash table.
353  */
354 static
355 struct varsyment *
356 varsymlookup(struct varsymset *vss, const char *name, int namelen)
357 {
358     struct varsyment *ve;
359
360     KKASSERT(lockstatus(&vss->vx_lock, curthread) != 0);
361     TAILQ_FOREACH(ve, &vss->vx_queue, ve_entry) {
362         varsym_t var = ve->ve_sym;
363         if (var->vs_namelen == namelen && 
364             bcmp(name, var->vs_name, namelen) == 0
365         ) {
366             return(ve);
367         }
368     }
369     return(NULL);
370 }
371  
372 static
373 void
374 vsslock(struct varsymset **vss, struct varsymset *n)
375 {
376         if (*vss) {
377                 lockmgr(&(*vss)->vx_lock, LK_RELEASE);
378         }
379         lockmgr(&n->vx_lock, LK_SHARED);
380         *vss = n;
381 }
382
383 varsym_t
384 varsymfind(int mask, const char *name, int namelen)
385 {
386     struct varsyment *ve = NULL;
387     struct varsymset *vss = NULL;
388     struct thread *td;
389     struct lwp *lp;
390     struct proc *p;
391     varsym_t sym;
392
393     td = curthread;
394     lp = td->td_lwp;
395     p = lp ? lp->lwp_proc : NULL;
396
397     if ((mask & (VARSYM_PROC_MASK|VARSYM_USER_MASK)) && lp != NULL) {
398         if (mask & VARSYM_PROC_MASK) {
399             vsslock(&vss, &p->p_varsymset);
400             ve = varsymlookup(vss, name, namelen);
401         }
402         if (ve == NULL && (mask & VARSYM_USER_MASK)) {
403             vsslock(&vss, &td->td_ucred->cr_uidinfo->ui_varsymset);
404             ve = varsymlookup(vss, name, namelen);
405         }
406     }
407     if (ve == NULL && (mask & VARSYM_SYS_MASK)) {
408         if (lp != NULL && td->td_ucred->cr_prison) {
409             vsslock(&vss, &td->td_ucred->cr_prison->pr_varsymset);
410             ve = varsymlookup(vss, name, namelen);
411         } else {
412             vsslock(&vss, &varsymset_sys);
413             ve = varsymlookup(vss, name, namelen);
414         }
415     }
416     if (ve) {
417         sym = ve->ve_sym;
418         atomic_add_int(&sym->vs_refs, 1);
419     } else {
420         sym = NULL;
421     }
422     lockmgr(&vss->vx_lock, LK_RELEASE);
423     return sym;
424 }
425
426 int
427 varsymmake(int level, const char *name, const char *data)
428 {
429     struct varsymset *vss = NULL;
430     struct varsyment *ve;
431     struct thread *td;
432     struct proc *p;
433     struct lwp *lp;
434     varsym_t sym;
435     int namelen = strlen(name);
436     int datalen;
437     int error;
438
439     td = curthread;
440     lp = td->td_lwp;
441     p = lp ? lp->lwp_proc : NULL;
442
443     switch(level) {
444     case VARSYM_PROC:
445         if (p)
446             vss = &p->p_varsymset;
447         break;
448     case VARSYM_USER:
449         if (lp)
450             vss = &td->td_ucred->cr_uidinfo->ui_varsymset;
451         break;
452     case VARSYM_SYS:
453         vss = &varsymset_sys;
454         break;
455     case VARSYM_PRISON:
456         if (lp && td->td_ucred->cr_prison)
457             vss = &td->td_ucred->cr_prison->pr_varsymset;
458         break;
459     }
460     if (vss == NULL) {
461         return EINVAL;
462     }
463     lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
464     if (data && vss->vx_setsize >= MAXVARSYM_SET) {
465         error = E2BIG;
466     } else if (data) {
467         datalen = strlen(data);
468         ve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
469         sym = kmalloc(sizeof(struct varsym) + namelen + datalen + 2, M_VARSYM, M_WAITOK);
470         ve->ve_sym = sym;
471         sym->vs_refs = 1;
472         sym->vs_namelen = namelen;
473         sym->vs_name = (char *)(sym + 1);
474         sym->vs_data = sym->vs_name + namelen + 1;
475         strcpy(sym->vs_name, name);
476         strcpy(sym->vs_data, data);
477         TAILQ_INSERT_TAIL(&vss->vx_queue, ve, ve_entry);
478         vss->vx_setsize += sizeof(struct varsyment) + sizeof(struct varsym) + namelen + datalen + 8;
479         error = 0;
480     } else {
481         if ((ve = varsymlookup(vss, name, namelen)) != NULL) {
482             TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
483             vss->vx_setsize -= sizeof(struct varsyment) + sizeof(struct varsym) + namelen + strlen(ve->ve_sym->vs_data) + 8;
484             varsymdrop(ve->ve_sym);
485             kfree(ve, M_VARSYM);
486             error = 0;
487         } else {
488             error = ENOENT;
489         }
490     }
491     lockmgr(&vss->vx_lock, LK_RELEASE);
492     return(error);
493 }
494
495 void
496 varsymdrop(varsym_t sym)
497 {
498     KKASSERT(sym->vs_refs > 0);
499     if (atomic_fetchadd_int(&sym->vs_refs, -1) == 1) {
500         kfree(sym, M_VARSYM);
501     }
502 }
503
504 /*
505  * Insert a duplicate of ve in vss. Does not do any locking,
506  * so it is the callers responsibility to make sure nobody
507  * else can mess with the TAILQ in vss at the same time.
508  */
509 static void
510 varsymdup(struct varsymset *vss, struct varsyment *ve)
511 {
512     struct varsyment *nve;
513
514     nve = kmalloc(sizeof(struct varsyment), M_VARSYM, M_WAITOK|M_ZERO);
515     nve->ve_sym = ve->ve_sym;
516     ++nve->ve_sym->vs_refs;     /* can't be reached, no need for atomic add */
517     /*
518      * We're only called through varsymset_init() so vss is not yet reachable,
519      * no need to lock.
520      */
521     TAILQ_INSERT_TAIL(&vss->vx_queue, nve, ve_entry);
522 }
523
524 void
525 varsymset_init(struct varsymset *vss, struct varsymset *copy)
526 {
527     struct varsyment *ve;
528
529     TAILQ_INIT(&vss->vx_queue);
530     lockinit(&vss->vx_lock, "vx", 0, 0);
531     if (copy) {
532         TAILQ_FOREACH(ve, &copy->vx_queue, ve_entry) {
533             varsymdup(vss, ve);
534         }
535         vss->vx_setsize = copy->vx_setsize;
536     }
537 }
538
539 void
540 varsymset_clean(struct varsymset *vss)
541 {
542     struct varsyment *ve;
543
544     lockmgr(&vss->vx_lock, LK_EXCLUSIVE);
545     while ((ve = TAILQ_FIRST(&vss->vx_queue)) != NULL) {
546         TAILQ_REMOVE(&vss->vx_queue, ve, ve_entry);
547         varsymdrop(ve->ve_sym);
548         kfree(ve, M_VARSYM);
549     }
550     vss->vx_setsize = 0;
551     lockmgr(&vss->vx_lock, LK_RELEASE);
552 }
553