64-bit VFS_LSEEK_OFF
[minix3.git] / servers / pm / exec.c
blob1c4af3099128126652bf3d6465fc62ac6f4d0c37
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 procfs)
13 * The entry points into this file are:
14 * do_exec: perform the EXEC system call
15 * do_newexec: handle PM part of exec call after VFS
16 * do_execrestart: finish the special exec call for RS
17 * exec_restart: finish a regular exec call
20 #include "pm.h"
21 #include <sys/stat.h>
22 #include <minix/callnr.h>
23 #include <minix/endpoint.h>
24 #include <minix/com.h>
25 #include <minix/vm.h>
26 #include <signal.h>
27 #include <libexec.h>
28 #include <sys/ptrace.h>
29 #include "mproc.h"
31 #define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
32 #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
34 /*===========================================================================*
35 * do_exec *
36 *===========================================================================*/
37 int do_exec()
39 message m;
41 /* Forward call to VFS */
42 memset(&m, 0, sizeof(m));
43 m.m_type = VFS_PM_EXEC;
44 m.VFS_PM_ENDPT = mp->mp_endpoint;
45 m.VFS_PM_PATH = m_in.PM_EXEC_NAME;
46 m.VFS_PM_PATH_LEN = m_in.PM_EXEC_NAMELEN;
47 m.VFS_PM_FRAME = m_in.PM_EXEC_FRAME;
48 m.VFS_PM_FRAME_LEN = m_in.PM_EXEC_FRAMELEN;
49 m.VFS_PM_PS_STR = (vir_bytes) m_in.PM_EXEC_PS_STR;
51 tell_vfs(mp, &m);
53 /* Do not reply */
54 return SUSPEND;
58 /*===========================================================================*
59 * do_newexec *
60 *===========================================================================*/
61 int do_newexec(void)
63 int proc_e, proc_n, allow_setuid;
64 char *ptr;
65 struct mproc *rmp;
66 struct exec_info args;
67 int r;
69 if (who_e != VFS_PROC_NR && who_e != RS_PROC_NR)
70 return EPERM;
72 proc_e= m_in.PM_EXEC_NEW_ENDPT;
73 if (pm_isokendpt(proc_e, &proc_n) != OK) {
74 panic("do_newexec: got bad endpoint: %d", proc_e);
76 rmp= &mproc[proc_n];
77 ptr= m_in.PM_EXEC_NEW_PTR;
78 r= sys_datacopy(who_e, (vir_bytes)ptr,
79 SELF, (vir_bytes)&args, sizeof(args));
80 if (r != OK)
81 panic("do_newexec: sys_datacopy failed: %d", r);
83 allow_setuid = 0; /* Do not allow setuid execution */
84 rmp->mp_flags &= ~TAINTED; /* By default not tainted */
86 if (rmp->mp_tracer == NO_TRACER) {
87 /* Okay, setuid execution is allowed */
88 allow_setuid = 1;
91 if (allow_setuid && args.allow_setuid) {
92 rmp->mp_effuid = args.new_uid;
93 rmp->mp_effgid = args.new_gid;
96 /* A process is considered 'tainted' when it's executing with
97 * setuid or setgid bit set, or when the real{u,g}id doesn't
98 * match the eff{u,g}id, respectively. */
99 if (allow_setuid && args.allow_setuid) {
100 /* Program has setuid and/or setgid bits set */
101 rmp->mp_flags |= TAINTED;
102 } else if (rmp->mp_effuid != rmp->mp_realuid ||
103 rmp->mp_effgid != rmp->mp_realgid) {
104 rmp->mp_flags |= TAINTED;
107 /* System will save command line for debugging, ps(1) output, etc. */
108 strncpy(rmp->mp_name, args.progname, PROC_NAME_LEN-1);
109 rmp->mp_name[PROC_NAME_LEN-1] = '\0';
111 /* Save offset to initial argc (for procfs) */
112 rmp->mp_frame_addr = (vir_bytes) args.stack_high - args.frame_len;
113 rmp->mp_frame_len = args.frame_len;
115 /* Kill process if something goes wrong after this point. */
116 rmp->mp_flags |= PARTIAL_EXEC;
118 mp->mp_reply.PM_EXEC_NEW_SUID = (allow_setuid && args.allow_setuid);
120 return r;
123 /*===========================================================================*
124 * do_execrestart *
125 *===========================================================================*/
126 int do_execrestart(void)
128 int proc_e, proc_n, result;
129 struct mproc *rmp;
130 vir_bytes pc, ps_str;
132 if (who_e != RS_PROC_NR)
133 return EPERM;
135 proc_e = m_in.PM_EXEC_RESTART_ENDPT;
136 if (pm_isokendpt(proc_e, &proc_n) != OK) {
137 panic("do_execrestart: got bad endpoint: %d", proc_e);
139 rmp = &mproc[proc_n];
140 result = m_in.PM_EXEC_RESTART_RESULT;
141 pc = (vir_bytes)m_in.PM_EXEC_RESTART_PC;
142 ps_str = (vir_bytes)m_in.PM_EXEC_RESTART_PS_STR;
144 exec_restart(rmp, result, pc, rmp->mp_frame_addr, ps_str);
146 return OK;
149 /*===========================================================================*
150 * exec_restart *
151 *===========================================================================*/
152 void exec_restart(struct mproc *rmp, int result, vir_bytes pc, vir_bytes sp,
153 vir_bytes ps_str)
155 int r, sn;
157 if (result != OK)
159 if (rmp->mp_flags & PARTIAL_EXEC)
161 /* Use SIGKILL to signal that something went wrong */
162 sys_kill(rmp->mp_endpoint, SIGKILL);
163 return;
165 reply(rmp-mproc, result);
166 return;
169 rmp->mp_flags &= ~PARTIAL_EXEC;
171 /* Fix 'mproc' fields, tell kernel that exec is done, reset caught
172 * sigs.
174 for (sn = 1; sn < _NSIG; sn++) {
175 if (sigismember(&rmp->mp_catch, sn)) {
176 sigdelset(&rmp->mp_catch, sn);
177 rmp->mp_sigact[sn].sa_handler = SIG_DFL;
178 sigemptyset(&rmp->mp_sigact[sn].sa_mask);
182 /* Cause a signal if this process is traced.
183 * Do this before making the process runnable again!
185 if (rmp->mp_tracer != NO_TRACER && !(rmp->mp_trace_flags & TO_NOEXEC))
187 sn = (rmp->mp_trace_flags & TO_ALTEXEC) ? SIGSTOP : SIGTRAP;
189 check_sig(rmp->mp_pid, sn, FALSE /* ksig */);
192 /* Call kernel to exec with SP and PC set by VFS. */
193 r = sys_exec(rmp->mp_endpoint, (char *) sp, rmp->mp_name, pc, ps_str);
194 if (r != OK) panic("sys_exec failed: %d", r);