panic() cleanup.
[minix.git] / kernel / system / do_privctl.c
blob11568811ddc56cd593fdf773f514ba1b5aed31db
1 /* The kernel call implemented in this file:
2 * m_type: SYS_PRIVCTL
4 * The parameters for this kernel call are:
5 * m2_i1: CTL_ENDPT (process endpoint of target)
6 * m2_i2: CTL_REQUEST (privilege control request)
7 * m2_p1: CTL_ARG_PTR (pointer to request data)
8 */
10 #include "../system.h"
11 #include "../ipc.h"
12 #include <signal.h>
13 #include <string.h>
14 #include <minix/endpoint.h>
16 #if USE_PRIVCTL
18 /*===========================================================================*
19 * do_privctl *
20 *===========================================================================*/
21 PUBLIC int do_privctl(struct proc * caller, message * m_ptr)
23 /* Handle sys_privctl(). Update a process' privileges. If the process is not
24 * yet a system process, make sure it gets its own privilege structure.
26 struct proc *rp;
27 int proc_nr;
28 int priv_id;
29 int ipc_to_m, kcalls;
30 int i, r;
31 struct io_range io_range;
32 struct mem_range mem_range;
33 struct priv priv;
34 int irq;
36 /* Check whether caller is allowed to make this call. Privileged proceses
37 * can only update the privileges of processes that are inhibited from
38 * running by the RTS_NO_PRIV flag. This flag is set when a privileged process
39 * forks.
41 if (! (priv(caller)->s_flags & SYS_PROC)) return(EPERM);
42 if(m_ptr->CTL_ENDPT == SELF) proc_nr = _ENDPOINT_P(caller->p_endpoint);
43 else if(!isokendpt(m_ptr->CTL_ENDPT, &proc_nr)) return(EINVAL);
44 rp = proc_addr(proc_nr);
46 switch(m_ptr->CTL_REQUEST)
48 case SYS_PRIV_ALLOW:
49 /* Allow process to run. Make sure its privilege structure has already
50 * been set.
52 if (!RTS_ISSET(rp, RTS_NO_PRIV) || priv(rp)->s_proc_nr == NONE) {
53 return(EPERM);
55 RTS_UNSET(rp, RTS_NO_PRIV);
56 return(OK);
58 case SYS_PRIV_DISALLOW:
59 /* Disallow process from running. */
60 if (RTS_ISSET(rp, RTS_NO_PRIV)) return(EPERM);
61 RTS_SET(rp, RTS_NO_PRIV);
62 return(OK);
64 case SYS_PRIV_SET_SYS:
65 /* Set a privilege structure of a blocked system process. */
66 if (! RTS_ISSET(rp, RTS_NO_PRIV)) return(EPERM);
68 /* Check whether a static or dynamic privilege id must be allocated. */
69 priv_id = NULL_PRIV_ID;
70 if (m_ptr->CTL_ARG_PTR)
72 /* Copy privilege structure from caller */
73 if((r=data_copy(caller->p_endpoint, (vir_bytes) m_ptr->CTL_ARG_PTR,
74 KERNEL, (vir_bytes) &priv, sizeof(priv))) != OK)
75 return r;
77 /* See if the caller wants to assign a static privilege id. */
78 if(!(priv.s_flags & DYN_PRIV_ID)) {
79 priv_id = priv.s_id;
83 /* Make sure this process has its own privileges structure. This may
84 * fail, since there are only a limited number of system processes.
85 * Then copy privileges from the caller and restore some defaults.
87 if ((i=get_priv(rp, priv_id)) != OK)
89 printf("do_privctl: unable to allocate priv_id %d: %d\n",
90 priv_id, i);
91 return(i);
93 priv_id = priv(rp)->s_id; /* backup privilege id */
94 *priv(rp) = *priv(caller); /* copy from caller */
95 priv(rp)->s_id = priv_id; /* restore privilege id */
96 priv(rp)->s_proc_nr = proc_nr; /* reassociate process nr */
98 for (i=0; i< NR_SYS_CHUNKS; i++) /* remove pending: */
99 priv(rp)->s_notify_pending.chunk[i] = 0; /* - notifications */
100 priv(rp)->s_int_pending = 0; /* - interrupts */
101 sigemptyset(&priv(rp)->s_sig_pending); /* - signals */
102 priv(rp)->s_asyntab= -1; /* - asynsends */
103 priv(rp)->s_asynsize= 0;
105 /* Set defaults for privilege bitmaps. */
106 priv(rp)->s_flags= DEF_SYS_F; /* privilege flags */
107 priv(rp)->s_trap_mask= DEF_SYS_T; /* allowed traps */
108 ipc_to_m = DEF_SYS_M; /* allowed targets */
109 kcalls = DEF_SYS_KC; /* allowed kernel calls */
110 for(i = 0; i < SYS_CALL_MASK_SIZE; i++) {
111 priv(rp)->s_k_call_mask[i] = (kcalls == NO_C ? 0 : (~0));
114 /* Set defaults for resources: no I/O resources, no memory resources,
115 * no IRQs, no grant table
117 priv(rp)->s_nr_io_range= 0;
118 priv(rp)->s_nr_mem_range= 0;
119 priv(rp)->s_nr_irq= 0;
120 priv(rp)->s_grant_table= 0;
121 priv(rp)->s_grant_entries= 0;
123 /* Override defaults if the caller has supplied a privilege structure. */
124 if (m_ptr->CTL_ARG_PTR)
126 /* Copy s_flags. */
127 priv(rp)->s_flags = priv.s_flags;
129 /* Copy IRQs */
130 if(priv.s_flags & CHECK_IRQ) {
131 if (priv.s_nr_irq < 0 || priv.s_nr_irq > NR_IRQ)
132 return EINVAL;
133 priv(rp)->s_nr_irq= priv.s_nr_irq;
134 for (i= 0; i<priv.s_nr_irq; i++)
136 priv(rp)->s_irq_tab[i]= priv.s_irq_tab[i];
137 #if 0
138 printf("do_privctl: adding IRQ %d for %d\n",
139 priv(rp)->s_irq_tab[i], rp->p_endpoint);
140 #endif
144 /* Copy I/O ranges */
145 if(priv.s_flags & CHECK_IO_PORT) {
146 if (priv.s_nr_io_range < 0 || priv.s_nr_io_range > NR_IO_RANGE)
147 return EINVAL;
148 priv(rp)->s_nr_io_range= priv.s_nr_io_range;
149 for (i= 0; i<priv.s_nr_io_range; i++)
151 priv(rp)->s_io_tab[i]= priv.s_io_tab[i];
152 #if 0
153 printf("do_privctl: adding I/O range [%x..%x] for %d\n",
154 priv(rp)->s_io_tab[i].ior_base,
155 priv(rp)->s_io_tab[i].ior_limit,
156 rp->p_endpoint);
157 #endif
161 /* Copy memory ranges */
162 if(priv.s_flags & CHECK_MEM) {
163 if (priv.s_nr_mem_range < 0 || priv.s_nr_mem_range > NR_MEM_RANGE)
164 return EINVAL;
165 priv(rp)->s_nr_mem_range= priv.s_nr_mem_range;
166 for (i= 0; i<priv.s_nr_mem_range; i++)
168 priv(rp)->s_mem_tab[i]= priv.s_mem_tab[i];
169 #if 0
170 printf("do_privctl: adding mem range [%x..%x] for %d\n",
171 priv(rp)->s_mem_tab[i].mr_base,
172 priv(rp)->s_mem_tab[i].mr_limit,
173 rp->p_endpoint);
174 #endif
178 /* Copy trap mask. */
179 priv(rp)->s_trap_mask = priv.s_trap_mask;
181 /* Copy target mask. */
182 memcpy(&ipc_to_m, &priv.s_ipc_to, sizeof(ipc_to_m));
184 /* Copy kernel call mask. */
185 memcpy(priv(rp)->s_k_call_mask, priv.s_k_call_mask,
186 sizeof(priv(rp)->s_k_call_mask));
189 /* Fill in target mask. */
190 for (i=0; i < NR_SYS_PROCS; i++) {
191 if (ipc_to_m & (1 << i))
192 set_sendto_bit(rp, i);
193 else
194 unset_sendto_bit(rp, i);
197 return(OK);
199 case SYS_PRIV_SET_USER:
200 /* Set a privilege structure of a blocked user process. */
201 if (!RTS_ISSET(rp, RTS_NO_PRIV)) return(EPERM);
203 /* Link the process to the privilege structure of the root user
204 * process all the user processes share.
206 priv(rp) = priv_addr(USER_PRIV_ID);
208 return(OK);
210 case SYS_PRIV_ADD_IO:
211 if (RTS_ISSET(rp, RTS_NO_PRIV))
212 return(EPERM);
214 /* Only system processes get I/O resources? */
215 if (!(priv(rp)->s_flags & SYS_PROC))
216 return EPERM;
218 #if 0 /* XXX -- do we need a call for this? */
219 if (strcmp(rp->p_name, "fxp") == 0 ||
220 strcmp(rp->p_name, "rtl8139") == 0)
222 printf("setting ipc_stats_target to %d\n", rp->p_endpoint);
223 ipc_stats_target= rp->p_endpoint;
225 #endif
227 /* Get the I/O range */
228 data_copy(caller->p_endpoint, (vir_bytes) m_ptr->CTL_ARG_PTR,
229 KERNEL, (vir_bytes) &io_range, sizeof(io_range));
230 priv(rp)->s_flags |= CHECK_IO_PORT; /* Check I/O accesses */
231 i= priv(rp)->s_nr_io_range;
232 if (i >= NR_IO_RANGE)
233 return ENOMEM;
235 priv(rp)->s_io_tab[i].ior_base= io_range.ior_base;
236 priv(rp)->s_io_tab[i].ior_limit= io_range.ior_limit;
237 priv(rp)->s_nr_io_range++;
239 return OK;
241 case SYS_PRIV_ADD_MEM:
242 if (RTS_ISSET(rp, RTS_NO_PRIV))
243 return(EPERM);
245 /* Only system processes get memory resources? */
246 if (!(priv(rp)->s_flags & SYS_PROC))
247 return EPERM;
249 /* Get the memory range */
250 if((r=data_copy(caller->p_endpoint, (vir_bytes) m_ptr->CTL_ARG_PTR,
251 KERNEL, (vir_bytes) &mem_range, sizeof(mem_range))) != OK)
252 return r;
253 priv(rp)->s_flags |= CHECK_MEM; /* Check memory mappings */
254 i= priv(rp)->s_nr_mem_range;
255 if (i >= NR_MEM_RANGE)
256 return ENOMEM;
258 priv(rp)->s_mem_tab[i].mr_base= mem_range.mr_base;
259 priv(rp)->s_mem_tab[i].mr_limit= mem_range.mr_limit;
260 priv(rp)->s_nr_mem_range++;
262 return OK;
264 case SYS_PRIV_ADD_IRQ:
265 if (RTS_ISSET(rp, RTS_NO_PRIV))
266 return(EPERM);
268 /* Only system processes get IRQs? */
269 if (!(priv(rp)->s_flags & SYS_PROC))
270 return EPERM;
272 data_copy(caller->p_endpoint, (vir_bytes) m_ptr->CTL_ARG_PTR,
273 KERNEL, (vir_bytes) &irq, sizeof(irq));
274 priv(rp)->s_flags |= CHECK_IRQ; /* Check IRQs */
276 i= priv(rp)->s_nr_irq;
277 if (i >= NR_IRQ)
278 return ENOMEM;
279 priv(rp)->s_irq_tab[i]= irq;
280 priv(rp)->s_nr_irq++;
282 return OK;
283 case SYS_PRIV_QUERY_MEM:
285 phys_bytes addr, limit;
286 struct priv *sp;
287 /* See if a certain process is allowed to map in certain physical
288 * memory.
290 addr = (phys_bytes) m_ptr->CTL_PHYSSTART;
291 limit = addr + (phys_bytes) m_ptr->CTL_PHYSLEN - 1;
292 if(limit < addr)
293 return EPERM;
294 if(!(sp = priv(rp)))
295 return EPERM;
296 if (!(sp->s_flags & SYS_PROC))
297 return EPERM;
298 for(i = 0; i < sp->s_nr_mem_range; i++) {
299 if(addr >= sp->s_mem_tab[i].mr_base &&
300 limit <= sp->s_mem_tab[i].mr_limit)
301 return OK;
303 return EPERM;
305 default:
306 printf("do_privctl: bad request %d\n", m_ptr->CTL_REQUEST);
307 return EINVAL;
311 #endif /* USE_PRIVCTL */