audit.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* audit.h -- Auditing support
  2. *
  3. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  21. *
  22. */
  23. #ifndef _LINUX_AUDIT_H_
  24. #define _LINUX_AUDIT_H_
  25. #include <linux/sched.h>
  26. #include <uapi/linux/audit.h>
  27. struct audit_sig_info {
  28. uid_t uid;
  29. pid_t pid;
  30. char ctx[0];
  31. };
  32. struct audit_buffer;
  33. struct audit_context;
  34. struct inode;
  35. struct netlink_skb_parms;
  36. struct path;
  37. struct linux_binprm;
  38. struct mq_attr;
  39. struct mqstat;
  40. struct audit_watch;
  41. struct audit_tree;
  42. struct audit_krule {
  43. int vers_ops;
  44. u32 flags;
  45. u32 listnr;
  46. u32 action;
  47. u32 mask[AUDIT_BITMASK_SIZE];
  48. u32 buflen; /* for data alloc on list rules */
  49. u32 field_count;
  50. char *filterkey; /* ties events to rules */
  51. struct audit_field *fields;
  52. struct audit_field *arch_f; /* quick access to arch field */
  53. struct audit_field *inode_f; /* quick access to an inode field */
  54. struct audit_watch *watch; /* associated watch */
  55. struct audit_tree *tree; /* associated watched tree */
  56. struct list_head rlist; /* entry in audit_{watch,tree}.rules list */
  57. struct list_head list; /* for AUDIT_LIST* purposes only */
  58. u64 prio;
  59. };
  60. struct audit_field {
  61. u32 type;
  62. u32 val;
  63. kuid_t uid;
  64. kgid_t gid;
  65. u32 op;
  66. char *lsm_str;
  67. void *lsm_rule;
  68. };
  69. extern int __init audit_register_class(int class, unsigned *list);
  70. extern int audit_classify_syscall(int abi, unsigned syscall);
  71. extern int audit_classify_arch(int arch);
  72. /* audit_names->type values */
  73. #define AUDIT_TYPE_UNKNOWN 0 /* we don't know yet */
  74. #define AUDIT_TYPE_NORMAL 1 /* a "normal" audit record */
  75. #define AUDIT_TYPE_PARENT 2 /* a parent audit record */
  76. #define AUDIT_TYPE_CHILD_DELETE 3 /* a child being deleted */
  77. #define AUDIT_TYPE_CHILD_CREATE 4 /* a child being created */
  78. struct filename;
  79. #ifdef CONFIG_AUDITSYSCALL
  80. /* These are defined in auditsc.c */
  81. /* Public API */
  82. extern int audit_alloc(struct task_struct *task);
  83. extern void __audit_free(struct task_struct *task);
  84. extern void __audit_syscall_entry(int arch,
  85. int major, unsigned long a0, unsigned long a1,
  86. unsigned long a2, unsigned long a3);
  87. extern void __audit_syscall_exit(int ret_success, long ret_value);
  88. extern struct filename *__audit_reusename(const __user char *uptr);
  89. extern void __audit_getname(struct filename *name);
  90. extern void audit_putname(struct filename *name);
  91. extern void __audit_inode(struct filename *name, const struct dentry *dentry,
  92. unsigned int parent);
  93. extern void __audit_inode_child(const struct inode *parent,
  94. const struct dentry *dentry,
  95. const unsigned char type);
  96. extern void __audit_seccomp(unsigned long syscall, long signr, int code);
  97. extern void __audit_ptrace(struct task_struct *t);
  98. static inline int audit_dummy_context(void)
  99. {
  100. void *p = current->audit_context;
  101. return !p || *(int *)p;
  102. }
  103. static inline void audit_free(struct task_struct *task)
  104. {
  105. if (unlikely(task->audit_context))
  106. __audit_free(task);
  107. }
  108. static inline void audit_syscall_entry(int arch, int major, unsigned long a0,
  109. unsigned long a1, unsigned long a2,
  110. unsigned long a3)
  111. {
  112. if (unlikely(!audit_dummy_context()))
  113. __audit_syscall_entry(arch, major, a0, a1, a2, a3);
  114. }
  115. static inline void audit_syscall_exit(void *pt_regs)
  116. {
  117. if (unlikely(current->audit_context)) {
  118. int success = is_syscall_success(pt_regs);
  119. int return_code = regs_return_value(pt_regs);
  120. __audit_syscall_exit(success, return_code);
  121. }
  122. }
  123. static inline struct filename *audit_reusename(const __user char *name)
  124. {
  125. if (unlikely(!audit_dummy_context()))
  126. return __audit_reusename(name);
  127. return NULL;
  128. }
  129. static inline void audit_getname(struct filename *name)
  130. {
  131. if (unlikely(!audit_dummy_context()))
  132. __audit_getname(name);
  133. }
  134. static inline void audit_inode(struct filename *name, const struct dentry *dentry,
  135. unsigned int parent) {
  136. if (unlikely(!audit_dummy_context()))
  137. __audit_inode(name, dentry, parent);
  138. }
  139. static inline void audit_inode_child(const struct inode *parent,
  140. const struct dentry *dentry,
  141. const unsigned char type) {
  142. if (unlikely(!audit_dummy_context()))
  143. __audit_inode_child(parent, dentry, type);
  144. }
  145. void audit_core_dumps(long signr);
  146. static inline void audit_seccomp(unsigned long syscall, long signr, int code)
  147. {
  148. if (unlikely(!audit_dummy_context()))
  149. __audit_seccomp(syscall, signr, code);
  150. }
  151. static inline void audit_ptrace(struct task_struct *t)
  152. {
  153. if (unlikely(!audit_dummy_context()))
  154. __audit_ptrace(t);
  155. }
  156. /* Private API (for audit.c only) */
  157. extern unsigned int audit_serial(void);
  158. extern int auditsc_get_stamp(struct audit_context *ctx,
  159. struct timespec *t, unsigned int *serial);
  160. extern int audit_set_loginuid(kuid_t loginuid);
  161. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  162. {
  163. return tsk->loginuid;
  164. }
  165. static inline int audit_get_sessionid(struct task_struct *tsk)
  166. {
  167. return tsk->sessionid;
  168. }
  169. extern void audit_log_task_context(struct audit_buffer *ab);
  170. extern void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk);
  171. extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
  172. extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
  173. extern int __audit_bprm(struct linux_binprm *bprm);
  174. extern void __audit_socketcall(int nargs, unsigned long *args);
  175. extern int __audit_sockaddr(int len, void *addr);
  176. extern void __audit_fd_pair(int fd1, int fd2);
  177. extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr);
  178. extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout);
  179. extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification);
  180. extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
  181. extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  182. const struct cred *new,
  183. const struct cred *old);
  184. extern void __audit_log_capset(pid_t pid, const struct cred *new, const struct cred *old);
  185. extern void __audit_mmap_fd(int fd, int flags);
  186. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  187. {
  188. if (unlikely(!audit_dummy_context()))
  189. __audit_ipc_obj(ipcp);
  190. }
  191. static inline void audit_fd_pair(int fd1, int fd2)
  192. {
  193. if (unlikely(!audit_dummy_context()))
  194. __audit_fd_pair(fd1, fd2);
  195. }
  196. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  197. {
  198. if (unlikely(!audit_dummy_context()))
  199. __audit_ipc_set_perm(qbytes, uid, gid, mode);
  200. }
  201. static inline int audit_bprm(struct linux_binprm *bprm)
  202. {
  203. if (unlikely(!audit_dummy_context()))
  204. return __audit_bprm(bprm);
  205. return 0;
  206. }
  207. static inline void audit_socketcall(int nargs, unsigned long *args)
  208. {
  209. if (unlikely(!audit_dummy_context()))
  210. __audit_socketcall(nargs, args);
  211. }
  212. static inline int audit_sockaddr(int len, void *addr)
  213. {
  214. if (unlikely(!audit_dummy_context()))
  215. return __audit_sockaddr(len, addr);
  216. return 0;
  217. }
  218. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  219. {
  220. if (unlikely(!audit_dummy_context()))
  221. __audit_mq_open(oflag, mode, attr);
  222. }
  223. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout)
  224. {
  225. if (unlikely(!audit_dummy_context()))
  226. __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
  227. }
  228. static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  229. {
  230. if (unlikely(!audit_dummy_context()))
  231. __audit_mq_notify(mqdes, notification);
  232. }
  233. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  234. {
  235. if (unlikely(!audit_dummy_context()))
  236. __audit_mq_getsetattr(mqdes, mqstat);
  237. }
  238. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  239. const struct cred *new,
  240. const struct cred *old)
  241. {
  242. if (unlikely(!audit_dummy_context()))
  243. return __audit_log_bprm_fcaps(bprm, new, old);
  244. return 0;
  245. }
  246. static inline void audit_log_capset(pid_t pid, const struct cred *new,
  247. const struct cred *old)
  248. {
  249. if (unlikely(!audit_dummy_context()))
  250. __audit_log_capset(pid, new, old);
  251. }
  252. static inline void audit_mmap_fd(int fd, int flags)
  253. {
  254. if (unlikely(!audit_dummy_context()))
  255. __audit_mmap_fd(fd, flags);
  256. }
  257. extern int audit_n_rules;
  258. extern int audit_signals;
  259. #else /* CONFIG_AUDITSYSCALL */
  260. static inline int audit_alloc(struct task_struct *task)
  261. {
  262. return 0;
  263. }
  264. static inline void audit_free(struct task_struct *task)
  265. { }
  266. static inline void audit_syscall_entry(int arch, int major, unsigned long a0,
  267. unsigned long a1, unsigned long a2,
  268. unsigned long a3)
  269. { }
  270. static inline void audit_syscall_exit(void *pt_regs)
  271. { }
  272. static inline int audit_dummy_context(void)
  273. {
  274. return 1;
  275. }
  276. static inline struct filename *audit_reusename(const __user char *name)
  277. {
  278. return NULL;
  279. }
  280. static inline void audit_getname(struct filename *name)
  281. { }
  282. static inline void audit_putname(struct filename *name)
  283. { }
  284. static inline void __audit_inode(struct filename *name,
  285. const struct dentry *dentry,
  286. unsigned int parent)
  287. { }
  288. static inline void __audit_inode_child(const struct inode *parent,
  289. const struct dentry *dentry,
  290. const unsigned char type)
  291. { }
  292. static inline void audit_inode(struct filename *name,
  293. const struct dentry *dentry,
  294. unsigned int parent)
  295. { }
  296. static inline void audit_inode_child(const struct inode *parent,
  297. const struct dentry *dentry,
  298. const unsigned char type)
  299. { }
  300. static inline void audit_core_dumps(long signr)
  301. { }
  302. static inline void __audit_seccomp(unsigned long syscall, long signr, int code)
  303. { }
  304. static inline void audit_seccomp(unsigned long syscall, long signr, int code)
  305. { }
  306. static inline int auditsc_get_stamp(struct audit_context *ctx,
  307. struct timespec *t, unsigned int *serial)
  308. {
  309. return 0;
  310. }
  311. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  312. {
  313. return INVALID_UID;
  314. }
  315. static inline int audit_get_sessionid(struct task_struct *tsk)
  316. {
  317. return -1;
  318. }
  319. static inline void audit_log_task_context(struct audit_buffer *ab)
  320. { }
  321. static inline void audit_log_task_info(struct audit_buffer *ab,
  322. struct task_struct *tsk)
  323. { }
  324. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  325. { }
  326. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
  327. gid_t gid, umode_t mode)
  328. { }
  329. static inline int audit_bprm(struct linux_binprm *bprm)
  330. {
  331. return 0;
  332. }
  333. static inline void audit_socketcall(int nargs, unsigned long *args)
  334. { }
  335. static inline void audit_fd_pair(int fd1, int fd2)
  336. { }
  337. static inline int audit_sockaddr(int len, void *addr)
  338. {
  339. return 0;
  340. }
  341. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  342. { }
  343. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len,
  344. unsigned int msg_prio,
  345. const struct timespec *abs_timeout)
  346. { }
  347. static inline void audit_mq_notify(mqd_t mqdes,
  348. const struct sigevent *notification)
  349. { }
  350. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  351. { }
  352. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  353. const struct cred *new,
  354. const struct cred *old)
  355. {
  356. return 0;
  357. }
  358. static inline void audit_log_capset(pid_t pid, const struct cred *new,
  359. const struct cred *old)
  360. { }
  361. static inline void audit_mmap_fd(int fd, int flags)
  362. { }
  363. static inline void audit_ptrace(struct task_struct *t)
  364. { }
  365. #define audit_n_rules 0
  366. #define audit_signals 0
  367. #endif /* CONFIG_AUDITSYSCALL */
  368. #ifdef CONFIG_AUDIT
  369. /* These are defined in audit.c */
  370. /* Public API */
  371. extern __printf(4, 5)
  372. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  373. const char *fmt, ...);
  374. extern struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type);
  375. extern __printf(2, 3)
  376. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...);
  377. extern void audit_log_end(struct audit_buffer *ab);
  378. extern int audit_string_contains_control(const char *string,
  379. size_t len);
  380. extern void audit_log_n_hex(struct audit_buffer *ab,
  381. const unsigned char *buf,
  382. size_t len);
  383. extern void audit_log_n_string(struct audit_buffer *ab,
  384. const char *buf,
  385. size_t n);
  386. extern void audit_log_n_untrustedstring(struct audit_buffer *ab,
  387. const char *string,
  388. size_t n);
  389. extern void audit_log_untrustedstring(struct audit_buffer *ab,
  390. const char *string);
  391. extern void audit_log_d_path(struct audit_buffer *ab,
  392. const char *prefix,
  393. const struct path *path);
  394. extern void audit_log_key(struct audit_buffer *ab,
  395. char *key);
  396. extern void audit_log_link_denied(const char *operation,
  397. struct path *link);
  398. extern void audit_log_lost(const char *message);
  399. #ifdef CONFIG_SECURITY
  400. extern void audit_log_secctx(struct audit_buffer *ab, u32 secid);
  401. #else
  402. static inline void audit_log_secctx(struct audit_buffer *ab, u32 secid)
  403. { }
  404. #endif
  405. extern int audit_update_lsm_rules(void);
  406. /* Private API (for audit.c only) */
  407. extern int audit_filter_user(void);
  408. extern int audit_filter_type(int type);
  409. extern int audit_receive_filter(int type, int pid, int seq,
  410. void *data, size_t datasz, kuid_t loginuid,
  411. u32 sessionid, u32 sid);
  412. extern int audit_enabled;
  413. #else /* CONFIG_AUDIT */
  414. static inline __printf(4, 5)
  415. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  416. const char *fmt, ...)
  417. { }
  418. static inline struct audit_buffer *audit_log_start(struct audit_context *ctx,
  419. gfp_t gfp_mask, int type)
  420. {
  421. return NULL;
  422. }
  423. static inline __printf(2, 3)
  424. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  425. { }
  426. static inline void audit_log_end(struct audit_buffer *ab)
  427. { }
  428. static inline void audit_log_n_hex(struct audit_buffer *ab,
  429. const unsigned char *buf, size_t len)
  430. { }
  431. static inline void audit_log_n_string(struct audit_buffer *ab,
  432. const char *buf, size_t n)
  433. { }
  434. static inline void audit_log_n_untrustedstring(struct audit_buffer *ab,
  435. const char *string, size_t n)
  436. { }
  437. static inline void audit_log_untrustedstring(struct audit_buffer *ab,
  438. const char *string)
  439. { }
  440. static inline void audit_log_d_path(struct audit_buffer *ab,
  441. const char *prefix,
  442. const struct path *path)
  443. { }
  444. static inline void audit_log_key(struct audit_buffer *ab, char *key)
  445. { }
  446. static inline void audit_log_link_denied(const char *string,
  447. const struct path *link)
  448. { }
  449. static inline void audit_log_secctx(struct audit_buffer *ab, u32 secid)
  450. { }
  451. #define audit_enabled 0
  452. #endif /* CONFIG_AUDIT */
  453. static inline void audit_log_string(struct audit_buffer *ab, const char *buf)
  454. {
  455. audit_log_n_string(ab, buf, strlen(buf));
  456. }
  457. #endif