audit.h 17 KB

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