audit.h 10 KB

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