audit.h 17 KB

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