exec_domain.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kmod.h>
  12. #include <linux/module.h>
  13. #include <linux/personality.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/sched.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/types.h>
  20. static void default_handler(int, struct pt_regs *);
  21. static struct exec_domain *exec_domains = &default_exec_domain;
  22. static DEFINE_RWLOCK(exec_domains_lock);
  23. static u_long ident_map[32] = {
  24. 0, 1, 2, 3, 4, 5, 6, 7,
  25. 8, 9, 10, 11, 12, 13, 14, 15,
  26. 16, 17, 18, 19, 20, 21, 22, 23,
  27. 24, 25, 26, 27, 28, 29, 30, 31
  28. };
  29. struct exec_domain default_exec_domain = {
  30. .name = "Linux", /* name */
  31. .handler = default_handler, /* lcall7 causes a seg fault. */
  32. .pers_low = 0, /* PER_LINUX personality. */
  33. .pers_high = 0, /* PER_LINUX personality. */
  34. .signal_map = ident_map, /* Identity map signals. */
  35. .signal_invmap = ident_map, /* - both ways. */
  36. };
  37. static void
  38. default_handler(int segment, struct pt_regs *regp)
  39. {
  40. set_personality(0);
  41. if (current_thread_info()->exec_domain->handler != default_handler)
  42. current_thread_info()->exec_domain->handler(segment, regp);
  43. else
  44. send_sig(SIGSEGV, current, 1);
  45. }
  46. static struct exec_domain *
  47. lookup_exec_domain(u_long personality)
  48. {
  49. struct exec_domain * ep;
  50. u_long pers = personality(personality);
  51. read_lock(&exec_domains_lock);
  52. for (ep = exec_domains; ep; ep = ep->next) {
  53. if (pers >= ep->pers_low && pers <= ep->pers_high)
  54. if (try_module_get(ep->module))
  55. goto out;
  56. }
  57. #ifdef CONFIG_MODULES
  58. read_unlock(&exec_domains_lock);
  59. request_module("personality-%ld", pers);
  60. read_lock(&exec_domains_lock);
  61. for (ep = exec_domains; ep; ep = ep->next) {
  62. if (pers >= ep->pers_low && pers <= ep->pers_high)
  63. if (try_module_get(ep->module))
  64. goto out;
  65. }
  66. #endif
  67. ep = &default_exec_domain;
  68. out:
  69. read_unlock(&exec_domains_lock);
  70. return (ep);
  71. }
  72. int
  73. register_exec_domain(struct exec_domain *ep)
  74. {
  75. struct exec_domain *tmp;
  76. int err = -EBUSY;
  77. if (ep == NULL)
  78. return -EINVAL;
  79. if (ep->next != NULL)
  80. return -EBUSY;
  81. write_lock(&exec_domains_lock);
  82. for (tmp = exec_domains; tmp; tmp = tmp->next) {
  83. if (tmp == ep)
  84. goto out;
  85. }
  86. ep->next = exec_domains;
  87. exec_domains = ep;
  88. err = 0;
  89. out:
  90. write_unlock(&exec_domains_lock);
  91. return (err);
  92. }
  93. int
  94. unregister_exec_domain(struct exec_domain *ep)
  95. {
  96. struct exec_domain **epp;
  97. epp = &exec_domains;
  98. write_lock(&exec_domains_lock);
  99. for (epp = &exec_domains; *epp; epp = &(*epp)->next) {
  100. if (ep == *epp)
  101. goto unregister;
  102. }
  103. write_unlock(&exec_domains_lock);
  104. return -EINVAL;
  105. unregister:
  106. *epp = ep->next;
  107. ep->next = NULL;
  108. write_unlock(&exec_domains_lock);
  109. return 0;
  110. }
  111. int
  112. __set_personality(u_long personality)
  113. {
  114. struct exec_domain *ep, *oep;
  115. ep = lookup_exec_domain(personality);
  116. if (ep == current_thread_info()->exec_domain) {
  117. current->personality = personality;
  118. module_put(ep->module);
  119. return 0;
  120. }
  121. if (atomic_read(&current->fs->count) != 1) {
  122. struct fs_struct *fsp, *ofsp;
  123. fsp = copy_fs_struct(current->fs);
  124. if (fsp == NULL) {
  125. module_put(ep->module);
  126. return -ENOMEM;
  127. }
  128. task_lock(current);
  129. ofsp = current->fs;
  130. current->fs = fsp;
  131. task_unlock(current);
  132. put_fs_struct(ofsp);
  133. }
  134. /*
  135. * At that point we are guaranteed to be the sole owner of
  136. * current->fs.
  137. */
  138. current->personality = personality;
  139. oep = current_thread_info()->exec_domain;
  140. current_thread_info()->exec_domain = ep;
  141. module_put(oep->module);
  142. return 0;
  143. }
  144. #ifdef CONFIG_PROC_FS
  145. static int execdomains_proc_show(struct seq_file *m, void *v)
  146. {
  147. struct exec_domain *ep;
  148. read_lock(&exec_domains_lock);
  149. for (ep = exec_domains; ep; ep = ep->next)
  150. seq_printf(m, "%d-%d\t%-16s\t[%s]\n",
  151. ep->pers_low, ep->pers_high, ep->name,
  152. module_name(ep->module));
  153. read_unlock(&exec_domains_lock);
  154. return 0;
  155. }
  156. static int execdomains_proc_open(struct inode *inode, struct file *file)
  157. {
  158. return single_open(file, execdomains_proc_show, NULL);
  159. }
  160. static const struct file_operations execdomains_proc_fops = {
  161. .open = execdomains_proc_open,
  162. .read = seq_read,
  163. .llseek = seq_lseek,
  164. .release = single_release,
  165. };
  166. static int __init proc_execdomains_init(void)
  167. {
  168. proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
  169. return 0;
  170. }
  171. module_init(proc_execdomains_init);
  172. #endif
  173. SYSCALL_DEFINE1(personality, u_long, personality)
  174. {
  175. u_long old = current->personality;
  176. if (personality != 0xffffffff) {
  177. set_personality(personality);
  178. if (current->personality != personality)
  179. return -EINVAL;
  180. }
  181. return (long)old;
  182. }
  183. EXPORT_SYMBOL(register_exec_domain);
  184. EXPORT_SYMBOL(unregister_exec_domain);
  185. EXPORT_SYMBOL(__set_personality);