systrace.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* $OpenBSD: systrace.h,v 1.25 2015/01/20 01:48:13 deraadt Exp $ */
  2. /*
  3. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Niels Provos.
  17. * 4. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _SYSTRACE_H_
  32. #define _SYSTRACE_H_
  33. #include <sys/syslimits.h>
  34. #include <sys/ioccom.h>
  35. #include <sys/rwlock.h>
  36. #define SYSTR_EMULEN 8 /* sync with sys proc */
  37. struct str_msg_emul {
  38. char emul[SYSTR_EMULEN];
  39. };
  40. struct str_msg_ugid {
  41. uid_t uid;
  42. gid_t gid;
  43. };
  44. struct str_msg_execve {
  45. char path[PATH_MAX];
  46. };
  47. #define SYSTR_MAX_POLICIES 64
  48. #define SYSTR_MAXARGS 64
  49. #define SYSTR_MAXFNAME 8
  50. #define SYSTR_MAXINJECTS 8
  51. #define SYSTR_MAXREPLEN 2048
  52. struct str_msg_ask {
  53. int code;
  54. int argsize;
  55. register_t args[SYSTR_MAXARGS];
  56. register_t rval[2];
  57. int result;
  58. };
  59. /* Queued on fork or exit of a process */
  60. struct str_msg_child {
  61. pid_t new_pid;
  62. };
  63. #define SYSTR_MSG_ASK 1
  64. #define SYSTR_MSG_RES 2
  65. #define SYSTR_MSG_EMUL 3
  66. #define SYSTR_MSG_CHILD 4
  67. #define SYSTR_MSG_UGID 5
  68. #define SYSTR_MSG_POLICYFREE 6
  69. #define SYSTR_MSG_EXECVE 7
  70. #define SYSTR_MSG_NOPROCESS(x) \
  71. ((x)->msg.msg_type == SYSTR_MSG_CHILD || \
  72. (x)->msg.msg_type == SYSTR_MSG_POLICYFREE)
  73. struct str_message {
  74. int msg_type;
  75. pid_t msg_pid;
  76. u_int16_t msg_seqnr; /* answer has to match seqnr */
  77. short msg_policy;
  78. union {
  79. struct str_msg_emul msg_emul;
  80. struct str_msg_ugid msg_ugid;
  81. struct str_msg_ask msg_ask;
  82. struct str_msg_child msg_child;
  83. struct str_msg_execve msg_execve;
  84. } msg_data;
  85. };
  86. struct systrace_answer {
  87. pid_t stra_pid;
  88. u_int16_t stra_seqnr;
  89. short reserved;
  90. uid_t stra_seteuid; /* elevated privileges for system call */
  91. uid_t stra_setegid;
  92. int stra_policy;
  93. int stra_error;
  94. int stra_flags;
  95. };
  96. struct systrace_scriptname {
  97. pid_t sn_pid;
  98. char sn_scriptname[PATH_MAX];
  99. };
  100. #define SYSTR_READ 1
  101. #define SYSTR_WRITE 2
  102. struct systrace_io {
  103. pid_t strio_pid;
  104. int strio_op;
  105. void *strio_offs;
  106. void *strio_addr;
  107. size_t strio_len;
  108. };
  109. #define SYSTR_POLICY_NEW 1
  110. #define SYSTR_POLICY_ASSIGN 2
  111. #define SYSTR_POLICY_MODIFY 3
  112. struct systrace_policy {
  113. int strp_op;
  114. int strp_num;
  115. union {
  116. struct {
  117. short code;
  118. short policy;
  119. } assign;
  120. pid_t pid;
  121. int maxents;
  122. } strp_data;
  123. };
  124. #define strp_pid strp_data.pid
  125. #define strp_maxents strp_data.maxents
  126. #define strp_code strp_data.assign.code
  127. #define strp_policy strp_data.assign.policy
  128. #define SYSTR_NOLINKS 1
  129. struct systrace_replace {
  130. pid_t strr_pid;
  131. u_int16_t strr_seqnr;
  132. int16_t reserved;
  133. int strr_nrepl;
  134. caddr_t strr_base; /* Base memory */
  135. size_t strr_len; /* Length of memory */
  136. int strr_argind[SYSTR_MAXARGS];
  137. size_t strr_off[SYSTR_MAXARGS];
  138. size_t strr_offlen[SYSTR_MAXARGS];
  139. int32_t strr_flags[SYSTR_MAXARGS];
  140. };
  141. struct systrace_inject {
  142. /* On return, this contains the stackgap address. */
  143. caddr_t stri_addr;
  144. size_t stri_len;
  145. pid_t stri_pid;
  146. };
  147. struct systrace_getcwd {
  148. pid_t strgd_pid;
  149. int strgd_atfd;
  150. };
  151. #define STRIOCCLONE _IOR('s', 100, int)
  152. #define SYSTR_CLONE STRIOCCLONE
  153. #define STRIOCATTACH _IOW('s', 101, pid_t)
  154. #define STRIOCDETACH _IOW('s', 102, pid_t)
  155. #define STRIOCANSWER _IOW('s', 103, struct systrace_answer)
  156. #define STRIOCIO _IOWR('s', 104, struct systrace_io)
  157. #define STRIOCPOLICY _IOWR('s', 105, struct systrace_policy)
  158. #define STRIOCGETCWD _IOW('s', 106, struct systrace_getcwd)
  159. #define STRIOCRESCWD _IO('s', 107)
  160. #define STRIOCREPORT _IOW('s', 108, pid_t)
  161. #define STRIOCREPLACE _IOW('s', 109, struct systrace_replace)
  162. #define STRIOCSCRIPTNAME _IOW('s', 110, struct systrace_scriptname)
  163. #define STRIOCINJECT _IOWR('s', 111, struct systrace_inject)
  164. #define SYSTR_POLICY_ASK 0
  165. #define SYSTR_POLICY_PERMIT 1
  166. #define SYSTR_POLICY_NEVER 2
  167. #define SYSTR_POLICY_KILL 3
  168. #define SYSTR_FLAGS_RESULT 0x001
  169. #define SYSTR_FLAGS_SETEUID 0x002
  170. #define SYSTR_FLAGS_SETEGID 0x004
  171. #ifdef _KERNEL
  172. #include <sys/namei.h>
  173. struct str_process;
  174. struct fsystrace {
  175. struct rwlock lock;
  176. struct selinfo si;
  177. TAILQ_HEAD(strprocessq, str_process) processes;
  178. int nprocesses;
  179. TAILQ_HEAD(strpolicyq, str_policy) policies;
  180. struct strprocessq messages;
  181. int npolicynr;
  182. int npolicies;
  183. int issuser;
  184. uid_t p_ruid;
  185. gid_t p_rgid;
  186. /* cwd magic */
  187. pid_t fd_pid;
  188. struct vnode *fd_cdir;
  189. struct vnode *fd_rdir;
  190. };
  191. /* Internal prototypes */
  192. void systrace_namei(struct nameidata *);
  193. int systrace_redirect(int, struct proc *, void *, register_t *);
  194. void systrace_exit(struct proc *);
  195. struct str_process *systrace_getproc(void);
  196. void systrace_freeproc(struct str_process *);
  197. void systrace_fork(struct proc *, struct proc *, struct str_process *newstrp);
  198. void systrace_execve0(struct proc *);
  199. void systrace_execve1(char *, struct proc *);
  200. int systrace_scriptname(struct proc *, char *);
  201. #endif /* _KERNEL */
  202. #endif /* _SYSTRACE_H_ */