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/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/sched.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/types.h>
  18. static void default_handler(int, struct pt_regs *);
  19. static struct exec_domain *exec_domains = &default_exec_domain;
  20. static DEFINE_RWLOCK(exec_domains_lock);
  21. static u_long ident_map[32] = {
  22. 0, 1, 2, 3, 4, 5, 6, 7,
  23. 8, 9, 10, 11, 12, 13, 14, 15,
  24. 16, 17, 18, 19, 20, 21, 22, 23,
  25. 24, 25, 26, 27, 28, 29, 30, 31
  26. };
  27. struct exec_domain default_exec_domain = {
  28. .name = "Linux", /* name */
  29. .handler = default_handler, /* lcall7 causes a seg fault. */
  30. .pers_low = 0, /* PER_LINUX personality. */
  31. .pers_high = 0, /* PER_LINUX personality. */
  32. .signal_map = ident_map, /* Identity map signals. */
  33. .signal_invmap = ident_map, /* - both ways. */
  34. };
  35. static void
  36. default_handler(int segment, struct pt_regs *regp)
  37. {
  38. set_personality(0);
  39. if (current_thread_info()->exec_domain->handler != default_handler)
  40. current_thread_info()->exec_domain->handler(segment, regp);
  41. else
  42. send_sig(SIGSEGV, current, 1);
  43. }
  44. static struct exec_domain *
  45. lookup_exec_domain(u_long personality)
  46. {
  47. struct exec_domain * ep;
  48. u_long pers = personality(personality);
  49. read_lock(&exec_domains_lock);
  50. for (ep = exec_domains; ep; ep = ep->next) {
  51. if (pers >= ep->pers_low && pers <= ep->pers_high)
  52. if (try_module_get(ep->module))
  53. goto out;
  54. }
  55. #ifdef CONFIG_KMOD
  56. read_unlock(&exec_domains_lock);
  57. request_module("personality-%ld", pers);
  58. read_lock(&exec_domains_lock);
  59. for (ep = exec_domains; ep; ep = ep->next) {
  60. if (pers >= ep->pers_low && pers <= ep->pers_high)
  61. if (try_module_get(ep->module))
  62. goto out;
  63. }
  64. #endif
  65. ep = &default_exec_domain;
  66. out:
  67. read_unlock(&exec_domains_lock);
  68. return (ep);
  69. }
  70. int
  71. register_exec_domain(struct exec_domain *ep)
  72. {
  73. struct exec_domain *tmp;
  74. int err = -EBUSY;
  75. if (ep == NULL)
  76. return -EINVAL;
  77. if (ep->next != NULL)
  78. return -EBUSY;
  79. write_lock(&exec_domains_lock);
  80. for (tmp = exec_domains; tmp; tmp = tmp->next) {
  81. if (tmp == ep)
  82. goto out;
  83. }
  84. ep->next = exec_domains;
  85. exec_domains = ep;
  86. err = 0;
  87. out:
  88. write_unlock(&exec_domains_lock);
  89. return (err);
  90. }
  91. int
  92. unregister_exec_domain(struct exec_domain *ep)
  93. {
  94. struct exec_domain **epp;
  95. epp = &exec_domains;
  96. write_lock(&exec_domains_lock);
  97. for (epp = &exec_domains; *epp; epp = &(*epp)->next) {
  98. if (ep == *epp)
  99. goto unregister;
  100. }
  101. write_unlock(&exec_domains_lock);
  102. return -EINVAL;
  103. unregister:
  104. *epp = ep->next;
  105. ep->next = NULL;
  106. write_unlock(&exec_domains_lock);
  107. return 0;
  108. }
  109. int
  110. __set_personality(u_long personality)
  111. {
  112. struct exec_domain *ep, *oep;
  113. ep = lookup_exec_domain(personality);
  114. if (ep == current_thread_info()->exec_domain) {
  115. current->personality = personality;
  116. module_put(ep->module);
  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);