audit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* audit -- definition of audit_context structure and supporting types
  3. *
  4. * Copyright 2003-2004 Red Hat, Inc.
  5. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  6. * Copyright 2005 IBM Corporation
  7. */
  8. #ifndef _KERNEL_AUDIT_H_
  9. #define _KERNEL_AUDIT_H_
  10. #include <linux/fs.h>
  11. #include <linux/audit.h>
  12. #include <linux/skbuff.h>
  13. #include <uapi/linux/mqueue.h>
  14. #include <linux/tty.h>
  15. #include <uapi/linux/openat2.h> // struct open_how
  16. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  17. * for saving names from getname(). If we get more names we will allocate
  18. * a name dynamically and also add those to the list anchored by names_list. */
  19. #define AUDIT_NAMES 5
  20. /* At task start time, the audit_state is set in the audit_context using
  21. a per-task filter. At syscall entry, the audit_state is augmented by
  22. the syscall filter. */
  23. enum audit_state {
  24. AUDIT_STATE_DISABLED, /* Do not create per-task audit_context.
  25. * No syscall-specific audit records can
  26. * be generated. */
  27. AUDIT_STATE_BUILD, /* Create the per-task audit_context,
  28. * and fill it in at syscall
  29. * entry time. This makes a full
  30. * syscall record available if some
  31. * other part of the kernel decides it
  32. * should be recorded. */
  33. AUDIT_STATE_RECORD /* Create the per-task audit_context,
  34. * always fill it in at syscall entry
  35. * time, and always write out the audit
  36. * record at syscall exit time. */
  37. };
  38. /* Rule lists */
  39. struct audit_watch;
  40. struct audit_fsnotify_mark;
  41. struct audit_tree;
  42. struct audit_chunk;
  43. struct audit_entry {
  44. struct list_head list;
  45. struct rcu_head rcu;
  46. struct audit_krule rule;
  47. };
  48. struct audit_cap_data {
  49. kernel_cap_t permitted;
  50. kernel_cap_t inheritable;
  51. union {
  52. unsigned int fE; /* effective bit of file cap */
  53. kernel_cap_t effective; /* effective set of process */
  54. };
  55. kernel_cap_t ambient;
  56. kuid_t rootid;
  57. };
  58. /* When fs/namei.c:getname() is called, we store the pointer in name and bump
  59. * the refcnt in the associated filename struct.
  60. *
  61. * Further, in fs/namei.c:path_lookup() we store the inode and device.
  62. */
  63. struct audit_names {
  64. struct list_head list; /* audit_context->names_list */
  65. struct filename *name;
  66. int name_len; /* number of chars to log */
  67. bool hidden; /* don't log this record */
  68. unsigned long ino;
  69. dev_t dev;
  70. umode_t mode;
  71. kuid_t uid;
  72. kgid_t gid;
  73. dev_t rdev;
  74. u32 osid;
  75. struct audit_cap_data fcap;
  76. unsigned int fcap_ver;
  77. unsigned char type; /* record type */
  78. /*
  79. * This was an allocated audit_names and not from the array of
  80. * names allocated in the task audit context. Thus this name
  81. * should be freed on syscall exit.
  82. */
  83. bool should_free;
  84. };
  85. struct audit_proctitle {
  86. int len; /* length of the cmdline field. */
  87. char *value; /* the cmdline field */
  88. };
  89. /* The per-task audit context. */
  90. struct audit_context {
  91. int dummy; /* must be the first element */
  92. enum {
  93. AUDIT_CTX_UNUSED, /* audit_context is currently unused */
  94. AUDIT_CTX_SYSCALL, /* in use by syscall */
  95. AUDIT_CTX_URING, /* in use by io_uring */
  96. } context;
  97. enum audit_state state, current_state;
  98. unsigned int serial; /* serial number for record */
  99. int major; /* syscall number */
  100. int uring_op; /* uring operation */
  101. struct timespec64 ctime; /* time of syscall entry */
  102. unsigned long argv[4]; /* syscall arguments */
  103. long return_code;/* syscall return code */
  104. u64 prio;
  105. int return_valid; /* return code is valid */
  106. /*
  107. * The names_list is the list of all audit_names collected during this
  108. * syscall. The first AUDIT_NAMES entries in the names_list will
  109. * actually be from the preallocated_names array for performance
  110. * reasons. Except during allocation they should never be referenced
  111. * through the preallocated_names array and should only be found/used
  112. * by running the names_list.
  113. */
  114. struct audit_names preallocated_names[AUDIT_NAMES];
  115. int name_count; /* total records in names_list */
  116. struct list_head names_list; /* struct audit_names->list anchor */
  117. char *filterkey; /* key for rule that triggered record */
  118. struct path pwd;
  119. struct audit_aux_data *aux;
  120. struct audit_aux_data *aux_pids;
  121. struct sockaddr_storage *sockaddr;
  122. size_t sockaddr_len;
  123. /* Save things to print about task_struct */
  124. pid_t ppid;
  125. kuid_t uid, euid, suid, fsuid;
  126. kgid_t gid, egid, sgid, fsgid;
  127. unsigned long personality;
  128. int arch;
  129. pid_t target_pid;
  130. kuid_t target_auid;
  131. kuid_t target_uid;
  132. unsigned int target_sessionid;
  133. u32 target_sid;
  134. char target_comm[TASK_COMM_LEN];
  135. struct audit_tree_refs *trees, *first_trees;
  136. struct list_head killed_trees;
  137. int tree_count;
  138. int type;
  139. union {
  140. struct {
  141. int nargs;
  142. long args[6];
  143. } socketcall;
  144. struct {
  145. kuid_t uid;
  146. kgid_t gid;
  147. umode_t mode;
  148. u32 osid;
  149. int has_perm;
  150. uid_t perm_uid;
  151. gid_t perm_gid;
  152. umode_t perm_mode;
  153. unsigned long qbytes;
  154. } ipc;
  155. struct {
  156. mqd_t mqdes;
  157. struct mq_attr mqstat;
  158. } mq_getsetattr;
  159. struct {
  160. mqd_t mqdes;
  161. int sigev_signo;
  162. } mq_notify;
  163. struct {
  164. mqd_t mqdes;
  165. size_t msg_len;
  166. unsigned int msg_prio;
  167. struct timespec64 abs_timeout;
  168. } mq_sendrecv;
  169. struct {
  170. int oflag;
  171. umode_t mode;
  172. struct mq_attr attr;
  173. } mq_open;
  174. struct {
  175. pid_t pid;
  176. struct audit_cap_data cap;
  177. } capset;
  178. struct {
  179. int fd;
  180. int flags;
  181. } mmap;
  182. struct open_how openat2;
  183. struct {
  184. int argc;
  185. } execve;
  186. struct {
  187. char *name;
  188. } module;
  189. struct {
  190. struct audit_ntp_data ntp_data;
  191. struct timespec64 tk_injoffset;
  192. } time;
  193. };
  194. int fds[2];
  195. struct audit_proctitle proctitle;
  196. };
  197. extern bool audit_ever_enabled;
  198. extern void audit_log_session_info(struct audit_buffer *ab);
  199. extern int auditd_test_task(struct task_struct *task);
  200. #define AUDIT_INODE_BUCKETS 32
  201. extern struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
  202. static inline int audit_hash_ino(u32 ino)
  203. {
  204. return (ino & (AUDIT_INODE_BUCKETS-1));
  205. }
  206. /* Indicates that audit should log the full pathname. */
  207. #define AUDIT_NAME_FULL -1
  208. extern int audit_match_class(int class, unsigned syscall);
  209. extern int audit_comparator(const u32 left, const u32 op, const u32 right);
  210. extern int audit_uid_comparator(kuid_t left, u32 op, kuid_t right);
  211. extern int audit_gid_comparator(kgid_t left, u32 op, kgid_t right);
  212. extern int parent_len(const char *path);
  213. extern int audit_compare_dname_path(const struct qstr *dname, const char *path, int plen);
  214. extern struct sk_buff *audit_make_reply(int seq, int type, int done, int multi,
  215. const void *payload, int size);
  216. extern void audit_panic(const char *message);
  217. struct audit_netlink_list {
  218. __u32 portid;
  219. struct net *net;
  220. struct sk_buff_head q;
  221. };
  222. int audit_send_list_thread(void *_dest);
  223. extern struct mutex audit_filter_mutex;
  224. extern int audit_del_rule(struct audit_entry *entry);
  225. extern void audit_free_rule_rcu(struct rcu_head *head);
  226. extern struct list_head audit_filter_list[];
  227. extern struct audit_entry *audit_dupe_rule(struct audit_krule *old);
  228. extern void audit_log_d_path_exe(struct audit_buffer *ab,
  229. struct mm_struct *mm);
  230. extern struct tty_struct *audit_get_tty(void);
  231. extern void audit_put_tty(struct tty_struct *tty);
  232. /* audit watch/mark/tree functions */
  233. #ifdef CONFIG_AUDITSYSCALL
  234. extern unsigned int audit_serial(void);
  235. extern int auditsc_get_stamp(struct audit_context *ctx,
  236. struct timespec64 *t, unsigned int *serial);
  237. extern void audit_put_watch(struct audit_watch *watch);
  238. extern void audit_get_watch(struct audit_watch *watch);
  239. extern int audit_to_watch(struct audit_krule *krule, char *path, int len,
  240. u32 op);
  241. extern int audit_add_watch(struct audit_krule *krule, struct list_head **list);
  242. extern void audit_remove_watch_rule(struct audit_krule *krule);
  243. extern char *audit_watch_path(struct audit_watch *watch);
  244. extern int audit_watch_compare(struct audit_watch *watch, unsigned long ino,
  245. dev_t dev);
  246. extern struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule,
  247. char *pathname, int len);
  248. extern char *audit_mark_path(struct audit_fsnotify_mark *mark);
  249. extern void audit_remove_mark(struct audit_fsnotify_mark *audit_mark);
  250. extern void audit_remove_mark_rule(struct audit_krule *krule);
  251. extern int audit_mark_compare(struct audit_fsnotify_mark *mark,
  252. unsigned long ino, dev_t dev);
  253. extern int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old);
  254. extern int audit_exe_compare(struct task_struct *tsk,
  255. struct audit_fsnotify_mark *mark);
  256. extern struct audit_chunk *audit_tree_lookup(const struct inode *inode);
  257. extern void audit_put_chunk(struct audit_chunk *chunk);
  258. extern bool audit_tree_match(struct audit_chunk *chunk,
  259. struct audit_tree *tree);
  260. extern int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op);
  261. extern int audit_add_tree_rule(struct audit_krule *rule);
  262. extern int audit_remove_tree_rule(struct audit_krule *rule);
  263. extern void audit_trim_trees(void);
  264. extern int audit_tag_tree(char *old, char *new);
  265. extern const char *audit_tree_path(struct audit_tree *tree);
  266. extern void audit_put_tree(struct audit_tree *tree);
  267. extern void audit_kill_trees(struct audit_context *context);
  268. extern int audit_signal_info_syscall(struct task_struct *t);
  269. extern void audit_filter_inodes(struct task_struct *tsk,
  270. struct audit_context *ctx);
  271. extern struct list_head *audit_killed_trees(void);
  272. #else /* CONFIG_AUDITSYSCALL */
  273. #define auditsc_get_stamp(c, t, s) 0
  274. #define audit_put_watch(w) do { } while (0)
  275. #define audit_get_watch(w) do { } while (0)
  276. #define audit_to_watch(k, p, l, o) (-EINVAL)
  277. #define audit_add_watch(k, l) (-EINVAL)
  278. #define audit_remove_watch_rule(k) BUG()
  279. #define audit_watch_path(w) ""
  280. #define audit_watch_compare(w, i, d) 0
  281. #define audit_alloc_mark(k, p, l) (ERR_PTR(-EINVAL))
  282. #define audit_mark_path(m) ""
  283. #define audit_remove_mark(m) do { } while (0)
  284. #define audit_remove_mark_rule(k) do { } while (0)
  285. #define audit_mark_compare(m, i, d) 0
  286. #define audit_exe_compare(t, m) (-EINVAL)
  287. #define audit_dupe_exe(n, o) (-EINVAL)
  288. #define audit_remove_tree_rule(rule) BUG()
  289. #define audit_add_tree_rule(rule) -EINVAL
  290. #define audit_make_tree(rule, str, op) -EINVAL
  291. #define audit_trim_trees() do { } while (0)
  292. #define audit_put_tree(tree) do { } while (0)
  293. #define audit_tag_tree(old, new) -EINVAL
  294. #define audit_tree_path(rule) "" /* never called */
  295. #define audit_kill_trees(context) BUG()
  296. static inline int audit_signal_info_syscall(struct task_struct *t)
  297. {
  298. return 0;
  299. }
  300. #define audit_filter_inodes(t, c) AUDIT_STATE_DISABLED
  301. #endif /* CONFIG_AUDITSYSCALL */
  302. extern char *audit_unpack_string(void **bufp, size_t *remain, size_t len);
  303. extern int audit_filter(int msgtype, unsigned int listtype);
  304. extern void audit_ctl_lock(void);
  305. extern void audit_ctl_unlock(void);
  306. #endif