|
|
1.1.1.2 root 1: /*
2: * linux/fs/exec.c
3: *
4: * (C) 1991 Linus Torvalds
5: */
6:
1.1.1.3 root 7: /*
8: * #!-checking implemented by tytso.
9: */
10:
11: /*
12: * Demand-loading implemented 01.12.91 - no need to read anything but
13: * the header into memory. The inode of the executable is put into
14: * "current->executable", and page faults do the actual loading. Clean.
15: *
16: * Once more I can proudly say that linux stood up to being changed: it
17: * was less than 2 hours work to get demand-loading completely implemented.
18: */
19:
1.1.1.4 ! root 20: #include <signal.h>
1.1 root 21: #include <errno.h>
1.1.1.2 root 22: #include <string.h>
1.1 root 23: #include <sys/stat.h>
24: #include <a.out.h>
25:
26: #include <linux/fs.h>
27: #include <linux/sched.h>
28: #include <linux/kernel.h>
29: #include <linux/mm.h>
30: #include <asm/segment.h>
31:
32: extern int sys_exit(int exit_code);
33: extern int sys_close(int fd);
34:
35: /*
36: * MAX_ARG_PAGES defines the number of pages allocated for arguments
37: * and envelope for the new program. 32 should suffice, this gives
38: * a maximum env+arg of 128kB !
39: */
40: #define MAX_ARG_PAGES 32
41:
1.1.1.4 ! root 42: int sys_uselib(const char * library)
! 43: {
! 44: struct m_inode * inode;
! 45: unsigned long base;
! 46:
! 47: if (get_limit(0x17) != TASK_SIZE)
! 48: return -EINVAL;
! 49: if (library) {
! 50: if (!(inode=namei(library))) /* get library inode */
! 51: return -ENOENT;
! 52: } else
! 53: inode = NULL;
! 54: /* we should check filetypes (headers etc), but we don't */
! 55: iput(current->library);
! 56: current->library = NULL;
! 57: base = get_base(current->ldt[2]);
! 58: base += LIBRARY_OFFSET;
! 59: free_page_tables(base,LIBRARY_SIZE);
! 60: current->library = inode;
! 61: return 0;
! 62: }
! 63:
1.1 root 64: /*
65: * create_tables() parses the env- and arg-strings in new user
66: * memory and creates the pointer tables from them, and puts their
67: * addresses on the "stack", returning the new stack pointer value.
68: */
69: static unsigned long * create_tables(char * p,int argc,int envc)
70: {
71: unsigned long *argv,*envp;
72: unsigned long * sp;
73:
74: sp = (unsigned long *) (0xfffffffc & (unsigned long) p);
75: sp -= envc+1;
76: envp = sp;
77: sp -= argc+1;
78: argv = sp;
79: put_fs_long((unsigned long)envp,--sp);
80: put_fs_long((unsigned long)argv,--sp);
81: put_fs_long((unsigned long)argc,--sp);
82: while (argc-->0) {
83: put_fs_long((unsigned long) p,argv++);
84: while (get_fs_byte(p++)) /* nothing */ ;
85: }
86: put_fs_long(0,argv);
87: while (envc-->0) {
88: put_fs_long((unsigned long) p,envp++);
89: while (get_fs_byte(p++)) /* nothing */ ;
90: }
91: put_fs_long(0,envp);
92: return sp;
93: }
94:
95: /*
96: * count() counts the number of arguments/envelopes
97: */
98: static int count(char ** argv)
99: {
100: int i=0;
101: char ** tmp;
102:
103: if (tmp = argv)
104: while (get_fs_long((unsigned long *) (tmp++)))
105: i++;
106:
107: return i;
108: }
109:
110: /*
111: * 'copy_string()' copies argument/envelope strings from user
112: * memory to free pages in kernel mem. These are in a format ready
113: * to be put directly into the top of new user memory.
1.1.1.2 root 114: *
115: * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
116: * whether the string and the string array are from user or kernel segments:
117: *
118: * from_kmem argv * argv **
119: * 0 user space user space
120: * 1 kernel space user space
121: * 2 kernel space kernel space
122: *
123: * We do this by playing games with the fs segment register. Since it
124: * it is expensive to load a segment register, we try to avoid calling
125: * set_fs() unless we absolutely have to.
1.1 root 126: */
127: static unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
1.1.1.2 root 128: unsigned long p, int from_kmem)
1.1 root 129: {
1.1.1.2 root 130: char *tmp, *pag;
131: int len, offset = 0;
132: unsigned long old_fs, new_fs;
133:
134: if (!p)
135: return 0; /* bullet-proofing */
136: new_fs = get_ds();
137: old_fs = get_fs();
138: if (from_kmem==2)
139: set_fs(new_fs);
1.1 root 140: while (argc-- > 0) {
1.1.1.2 root 141: if (from_kmem == 1)
142: set_fs(new_fs);
143: if (!(tmp = (char *)get_fs_long(((unsigned long *)argv)+argc)))
1.1 root 144: panic("argc is wrong");
1.1.1.2 root 145: if (from_kmem == 1)
146: set_fs(old_fs);
1.1 root 147: len=0; /* remember zero-padding */
148: do {
149: len++;
150: } while (get_fs_byte(tmp++));
1.1.1.2 root 151: if (p-len < 0) { /* this shouldn't happen - 128kB */
152: set_fs(old_fs);
1.1 root 153: return 0;
154: }
1.1.1.2 root 155: while (len) {
156: --p; --tmp; --len;
157: if (--offset < 0) {
158: offset = p % PAGE_SIZE;
159: if (from_kmem==2)
160: set_fs(old_fs);
161: if (!(pag = (char *) page[p/PAGE_SIZE]) &&
162: !(pag = (char *) page[p/PAGE_SIZE] =
163: (unsigned long *) get_free_page()))
164: return 0;
165: if (from_kmem==2)
166: set_fs(new_fs);
167:
168: }
169: *(pag + offset) = get_fs_byte(tmp);
170: }
1.1 root 171: }
1.1.1.2 root 172: if (from_kmem==2)
173: set_fs(old_fs);
1.1 root 174: return p;
175: }
176:
177: static unsigned long change_ldt(unsigned long text_size,unsigned long * page)
178: {
179: unsigned long code_limit,data_limit,code_base,data_base;
180: int i;
181:
1.1.1.4 ! root 182: code_limit = TASK_SIZE;
! 183: data_limit = TASK_SIZE;
1.1 root 184: code_base = get_base(current->ldt[1]);
185: data_base = code_base;
186: set_base(current->ldt[1],code_base);
187: set_limit(current->ldt[1],code_limit);
188: set_base(current->ldt[2],data_base);
189: set_limit(current->ldt[2],data_limit);
190: /* make sure fs points to the NEW data segment */
191: __asm__("pushl $0x17\n\tpop %%fs"::);
1.1.1.4 ! root 192: data_base += data_limit - LIBRARY_SIZE;
1.1 root 193: for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {
194: data_base -= PAGE_SIZE;
195: if (page[i])
1.1.1.4 ! root 196: put_dirty_page(page[i],data_base);
1.1 root 197: }
198: return data_limit;
199: }
200:
201: /*
202: * 'do_execve()' executes a new program.
1.1.1.4 ! root 203: *
! 204: * NOTE! We leave 4MB free at the top of the data-area for a loadable
! 205: * library.
1.1 root 206: */
207: int do_execve(unsigned long * eip,long tmp,char * filename,
208: char ** argv, char ** envp)
209: {
210: struct m_inode * inode;
211: struct buffer_head * bh;
212: struct exec ex;
213: unsigned long page[MAX_ARG_PAGES];
214: int i,argc,envc;
1.1.1.2 root 215: int e_uid, e_gid;
216: int retval;
217: int sh_bang = 0;
218: unsigned long p=PAGE_SIZE*MAX_ARG_PAGES-4;
1.1 root 219:
220: if ((0xffff & eip[1]) != 0x000f)
221: panic("execve called from supervisor mode");
222: for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
223: page[i]=0;
224: if (!(inode=namei(filename))) /* get executables inode */
225: return -ENOENT;
1.1.1.2 root 226: argc = count(argv);
227: envc = count(envp);
228:
229: restart_interp:
1.1 root 230: if (!S_ISREG(inode->i_mode)) { /* must be regular file */
1.1.1.2 root 231: retval = -EACCES;
232: goto exec_error2;
1.1 root 233: }
234: i = inode->i_mode;
1.1.1.2 root 235: e_uid = (i & S_ISUID) ? inode->i_uid : current->euid;
236: e_gid = (i & S_ISGID) ? inode->i_gid : current->egid;
237: if (current->euid == inode->i_uid)
238: i >>= 6;
1.1.1.4 ! root 239: else if (in_group_p(inode->i_gid))
1.1.1.2 root 240: i >>= 3;
241: if (!(i & 1) &&
242: !((inode->i_mode & 0111) && suser())) {
243: retval = -ENOEXEC;
244: goto exec_error2;
1.1 root 245: }
246: if (!(bh = bread(inode->i_dev,inode->i_zone[0]))) {
1.1.1.2 root 247: retval = -EACCES;
248: goto exec_error2;
1.1 root 249: }
250: ex = *((struct exec *) bh->b_data); /* read exec-header */
1.1.1.2 root 251: if ((bh->b_data[0] == '#') && (bh->b_data[1] == '!') && (!sh_bang)) {
252: /*
253: * This section does the #! interpretation.
254: * Sorta complicated, but hopefully it will work. -TYT
255: */
256:
1.1.1.4 ! root 257: char buf[128], *cp, *interp, *i_name, *i_arg;
1.1.1.2 root 258: unsigned long old_fs;
259:
1.1.1.4 ! root 260: strncpy(buf, bh->b_data+2, 127);
1.1.1.2 root 261: brelse(bh);
262: iput(inode);
1.1.1.4 ! root 263: buf[127] = '\0';
1.1.1.2 root 264: if (cp = strchr(buf, '\n')) {
265: *cp = '\0';
266: for (cp = buf; (*cp == ' ') || (*cp == '\t'); cp++);
267: }
268: if (!cp || *cp == '\0') {
269: retval = -ENOEXEC; /* No interpreter name found */
270: goto exec_error1;
271: }
272: interp = i_name = cp;
273: i_arg = 0;
274: for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
275: if (*cp == '/')
276: i_name = cp+1;
277: }
278: if (*cp) {
279: *cp++ = '\0';
280: i_arg = cp;
281: }
282: /*
283: * OK, we've parsed out the interpreter name and
284: * (optional) argument.
285: */
286: if (sh_bang++ == 0) {
287: p = copy_strings(envc, envp, page, p, 0);
288: p = copy_strings(--argc, argv+1, page, p, 0);
289: }
290: /*
291: * Splice in (1) the interpreter's name for argv[0]
292: * (2) (optional) argument to interpreter
293: * (3) filename of shell script
294: *
295: * This is done in reverse order, because of how the
296: * user environment and arguments are stored.
297: */
298: p = copy_strings(1, &filename, page, p, 1);
299: argc++;
300: if (i_arg) {
301: p = copy_strings(1, &i_arg, page, p, 2);
302: argc++;
303: }
304: p = copy_strings(1, &i_name, page, p, 2);
305: argc++;
306: if (!p) {
307: retval = -ENOMEM;
308: goto exec_error1;
309: }
310: /*
311: * OK, now restart the process with the interpreter's inode.
312: */
313: old_fs = get_fs();
314: set_fs(get_ds());
315: if (!(inode=namei(interp))) { /* get executables inode */
316: set_fs(old_fs);
317: retval = -ENOENT;
318: goto exec_error1;
319: }
320: set_fs(old_fs);
321: goto restart_interp;
322: }
1.1 root 323: brelse(bh);
324: if (N_MAGIC(ex) != ZMAGIC || ex.a_trsize || ex.a_drsize ||
325: ex.a_text+ex.a_data+ex.a_bss>0x3000000 ||
326: inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) {
1.1.1.2 root 327: retval = -ENOEXEC;
328: goto exec_error2;
1.1 root 329: }
1.1.1.2 root 330: if (N_TXTOFF(ex) != BLOCK_SIZE) {
331: printk("%s: N_TXTOFF != BLOCK_SIZE. See a.out.h.", filename);
332: retval = -ENOEXEC;
333: goto exec_error2;
334: }
335: if (!sh_bang) {
336: p = copy_strings(envc,envp,page,p,0);
337: p = copy_strings(argc,argv,page,p,0);
338: if (!p) {
339: retval = -ENOMEM;
340: goto exec_error2;
341: }
1.1 root 342: }
343: /* OK, This is the point of no return */
1.1.1.4 ! root 344: /* note that current->library stays unchanged by an exec */
1.1.1.3 root 345: if (current->executable)
346: iput(current->executable);
347: current->executable = inode;
1.1.1.4 ! root 348: current->signal = 0;
! 349: for (i=0 ; i<32 ; i++) {
! 350: current->sigaction[i].sa_mask = 0;
! 351: current->sigaction[i].sa_flags = 0;
! 352: if (current->sigaction[i].sa_handler != SIG_IGN)
! 353: current->sigaction[i].sa_handler = NULL;
! 354: }
1.1 root 355: for (i=0 ; i<NR_OPEN ; i++)
356: if ((current->close_on_exec>>i)&1)
357: sys_close(i);
358: current->close_on_exec = 0;
359: free_page_tables(get_base(current->ldt[1]),get_limit(0x0f));
360: free_page_tables(get_base(current->ldt[2]),get_limit(0x17));
361: if (last_task_used_math == current)
362: last_task_used_math = NULL;
363: current->used_math = 0;
1.1.1.4 ! root 364: p += change_ldt(ex.a_text,page);
! 365: p -= LIBRARY_SIZE + MAX_ARG_PAGES*PAGE_SIZE;
1.1 root 366: p = (unsigned long) create_tables((char *)p,argc,envc);
367: current->brk = ex.a_bss +
368: (current->end_data = ex.a_data +
369: (current->end_code = ex.a_text));
370: current->start_stack = p & 0xfffff000;
1.1.1.4 ! root 371: current->suid = current->euid = e_uid;
! 372: current->sgid = current->egid = e_gid;
1.1 root 373: eip[0] = ex.a_entry; /* eip, magic happens :-) */
374: eip[3] = p; /* stack pointer */
375: return 0;
1.1.1.2 root 376: exec_error2:
377: iput(inode);
378: exec_error1:
379: for (i=0 ; i<MAX_ARG_PAGES ; i++)
380: free_page(page[i]);
381: return(retval);
1.1 root 382: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.