exec_domain.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /*
  59. * Disable the request_module here to avoid trying to
  60. * load the personality-8 module, which doesn't exist,
  61. * and results in selinux audit noise.
  62. * Disabling this here avoids folks adding module_request
  63. * to their sepolicy, which is maybe too generous
  64. */
  65. #if 0
  66. read_unlock(&exec_domains_lock);
  67. request_module("personality-%d", pers);
  68. read_lock(&exec_domains_lock);
  69. for (ep = exec_domains; ep; ep = ep->next) {
  70. if (pers >= ep->pers_low && pers <= ep->pers_high)
  71. if (try_module_get(ep->module))
  72. goto out;
  73. }
  74. #endif
  75. ep = &default_exec_domain;
  76. out:
  77. read_unlock(&exec_domains_lock);
  78. return (ep);
  79. }
  80. int
  81. register_exec_domain(struct exec_domain *ep)
  82. {
  83. struct exec_domain *tmp;
  84. int err = -EBUSY;
  85. if (ep == NULL)
  86. return -EINVAL;
  87. if (ep->next != NULL)
  88. return -EBUSY;
  89. write_lock(&exec_domains_lock);
  90. for (tmp = exec_domains; tmp; tmp = tmp->next) {
  91. if (tmp == ep)
  92. goto out;
  93. }
  94. ep->next = exec_domains;
  95. exec_domains = ep;
  96. err = 0;
  97. out:
  98. write_unlock(&exec_domains_lock);
  99. return (err);
  100. }
  101. int
  102. unregister_exec_domain(struct exec_domain *ep)
  103. {
  104. struct exec_domain **epp;
  105. epp = &exec_domains;
  106. write_lock(&exec_domains_lock);
  107. for (epp = &exec_domains; *epp; epp = &(*epp)->next) {
  108. if (ep == *epp)
  109. goto unregister;
  110. }
  111. write_unlock(&exec_domains_lock);
  112. return -EINVAL;
  113. unregister:
  114. *epp = ep->next;
  115. ep->next = NULL;
  116. write_unlock(&exec_domains_lock);
  117. return 0;
  118. }
  119. int __set_personality(unsigned int personality)
  120. {
  121. struct exec_domain *oep = current_thread_info()->exec_domain;
  122. current_thread_info()->exec_domain = lookup_exec_domain(personality);
  123. current->personality = personality;
  124. module_put(oep->module);
  125. return 0;
  126. }
  127. #ifdef CONFIG_PROC_FS
  128. static int execdomains_proc_show(struct seq_file *m, void *v)
  129. {
  130. struct exec_domain *ep;
  131. read_lock(&exec_domains_lock);
  132. for (ep = exec_domains; ep; ep = ep->next)
  133. seq_printf(m, "%d-%d\t%-16s\t[%s]\n",
  134. ep->pers_low, ep->pers_high, ep->name,
  135. module_name(ep->module));
  136. read_unlock(&exec_domains_lock);
  137. return 0;
  138. }
  139. static int execdomains_proc_open(struct inode *inode, struct file *file)
  140. {
  141. return single_open(file, execdomains_proc_show, NULL);
  142. }
  143. static const struct file_operations execdomains_proc_fops = {
  144. .open = execdomains_proc_open,
  145. .read = seq_read,
  146. .llseek = seq_lseek,
  147. .release = single_release,
  148. };
  149. static int __init proc_execdomains_init(void)
  150. {
  151. proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
  152. return 0;
  153. }
  154. module_init(proc_execdomains_init);
  155. #endif
  156. SYSCALL_DEFINE1(personality, unsigned int, personality)
  157. {
  158. unsigned int old = current->personality;
  159. if (personality != 0xffffffff)
  160. set_personality(personality);
  161. return old;
  162. }
  163. EXPORT_SYMBOL(register_exec_domain);
  164. EXPORT_SYMBOL(unregister_exec_domain);
  165. EXPORT_SYMBOL(__set_personality);