kdp.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef __KDP_H__
  2. #define __KDP_H__
  3. #ifndef __ASSEMBLY__
  4. #include <linux/mm_types.h>
  5. #include <linux/stddef.h>
  6. #include <linux/uh.h>
  7. #define __kdp_ro __section(".kdp_ro")
  8. #define __lsm_ro_after_init_kdp __section(".kdp_ro")
  9. #define CRED_JAR_RO "cred_jar_ro"
  10. #define TSEC_JAR "tsec_jar"
  11. #define VFSMNT_JAR "vfsmnt_cache"
  12. struct kmem_cache;
  13. enum __KDP_CMD_ID {
  14. KDP_INIT = 0x00,
  15. //SET_VERIFIED = 0x01, // for BL. change to 0x00
  16. JARRO_TSEC_SIZE = 0x02,
  17. SET_SLAB_RO = 0x03,
  18. SET_FREEPTR = 0x04,
  19. PREPARE_RO_CRED = 0x05,
  20. SET_CRED_PGD = 0x06,
  21. SELINUX_CRED_FREE = 0x07,
  22. PGD_RWX = 0x08,
  23. MARK_PPT = 0x09,
  24. PROTECT_SELINUX_VAR = 0x0A,
  25. SET_CRED_UCOUNTS = 0x0B,
  26. NS_INIT = 0x10,
  27. SET_NS_BP = 0x11,
  28. SET_NS_DATA = 0x12,
  29. SET_NS_ROOT_SB = 0x13,
  30. SET_NS_SB_VFSMOUNT = 0x14,
  31. SET_NS_FLAGS = 0x15,
  32. #ifdef CONFIG_KDP_TEST
  33. TEST_INIT = 0x16,
  34. TEST_GET_PAR = 0x17,
  35. TEST_EXIT = 0x18,
  36. #endif
  37. SET_NS_USERNS = 0x20,
  38. };
  39. //kernel/cred.c
  40. enum __CRED_CMD_ID {
  41. CMD_COPY_CREDS = 0,
  42. CMD_COMMIT_CREDS,
  43. CMD_OVRD_CREDS,
  44. };
  45. enum _KMEM_TYPE {
  46. UNKNOWN_JAR_TYPE = 0,
  47. CRED_JAR_TYPE,
  48. TSEC_JAR_TYPE,
  49. VFSMNT_JAR_TYPE
  50. };
  51. struct kdp_init {
  52. u64 _srodata;
  53. u64 _erodata;
  54. u64 init_mm_pgd;
  55. u32 credSize;
  56. u32 sp_size;
  57. u32 pgd_mm;
  58. u32 uid_cred;
  59. u32 euid_cred;
  60. u32 gid_cred;
  61. u32 egid_cred;
  62. u32 bp_pgd_cred;
  63. u32 bp_task_cred;
  64. u32 type_cred;
  65. u32 security_cred;
  66. u32 usage_cred;
  67. u32 cred_task;
  68. u32 mm_task;
  69. u32 pid_task;
  70. u32 rp_task;
  71. u32 comm_task;
  72. u32 bp_cred_secptr;
  73. u32 task_threadinfo;
  74. u64 verifiedbootstate;
  75. struct {
  76. u64 selinux_enforcing_va;
  77. u64 ss_initialized_va;
  78. } selinux;
  79. };
  80. extern bool kdp_enable;
  81. extern void __init kdp_init(void);
  82. extern int get_kdp_kmem_cache_type(const char *name);
  83. extern bool is_kdp_kmem_cache(struct kmem_cache *s);
  84. extern bool is_kdp_kmem_cache_name(const char *name);
  85. #ifdef CONFIG_KDP_CRED
  86. /***************** KDP_CRED *****************/
  87. struct ro_rcu_head {
  88. /* RCU deletion */
  89. union {
  90. int non_rcu; /* Can we skip RCU deletion? */
  91. struct rcu_head rcu; /* RCU deletion hook */
  92. };
  93. void *bp_cred;
  94. void *reflected_cred;
  95. };
  96. struct cred_param {
  97. struct cred_kdp *cred;
  98. struct cred_kdp *cred_ro;
  99. void *use_cnt_ptr;
  100. void *sec_ptr;
  101. unsigned long type;
  102. union {
  103. void *task_ptr;
  104. u64 use_cnt;
  105. };
  106. };
  107. #define PROTECT_INIT 1
  108. #define PROTECT_KMEM 2
  109. #define GET_ROCRED_RCU(cred) \
  110. ( \
  111. ((u64)cred == (u64)&init_cred)? \
  112. (struct ro_rcu_head *)((atomic_t *)init_cred_kdp.use_cnt + 1): \
  113. (struct ro_rcu_head *)((atomic_t *)((struct cred_kdp *)cred)->use_cnt + 1) \
  114. )
  115. extern struct cred init_cred;
  116. extern struct cred_kdp init_cred_kdp;
  117. extern struct task_security_struct init_sec;
  118. struct filename;
  119. extern void __init kdp_cred_init(void);
  120. extern void __init kdp_do_early_param_setup(char *param, char *val);
  121. // match for kernel/cred.c function
  122. extern inline void set_cred_subscribers(struct cred *cred, int n);
  123. extern void put_rocred_rcu(struct rcu_head *rcu);
  124. extern void kdp_put_cred_rcu(struct cred *cred, void *put_cred_rcu);
  125. extern unsigned int kdp_get_usecount(struct cred *cred);
  126. extern void kdp_usecount_inc(struct cred *cred);
  127. extern unsigned int kdp_usecount_inc_not_zero(struct cred *cred);
  128. extern unsigned int kdp_usecount_dec_and_test(struct cred *cred);
  129. extern void kdp_set_cred_non_rcu(struct cred *cred, int val);
  130. // linux/cred.h
  131. extern int security_integrity_current(void);
  132. extern struct cred *prepare_ro_creds(struct cred *old, int kdp_cmd, u64 p);
  133. extern void kdp_assign_pgd(struct task_struct *p);
  134. extern inline int kdp_restrict_fork(struct filename *path);
  135. extern void kdp_free_security(unsigned long tsec);
  136. extern int is_kdp_protect_addr(unsigned long addr);
  137. extern void set_rocred_ucounts(struct cred *cred, struct ucounts *new_ucounts);
  138. #endif /* CONFIG_KDP_CRED */
  139. #ifdef CONFIG_KDP_NS
  140. /***************** KDP_NS *****************/
  141. struct ns_param {
  142. u32 ns_buff_size;
  143. u32 ns_size;
  144. u32 bp_offset;
  145. u32 sb_offset;
  146. u32 flag_offset;
  147. u32 data_offset;
  148. u32 userns_offset;
  149. };
  150. /* Populate all superblocks required for NS Protection */
  151. enum __KDP_SB {
  152. KDP_SB_ROOTFS = 0,
  153. KDP_SB_ODM,
  154. KDP_SB_SYS,
  155. KDP_SB_VENDOR,
  156. KDP_SB_ART,
  157. KDP_SB_CRYPT,
  158. KDP_SB_DEX2OAT,
  159. KDP_SB_ADBD,
  160. KDP_SB_SYS_EXT,
  161. KDP_SB_MAX
  162. };
  163. /* fs/pnode.h */
  164. #define IS_MNT_SHARED(m) (((struct kdp_mount *)(m))->mnt->mnt_flags & MNT_SHARED)
  165. #define CLEAR_MNT_SHARED(m) kdp_clear_mnt_flags(((struct kdp_mount *)(m))->mnt,MNT_SHARED)
  166. #define IS_MNT_UNBINDABLE(m) (((struct kdp_mount *)(m))->mnt->mnt_flags & MNT_UNBINDABLE)
  167. #define IS_MNT_MARKED(m) (((struct kdp_mount *)(m))->mnt->mnt_flags & MNT_MARKED)
  168. #define SET_MNT_MARK(m) kdp_set_mnt_flags(((struct kdp_mount *)(m))->mnt,MNT_MARKED)
  169. #define CLEAR_MNT_MARK(m) kdp_clear_mnt_flags(((struct kdp_mount *)(m))->mnt,MNT_MARKED)
  170. #define IS_MNT_LOCKED(m) (((struct kdp_mount *)(m))->mnt->mnt_flags & MNT_LOCKED)
  171. struct mount;
  172. struct vfsmount;
  173. struct super_block;
  174. struct path;
  175. extern void __init kdp_mnt_init(void);
  176. extern void __init kdp_init_mount_tree(struct vfsmount *mnt);
  177. extern int kdp_mnt_alloc_vfsmount(struct mount *mnt);
  178. extern void kdp_set_ns_data(struct vfsmount *mnt,void *data);
  179. inline extern void kdp_set_mnt_root_sb(struct vfsmount *mnt, struct dentry *mnt_root, struct super_block *mnt_sb);
  180. extern void kdp_set_mnt_flags(struct vfsmount *mnt, int flags);
  181. inline extern void kdp_clear_mnt_flags(struct vfsmount *mnt,int flags);
  182. inline extern void kdp_assign_mnt_flags(struct vfsmount *mnt, int flags);
  183. extern void kdp_set_mnt_userns(struct vfsmount *mnt, struct user_namespace *userns);
  184. extern bool is_kdp_vfsmnt_cache(unsigned long addr);
  185. extern void kdp_free_vfsmount(void *objp);
  186. #endif /* CONFIG_KDP_NS */
  187. #endif //__ASSEMBLY__
  188. #endif //__KDP_H__