audit.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. extern void audit_log_session_info(struct audit_buffer *ab);
  83. #ifdef CONFIG_AUDITSYSCALL
  84. /* These are defined in auditsc.c */
  85. /* Public API */
  86. extern int audit_alloc(struct task_struct *task);
  87. extern void __audit_free(struct task_struct *task);
  88. extern void __audit_syscall_entry(int arch,
  89. int major, unsigned long a0, unsigned long a1,
  90. unsigned long a2, unsigned long a3);
  91. extern void __audit_syscall_exit(int ret_success, long ret_value);
  92. extern struct filename *__audit_reusename(const __user char *uptr);
  93. extern void __audit_getname(struct filename *name);
  94. extern void audit_putname(struct filename *name);
  95. #define AUDIT_INODE_PARENT 1 /* dentry represents the parent */
  96. #define AUDIT_INODE_HIDDEN 2 /* audit record should be hidden */
  97. extern void __audit_inode(struct filename *name, const struct dentry *dentry,
  98. unsigned int flags);
  99. extern void __audit_inode_child(const struct inode *parent,
  100. const struct dentry *dentry,
  101. const unsigned char type);
  102. extern void __audit_seccomp(unsigned long syscall, long signr, int code);
  103. extern void __audit_ptrace(struct task_struct *t);
  104. static inline int audit_dummy_context(void)
  105. {
  106. void *p = current->audit_context;
  107. return !p || *(int *)p;
  108. }
  109. static inline void audit_free(struct task_struct *task)
  110. {
  111. if (unlikely(task->audit_context))
  112. __audit_free(task);
  113. }
  114. static inline void audit_syscall_entry(int arch, int major, unsigned long a0,
  115. unsigned long a1, unsigned long a2,
  116. unsigned long a3)
  117. {
  118. if (unlikely(current->audit_context))
  119. __audit_syscall_entry(arch, major, a0, a1, a2, a3);
  120. }
  121. static inline void audit_syscall_exit(void *pt_regs)
  122. {
  123. if (unlikely(current->audit_context)) {
  124. int success = is_syscall_success(pt_regs);
  125. int return_code = regs_return_value(pt_regs);
  126. __audit_syscall_exit(success, return_code);
  127. }
  128. }
  129. static inline struct filename *audit_reusename(const __user char *name)
  130. {
  131. if (unlikely(!audit_dummy_context()))
  132. return __audit_reusename(name);
  133. return NULL;
  134. }
  135. static inline void audit_getname(struct filename *name)
  136. {
  137. if (unlikely(!audit_dummy_context()))
  138. __audit_getname(name);
  139. }
  140. static inline void audit_inode(struct filename *name,
  141. const struct dentry *dentry,
  142. unsigned int parent) {
  143. if (unlikely(!audit_dummy_context())) {
  144. unsigned int flags = 0;
  145. if (parent)
  146. flags |= AUDIT_INODE_PARENT;
  147. __audit_inode(name, dentry, flags);
  148. }
  149. }
  150. static inline void audit_inode_parent_hidden(struct filename *name,
  151. const struct dentry *dentry)
  152. {
  153. if (unlikely(!audit_dummy_context()))
  154. __audit_inode(name, dentry,
  155. AUDIT_INODE_PARENT | AUDIT_INODE_HIDDEN);
  156. }
  157. static inline void audit_inode_child(const struct inode *parent,
  158. const struct dentry *dentry,
  159. const unsigned char type) {
  160. if (unlikely(!audit_dummy_context()))
  161. __audit_inode_child(parent, dentry, type);
  162. }
  163. void audit_core_dumps(long signr);
  164. static inline void audit_seccomp(unsigned long syscall, long signr, int code)
  165. {
  166. /* Force a record to be reported if a signal was delivered. */
  167. if (signr || unlikely(!audit_dummy_context()))
  168. __audit_seccomp(syscall, signr, code);
  169. }
  170. static inline void audit_ptrace(struct task_struct *t)
  171. {
  172. if (unlikely(!audit_dummy_context()))
  173. __audit_ptrace(t);
  174. }
  175. /* Private API (for audit.c only) */
  176. extern unsigned int audit_serial(void);
  177. extern int auditsc_get_stamp(struct audit_context *ctx,
  178. struct timespec *t, unsigned int *serial);
  179. extern int audit_set_loginuid(kuid_t loginuid);
  180. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  181. {
  182. return tsk->loginuid;
  183. }
  184. static inline int audit_get_sessionid(struct task_struct *tsk)
  185. {
  186. return tsk->sessionid;
  187. }
  188. extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
  189. extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
  190. extern int __audit_bprm(struct linux_binprm *bprm);
  191. extern int __audit_socketcall(int nargs, unsigned long *args);
  192. extern int __audit_sockaddr(int len, void *addr);
  193. extern void __audit_fd_pair(int fd1, int fd2);
  194. extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr);
  195. extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout);
  196. extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification);
  197. extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
  198. extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  199. const struct cred *new,
  200. const struct cred *old);
  201. extern void __audit_log_capset(pid_t pid, const struct cred *new, const struct cred *old);
  202. extern void __audit_mmap_fd(int fd, int flags);
  203. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  204. {
  205. if (unlikely(!audit_dummy_context()))
  206. __audit_ipc_obj(ipcp);
  207. }
  208. static inline void audit_fd_pair(int fd1, int fd2)
  209. {
  210. if (unlikely(!audit_dummy_context()))
  211. __audit_fd_pair(fd1, fd2);
  212. }
  213. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  214. {
  215. if (unlikely(!audit_dummy_context()))
  216. __audit_ipc_set_perm(qbytes, uid, gid, mode);
  217. }
  218. static inline int audit_bprm(struct linux_binprm *bprm)
  219. {
  220. if (unlikely(!audit_dummy_context()))
  221. return __audit_bprm(bprm);
  222. return 0;
  223. }
  224. static inline int audit_socketcall(int nargs, unsigned long *args)
  225. {
  226. if (unlikely(!audit_dummy_context()))
  227. return __audit_socketcall(nargs, args);
  228. return 0;
  229. }
  230. static inline int audit_sockaddr(int len, void *addr)
  231. {
  232. if (unlikely(!audit_dummy_context()))
  233. return __audit_sockaddr(len, addr);
  234. return 0;
  235. }
  236. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  237. {
  238. if (unlikely(!audit_dummy_context()))
  239. __audit_mq_open(oflag, mode, attr);
  240. }
  241. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout)
  242. {
  243. if (unlikely(!audit_dummy_context()))
  244. __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
  245. }
  246. static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  247. {
  248. if (unlikely(!audit_dummy_context()))
  249. __audit_mq_notify(mqdes, notification);
  250. }
  251. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  252. {
  253. if (unlikely(!audit_dummy_context()))
  254. __audit_mq_getsetattr(mqdes, mqstat);
  255. }
  256. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  257. const struct cred *new,
  258. const struct cred *old)
  259. {
  260. if (unlikely(!audit_dummy_context()))
  261. return __audit_log_bprm_fcaps(bprm, new, old);
  262. return 0;
  263. }
  264. static inline void audit_log_capset(pid_t pid, const struct cred *new,
  265. const struct cred *old)
  266. {
  267. if (unlikely(!audit_dummy_context()))
  268. __audit_log_capset(pid, new, old);
  269. }
  270. static inline void audit_mmap_fd(int fd, int flags)
  271. {
  272. if (unlikely(!audit_dummy_context()))
  273. __audit_mmap_fd(fd, flags);
  274. }
  275. extern int audit_n_rules;
  276. extern int audit_signals;
  277. #else /* CONFIG_AUDITSYSCALL */
  278. static inline int audit_alloc(struct task_struct *task)
  279. {
  280. return 0;
  281. }
  282. static inline void audit_free(struct task_struct *task)
  283. { }
  284. static inline void audit_syscall_entry(int arch, int major, unsigned long a0,
  285. unsigned long a1, unsigned long a2,
  286. unsigned long a3)
  287. { }
  288. static inline void audit_syscall_exit(void *pt_regs)
  289. { }
  290. static inline int audit_dummy_context(void)
  291. {
  292. return 1;
  293. }
  294. static inline struct filename *audit_reusename(const __user char *name)
  295. {
  296. return NULL;
  297. }
  298. static inline void audit_getname(struct filename *name)
  299. { }
  300. static inline void audit_putname(struct filename *name)
  301. { }
  302. static inline void __audit_inode(struct filename *name,
  303. const struct dentry *dentry,
  304. unsigned int flags)
  305. { }
  306. static inline void __audit_inode_child(const struct inode *parent,
  307. const struct dentry *dentry,
  308. const unsigned char type)
  309. { }
  310. static inline void audit_inode(struct filename *name,
  311. const struct dentry *dentry,
  312. unsigned int parent)
  313. { }
  314. static inline void audit_inode_parent_hidden(struct filename *name,
  315. const struct dentry *dentry)
  316. { }
  317. static inline void audit_inode_child(const struct inode *parent,
  318. const struct dentry *dentry,
  319. const unsigned char type)
  320. { }
  321. static inline void audit_core_dumps(long signr)
  322. { }
  323. static inline void __audit_seccomp(unsigned long syscall, long signr, int code)
  324. { }
  325. static inline void audit_seccomp(unsigned long syscall, long signr, int code)
  326. { }
  327. static inline int auditsc_get_stamp(struct audit_context *ctx,
  328. struct timespec *t, unsigned int *serial)
  329. {
  330. return 0;
  331. }
  332. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  333. {
  334. return INVALID_UID;
  335. }
  336. static inline int audit_get_sessionid(struct task_struct *tsk)
  337. {
  338. return -1;
  339. }
  340. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  341. { }
  342. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
  343. gid_t gid, umode_t mode)
  344. { }
  345. static inline int audit_bprm(struct linux_binprm *bprm)
  346. {
  347. return 0;
  348. }
  349. static inline int audit_socketcall(int nargs, unsigned long *args)
  350. {
  351. return 0;
  352. }
  353. static inline void audit_fd_pair(int fd1, int fd2)
  354. { }
  355. static inline int audit_sockaddr(int len, void *addr)
  356. {
  357. return 0;
  358. }
  359. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  360. { }
  361. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len,
  362. unsigned int msg_prio,
  363. const struct timespec *abs_timeout)
  364. { }
  365. static inline void audit_mq_notify(mqd_t mqdes,
  366. const struct sigevent *notification)
  367. { }
  368. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  369. { }
  370. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  371. const struct cred *new,
  372. const struct cred *old)
  373. {
  374. return 0;
  375. }
  376. static inline void audit_log_capset(pid_t pid, const struct cred *new,
  377. const struct cred *old)
  378. { }
  379. static inline void audit_mmap_fd(int fd, int flags)
  380. { }
  381. static inline void audit_ptrace(struct task_struct *t)
  382. { }
  383. #define audit_n_rules 0
  384. #define audit_signals 0
  385. #endif /* CONFIG_AUDITSYSCALL */
  386. static inline bool audit_loginuid_set(struct task_struct *tsk)
  387. {
  388. return uid_valid(audit_get_loginuid(tsk));
  389. }
  390. #ifdef CONFIG_AUDIT
  391. /* These are defined in audit.c */
  392. /* Public API */
  393. extern __printf(4, 5)
  394. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  395. const char *fmt, ...);
  396. extern struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type);
  397. extern __printf(2, 3)
  398. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...);
  399. extern void audit_log_end(struct audit_buffer *ab);
  400. extern int audit_string_contains_control(const char *string,
  401. size_t len);
  402. extern void audit_log_n_hex(struct audit_buffer *ab,
  403. const unsigned char *buf,
  404. size_t len);
  405. extern void audit_log_n_string(struct audit_buffer *ab,
  406. const char *buf,
  407. size_t n);
  408. extern void audit_log_n_untrustedstring(struct audit_buffer *ab,
  409. const char *string,
  410. size_t n);
  411. extern void audit_log_untrustedstring(struct audit_buffer *ab,
  412. const char *string);
  413. extern void audit_log_d_path(struct audit_buffer *ab,
  414. const char *prefix,
  415. const struct path *path);
  416. extern void audit_log_key(struct audit_buffer *ab,
  417. char *key);
  418. extern void audit_log_link_denied(const char *operation,
  419. struct path *link);
  420. extern void audit_log_lost(const char *message);
  421. #ifdef CONFIG_SECURITY
  422. extern void audit_log_secctx(struct audit_buffer *ab, u32 secid);
  423. #else
  424. static inline void audit_log_secctx(struct audit_buffer *ab, u32 secid)
  425. { }
  426. #endif
  427. extern int audit_log_task_context(struct audit_buffer *ab);
  428. extern void audit_log_task_info(struct audit_buffer *ab,
  429. struct task_struct *tsk);
  430. extern int audit_update_lsm_rules(void);
  431. /* Private API (for audit.c only) */
  432. extern int audit_filter_user(int type);
  433. extern int audit_filter_type(int type);
  434. extern int audit_receive_filter(int type, int pid, int seq,
  435. void *data, size_t datasz);
  436. extern int audit_enabled;
  437. #else /* CONFIG_AUDIT */
  438. static inline __printf(4, 5)
  439. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  440. const char *fmt, ...)
  441. { }
  442. static inline struct audit_buffer *audit_log_start(struct audit_context *ctx,
  443. gfp_t gfp_mask, int type)
  444. {
  445. return NULL;
  446. }
  447. static inline __printf(2, 3)
  448. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  449. { }
  450. static inline void audit_log_end(struct audit_buffer *ab)
  451. { }
  452. static inline void audit_log_n_hex(struct audit_buffer *ab,
  453. const unsigned char *buf, size_t len)
  454. { }
  455. static inline void audit_log_n_string(struct audit_buffer *ab,
  456. const char *buf, size_t n)
  457. { }
  458. static inline void audit_log_n_untrustedstring(struct audit_buffer *ab,
  459. const char *string, size_t n)
  460. { }
  461. static inline void audit_log_untrustedstring(struct audit_buffer *ab,
  462. const char *string)
  463. { }
  464. static inline void audit_log_d_path(struct audit_buffer *ab,
  465. const char *prefix,
  466. const struct path *path)
  467. { }
  468. static inline void audit_log_key(struct audit_buffer *ab, char *key)
  469. { }
  470. static inline void audit_log_link_denied(const char *string,
  471. const struct path *link)
  472. { }
  473. static inline void audit_log_secctx(struct audit_buffer *ab, u32 secid)
  474. { }
  475. static inline int audit_log_task_context(struct audit_buffer *ab)
  476. {
  477. return 0;
  478. }
  479. static inline void audit_log_task_info(struct audit_buffer *ab,
  480. struct task_struct *tsk)
  481. { }
  482. #define audit_enabled 0
  483. #endif /* CONFIG_AUDIT */
  484. static inline void audit_log_string(struct audit_buffer *ab, const char *buf)
  485. {
  486. audit_log_n_string(ab, buf, strlen(buf));
  487. }
  488. #endif