exec_script.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* $OpenBSD: exec_script.c,v 1.35 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: exec_script.c,v 1.13 1996/02/04 02:15:06 christos Exp $ */
  3. /*
  4. * Copyright (c) 1993, 1994 Christopher G. Demetriou
  5. * All rights reserved.
  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. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed by Christopher G. Demetriou.
  18. * 4. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/proc.h>
  35. #include <sys/malloc.h>
  36. #include <sys/pool.h>
  37. #include <sys/vnode.h>
  38. #include <sys/lock.h>
  39. #include <sys/namei.h>
  40. #include <sys/file.h>
  41. #include <sys/filedesc.h>
  42. #include <sys/exec.h>
  43. #include <sys/exec_script.h>
  44. #include "systrace.h"
  45. #if NSYSTRACE > 0
  46. #include <dev/systrace.h>
  47. #endif
  48. /*
  49. * exec_script_makecmds(): Check if it's an executable shell script.
  50. *
  51. * Given a proc pointer and an exec package pointer, see if the referent
  52. * of the epp is in shell script. If it is, then set things up so that
  53. * the script can be run. This involves preparing the address space
  54. * and arguments for the shell which will run the script.
  55. *
  56. * This function is ultimately responsible for creating a set of vmcmds
  57. * which can be used to build the process's vm space and inserting them
  58. * into the exec package.
  59. */
  60. int
  61. exec_script_makecmds(struct proc *p, struct exec_package *epp)
  62. {
  63. int error, hdrlinelen, shellnamelen, shellarglen;
  64. char *hdrstr = epp->ep_hdr;
  65. char *cp, *shellname, *shellarg, *oldpnbuf;
  66. char **shellargp = NULL, **tmpsap;
  67. struct vnode *scriptvp;
  68. uid_t script_uid = -1;
  69. gid_t script_gid = -1;
  70. u_short script_sbits;
  71. /*
  72. * remember the old vp and pnbuf for later, so we can restore
  73. * them if check_exec() fails.
  74. */
  75. scriptvp = epp->ep_vp;
  76. oldpnbuf = epp->ep_ndp->ni_cnd.cn_pnbuf;
  77. /*
  78. * if the magic isn't that of a shell script, or we've already
  79. * done shell script processing for this exec, punt on it.
  80. */
  81. if ((epp->ep_flags & EXEC_INDIR) != 0 ||
  82. epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
  83. strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
  84. return ENOEXEC;
  85. /*
  86. * check that the shell spec is terminated by a newline,
  87. * and that it isn't too large. Don't modify the
  88. * buffer unless we're ready to commit to handling it.
  89. * (The latter requirement means that we have to check
  90. * for both spaces and tabs later on.)
  91. */
  92. hdrlinelen = min(epp->ep_hdrvalid, MAXINTERP);
  93. for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
  94. cp++) {
  95. if (*cp == '\n') {
  96. *cp = '\0';
  97. break;
  98. }
  99. }
  100. if (cp >= hdrstr + hdrlinelen)
  101. return ENOEXEC;
  102. shellname = NULL;
  103. shellarg = NULL;
  104. shellarglen = 0;
  105. /* strip spaces before the shell name */
  106. for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
  107. cp++)
  108. ;
  109. /* collect the shell name; remember its length for later */
  110. shellname = cp;
  111. shellnamelen = 0;
  112. if (*cp == '\0')
  113. goto check_shell;
  114. for ( /* cp = cp */ ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
  115. shellnamelen++;
  116. if (*cp == '\0')
  117. goto check_shell;
  118. *cp++ = '\0';
  119. /* skip spaces before any argument */
  120. for ( /* cp = cp */ ; *cp == ' ' || *cp == '\t'; cp++)
  121. ;
  122. if (*cp == '\0')
  123. goto check_shell;
  124. /*
  125. * collect the shell argument. everything after the shell name
  126. * is passed as ONE argument; that's the correct (historical)
  127. * behaviour.
  128. */
  129. shellarg = cp;
  130. for ( /* cp = cp */ ; *cp != '\0'; cp++)
  131. shellarglen++;
  132. *cp++ = '\0';
  133. check_shell:
  134. /*
  135. * MNT_NOSUID and STRC are already taken care of by check_exec,
  136. * so we don't need to worry about them now or later.
  137. */
  138. script_sbits = epp->ep_vap->va_mode & (VSUID | VSGID);
  139. if (script_sbits != 0) {
  140. script_uid = epp->ep_vap->va_uid;
  141. script_gid = epp->ep_vap->va_gid;
  142. }
  143. /*
  144. * if the script isn't readable, or it's set-id, then we've
  145. * gotta supply a "/dev/fd/..." for the shell to read.
  146. * Note that stupid shells (csh) do the wrong thing, and
  147. * close all open fd's when they start. That kills this
  148. * method of implementing "safe" set-id and x-only scripts.
  149. */
  150. vn_lock(scriptvp, LK_EXCLUSIVE|LK_RETRY, p);
  151. error = VOP_ACCESS(scriptvp, VREAD, p->p_ucred, p);
  152. VOP_UNLOCK(scriptvp, 0, p);
  153. if (error == EACCES || script_sbits) {
  154. struct file *fp;
  155. #ifdef DIAGNOSTIC
  156. if (epp->ep_flags & EXEC_HASFD)
  157. panic("exec_script_makecmds: epp already has a fd");
  158. #endif
  159. fdplock(p->p_fd);
  160. error = falloc(p, &fp, &epp->ep_fd);
  161. fdpunlock(p->p_fd);
  162. if (error)
  163. goto fail;
  164. epp->ep_flags |= EXEC_HASFD;
  165. fp->f_type = DTYPE_VNODE;
  166. fp->f_ops = &vnops;
  167. fp->f_data = (caddr_t) scriptvp;
  168. fp->f_flag = FREAD;
  169. FILE_SET_MATURE(fp, p);
  170. }
  171. /* set up the parameters for the recursive check_exec() call */
  172. epp->ep_ndp->ni_dirfd = AT_FDCWD;
  173. epp->ep_ndp->ni_dirp = shellname;
  174. epp->ep_ndp->ni_segflg = UIO_SYSSPACE;
  175. epp->ep_flags |= EXEC_INDIR;
  176. /* and set up the fake args list, for later */
  177. shellargp = mallocarray(4, sizeof(char *), M_EXEC, M_WAITOK);
  178. tmpsap = shellargp;
  179. *tmpsap = malloc(shellnamelen + 1, M_EXEC, M_WAITOK);
  180. strlcpy(*tmpsap++, shellname, shellnamelen + 1);
  181. if (shellarg != NULL) {
  182. *tmpsap = malloc(shellarglen + 1, M_EXEC, M_WAITOK);
  183. strlcpy(*tmpsap++, shellarg, shellarglen + 1);
  184. }
  185. *tmpsap = malloc(MAXPATHLEN, M_EXEC, M_WAITOK);
  186. if ((epp->ep_flags & EXEC_HASFD) == 0) {
  187. #if NSYSTRACE > 0
  188. if (ISSET(p->p_flag, P_SYSTRACE)) {
  189. error = systrace_scriptname(p, *tmpsap);
  190. if (error == 0)
  191. tmpsap++;
  192. else
  193. /*
  194. * Since systrace_scriptname() provides a
  195. * convenience, not a security issue, we are
  196. * safe to do this.
  197. */
  198. error = copystr(epp->ep_name, *tmpsap++,
  199. MAXPATHLEN, NULL);
  200. } else
  201. #endif
  202. error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
  203. NULL);
  204. if (error != 0)
  205. goto fail;
  206. } else
  207. snprintf(*tmpsap++, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
  208. *tmpsap = NULL;
  209. /*
  210. * mark the header we have as invalid; check_exec will read
  211. * the header from the new executable
  212. */
  213. epp->ep_hdrvalid = 0;
  214. if ((error = check_exec(p, epp)) == 0) {
  215. /* note that we've clobbered the header */
  216. epp->ep_flags |= EXEC_DESTR;
  217. /*
  218. * It succeeded. Unlock the script and
  219. * close it if we aren't using it any more.
  220. * Also, set things up so that the fake args
  221. * list will be used.
  222. */
  223. if ((epp->ep_flags & EXEC_HASFD) == 0)
  224. vn_close(scriptvp, FREAD, p->p_ucred, p);
  225. /* free the old pathname buffer */
  226. pool_put(&namei_pool, oldpnbuf);
  227. epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
  228. epp->ep_fa = shellargp;
  229. /*
  230. * set things up so that set-id scripts will be
  231. * handled appropriately
  232. */
  233. epp->ep_vap->va_mode |= script_sbits;
  234. if (script_sbits & VSUID)
  235. epp->ep_vap->va_uid = script_uid;
  236. if (script_sbits & VSGID)
  237. epp->ep_vap->va_gid = script_gid;
  238. return (0);
  239. }
  240. /* XXX oldpnbuf not set for "goto fail" path */
  241. epp->ep_ndp->ni_cnd.cn_pnbuf = oldpnbuf;
  242. fail:
  243. /* note that we've clobbered the header */
  244. epp->ep_flags |= EXEC_DESTR;
  245. /* kill the opened file descriptor, else close the file */
  246. if (epp->ep_flags & EXEC_HASFD) {
  247. epp->ep_flags &= ~EXEC_HASFD;
  248. fdplock(p->p_fd);
  249. (void) fdrelease(p, epp->ep_fd);
  250. fdpunlock(p->p_fd);
  251. } else
  252. vn_close(scriptvp, FREAD, p->p_ucred, p);
  253. pool_put(&namei_pool, epp->ep_ndp->ni_cnd.cn_pnbuf);
  254. /* free the fake arg list, because we're not returning it */
  255. if ((tmpsap = shellargp) != NULL) {
  256. while (*tmpsap != NULL) {
  257. free(*tmpsap, M_EXEC, 0);
  258. tmpsap++;
  259. }
  260. free(shellargp, M_EXEC, 0);
  261. }
  262. /*
  263. * free any vmspace-creation commands,
  264. * and release their references
  265. */
  266. kill_vmcmds(&epp->ep_vmcmds);
  267. return error;
  268. }