source: trunk/minix/kernel/system/do_privctl.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 4.9 KB
Line 
1/* The kernel call implemented in this file:
2 * m_type: SYS_PRIVCTL
3 *
4 * The parameters for this kernel call are:
5 * m1_i1: PR_ENDPT (process number of caller)
6 */
7
8#include "../system.h"
9#include "../ipc.h"
10#include <signal.h>
11
12#if USE_PRIVCTL
13
14#define FILLED_MASK (~0)
15
16/*===========================================================================*
17 * do_privctl *
18 *===========================================================================*/
19PUBLIC int do_privctl(m_ptr)
20message *m_ptr; /* pointer to request message */
21{
22/* Handle sys_privctl(). Update a process' privileges. If the process is not
23 * yet a system process, make sure it gets its own privilege structure.
24 */
25 register struct proc *caller_ptr;
26 register struct proc *rp;
27 register struct priv *sp;
28 int proc_nr;
29 int priv_id;
30 int old_flags;
31 int i;
32 phys_bytes caller_phys, kernel_phys;
33 struct io_range io_range;
34 struct mem_range mem_range;
35
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 NO_PRIV flag. This flag is set when a privileged process
39 * forks.
40 */
41 caller_ptr = proc_addr(who_p);
42 if (! (priv(caller_ptr)->s_flags & SYS_PROC)) return(EPERM);
43 if(!isokendpt(m_ptr->PR_ENDPT, &proc_nr)) return(EINVAL);
44 rp = proc_addr(proc_nr);
45
46 switch(m_ptr->CTL_REQUEST)
47 {
48 case SYS_PRIV_INIT:
49 if (! (rp->p_rts_flags & NO_PRIV)) return(EPERM);
50
51 /* Make sure this process has its own privileges structure. This may
52 * fail, since there are only a limited number of system processes.
53 * Then copy the privileges from the caller and restore some defaults.
54 */
55 if ((i=get_priv(rp, SYS_PROC)) != OK) return(i);
56 priv_id = priv(rp)->s_id; /* backup privilege id */
57 *priv(rp) = *priv(caller_ptr); /* copy from caller */
58 priv(rp)->s_id = priv_id; /* restore privilege id */
59 priv(rp)->s_proc_nr = proc_nr; /* reassociate process nr */
60
61 for (i=0; i< BITMAP_CHUNKS(NR_SYS_PROCS); i++) /* remove pending: */
62 priv(rp)->s_notify_pending.chunk[i] = 0; /* - notifications */
63 priv(rp)->s_int_pending = 0; /* - interrupts */
64 sigemptyset(&priv(rp)->s_sig_pending); /* - signals */
65
66 /* Now update the process' privileges as requested. */
67 rp->p_priv->s_trap_mask = FILLED_MASK;
68 for (i=0; i<BITMAP_CHUNKS(NR_SYS_PROCS); i++) {
69 rp->p_priv->s_ipc_to.chunk[i] = FILLED_MASK;
70 }
71 unset_sys_bit(rp->p_priv->s_ipc_to, USER_PRIV_ID);
72
73 /* All process that this process can send to must be able to reply.
74 * Therefore, their send masks should be updated as well.
75 */
76 for (i=0; i<NR_SYS_PROCS; i++) {
77 if (get_sys_bit(rp->p_priv->s_ipc_to, i)) {
78 set_sys_bit(priv_addr(i)->s_ipc_to, priv_id(rp));
79 }
80 }
81
82 /* No I/O resources, no memory resources, no IRQs */
83 priv(rp)->s_nr_io_range= 0;
84 priv(rp)->s_nr_mem_range= 0;
85 priv(rp)->s_nr_irq= 0;
86
87 /* Done. Privileges have been set. Allow process to run again. */
88 old_flags = rp->p_rts_flags; /* save value of the flags */
89 rp->p_rts_flags &= ~NO_PRIV;
90 if (old_flags != 0 && rp->p_rts_flags == 0) lock_enqueue(rp);
91 return(OK);
92 case SYS_PRIV_ADD_IO:
93 if (rp->p_rts_flags & NO_PRIV)
94 return(EPERM);
95
96 /* Only system processes get I/O resources? */
97 if (!(priv(rp)->s_flags & SYS_PROC))
98 return EPERM;
99
100 /* Get the I/O range */
101 caller_phys = umap_local(caller_ptr, D, (vir_bytes) m_ptr->CTL_ARG_PTR,
102 sizeof(io_range));
103 if (caller_phys == 0)
104 return EFAULT;
105 kernel_phys = vir2phys(&io_range);
106 phys_copy(caller_phys, kernel_phys, sizeof(io_range));
107 priv(rp)->s_flags |= CHECK_IO_PORT; /* Check I/O accesses */
108 i= priv(rp)->s_nr_io_range;
109 if (i >= NR_IO_RANGE)
110 return ENOMEM;
111
112 priv(rp)->s_io_tab[i].ior_base= io_range.ior_base;
113 priv(rp)->s_io_tab[i].ior_limit= io_range.ior_limit;
114 priv(rp)->s_nr_io_range++;
115
116 return OK;
117
118 case SYS_PRIV_ADD_MEM:
119 if (rp->p_rts_flags & NO_PRIV)
120 return(EPERM);
121
122 /* Only system processes get memory resources? */
123 if (!(priv(rp)->s_flags & SYS_PROC))
124 return EPERM;
125
126 /* Get the memory range */
127 caller_phys = umap_local(caller_ptr, D, (vir_bytes) m_ptr->CTL_ARG_PTR,
128 sizeof(mem_range));
129 if (caller_phys == 0)
130 return EFAULT;
131 kernel_phys = vir2phys(&mem_range);
132 phys_copy(caller_phys, kernel_phys, sizeof(mem_range));
133 priv(rp)->s_flags |= CHECK_MEM; /* Check I/O accesses */
134 i= priv(rp)->s_nr_mem_range;
135 if (i >= NR_MEM_RANGE)
136 return ENOMEM;
137
138#if 0
139 priv(rp)->s_mem_tab[i].mr_base= mem_range.mr_base;
140 priv(rp)->s_mem_tab[i].mr_limit= mem_range.mr_limit;
141 priv(rp)->s_nr_mem_range++;
142#endif
143
144 return OK;
145
146 case SYS_PRIV_ADD_IRQ:
147 if (rp->p_rts_flags & NO_PRIV)
148 return(EPERM);
149
150 /* Only system processes get IRQs? */
151 if (!(priv(rp)->s_flags & SYS_PROC))
152 return EPERM;
153
154 priv(rp)->s_flags |= CHECK_IRQ; /* Check IRQs */
155
156 i= priv(rp)->s_nr_irq;
157 if (i >= NR_IRQ)
158 return ENOMEM;
159 priv(rp)->s_irq_tab[i]= m_ptr->CTL_MM_PRIV;
160 priv(rp)->s_nr_irq++;
161
162 return OK;
163
164 default:
165 kprintf("do_privctl: bad request %d\n", m_ptr->CTL_REQUEST);
166 return EINVAL;
167 }
168}
169
170#endif /* USE_PRIVCTL */
171
Note: See TracBrowser for help on using the repository browser.