audit.h 16 KB

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