audit.h 17 KB

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