extable.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on arch/arm/mm/extable.c
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/extable.h>
  7. #include <linux/uaccess.h>
  8. #include <asm/asm-extable.h>
  9. #include <asm/esr.h>
  10. #include <asm/ptrace.h>
  11. static bool cpy_faulted_on_uaccess(const struct exception_table_entry *ex,
  12. unsigned long esr)
  13. {
  14. bool uaccess_is_write = FIELD_GET(EX_DATA_UACCESS_WRITE, ex->data);
  15. bool fault_on_write = esr & ESR_ELx_WNR;
  16. return uaccess_is_write == fault_on_write;
  17. }
  18. bool insn_may_access_user(unsigned long addr, unsigned long esr)
  19. {
  20. const struct exception_table_entry *ex = search_exception_tables(addr);
  21. if (!ex)
  22. return false;
  23. switch (ex->type) {
  24. case EX_TYPE_UACCESS_CPY:
  25. return cpy_faulted_on_uaccess(ex, esr);
  26. default:
  27. return true;
  28. }
  29. }
  30. static inline unsigned long
  31. get_ex_fixup(const struct exception_table_entry *ex)
  32. {
  33. return ((unsigned long)&ex->fixup + ex->fixup);
  34. }
  35. static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
  36. struct pt_regs *regs)
  37. {
  38. int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
  39. int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);
  40. pt_regs_write_reg(regs, reg_err, -EFAULT);
  41. pt_regs_write_reg(regs, reg_zero, 0);
  42. regs->pc = get_ex_fixup(ex);
  43. return true;
  44. }
  45. static bool ex_handler_uaccess_cpy(const struct exception_table_entry *ex,
  46. struct pt_regs *regs, unsigned long esr)
  47. {
  48. /* Do not fix up faults on kernel memory accesses */
  49. if (!cpy_faulted_on_uaccess(ex, esr))
  50. return false;
  51. regs->pc = get_ex_fixup(ex);
  52. return true;
  53. }
  54. static bool
  55. ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex,
  56. struct pt_regs *regs)
  57. {
  58. int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->data);
  59. int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->data);
  60. unsigned long data, addr, offset;
  61. addr = pt_regs_read_reg(regs, reg_addr);
  62. offset = addr & 0x7UL;
  63. addr &= ~0x7UL;
  64. data = *(unsigned long*)addr;
  65. #ifndef __AARCH64EB__
  66. data >>= 8 * offset;
  67. #else
  68. data <<= 8 * offset;
  69. #endif
  70. pt_regs_write_reg(regs, reg_data, data);
  71. regs->pc = get_ex_fixup(ex);
  72. return true;
  73. }
  74. bool fixup_exception(struct pt_regs *regs, unsigned long esr)
  75. {
  76. const struct exception_table_entry *ex;
  77. ex = search_exception_tables(instruction_pointer(regs));
  78. if (!ex)
  79. return false;
  80. switch (ex->type) {
  81. case EX_TYPE_BPF:
  82. return ex_handler_bpf(ex, regs);
  83. case EX_TYPE_UACCESS_ERR_ZERO:
  84. case EX_TYPE_KACCESS_ERR_ZERO:
  85. return ex_handler_uaccess_err_zero(ex, regs);
  86. case EX_TYPE_UACCESS_CPY:
  87. return ex_handler_uaccess_cpy(ex, regs, esr);
  88. case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
  89. return ex_handler_load_unaligned_zeropad(ex, regs);
  90. }
  91. BUG();
  92. }