1
2
3
4
5
6
7
8
9
10#include <linux/config.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/kmod.h>
14#include <linux/module.h>
15#include <linux/personality.h>
16#include <linux/sched.h>
17#include <linux/sysctl.h>
18#include <linux/types.h>
19
20
21static void default_handler(int, struct pt_regs *);
22
23static struct exec_domain *exec_domains = &default_exec_domain;
24static rwlock_t exec_domains_lock = RW_LOCK_UNLOCKED;
25
26
27static u_long ident_map[32] = {
28 0, 1, 2, 3, 4, 5, 6, 7,
29 8, 9, 10, 11, 12, 13, 14, 15,
30 16, 17, 18, 19, 20, 21, 22, 23,
31 24, 25, 26, 27, 28, 29, 30, 31
32};
33
34struct exec_domain default_exec_domain = {
35 "Linux",
36 default_handler,
37 0, 0,
38 ident_map,
39 ident_map,
40};
41
42
43static void
44default_handler(int segment, struct pt_regs *regp)
45{
46 u_long pers = 0;
47
48
49
50
51
52
53
54
55
56
57
58 switch (segment) {
59#ifdef __i386__
60 case 0x07:
61 pers = abi_defhandler_lcall7;
62 break;
63 case 0x27:
64 pers = PER_SOLARIS;
65 break;
66#endif
67 }
68 set_personality(pers);
69
70 if (current_thread_info()->exec_domain->handler != default_handler)
71 current_thread_info()->exec_domain->handler(segment, regp);
72 else
73 send_sig(SIGSEGV, current, 1);
74}
75
76static struct exec_domain *
77lookup_exec_domain(u_long personality)
78{
79 struct exec_domain * ep;
80 u_long pers = personality(personality);
81
82 read_lock(&exec_domains_lock);
83 for (ep = exec_domains; ep; ep = ep->next) {
84 if (pers >= ep->pers_low && pers <= ep->pers_high)
85 if (try_inc_mod_count(ep->module))
86 goto out;
87 }
88
89#ifdef CONFIG_KMOD
90 read_unlock(&exec_domains_lock);
91 {
92 char buffer[30];
93 sprintf(buffer, "personality-%ld", pers);
94 request_module(buffer);
95 }
96 read_lock(&exec_domains_lock);
97
98 for (ep = exec_domains; ep; ep = ep->next) {
99 if (pers >= ep->pers_low && pers <= ep->pers_high)
100 if (try_inc_mod_count(ep->module))
101 goto out;
102 }
103#endif
104
105 ep = &default_exec_domain;
106out:
107 read_unlock(&exec_domains_lock);
108 return (ep);
109}
110
111int
112register_exec_domain(struct exec_domain *ep)
113{
114 struct exec_domain *tmp;
115 int err = -EBUSY;
116
117 if (ep == NULL)
118 return -EINVAL;
119
120 if (ep->next != NULL)
121 return -EBUSY;
122
123 write_lock(&exec_domains_lock);
124 for (tmp = exec_domains; tmp; tmp = tmp->next) {
125 if (tmp == ep)
126 goto out;
127 }
128
129 ep->next = exec_domains;
130 exec_domains = ep;
131 err = 0;
132
133out:
134 write_unlock(&exec_domains_lock);
135 return (err);
136}
137
138int
139unregister_exec_domain(struct exec_domain *ep)
140{
141 struct exec_domain **epp;
142
143 epp = &exec_domains;
144 write_lock(&exec_domains_lock);
145 for (epp = &exec_domains; *epp; epp = &(*epp)->next) {
146 if (ep == *epp)
147 goto unregister;
148 }
149 write_unlock(&exec_domains_lock);
150 return -EINVAL;
151
152unregister:
153 *epp = ep->next;
154 ep->next = NULL;
155 write_unlock(&exec_domains_lock);
156 return 0;
157}
158
159int
160__set_personality(u_long personality)
161{
162 struct exec_domain *ep, *oep;
163
164 ep = lookup_exec_domain(personality);
165 if (ep == current_thread_info()->exec_domain) {
166 current->personality = personality;
167 return 0;
168 }
169
170 if (atomic_read(¤t->fs->count) != 1) {
171 struct fs_struct *fsp, *ofsp;
172
173 fsp = copy_fs_struct(current->fs);
174 if (fsp == NULL) {
175 put_exec_domain(ep);
176 return -ENOMEM;;
177 }
178
179 task_lock(current);
180 ofsp = current->fs;
181 current->fs = fsp;
182 task_unlock(current);
183
184 put_fs_struct(ofsp);
185 }
186
187
188
189
190
191
192 current->personality = personality;
193 oep = current_thread_info()->exec_domain;
194 current_thread_info()->exec_domain = ep;
195 set_fs_altroot();
196
197 put_exec_domain(oep);
198
199 printk(KERN_DEBUG "[%s:%d]: set personality to %lx\n",
200 current->comm, current->pid, personality);
201 return 0;
202}
203
204int
205get_exec_domain_list(char *page)
206{
207 struct exec_domain *ep;
208 int len = 0;
209
210 read_lock(&exec_domains_lock);
211 for (ep = exec_domains; ep && len < PAGE_SIZE - 80; ep = ep->next)
212 len += sprintf(page + len, "%d-%d\t%-16s\t[%s]\n",
213 ep->pers_low, ep->pers_high, ep->name,
214 ep->module ? ep->module->name : "kernel");
215 read_unlock(&exec_domains_lock);
216 return (len);
217}
218
219asmlinkage long
220sys_personality(u_long personality)
221{
222 u_long old = current->personality;;
223
224 if (personality != 0xffffffff) {
225 set_personality(personality);
226 if (current->personality != personality)
227 return -EINVAL;
228 }
229
230 return (long)old;
231}
232
233
234EXPORT_SYMBOL(register_exec_domain);
235EXPORT_SYMBOL(unregister_exec_domain);
236EXPORT_SYMBOL(__set_personality);
237
238
239
240
241
242
243
244
245
246
247
248
249u_long abi_defhandler_coff = PER_SCOSVR3;
250u_long abi_defhandler_elf = PER_LINUX;
251u_long abi_defhandler_lcall7 = PER_SVR4;
252u_long abi_defhandler_libcso = PER_SVR4;
253u_int abi_traceflg;
254int abi_fake_utsname;
255
256static struct ctl_table abi_table[] = {
257 {ABI_DEFHANDLER_COFF, "defhandler_coff", &abi_defhandler_coff,
258 sizeof(int), 0644, NULL, &proc_doulongvec_minmax},
259 {ABI_DEFHANDLER_ELF, "defhandler_elf", &abi_defhandler_elf,
260 sizeof(int), 0644, NULL, &proc_doulongvec_minmax},
261 {ABI_DEFHANDLER_LCALL7, "defhandler_lcall7", &abi_defhandler_lcall7,
262 sizeof(int), 0644, NULL, &proc_doulongvec_minmax},
263 {ABI_DEFHANDLER_LIBCSO, "defhandler_libcso", &abi_defhandler_libcso,
264 sizeof(int), 0644, NULL, &proc_doulongvec_minmax},
265 {ABI_TRACE, "trace", &abi_traceflg,
266 sizeof(u_int), 0644, NULL, &proc_dointvec},
267 {ABI_FAKE_UTSNAME, "fake_utsname", &abi_fake_utsname,
268 sizeof(int), 0644, NULL, &proc_dointvec},
269 {0}
270};
271
272static struct ctl_table abi_root_table[] = {
273 {CTL_ABI, "abi", NULL, 0, 0555, abi_table},
274 {0}
275};
276
277static int __init
278abi_register_sysctl(void)
279{
280 register_sysctl_table(abi_root_table, 1);
281 return 0;
282}
283
284__initcall(abi_register_sysctl);
285
286
287EXPORT_SYMBOL(abi_defhandler_coff);
288EXPORT_SYMBOL(abi_defhandler_elf);
289EXPORT_SYMBOL(abi_defhandler_lcall7);
290EXPORT_SYMBOL(abi_defhandler_libcso);
291EXPORT_SYMBOL(abi_traceflg);
292EXPORT_SYMBOL(abi_fake_utsname);
293