exec_domain.c 4.3 KB

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