| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Handling of different ABIs (personalities). | 
|  | 3 | * | 
|  | 4 | * We group personalities into execution domains which have their | 
|  | 5 | * own handlers for kernel entry points, signal mapping, etc... | 
|  | 6 | * | 
|  | 7 | * 2001-05-06	Complete rewrite,  Christoph Hellwig (hch@infradead.org) | 
|  | 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/syscalls.h> | 
|  | 18 | #include <linux/sysctl.h> | 
|  | 19 | #include <linux/types.h> | 
|  | 20 |  | 
|  | 21 |  | 
|  | 22 | static void default_handler(int, struct pt_regs *); | 
|  | 23 |  | 
|  | 24 | static struct exec_domain *exec_domains = &default_exec_domain; | 
|  | 25 | static DEFINE_RWLOCK(exec_domains_lock); | 
|  | 26 |  | 
|  | 27 |  | 
|  | 28 | static u_long ident_map[32] = { | 
|  | 29 | 0,	1,	2,	3,	4,	5,	6,	7, | 
|  | 30 | 8,	9,	10,	11,	12,	13,	14,	15, | 
|  | 31 | 16,	17,	18,	19,	20,	21,	22,	23, | 
|  | 32 | 24,	25,	26,	27,	28,	29,	30,	31 | 
|  | 33 | }; | 
|  | 34 |  | 
|  | 35 | struct exec_domain default_exec_domain = { | 
|  | 36 | .name		= "Linux",		/* name */ | 
|  | 37 | .handler	= default_handler,	/* lcall7 causes a seg fault. */ | 
|  | 38 | .pers_low	= 0, 			/* PER_LINUX personality. */ | 
|  | 39 | .pers_high	= 0,			/* PER_LINUX personality. */ | 
|  | 40 | .signal_map	= ident_map,		/* Identity map signals. */ | 
|  | 41 | .signal_invmap	= ident_map,		/*  - both ways. */ | 
|  | 42 | }; | 
|  | 43 |  | 
|  | 44 |  | 
|  | 45 | static void | 
|  | 46 | default_handler(int segment, struct pt_regs *regp) | 
|  | 47 | { | 
|  | 48 | set_personality(0); | 
|  | 49 |  | 
|  | 50 | if (current_thread_info()->exec_domain->handler != default_handler) | 
|  | 51 | current_thread_info()->exec_domain->handler(segment, regp); | 
|  | 52 | else | 
|  | 53 | send_sig(SIGSEGV, current, 1); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | static struct exec_domain * | 
|  | 57 | lookup_exec_domain(u_long personality) | 
|  | 58 | { | 
|  | 59 | struct exec_domain *	ep; | 
|  | 60 | u_long			pers = personality(personality); | 
|  | 61 |  | 
|  | 62 | read_lock(&exec_domains_lock); | 
|  | 63 | for (ep = exec_domains; ep; ep = ep->next) { | 
|  | 64 | if (pers >= ep->pers_low && pers <= ep->pers_high) | 
|  | 65 | if (try_module_get(ep->module)) | 
|  | 66 | goto out; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | #ifdef CONFIG_KMOD | 
|  | 70 | read_unlock(&exec_domains_lock); | 
|  | 71 | request_module("personality-%ld", pers); | 
|  | 72 | read_lock(&exec_domains_lock); | 
|  | 73 |  | 
|  | 74 | for (ep = exec_domains; ep; ep = ep->next) { | 
|  | 75 | if (pers >= ep->pers_low && pers <= ep->pers_high) | 
|  | 76 | if (try_module_get(ep->module)) | 
|  | 77 | goto out; | 
|  | 78 | } | 
|  | 79 | #endif | 
|  | 80 |  | 
|  | 81 | ep = &default_exec_domain; | 
|  | 82 | out: | 
|  | 83 | read_unlock(&exec_domains_lock); | 
|  | 84 | return (ep); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | int | 
|  | 88 | register_exec_domain(struct exec_domain *ep) | 
|  | 89 | { | 
|  | 90 | struct exec_domain	*tmp; | 
|  | 91 | int			err = -EBUSY; | 
|  | 92 |  | 
|  | 93 | if (ep == NULL) | 
|  | 94 | return -EINVAL; | 
|  | 95 |  | 
|  | 96 | if (ep->next != NULL) | 
|  | 97 | return -EBUSY; | 
|  | 98 |  | 
|  | 99 | write_lock(&exec_domains_lock); | 
|  | 100 | for (tmp = exec_domains; tmp; tmp = tmp->next) { | 
|  | 101 | if (tmp == ep) | 
|  | 102 | goto out; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | ep->next = exec_domains; | 
|  | 106 | exec_domains = ep; | 
|  | 107 | err = 0; | 
|  | 108 |  | 
|  | 109 | out: | 
|  | 110 | write_unlock(&exec_domains_lock); | 
|  | 111 | return (err); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | int | 
|  | 115 | unregister_exec_domain(struct exec_domain *ep) | 
|  | 116 | { | 
|  | 117 | struct exec_domain	**epp; | 
|  | 118 |  | 
|  | 119 | epp = &exec_domains; | 
|  | 120 | write_lock(&exec_domains_lock); | 
|  | 121 | for (epp = &exec_domains; *epp; epp = &(*epp)->next) { | 
|  | 122 | if (ep == *epp) | 
|  | 123 | goto unregister; | 
|  | 124 | } | 
|  | 125 | write_unlock(&exec_domains_lock); | 
|  | 126 | return -EINVAL; | 
|  | 127 |  | 
|  | 128 | unregister: | 
|  | 129 | *epp = ep->next; | 
|  | 130 | ep->next = NULL; | 
|  | 131 | write_unlock(&exec_domains_lock); | 
|  | 132 | return 0; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | int | 
|  | 136 | __set_personality(u_long personality) | 
|  | 137 | { | 
|  | 138 | struct exec_domain	*ep, *oep; | 
|  | 139 |  | 
|  | 140 | ep = lookup_exec_domain(personality); | 
|  | 141 | if (ep == current_thread_info()->exec_domain) { | 
|  | 142 | current->personality = personality; | 
| Sergey Vlasov | 6a4d11c | 2006-03-24 03:18:38 -0800 | [diff] [blame] | 143 | module_put(ep->module); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return 0; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | if (atomic_read(¤t->fs->count) != 1) { | 
|  | 148 | struct fs_struct *fsp, *ofsp; | 
|  | 149 |  | 
|  | 150 | fsp = copy_fs_struct(current->fs); | 
|  | 151 | if (fsp == NULL) { | 
|  | 152 | module_put(ep->module); | 
|  | 153 | return -ENOMEM; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | task_lock(current); | 
|  | 157 | ofsp = current->fs; | 
|  | 158 | current->fs = fsp; | 
|  | 159 | task_unlock(current); | 
|  | 160 |  | 
|  | 161 | put_fs_struct(ofsp); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | /* | 
|  | 165 | * At that point we are guaranteed to be the sole owner of | 
|  | 166 | * current->fs. | 
|  | 167 | */ | 
|  | 168 |  | 
|  | 169 | current->personality = personality; | 
|  | 170 | oep = current_thread_info()->exec_domain; | 
|  | 171 | current_thread_info()->exec_domain = ep; | 
|  | 172 | set_fs_altroot(); | 
|  | 173 |  | 
|  | 174 | module_put(oep->module); | 
|  | 175 | return 0; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | int | 
|  | 179 | get_exec_domain_list(char *page) | 
|  | 180 | { | 
|  | 181 | struct exec_domain	*ep; | 
|  | 182 | int			len = 0; | 
|  | 183 |  | 
|  | 184 | read_lock(&exec_domains_lock); | 
|  | 185 | for (ep = exec_domains; ep && len < PAGE_SIZE - 80; ep = ep->next) | 
|  | 186 | len += sprintf(page + len, "%d-%d\t%-16s\t[%s]\n", | 
|  | 187 | ep->pers_low, ep->pers_high, ep->name, | 
|  | 188 | module_name(ep->module)); | 
|  | 189 | read_unlock(&exec_domains_lock); | 
|  | 190 | return (len); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | asmlinkage long | 
|  | 194 | sys_personality(u_long personality) | 
|  | 195 | { | 
|  | 196 | u_long old = current->personality; | 
|  | 197 |  | 
|  | 198 | if (personality != 0xffffffff) { | 
|  | 199 | set_personality(personality); | 
|  | 200 | if (current->personality != personality) | 
|  | 201 | return -EINVAL; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | return (long)old; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 |  | 
|  | 208 | EXPORT_SYMBOL(register_exec_domain); | 
|  | 209 | EXPORT_SYMBOL(unregister_exec_domain); | 
|  | 210 | EXPORT_SYMBOL(__set_personality); |