1 /* This file handles the EXEC system call. It performs the work as follows:
2 * - see if the permissions allow the file to be executed
3 * - read the header and extract the sizes
4 * - fetch the initial args and environment from the user space
5 * - allocate the memory for the new process
6 * - copy the initial stack from PM to the process
7 * - read in the text and data segments and copy to the process
8 * - take care of setuid and setgid bits
9 * - fix up 'mproc' table
10 * - tell kernel about EXEC
11 * - save offset to initial argc (for ps)
13 * The entry points into this file are:
14 * pm_exec: perform the EXEC system call
17 * Aug 2006 (Balazs Gerofi)
22 #include <minix/callnr.h>
23 #include <minix/endpoint.h>
24 #include <minix/com.h>
25 #include <minix/u64.h>
35 #include <minix/vfsif.h>
37 FORWARD
_PROTOTYPE( int exec_newmem
, (int proc_e
, vir_bytes text_bytes
,
38 vir_bytes data_bytes
, vir_bytes bss_bytes
, vir_bytes tot_bytes
,
39 vir_bytes frame_len
, int sep_id
,
40 Dev_t st_dev
, ino_t st_ino
, time_t st_ctime
, char *progname
,
41 int new_uid
, int new_gid
,
42 vir_bytes
*stack_topp
, int *load_textp
, int *allow_setuidp
) );
43 FORWARD
_PROTOTYPE( int read_header
, (struct vnode
*vp
, int *sep_id
,
44 vir_bytes
*text_bytes
, vir_bytes
*data_bytes
,
45 vir_bytes
*bss_bytes
, phys_bytes
*tot_bytes
, vir_bytes
*pc
,
47 FORWARD
_PROTOTYPE( int patch_stack
, (struct vnode
*vp
,
48 char stack
[ARG_MAX
], vir_bytes
*stk_bytes
) );
49 FORWARD
_PROTOTYPE( int insert_arg
, (char stack
[ARG_MAX
],
50 vir_bytes
*stk_bytes
, char *arg
, int replace
) );
51 FORWARD
_PROTOTYPE( void patch_ptr
, (char stack
[ARG_MAX
],
53 FORWARD
_PROTOTYPE( int read_seg
, (struct vnode
*vp
, off_t off
,
54 int proc_e
, int seg
, phys_bytes seg_bytes
) );
55 FORWARD
_PROTOTYPE( void clo_exec
, (struct fproc
*rfp
) );
57 #define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
58 #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
60 /*===========================================================================*
62 *===========================================================================*/
63 PUBLIC
int pm_exec(proc_e
, path
, path_len
, frame
, frame_len
)
70 /* Perform the execve(name, argv, envp) call. The user library builds a
71 * complete stack image, including pointers, args, environ, etc. The stack
72 * is copied to a buffer inside FS, and then to the new core image.
74 int r
, sep_id
, round
, proc_s
, hdrlen
, load_text
, allow_setuid
;
75 vir_bytes text_bytes
, data_bytes
, bss_bytes
, pc
;
76 phys_bytes tot_bytes
; /* total space for program, including gap */
77 vir_bytes stack_top
, vsp
;
86 char progname
[PROC_NAME_LEN
];
87 static char mbuf
[ARG_MAX
]; /* buffer for stack and zeroes */
89 /* Request and response structures */
90 struct lookup_req lookup_req
;
91 struct node_details Xres
;
93 okendpt(proc_e
, &proc_s
);
94 rfp
= fp
= &fproc
[proc_s
];
97 super_user
= (fp
->fp_effuid
== SU_UID
? TRUE
: FALSE
); /* su? */
99 /* Get the exec file name. */
100 r
= fetch_name(path
, path_len
, 0);
103 printf("pm_exec: fetch_name failed\n");
104 printf("return at %s, %d\n", __FILE__
, __LINE__
);
105 return(r
); /* file name not in user data segment */
108 /* Fetch the stack from the user before destroying the old core image. */
109 if (frame_len
> ARG_MAX
)
111 printf("pm_exec: bad frame_len\n");
112 printf("return at %s, %d\n", __FILE__
, __LINE__
);
113 return(ENOMEM
); /* stack too big */
115 r
= sys_datacopy(proc_e
, (vir_bytes
) frame
,
116 SELF
, (vir_bytes
) mbuf
, (phys_bytes
)frame_len
);
117 /* can't fetch stack (e.g. bad virtual addr) */
120 printf("pm_exec: sys_datacopy failed\n");
121 printf("return at %s, %d\n", __FILE__
, __LINE__
);
125 /* The default is the keep the original user and group IDs */
126 new_uid
= rfp
->fp_effuid
;
127 new_gid
= rfp
->fp_effgid
;
129 for (round
= 0; round
< 2; round
++)
130 /* round = 0 (first attempt), or 1 (interpreted script) */
132 /* Save the name of the program */
133 (cp
= strrchr(user_fullpath
, '/')) ? cp
++ : (cp
= user_fullpath
);
135 strncpy(progname
, cp
, PROC_NAME_LEN
-1);
136 progname
[PROC_NAME_LEN
-1] = '\0';
138 /* Fill in lookup request fields */
139 lookup_req
.path
= user_fullpath
;
140 lookup_req
.lastc
= NULL
;
141 lookup_req
.flags
= EAT_PATH
;
144 if ((r
= lookup_vp(&lookup_req
, &vp
)) != OK
)
150 if ((vp
->v_mode
& I_TYPE
) != I_REGULAR
) {
151 printf("put_vnode at %s, %d\n", __FILE__
, __LINE__
);
153 printf("return at %s, %d\n", __FILE__
, __LINE__
);
158 if ((r
= forbidden(vp
, X_BIT
)) != OK
)
160 printf("put_vnode at %s, %d\n", __FILE__
, __LINE__
);
162 printf("return at %s, %d\n", __FILE__
, __LINE__
);
167 r
= req_stat(vp
->v_fs_e
, vp
->v_inode_nr
, FS_PROC_NR
, (char *)&sb
, 0);
173 v_ctime
= sb
.st_ctime
;
177 /* Deal with setuid/setgid executables */
178 if (vp
->v_mode
& I_SET_UID_BIT
)
180 if (vp
->v_mode
& I_SET_GID_BIT
)
184 /* Read the file header and extract the segment sizes. */
185 r
= read_header(vp
, &sep_id
, &text_bytes
, &data_bytes
, &bss_bytes
,
186 &tot_bytes
, &pc
, &hdrlen
);
187 if (r
!= ESCRIPT
|| round
!= 0)
190 /* Get fresh copy of the file name. */
191 r
= fetch_name(path
, path_len
, 0);
194 printf("pm_exec: 2nd fetch_name failed\n");
195 printf("put_vnode at %s, %d\n", __FILE__
, __LINE__
);
197 printf("return at %s, %d\n", __FILE__
, __LINE__
);
198 return(r
); /* strange */
200 r
= patch_stack(vp
, mbuf
, &frame_len
);
204 printf("pm_exec: patch stack\n");
205 printf("return at %s, %d\n", __FILE__
, __LINE__
);
212 printf("pm_exec: returning ENOEXEC, r = %d\n", r
);
213 printf("pm_exec: progname = '%s'\n", progname
);
218 r
= exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
219 frame_len
, sep_id
, vp
->v_dev
, vp
->v_inode_nr
, v_ctime
,
220 progname
, new_uid
, new_gid
, &stack_top
, &load_text
, &allow_setuid
);
223 printf("pm_exec: exec_newmap failed: %d\n", r
);
224 printf("put_vnode at %s, %d\n", __FILE__
, __LINE__
);
226 printf("return at %s, %d\n", __FILE__
, __LINE__
);
230 /* Patch up stack and copy it from FS to new core image. */
233 patch_ptr(mbuf
, vsp
);
234 r
= sys_datacopy(SELF
, (vir_bytes
) mbuf
,
235 proc_e
, (vir_bytes
) vsp
, (phys_bytes
)frame_len
);
236 if (r
!= OK
) panic(__FILE__
,"pm_exec stack copy err on", proc_e
);
240 /* Read in text and data segments. */
242 r
= read_seg(vp
, off
, proc_e
, T
, text_bytes
);
246 r
= read_seg(vp
, off
, proc_e
, D
, data_bytes
);
252 printf("return at %s, %d\n", __FILE__
, __LINE__
);
260 rfp
->fp_effuid
= new_uid
;
261 rfp
->fp_effgid
= new_gid
;
264 /* This child has now exec()ced. */
267 /* Check if this is a driver that can now be useful. */
268 dmap_endpt_up(rfp
->fp_endpoint
);
270 /*printf("VFSpm_exec: %s OK\n", user_fullpath);*/
274 /*===========================================================================*
276 *===========================================================================*/
277 PRIVATE
int exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
278 frame_len
, sep_id
, st_dev
, st_ino
, st_ctime
, progname
,
279 new_uid
, new_gid
, stack_topp
, load_textp
, allow_setuidp
)
281 vir_bytes text_bytes
;
282 vir_bytes data_bytes
;
293 vir_bytes
*stack_topp
;
298 struct exec_newmem e
;
301 e
.text_bytes
= text_bytes
;
302 e
.data_bytes
= data_bytes
;
303 e
.bss_bytes
= bss_bytes
;
304 e
.tot_bytes
= tot_bytes
;
305 e
.args_bytes
= frame_len
;
309 e
.st_ctime
= st_ctime
;
312 strncpy(e
.progname
, progname
, sizeof(e
.progname
)-1);
313 e
.progname
[sizeof(e
.progname
)-1]= '\0';
315 m
.m_type
= EXEC_NEWMEM
;
316 m
.EXC_NM_PROC
= proc_e
;
317 m
.EXC_NM_PTR
= (char *)&e
;
318 r
= sendrec(PM_PROC_NR
, &m
);
322 printf("exec_newmem: r = %d, m_type = %d\n", r
, m
.m_type
);
324 *stack_topp
= m
.m1_i1
;
325 *load_textp
= !!(m
.m1_i2
& EXC_NM_RF_LOAD_TEXT
);
326 *allow_setuidp
= !!(m
.m1_i2
& EXC_NM_RF_ALLOW_SETUID
);
328 printf("exec_newmem: stack_top = 0x%x\n", *stack_topp
);
329 printf("exec_newmem: load_text = %d\n", *load_textp
);
335 /*===========================================================================*
337 *===========================================================================*/
338 PRIVATE
int read_header(vp
, sep_id
, text_bytes
, data_bytes
, bss_bytes
,
339 tot_bytes
, pc
, hdrlenp
)
340 struct vnode
*vp
; /* inode for reading exec file */
341 int *sep_id
; /* true iff sep I&D */
342 vir_bytes
*text_bytes
; /* place to return text size */
343 vir_bytes
*data_bytes
; /* place to return initialized data size */
344 vir_bytes
*bss_bytes
; /* place to return bss size */
345 phys_bytes
*tot_bytes
; /* place to return total size */
346 vir_bytes
*pc
; /* program entry point (initial PC) */
349 /* Read the header and extract the text, data, bss and total sizes from it. */
351 struct exec hdr
; /* a.out header is read in here */
353 /* Read the header and check the magic number. The standard MINIX header
354 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
355 * Then come 4 more longs that are not used here.
356 * Byte 0: magic number 0x01
357 * Byte 1: magic number 0x03
358 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
359 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
360 * Motorola = 0x0B, Sun SPARC = 0x17
361 * Byte 4: Header length = 0x20
362 * Bytes 5-7 are not used.
364 * Now come the 6 longs
365 * Bytes 8-11: size of text segments in bytes
366 * Bytes 12-15: size of initialized data segment in bytes
367 * Bytes 16-19: size of bss in bytes
368 * Bytes 20-23: program entry point
369 * Bytes 24-27: total memory allocated to program (text, data + stack)
370 * Bytes 28-31: size of symbol table in bytes
371 * The longs are represented in a machine dependent order,
372 * little-endian on the 8088, big-endian on the 68000.
373 * The header is followed directly by the text and data segments, and the
374 * symbol table (if any). The sizes are given in the header. Only the
375 * text and data segments are copied into memory by exec. The header is
376 * used here only. The symbol table is for the benefit of a debugger and
380 struct readwrite_req req
;
381 struct readwrite_res res
;
384 pos
= 0; /* Read from the start of the file */
386 /* Fill in request structure */
387 req
.fs_e
= vp
->v_fs_e
;
388 req
.rw_flag
= READING
;
389 req
.inode_nr
= vp
->v_inode_nr
;
390 req
.user_e
= FS_PROC_NR
;
392 req
.pos
= cvul64(pos
);
393 req
.num_of_bytes
= sizeof(hdr
);
394 req
.user_addr
= (char*)&hdr
;
395 req
.inode_index
= vp
->v_index
;
398 if ((r
= req_readwrite(&req
, &res
)) != OK
) return r
;
400 /* Interpreted script? */
401 if (((char*)&hdr
)[0] == '#' && ((char*)&hdr
)[1] == '!' && vp
->v_size
>= 2)
404 if (vp
->v_size
< A_MINHDR
) return(ENOEXEC
);
406 /* Check magic number, cpu type, and flags. */
407 if (BADMAG(hdr
)) return(ENOEXEC
);
408 #if (CHIP == INTEL && _WORD_SIZE == 2)
409 if (hdr
.a_cpu
!= A_I8086
) return(ENOEXEC
);
411 #if (CHIP == INTEL && _WORD_SIZE == 4)
412 if (hdr
.a_cpu
!= A_I80386
) return(ENOEXEC
);
414 if ((hdr
.a_flags
& ~(A_NSYM
| A_EXEC
| A_SEP
)) != 0) return(ENOEXEC
);
416 *sep_id
= !!(hdr
.a_flags
& A_SEP
); /* separate I & D or not */
418 /* Get text and data sizes. */
419 *text_bytes
= (vir_bytes
) hdr
.a_text
; /* text size in bytes */
420 *data_bytes
= (vir_bytes
) hdr
.a_data
; /* data size in bytes */
421 *bss_bytes
= (vir_bytes
) hdr
.a_bss
; /* bss size in bytes */
422 *tot_bytes
= hdr
.a_total
; /* total bytes to allocate for prog */
423 if (*tot_bytes
== 0) return(ENOEXEC
);
426 /* If I & D space is not separated, it is all considered data. Text=0*/
427 *data_bytes
+= *text_bytes
;
430 *pc
= hdr
.a_entry
; /* initial address to start execution */
431 *hdrlenp
= hdr
.a_hdrlen
& BYTE
; /* header length */
436 /*===========================================================================*
438 *===========================================================================*/
439 PRIVATE
int patch_stack(vp
, stack
, stk_bytes
)
440 struct vnode
*vp
; /* pointer for open script file */
441 char stack
[ARG_MAX
]; /* pointer to stack image within FS */
442 vir_bytes
*stk_bytes
; /* size of initial stack */
444 /* Patch the argument vector to include the path name of the script to be
445 * interpreted, and all strings on the #! line. Returns the path name of
448 enum { INSERT
=FALSE
, REPLACE
=TRUE
};
451 char *sp
, *interp
= NULL
;
452 char buf
[_MAX_BLOCK_SIZE
];
453 struct readwrite_req req
;
454 struct readwrite_res res
;
456 /* Make user_path the new argv[0]. */
457 if (!insert_arg(stack
, stk_bytes
, user_fullpath
, REPLACE
)) return(ENOMEM
);
459 pos
= 0; /* Read from the start of the file */
461 /* Fill in request structure */
462 req
.fs_e
= vp
->v_fs_e
;
463 req
.rw_flag
= READING
;
464 req
.inode_nr
= vp
->v_inode_nr
;
465 req
.user_e
= FS_PROC_NR
;
467 req
.pos
= cvul64(pos
);
468 req
.num_of_bytes
= _MAX_BLOCK_SIZE
;
470 req
.inode_index
= vp
->v_index
;
473 if ((r
= req_readwrite(&req
, &res
)) != OK
) return r
;
476 if (n
> vp
->v_vmnt
->m_block_size
)
477 n
= vp
->v_vmnt
->m_block_size
;
478 if (n
< 2) return ENOEXEC
;
480 sp
= &(buf
[2]); /* just behind the #! */
482 if (n
> PATH_MAX
) n
= PATH_MAX
;
484 /* Use the user_path variable for temporary storage */
485 memcpy(user_fullpath
, sp
, n
);
487 if ((sp
= memchr(user_fullpath
, '\n', n
)) == NULL
) /* must be a proper line */
490 /* Move sp backwards through script[], prepending each string to stack. */
492 /* skip spaces behind argument. */
493 while (sp
> user_fullpath
&& (*--sp
== ' ' || *sp
== '\t')) {}
494 if (sp
== user_fullpath
) break;
497 /* Move to the start of the argument. */
498 while (sp
> user_fullpath
&& sp
[-1] != ' ' && sp
[-1] != '\t') --sp
;
501 if (!insert_arg(stack
, stk_bytes
, sp
, INSERT
)) return(ENOMEM
);
504 /* Round *stk_bytes up to the size of a pointer for alignment contraints. */
505 *stk_bytes
= ((*stk_bytes
+ PTRSIZE
- 1) / PTRSIZE
) * PTRSIZE
;
507 if (interp
!= user_fullpath
)
508 memmove(user_fullpath
, interp
, strlen(interp
)+1);
512 /*===========================================================================*
514 *===========================================================================*/
515 PRIVATE
int insert_arg(stack
, stk_bytes
, arg
, replace
)
516 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
517 vir_bytes
*stk_bytes
; /* size of initial stack */
518 char *arg
; /* argument to prepend/replace as new argv[0] */
521 /* Patch the stack so that arg will become argv[0]. Be careful, the stack may
522 * be filled with garbage, although it normally looks like this:
523 * nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL
524 * followed by the strings "pointed" to by the argv[i] and the envp[i]. The
525 * pointers are really offsets from the start of stack.
526 * Return true iff the operation succeeded.
528 int offset
, a0
, a1
, old_bytes
= *stk_bytes
;
530 /* Prepending arg adds at least one string and a zero byte. */
531 offset
= strlen(arg
) + 1;
533 a0
= (int) ((char **) stack
)[1]; /* argv[0] */
534 if (a0
< 4 * PTRSIZE
|| a0
>= old_bytes
) return(FALSE
);
536 a1
= a0
; /* a1 will point to the strings to be moved */
538 /* Move a1 to the end of argv[0][] (argv[1] if nargs > 1). */
540 if (a1
== old_bytes
) return(FALSE
);
542 } while (stack
[a1
++] != 0);
544 offset
+= PTRSIZE
; /* new argv[0] needs new pointer in argv[] */
545 a0
+= PTRSIZE
; /* location of new argv[0][]. */
548 /* stack will grow by offset bytes (or shrink by -offset bytes) */
549 if ((*stk_bytes
+= offset
) > ARG_MAX
) return(FALSE
);
551 /* Reposition the strings by offset bytes */
552 memmove(stack
+ a1
+ offset
, stack
+ a1
, old_bytes
- a1
);
554 strcpy(stack
+ a0
, arg
); /* Put arg in the new space. */
557 /* Make space for a new argv[0]. */
558 memmove(stack
+ 2 * PTRSIZE
, stack
+ 1 * PTRSIZE
, a0
- 2 * PTRSIZE
);
560 ((char **) stack
)[0]++; /* nargs++; */
562 /* Now patch up argv[] and envp[] by offset. */
563 patch_ptr(stack
, (vir_bytes
) offset
);
564 ((char **) stack
)[1] = (char *) a0
; /* set argv[0] correctly */
568 /*===========================================================================*
570 *===========================================================================*/
571 PRIVATE
void patch_ptr(stack
, base
)
572 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
573 vir_bytes base
; /* virtual address of stack base inside user */
575 /* When doing an exec(name, argv, envp) call, the user builds up a stack
576 * image with arg and env pointers relative to the start of the stack. Now
577 * these pointers must be relocated, since the stack is not positioned at
578 * address 0 in the user's address space.
584 flag
= 0; /* counts number of 0-pointers seen */
585 ap
= (char **) stack
; /* points initially to 'nargs' */
586 ap
++; /* now points to argv[0] */
588 if (ap
>= (char **) &stack
[ARG_MAX
]) return; /* too bad */
590 v
= (vir_bytes
) *ap
; /* v is relative pointer */
591 v
+= base
; /* relocate it */
592 *ap
= (char *) v
; /* put it back */
600 /*===========================================================================*
602 *===========================================================================*/
603 PRIVATE
int read_seg(vp
, off
, proc_e
, seg
, seg_bytes
)
604 struct vnode
*vp
; /* inode descriptor to read from */
605 off_t off
; /* offset in file */
606 int proc_e
; /* process number (endpoint) */
607 int seg
; /* T, D, or S */
608 phys_bytes seg_bytes
; /* how much is to be transferred? */
611 * The byte count on read is usually smaller than the segment count, because
612 * a segment is padded out to a click multiple, and the data segment is only
613 * partially initialized.
615 struct readwrite_req req
;
616 struct readwrite_res res
;
619 /* Make sure that the file is big enough */
620 if (vp
->v_size
< off
+seg_bytes
) return EIO
;
622 /* Fill in request structure */
623 req
.fs_e
= vp
->v_fs_e
;
624 req
.rw_flag
= READING
;
625 req
.inode_nr
= vp
->v_inode_nr
;
628 req
.pos
= cvul64(off
);
629 req
.num_of_bytes
= seg_bytes
;
631 req
.inode_index
= vp
->v_index
;
634 if ((r
= req_readwrite(&req
, &res
)) != OK
) return r
;
636 if (r
== OK
&& res
.cum_io
!= seg_bytes
)
637 printf("VFSread_seg segment has not been read properly by exec() \n");
643 /*===========================================================================*
645 *===========================================================================*/
646 PRIVATE
void clo_exec(rfp
)
649 /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec).
653 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
654 for (i
= 0; i
< OPEN_MAX
; i
++)
655 if ( FD_ISSET(i
, &rfp
->fp_cloexec_set
))
656 (void) close_fd(rfp
, i
);