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