audit.h 17 KB

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