exec_domain.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/config.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kmod.h>
  13. #include <linux/module.h>
  14. #include <linux/personality.h>
  15. #include <linux/sched.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/types.h>
  19. static void default_handler(int, struct pt_regs *);
  20. static struct exec_domain *exec_domains = &default_exec_domain;
  21. static DEFINE_RWLOCK(exec_domains_lock);
  22. static u_long ident_map[32] = {
  23. 0, 1, 2, 3, 4, 5, 6, 7,
  24. 8, 9, 10, 11, 12, 13, 14, 15,
  25. 16, 17, 18, 19, 20, 21, 22, 23,
  26. 24, 25, 26, 27, 28, 29, 30, 31
  27. };
  28. struct exec_domain default_exec_domain = {
  29. .name = "Linux", /* name */
  30. .handler = default_handler, /* lcall7 causes a seg fault. */
  31. .pers_low = 0, /* PER_LINUX personality. */
  32. .pers_high = 0, /* PER_LINUX personality. */
  33. .signal_map = ident_map, /* Identity map signals. */
  34. .signal_invmap = ident_map, /* - both ways. */
  35. };
  36. static void
  37. default_handler(int segment, struct pt_regs *regp)
  38. {
  39. set_personality(0);
  40. if (current_thread_info()->exec_domain->handler != default_handler)
  41. current_thread_info()->exec_domain->handler(segment, regp);
  42. else
  43. send_sig(SIGSEGV, current, 1);
  44. }
  45. static struct exec_domain *
  46. lookup_exec_domain(u_long personality)
  47. {
  48. struct exec_domain * ep;
  49. u_long pers = personality(personality);
  50. read_lock(&exec_domains_lock);
  51. for (ep = exec_domains; ep; ep = ep->next) {
  52. if (pers >= ep->pers_low && pers <= ep->pers_high)
  53. if (try_module_get(ep->module))
  54. goto out;
  55. }
  56. #ifdef CONFIG_KMOD
  57. read_unlock(&exec_domains_lock);
  58. request_module("personality-%ld", pers);
  59. read_lock(&exec_domains_lock);
  60. for (ep = exec_domains; ep; ep = ep->next) {
  61. if (pers >= ep->pers_low && pers <= ep->pers_high)
  62. if (try_module_get(ep->module))
  63. goto out;
  64. }
  65. #endif
  66. ep = &default_exec_domain;
  67. out:
  68. read_unlock(&exec_domains_lock);
  69. return (ep);
  70. }
  71. int
  72. register_exec_domain(struct exec_domain *ep)
  73. {
  74. struct exec_domain *tmp;
  75. int err = -EBUSY;
  76. if (ep == NULL)
  77. return -EINVAL;
  78. if (ep->next != NULL)
  79. return -EBUSY;
  80. write_lock(&exec_domains_lock);
  81. for (tmp = exec_domains; tmp; tmp = tmp->next) {
  82. if (tmp == ep)
  83. goto out;
  84. }
  85. ep->next = exec_domains;
  86. exec_domains = ep;
  87. err = 0;
  88. out:
  89. write_unlock(&exec_domains_lock);
  90. return (err);
  91. }
  92. int
  93. unregister_exec_domain(struct exec_domain *ep)
  94. {
  95. struct exec_domain **epp;
  96. epp = &exec_domains;
  97. write_lock(&exec_domains_lock);
  98. for (epp = &exec_domains; *epp; epp = &(*epp)->next) {
  99. if (ep == *epp)
  100. goto unregister;
  101. }
  102. write_unlock(&exec_domains_lock);
  103. return -EINVAL;
  104. unregister:
  105. *epp = ep->next;
  106. ep->next = NULL;
  107. write_unlock(&exec_domains_lock);
  108. return 0;
  109. }
  110. int
  111. __set_personality(u_long personality)
  112. {
  113. struct exec_domain *ep, *oep;
  114. ep = lookup_exec_domain(personality);
  115. if (ep == current_thread_info()->exec_domain) {
  116. current->personality = personality;
  117. return 0;
  118. }
  119. if (atomic_read(&current->fs->count) != 1) {
  120. struct fs_struct *fsp, *ofsp;
  121. fsp = copy_fs_struct(current->fs);
  122. if (fsp == NULL) {
  123. module_put(ep->module);
  124. return -ENOMEM;
  125. }
  126. task_lock(current);
  127. ofsp = current->fs;
  128. current->fs = fsp;
  129. task_unlock(current);
  130. put_fs_struct(ofsp);
  131. }
  132. /*
  133. * At that point we are guaranteed to be the sole owner of
  134. * current->fs.
  135. */
  136. current->personality = personality;
  137. oep = current_thread_info()->exec_domain;
  138. current_thread_info()->exec_domain = ep;
  139. set_fs_altroot();
  140. module_put(oep->module);
  141. return 0;
  142. }
  143. int
  144. get_exec_domain_list(char *page)
  145. {
  146. struct exec_domain *ep;
  147. int len = 0;
  148. read_lock(&exec_domains_lock);
  149. for (ep = exec_domains; ep && len < PAGE_SIZE - 80; ep = ep->next)
  150. len += sprintf(page + len, "%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 (len);
  155. }
  156. asmlinkage long
  157. sys_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);