exec_domain.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. current->personality = personality;
  122. oep = current_thread_info()->exec_domain;
  123. current_thread_info()->exec_domain = ep;
  124. module_put(oep->module);
  125. return 0;
  126. }
  127. #ifdef CONFIG_PROC_FS
  128. static int execdomains_proc_show(struct seq_file *m, void *v)
  129. {
  130. struct exec_domain *ep;
  131. read_lock(&exec_domains_lock);
  132. for (ep = exec_domains; ep; ep = ep->next)
  133. seq_printf(m, "%d-%d\t%-16s\t[%s]\n",
  134. ep->pers_low, ep->pers_high, ep->name,
  135. module_name(ep->module));
  136. read_unlock(&exec_domains_lock);
  137. return 0;
  138. }
  139. static int execdomains_proc_open(struct inode *inode, struct file *file)
  140. {
  141. return single_open(file, execdomains_proc_show, NULL);
  142. }
  143. static const struct file_operations execdomains_proc_fops = {
  144. .open = execdomains_proc_open,
  145. .read = seq_read,
  146. .llseek = seq_lseek,
  147. .release = single_release,
  148. };
  149. static int __init proc_execdomains_init(void)
  150. {
  151. proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
  152. return 0;
  153. }
  154. module_init(proc_execdomains_init);
  155. #endif
  156. SYSCALL_DEFINE1(personality, u_long, personality)
  157. {
  158. u_long old = current->personality;
  159. if (personality != 0xffffffff) {
  160. set_personality(personality);
  161. if (current->personality != personality)
  162. return -EINVAL;
  163. }
  164. return (long)old;
  165. }
  166. EXPORT_SYMBOL(register_exec_domain);
  167. EXPORT_SYMBOL(unregister_exec_domain);
  168. EXPORT_SYMBOL(__set_personality);