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>
34 #include <minix/vfsif.h>
36 FORWARD
_PROTOTYPE( int exec_newmem
, (int proc_e
, vir_bytes text_bytes
,
37 vir_bytes data_bytes
, vir_bytes bss_bytes
, vir_bytes tot_bytes
,
38 vir_bytes frame_len
, int sep_id
,
39 Dev_t st_dev
, ino_t st_ino
, time_t st_ctime
, char *progname
,
40 int new_uid
, int new_gid
,
41 vir_bytes
*stack_topp
, int *load_textp
, int *allow_setuidp
) );
42 FORWARD
_PROTOTYPE( int read_header
, (struct vnode
*vp
, int *sep_id
,
43 vir_bytes
*text_bytes
, vir_bytes
*data_bytes
,
44 vir_bytes
*bss_bytes
, phys_bytes
*tot_bytes
, vir_bytes
*pc
,
46 FORWARD
_PROTOTYPE( int patch_stack
, (struct vnode
*vp
,
47 char stack
[ARG_MAX
], vir_bytes
*stk_bytes
) );
48 FORWARD
_PROTOTYPE( int insert_arg
, (char stack
[ARG_MAX
],
49 vir_bytes
*stk_bytes
, char *arg
, int replace
) );
50 FORWARD
_PROTOTYPE( void patch_ptr
, (char stack
[ARG_MAX
],
52 FORWARD
_PROTOTYPE( int read_seg
, (struct vnode
*vp
, off_t off
,
53 int proc_e
, int seg
, phys_bytes seg_bytes
) );
54 FORWARD
_PROTOTYPE( void clo_exec
, (struct fproc
*rfp
) );
56 #define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
57 #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
59 /*===========================================================================*
61 *===========================================================================*/
62 PUBLIC
int pm_exec(proc_e
, path
, path_len
, frame
, frame_len
)
69 /* Perform the execve(name, argv, envp) call. The user library builds a
70 * complete stack image, including pointers, args, environ, etc. The stack
71 * is copied to a buffer inside FS, and then to the new core image.
73 int r
, sep_id
, round
, proc_s
, hdrlen
, load_text
, allow_setuid
;
74 vir_bytes text_bytes
, data_bytes
, bss_bytes
, pc
;
75 phys_bytes tot_bytes
; /* total space for program, including gap */
76 vir_bytes stack_top
, vsp
;
87 char progname
[PROC_NAME_LEN
];
88 static char mbuf
[ARG_MAX
]; /* buffer for stack and zeroes */
90 /* Request and response structures */
91 struct lookup_req lookup_req
;
92 struct access_req access_req
;
93 struct open_req open_req
;
94 struct node_details res
;
96 okendpt(proc_e
, &proc_s
);
97 rfp
= fp
= &fproc
[proc_s
];
100 super_user
= (fp
->fp_effuid
== SU_UID
? TRUE
: FALSE
); /* su? */
102 /* Get the exec file name. */
103 r
= fetch_name(path
, path_len
, 0);
106 printf("pm_exec: fetch_name failed\n");
107 return(r
); /* file name not in user data segment */
110 /* Fetch the stack from the user before destroying the old core image. */
111 if (frame_len
> ARG_MAX
)
113 printf("pm_exec: bad frame_len\n");
114 return(ENOMEM
); /* stack too big */
116 r
= sys_datacopy(proc_e
, (vir_bytes
) frame
,
117 SELF
, (vir_bytes
) mbuf
, (phys_bytes
)frame_len
);
118 /* can't fetch stack (e.g. bad virtual addr) */
121 printf("pm_exec: sys_datacopy failed\n");
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(&lookup_req
, &res
)) != OK
) return r
;
146 if ((res
.fmode
& I_TYPE
) != I_REGULAR
) {
150 /* Fill in request fields */
151 access_req
.fs_e
= res
.fs_e
;
152 access_req
.amode
= X_BIT
;
153 access_req
.inode_nr
= res
.inode_nr
;
154 access_req
.uid
= fp
->fp_effuid
;
155 access_req
.gid
= fp
->fp_effgid
;
159 if ((r
= req_access(&access_req
)) != OK
) {
160 printf("VFSexec: access failed\n");
166 open_req
.inode_nr
= res
.inode_nr
;
167 open_req
.fs_e
= res
.fs_e
;
170 open_req
.lastc
= NULL
;
175 if ((r
= req_open(&open_req
, &res
)) != OK
) {
176 printf("VFSexec: open failed\n");
180 /* Use the vnode to store file details */
181 vn
.v_inode_nr
= res
.inode_nr
;
182 vn
.v_mode
= res
.fmode
;
183 vn
.v_index
= res
.inode_index
;
184 vn
.v_size
= res
.fsize
;
185 vn
.v_fs_e
= res
.fs_e
;
187 if ( (vmp
= find_vmnt(vn
.v_fs_e
)) == NIL_VMNT
)
188 printf("VFS: vmnt not found by exec()");
190 vn
.v_dev
= vmp
->m_dev
;
199 /* Deal with setuid/setgid executables */
200 if (vn
.v_mode
& I_SET_UID_BIT
)
202 if (vn
.v_mode
& I_SET_GID_BIT
)
206 /* Read the file header and extract the segment sizes. */
207 r
= read_header(&vn
, &sep_id
, &text_bytes
, &data_bytes
, &bss_bytes
,
208 &tot_bytes
, &pc
, &hdrlen
);
209 if (r
!= ESCRIPT
|| round
!= 0)
212 /* Get fresh copy of the file name. */
213 r
= fetch_name(path
, path_len
, 0);
216 printf("pm_exec: 2nd fetch_name failed\n");
218 return(r
); /* strange */
220 r
= patch_stack(&vn
, mbuf
, &frame_len
);
224 printf("pm_exec: patch stack\n");
231 printf("pm_exec: returning ENOEXEC, r = %d\n", r
);
235 r
= exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
236 frame_len
, sep_id
, vn
.v_dev
, vn
.v_inode_nr
, v_ctime
,
237 progname
, new_uid
, new_gid
, &stack_top
, &load_text
, &allow_setuid
);
240 printf("pm_exec: exec_newmap failed: %d\n", r
);
245 /* Patch up stack and copy it from FS to new core image. */
248 patch_ptr(mbuf
, vsp
);
249 r
= sys_datacopy(SELF
, (vir_bytes
) mbuf
,
250 proc_e
, (vir_bytes
) vsp
, (phys_bytes
)frame_len
);
251 if (r
!= OK
) panic(__FILE__
,"pm_exec stack copy err on", proc_e
);
255 /* Read in text and data segments. */
257 r
= read_seg(&vn
, off
, proc_e
, T
, text_bytes
);
261 r
= read_seg(&vn
, off
, proc_e
, D
, data_bytes
);
265 if (r
!= OK
) return r
;
271 rfp
->fp_effuid
= new_uid
;
272 rfp
->fp_effgid
= new_gid
;
275 /* This child has now exec()ced. */
278 /* Check if this is a driver that can now be useful. */
279 dmap_endpt_up(rfp
->fp_endpoint
);
281 /*printf("VFSpm_exec: %s OK\n", user_fullpath);*/
285 /*===========================================================================*
287 *===========================================================================*/
288 PRIVATE
int exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
289 frame_len
, sep_id
, st_dev
, st_ino
, st_ctime
, progname
,
290 new_uid
, new_gid
, stack_topp
, load_textp
, allow_setuidp
)
292 vir_bytes text_bytes
;
293 vir_bytes data_bytes
;
304 vir_bytes
*stack_topp
;
309 struct exec_newmem e
;
312 e
.text_bytes
= text_bytes
;
313 e
.data_bytes
= data_bytes
;
314 e
.bss_bytes
= bss_bytes
;
315 e
.tot_bytes
= tot_bytes
;
316 e
.args_bytes
= frame_len
;
320 e
.st_ctime
= st_ctime
;
323 strncpy(e
.progname
, progname
, sizeof(e
.progname
)-1);
324 e
.progname
[sizeof(e
.progname
)-1]= '\0';
326 m
.m_type
= EXEC_NEWMEM
;
327 m
.EXC_NM_PROC
= proc_e
;
328 m
.EXC_NM_PTR
= (char *)&e
;
329 r
= sendrec(PM_PROC_NR
, &m
);
333 printf("exec_newmem: r = %d, m_type = %d\n", r
, m
.m_type
);
335 *stack_topp
= m
.m1_i1
;
336 *load_textp
= !!(m
.m1_i2
& EXC_NM_RF_LOAD_TEXT
);
337 *allow_setuidp
= !!(m
.m1_i2
& EXC_NM_RF_ALLOW_SETUID
);
339 printf("exec_newmem: stack_top = 0x%x\n", *stack_topp
);
340 printf("exec_newmem: load_text = %d\n", *load_textp
);
346 /*===========================================================================*
348 *===========================================================================*/
349 PRIVATE
int read_header(vp
, sep_id
, text_bytes
, data_bytes
, bss_bytes
,
350 tot_bytes
, pc
, hdrlenp
)
351 struct vnode
*vp
; /* inode for reading exec file */
352 int *sep_id
; /* true iff sep I&D */
353 vir_bytes
*text_bytes
; /* place to return text size */
354 vir_bytes
*data_bytes
; /* place to return initialized data size */
355 vir_bytes
*bss_bytes
; /* place to return bss size */
356 phys_bytes
*tot_bytes
; /* place to return total size */
357 vir_bytes
*pc
; /* program entry point (initial PC) */
360 /* Read the header and extract the text, data, bss and total sizes from it. */
362 struct exec hdr
; /* a.out header is read in here */
364 /* Read the header and check the magic number. The standard MINIX header
365 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
366 * Then come 4 more longs that are not used here.
367 * Byte 0: magic number 0x01
368 * Byte 1: magic number 0x03
369 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
370 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
371 * Motorola = 0x0B, Sun SPARC = 0x17
372 * Byte 4: Header length = 0x20
373 * Bytes 5-7 are not used.
375 * Now come the 6 longs
376 * Bytes 8-11: size of text segments in bytes
377 * Bytes 12-15: size of initialized data segment in bytes
378 * Bytes 16-19: size of bss in bytes
379 * Bytes 20-23: program entry point
380 * Bytes 24-27: total memory allocated to program (text, data + stack)
381 * Bytes 28-31: size of symbol table in bytes
382 * The longs are represented in a machine dependent order,
383 * little-endian on the 8088, big-endian on the 68000.
384 * The header is followed directly by the text and data segments, and the
385 * symbol table (if any). The sizes are given in the header. Only the
386 * text and data segments are copied into memory by exec. The header is
387 * used here only. The symbol table is for the benefit of a debugger and
391 struct readwrite_req req
;
392 struct readwrite_res res
;
395 pos
= 0; /* Read from the start of the file */
397 /* Fill in request structure */
398 req
.fs_e
= vp
->v_fs_e
;
399 req
.rw_flag
= READING
;
400 req
.inode_nr
= vp
->v_inode_nr
;
401 req
.user_e
= FS_PROC_NR
;
404 req
.num_of_bytes
= sizeof(hdr
);
405 req
.user_addr
= (char*)&hdr
;
406 req
.inode_index
= vp
->v_index
;
409 if ((r
= req_readwrite(&req
, &res
)) != OK
) return r
;
411 /* Interpreted script? */
412 if (((char*)&hdr
)[0] == '#' && ((char*)&hdr
)[1] == '!' && vp
->v_size
>= 2)
415 if (vp
->v_size
< A_MINHDR
) return(ENOEXEC
);
417 /* Check magic number, cpu type, and flags. */
418 if (BADMAG(hdr
)) return(ENOEXEC
);
419 #if (CHIP == INTEL && _WORD_SIZE == 2)
420 if (hdr
.a_cpu
!= A_I8086
) return(ENOEXEC
);
422 #if (CHIP == INTEL && _WORD_SIZE == 4)
423 if (hdr
.a_cpu
!= A_I80386
) return(ENOEXEC
);
425 if ((hdr
.a_flags
& ~(A_NSYM
| A_EXEC
| A_SEP
)) != 0) return(ENOEXEC
);
427 *sep_id
= !!(hdr
.a_flags
& A_SEP
); /* separate I & D or not */
429 /* Get text and data sizes. */
430 *text_bytes
= (vir_bytes
) hdr
.a_text
; /* text size in bytes */
431 *data_bytes
= (vir_bytes
) hdr
.a_data
; /* data size in bytes */
432 *bss_bytes
= (vir_bytes
) hdr
.a_bss
; /* bss size in bytes */
433 *tot_bytes
= hdr
.a_total
; /* total bytes to allocate for prog */
434 if (*tot_bytes
== 0) return(ENOEXEC
);
437 /* If I & D space is not separated, it is all considered data. Text=0*/
438 *data_bytes
+= *text_bytes
;
441 *pc
= hdr
.a_entry
; /* initial address to start execution */
442 *hdrlenp
= hdr
.a_hdrlen
& BYTE
; /* header length */
447 /*===========================================================================*
449 *===========================================================================*/
450 PRIVATE
int patch_stack(vp
, stack
, stk_bytes
)
451 struct vnode
*vp
; /* pointer for open script file */
452 char stack
[ARG_MAX
]; /* pointer to stack image within FS */
453 vir_bytes
*stk_bytes
; /* size of initial stack */
455 /* Patch the argument vector to include the path name of the script to be
456 * interpreted, and all strings on the #! line. Returns the path name of
459 enum { INSERT
=FALSE
, REPLACE
=TRUE
};
462 char *sp
, *interp
= NULL
;
463 char buf
[_MAX_BLOCK_SIZE
];
464 struct readwrite_req req
;
465 struct readwrite_res res
;
467 /* Make user_path the new argv[0]. */
468 if (!insert_arg(stack
, stk_bytes
, user_fullpath
, REPLACE
)) return(ENOMEM
);
470 pos
= 0; /* Read from the start of the file */
472 /* Fill in request structure */
473 req
.fs_e
= vp
->v_fs_e
;
474 req
.rw_flag
= READING
;
475 req
.inode_nr
= vp
->v_inode_nr
;
476 req
.user_e
= FS_PROC_NR
;
479 req
.num_of_bytes
= _MAX_BLOCK_SIZE
;
481 req
.inode_index
= vp
->v_index
;
484 if ((r
= req_readwrite(&req
, &res
)) != OK
) return r
;
487 if (n
> vp
->v_vmnt
->m_block_size
)
488 n
= vp
->v_vmnt
->m_block_size
;
489 if (n
< 2) return ENOEXEC
;
491 sp
= &(buf
[2]); /* just behind the #! */
493 if (n
> PATH_MAX
) n
= PATH_MAX
;
495 /* Use the user_path variable for temporary storage */
496 memcpy(user_fullpath
, sp
, n
);
498 if ((sp
= memchr(user_fullpath
, '\n', n
)) == NULL
) /* must be a proper line */
501 /* Move sp backwards through script[], prepending each string to stack. */
503 /* skip spaces behind argument. */
504 while (sp
> user_fullpath
&& (*--sp
== ' ' || *sp
== '\t')) {}
505 if (sp
== user_fullpath
) break;
508 /* Move to the start of the argument. */
509 while (sp
> user_fullpath
&& sp
[-1] != ' ' && sp
[-1] != '\t') --sp
;
512 if (!insert_arg(stack
, stk_bytes
, sp
, INSERT
)) return(ENOMEM
);
515 /* Round *stk_bytes up to the size of a pointer for alignment contraints. */
516 *stk_bytes
= ((*stk_bytes
+ PTRSIZE
- 1) / PTRSIZE
) * PTRSIZE
;
518 if (interp
!= user_fullpath
)
519 memmove(user_fullpath
, interp
, strlen(interp
)+1);
523 /*===========================================================================*
525 *===========================================================================*/
526 PRIVATE
int insert_arg(stack
, stk_bytes
, arg
, replace
)
527 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
528 vir_bytes
*stk_bytes
; /* size of initial stack */
529 char *arg
; /* argument to prepend/replace as new argv[0] */
532 /* Patch the stack so that arg will become argv[0]. Be careful, the stack may
533 * be filled with garbage, although it normally looks like this:
534 * nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL
535 * followed by the strings "pointed" to by the argv[i] and the envp[i]. The
536 * pointers are really offsets from the start of stack.
537 * Return true iff the operation succeeded.
539 int offset
, a0
, a1
, old_bytes
= *stk_bytes
;
541 /* Prepending arg adds at least one string and a zero byte. */
542 offset
= strlen(arg
) + 1;
544 a0
= (int) ((char **) stack
)[1]; /* argv[0] */
545 if (a0
< 4 * PTRSIZE
|| a0
>= old_bytes
) return(FALSE
);
547 a1
= a0
; /* a1 will point to the strings to be moved */
549 /* Move a1 to the end of argv[0][] (argv[1] if nargs > 1). */
551 if (a1
== old_bytes
) return(FALSE
);
553 } while (stack
[a1
++] != 0);
555 offset
+= PTRSIZE
; /* new argv[0] needs new pointer in argv[] */
556 a0
+= PTRSIZE
; /* location of new argv[0][]. */
559 /* stack will grow by offset bytes (or shrink by -offset bytes) */
560 if ((*stk_bytes
+= offset
) > ARG_MAX
) return(FALSE
);
562 /* Reposition the strings by offset bytes */
563 memmove(stack
+ a1
+ offset
, stack
+ a1
, old_bytes
- a1
);
565 strcpy(stack
+ a0
, arg
); /* Put arg in the new space. */
568 /* Make space for a new argv[0]. */
569 memmove(stack
+ 2 * PTRSIZE
, stack
+ 1 * PTRSIZE
, a0
- 2 * PTRSIZE
);
571 ((char **) stack
)[0]++; /* nargs++; */
573 /* Now patch up argv[] and envp[] by offset. */
574 patch_ptr(stack
, (vir_bytes
) offset
);
575 ((char **) stack
)[1] = (char *) a0
; /* set argv[0] correctly */
579 /*===========================================================================*
581 *===========================================================================*/
582 PRIVATE
void patch_ptr(stack
, base
)
583 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
584 vir_bytes base
; /* virtual address of stack base inside user */
586 /* When doing an exec(name, argv, envp) call, the user builds up a stack
587 * image with arg and env pointers relative to the start of the stack. Now
588 * these pointers must be relocated, since the stack is not positioned at
589 * address 0 in the user's address space.
595 flag
= 0; /* counts number of 0-pointers seen */
596 ap
= (char **) stack
; /* points initially to 'nargs' */
597 ap
++; /* now points to argv[0] */
599 if (ap
>= (char **) &stack
[ARG_MAX
]) return; /* too bad */
601 v
= (vir_bytes
) *ap
; /* v is relative pointer */
602 v
+= base
; /* relocate it */
603 *ap
= (char *) v
; /* put it back */
611 /*===========================================================================*
613 *===========================================================================*/
614 PRIVATE
int read_seg(vp
, off
, proc_e
, seg
, seg_bytes
)
615 struct vnode
*vp
; /* inode descriptor to read from */
616 off_t off
; /* offset in file */
617 int proc_e
; /* process number (endpoint) */
618 int seg
; /* T, D, or S */
619 phys_bytes seg_bytes
; /* how much is to be transferred? */
622 * The byte count on read is usually smaller than the segment count, because
623 * a segment is padded out to a click multiple, and the data segment is only
624 * partially initialized.
626 struct readwrite_req req
;
627 struct readwrite_res res
;
630 /* Make sure that the file is big enough */
631 if (vp
->v_size
< off
+seg_bytes
) return EIO
;
633 /* Fill in request structure */
634 req
.fs_e
= vp
->v_fs_e
;
635 req
.rw_flag
= READING
;
636 req
.inode_nr
= vp
->v_inode_nr
;
640 req
.num_of_bytes
= seg_bytes
;
642 req
.inode_index
= vp
->v_index
;
645 if ((r
= req_readwrite(&req
, &res
)) != OK
) return r
;
647 if (r
== OK
&& res
.cum_io
!= seg_bytes
)
648 printf("VFSread_seg segment has not been read properly by exec() \n");
654 /*===========================================================================*
656 *===========================================================================*/
657 PRIVATE
void clo_exec(rfp
)
660 /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec).
664 /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
665 for (i
= 0; i
< OPEN_MAX
; i
++)
666 if ( FD_ISSET(i
, &rfp
->fp_cloexec_set
))
667 (void) close_fd(rfp
, i
);