audit.h 15 KB

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