exec_domain.c 4.5 KB

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