syscall_user_dispatch.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Collabora Ltd.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/prctl.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/syscall_user_dispatch.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/signal.h>
  11. #include <linux/elf.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <asm/syscall.h>
  15. #include "common.h"
  16. static void trigger_sigsys(struct pt_regs *regs)
  17. {
  18. struct kernel_siginfo info;
  19. clear_siginfo(&info);
  20. info.si_signo = SIGSYS;
  21. info.si_code = SYS_USER_DISPATCH;
  22. info.si_call_addr = (void __user *)KSTK_EIP(current);
  23. info.si_errno = 0;
  24. info.si_arch = syscall_get_arch(current);
  25. info.si_syscall = syscall_get_nr(current, regs);
  26. force_sig_info(&info);
  27. }
  28. bool syscall_user_dispatch(struct pt_regs *regs)
  29. {
  30. struct syscall_user_dispatch *sd = &current->syscall_dispatch;
  31. char state;
  32. if (likely(instruction_pointer(regs) - sd->offset < sd->len))
  33. return false;
  34. if (unlikely(arch_syscall_is_vdso_sigreturn(regs)))
  35. return false;
  36. if (likely(sd->selector)) {
  37. /*
  38. * access_ok() is performed once, at prctl time, when
  39. * the selector is loaded by userspace.
  40. */
  41. if (unlikely(__get_user(state, sd->selector))) {
  42. force_exit_sig(SIGSEGV);
  43. return true;
  44. }
  45. if (likely(state == SYSCALL_DISPATCH_FILTER_ALLOW))
  46. return false;
  47. if (state != SYSCALL_DISPATCH_FILTER_BLOCK) {
  48. force_exit_sig(SIGSYS);
  49. return true;
  50. }
  51. }
  52. sd->on_dispatch = true;
  53. syscall_rollback(current, regs);
  54. trigger_sigsys(regs);
  55. return true;
  56. }
  57. static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned long mode,
  58. unsigned long offset, unsigned long len,
  59. char __user *selector)
  60. {
  61. switch (mode) {
  62. case PR_SYS_DISPATCH_OFF:
  63. if (offset || len || selector)
  64. return -EINVAL;
  65. break;
  66. case PR_SYS_DISPATCH_ON:
  67. /*
  68. * Validate the direct dispatcher region just for basic
  69. * sanity against overflow and a 0-sized dispatcher
  70. * region. If the user is able to submit a syscall from
  71. * an address, that address is obviously valid.
  72. */
  73. if (offset && offset + len <= offset)
  74. return -EINVAL;
  75. /*
  76. * access_ok() will clear memory tags for tagged addresses
  77. * if current has memory tagging enabled.
  78. * To enable a tracer to set a tracees selector the
  79. * selector address must be untagged for access_ok(),
  80. * otherwise an untagged tracer will always fail to set a
  81. * tagged tracees selector.
  82. */
  83. if (selector && !access_ok(untagged_addr(selector), sizeof(*selector)))
  84. return -EFAULT;
  85. break;
  86. default:
  87. return -EINVAL;
  88. }
  89. task->syscall_dispatch.selector = selector;
  90. task->syscall_dispatch.offset = offset;
  91. task->syscall_dispatch.len = len;
  92. task->syscall_dispatch.on_dispatch = false;
  93. if (mode == PR_SYS_DISPATCH_ON)
  94. set_task_syscall_work(task, SYSCALL_USER_DISPATCH);
  95. else
  96. clear_task_syscall_work(task, SYSCALL_USER_DISPATCH);
  97. return 0;
  98. }
  99. int set_syscall_user_dispatch(unsigned long mode, unsigned long offset,
  100. unsigned long len, char __user *selector)
  101. {
  102. return task_set_syscall_user_dispatch(current, mode, offset, len, selector);
  103. }
  104. int syscall_user_dispatch_get_config(struct task_struct *task, unsigned long size,
  105. void __user *data)
  106. {
  107. struct syscall_user_dispatch *sd = &task->syscall_dispatch;
  108. struct ptrace_sud_config cfg;
  109. if (size != sizeof(cfg))
  110. return -EINVAL;
  111. if (test_task_syscall_work(task, SYSCALL_USER_DISPATCH))
  112. cfg.mode = PR_SYS_DISPATCH_ON;
  113. else
  114. cfg.mode = PR_SYS_DISPATCH_OFF;
  115. cfg.offset = sd->offset;
  116. cfg.len = sd->len;
  117. cfg.selector = (__u64)(uintptr_t)sd->selector;
  118. if (copy_to_user(data, &cfg, sizeof(cfg)))
  119. return -EFAULT;
  120. return 0;
  121. }
  122. int syscall_user_dispatch_set_config(struct task_struct *task, unsigned long size,
  123. void __user *data)
  124. {
  125. struct ptrace_sud_config cfg;
  126. if (size != sizeof(cfg))
  127. return -EINVAL;
  128. if (copy_from_user(&cfg, data, sizeof(cfg)))
  129. return -EFAULT;
  130. return task_set_syscall_user_dispatch(task, cfg.mode, cfg.offset, cfg.len,
  131. (char __user *)(uintptr_t)cfg.selector);
  132. }