1 /* The kernel call implemented in this file:
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)
10 #include "../system.h"
14 #include <minix/endpoint.h>
18 /*===========================================================================*
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.
31 struct io_range io_range
;
32 struct mem_range mem_range
;
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
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
)
49 /* Allow process to run. Make sure its privilege structure has already
52 if (!RTS_ISSET(rp
, RTS_NO_PRIV
) || priv(rp
)->s_proc_nr
== NONE
) {
55 RTS_UNSET(rp
, RTS_NO_PRIV
);
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
);
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
)
77 /* See if the caller wants to assign a static privilege id. */
78 if(!(priv
.s_flags
& DYN_PRIV_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",
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
)
127 priv(rp
)->s_flags
= priv
.s_flags
;
130 if(priv
.s_flags
& CHECK_IRQ
) {
131 if (priv
.s_nr_irq
< 0 || priv
.s_nr_irq
> NR_IRQ
)
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
];
138 printf("do_privctl: adding IRQ %d for %d\n",
139 priv(rp
)->s_irq_tab
[i
], rp
->p_endpoint
);
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
)
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
];
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
,
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
)
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
];
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
,
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
);
194 unset_sendto_bit(rp
, i
);
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
);
210 case SYS_PRIV_ADD_IO
:
211 if (RTS_ISSET(rp
, RTS_NO_PRIV
))
214 /* Only system processes get I/O resources? */
215 if (!(priv(rp
)->s_flags
& SYS_PROC
))
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
;
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
)
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
++;
241 case SYS_PRIV_ADD_MEM
:
242 if (RTS_ISSET(rp
, RTS_NO_PRIV
))
245 /* Only system processes get memory resources? */
246 if (!(priv(rp
)->s_flags
& SYS_PROC
))
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
)
253 priv(rp
)->s_flags
|= CHECK_MEM
; /* Check memory mappings */
254 i
= priv(rp
)->s_nr_mem_range
;
255 if (i
>= NR_MEM_RANGE
)
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
++;
264 case SYS_PRIV_ADD_IRQ
:
265 if (RTS_ISSET(rp
, RTS_NO_PRIV
))
268 /* Only system processes get IRQs? */
269 if (!(priv(rp
)->s_flags
& SYS_PROC
))
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
;
279 priv(rp
)->s_irq_tab
[i
]= irq
;
280 priv(rp
)->s_nr_irq
++;
283 case SYS_PRIV_QUERY_MEM
:
285 phys_bytes addr
, limit
;
287 /* See if a certain process is allowed to map in certain physical
290 addr
= (phys_bytes
) m_ptr
->CTL_PHYSSTART
;
291 limit
= addr
+ (phys_bytes
) m_ptr
->CTL_PHYSLEN
- 1;
296 if (!(sp
->s_flags
& SYS_PROC
))
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
)
306 printf("do_privctl: bad request %d\n", m_ptr
->CTL_REQUEST
);
311 #endif /* USE_PRIVCTL */