exec_domain.c 4.3 KB

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