This source file includes following definitions.
- exec_script_makecmds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/pool.h>
39 #include <sys/vnode.h>
40 #include <sys/namei.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/exec.h>
44 #include <sys/resourcevar.h>
45 #include <uvm/uvm_extern.h>
46
47 #include <sys/exec_script.h>
48
49 #include "systrace.h"
50
51 #if NSYSTRACE > 0
52 #include <dev/systrace.h>
53 #endif
54
55 #if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
56 #define FDSCRIPTS
57 #endif
58
59
60
61
62
63
64
65
66
67
68
69
70
71 int
72 exec_script_makecmds(struct proc *p, struct exec_package *epp)
73 {
74 int error, hdrlinelen, shellnamelen, shellarglen;
75 char *hdrstr = epp->ep_hdr;
76 char *cp, *shellname, *shellarg, *oldpnbuf;
77 char **shellargp = NULL, **tmpsap;
78 struct vnode *scriptvp;
79 #ifdef SETUIDSCRIPTS
80 uid_t script_uid = -1;
81 gid_t script_gid = -1;
82 u_short script_sbits;
83 #endif
84
85
86
87
88
89 scriptvp = epp->ep_vp;
90 oldpnbuf = epp->ep_ndp->ni_cnd.cn_pnbuf;
91
92
93
94
95
96 if ((epp->ep_flags & EXEC_INDIR) != 0 ||
97 epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
98 strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
99 return ENOEXEC;
100
101
102
103
104
105
106
107
108 hdrlinelen = min(epp->ep_hdrvalid, MAXINTERP);
109 for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
110 cp++) {
111 if (*cp == '\n') {
112 *cp = '\0';
113 break;
114 }
115 }
116 if (cp >= hdrstr + hdrlinelen)
117 return ENOEXEC;
118
119 shellname = NULL;
120 shellarg = NULL;
121 shellarglen = 0;
122
123
124 for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
125 cp++)
126 ;
127
128
129 shellname = cp;
130 shellnamelen = 0;
131 if (*cp == '\0')
132 goto check_shell;
133 for ( ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
134 shellnamelen++;
135 if (*cp == '\0')
136 goto check_shell;
137 *cp++ = '\0';
138
139
140 for ( ; *cp == ' ' || *cp == '\t'; cp++)
141 ;
142 if (*cp == '\0')
143 goto check_shell;
144
145
146
147
148
149
150 shellarg = cp;
151 for ( ; *cp != '\0'; cp++)
152 shellarglen++;
153 *cp++ = '\0';
154
155 check_shell:
156 #ifdef SETUIDSCRIPTS
157
158
159
160
161 script_sbits = epp->ep_vap->va_mode & (VSUID | VSGID);
162 if (script_sbits != 0) {
163 script_uid = epp->ep_vap->va_uid;
164 script_gid = epp->ep_vap->va_gid;
165 }
166 #endif
167 #ifdef FDSCRIPTS
168
169
170
171
172
173
174
175 vn_lock(scriptvp, LK_EXCLUSIVE|LK_RETRY, p);
176 error = VOP_ACCESS(scriptvp, VREAD, p->p_ucred, p);
177 VOP_UNLOCK(scriptvp, 0, p);
178 if (error == EACCES
179 #ifdef SETUIDSCRIPTS
180 || script_sbits
181 #endif
182 ) {
183 struct file *fp;
184
185 #ifdef DIAGNOSTIC
186 if (epp->ep_flags & EXEC_HASFD)
187 panic("exec_script_makecmds: epp already has a fd");
188 #endif
189
190 if ((error = falloc(p, &fp, &epp->ep_fd)))
191 goto fail;
192
193 epp->ep_flags |= EXEC_HASFD;
194 fp->f_type = DTYPE_VNODE;
195 fp->f_ops = &vnops;
196 fp->f_data = (caddr_t) scriptvp;
197 fp->f_flag = FREAD;
198 FILE_SET_MATURE(fp);
199 }
200 #endif
201
202
203 epp->ep_ndp->ni_dirp = shellname;
204 epp->ep_ndp->ni_segflg = UIO_SYSSPACE;
205 epp->ep_flags |= EXEC_INDIR;
206
207
208 MALLOC(shellargp, char **, 4 * sizeof(char *), M_EXEC, M_WAITOK);
209 tmpsap = shellargp;
210 *tmpsap = malloc(shellnamelen + 1, M_EXEC, M_WAITOK);
211 strlcpy(*tmpsap++, shellname, shellnamelen + 1);
212 if (shellarg != NULL) {
213 *tmpsap = malloc(shellarglen + 1, M_EXEC, M_WAITOK);
214 strlcpy(*tmpsap++, shellarg, shellarglen + 1);
215 }
216 *tmpsap = malloc(MAXPATHLEN, M_EXEC, M_WAITOK);
217 #ifdef FDSCRIPTS
218 if ((epp->ep_flags & EXEC_HASFD) == 0) {
219 #endif
220
221 #if NSYSTRACE > 0
222 if (ISSET(p->p_flag, P_SYSTRACE)) {
223 error = systrace_scriptname(p, *tmpsap);
224 if (error == 0)
225 tmpsap++;
226 else
227
228
229
230
231
232 error = copystr(epp->ep_name, *tmpsap++,
233 MAXPATHLEN, NULL);
234 } else
235 error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
236 NULL);
237 #else
238 error = copyinstr(epp->ep_name, *tmpsap++, MAXPATHLEN,
239 (size_t *)0);
240 #endif
241 #ifdef DIAGNOSTIC
242 if (error != 0)
243 panic("exec_script: copyinstr couldn't fail");
244 #endif
245 #ifdef FDSCRIPTS
246 } else
247 snprintf(*tmpsap++, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
248 #endif
249 *tmpsap = NULL;
250
251
252
253
254
255 epp->ep_hdrvalid = 0;
256
257 if ((error = check_exec(p, epp)) == 0) {
258
259 epp->ep_flags |= EXEC_DESTR;
260
261
262
263
264
265
266
267 if ((epp->ep_flags & EXEC_HASFD) == 0)
268 vn_close(scriptvp, FREAD, p->p_ucred, p);
269
270
271 pool_put(&namei_pool, oldpnbuf);
272
273 epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
274 epp->ep_fa = shellargp;
275 #ifdef SETUIDSCRIPTS
276
277
278
279
280 epp->ep_vap->va_mode |= script_sbits;
281 if (script_sbits & VSUID)
282 epp->ep_vap->va_uid = script_uid;
283 if (script_sbits & VSGID)
284 epp->ep_vap->va_gid = script_gid;
285 #endif
286 return (0);
287 }
288
289
290 epp->ep_ndp->ni_cnd.cn_pnbuf = oldpnbuf;
291 #ifdef FDSCRIPTS
292 fail:
293 #endif
294
295 epp->ep_flags |= EXEC_DESTR;
296
297
298 if (epp->ep_flags & EXEC_HASFD) {
299 epp->ep_flags &= ~EXEC_HASFD;
300 (void) fdrelease(p, epp->ep_fd);
301 } else
302 vn_close(scriptvp, FREAD, p->p_ucred, p);
303
304 pool_put(&namei_pool, epp->ep_ndp->ni_cnd.cn_pnbuf);
305
306
307 if ((tmpsap = shellargp) != NULL) {
308 while (*tmpsap != NULL) {
309 free(*tmpsap, M_EXEC);
310 tmpsap++;
311 }
312 FREE(shellargp, M_EXEC);
313 }
314
315
316
317
318
319 kill_vmcmds(&epp->ep_vmcmds);
320
321 return error;
322 }