audit.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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, int seq, 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 timespec64 *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 timespec64 *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. extern void __audit_log_kern_module(char *name);
  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_socketcall_compat(int nargs, u32 *args)
  356. {
  357. unsigned long a[AUDITSC_ARGS];
  358. int i;
  359. if (audit_dummy_context())
  360. return 0;
  361. for (i = 0; i < nargs; i++)
  362. a[i] = (unsigned long)args[i];
  363. return __audit_socketcall(nargs, a);
  364. }
  365. static inline int audit_sockaddr(int len, void *addr)
  366. {
  367. if (unlikely(!audit_dummy_context()))
  368. return __audit_sockaddr(len, addr);
  369. return 0;
  370. }
  371. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  372. {
  373. if (unlikely(!audit_dummy_context()))
  374. __audit_mq_open(oflag, mode, attr);
  375. }
  376. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout)
  377. {
  378. if (unlikely(!audit_dummy_context()))
  379. __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
  380. }
  381. static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  382. {
  383. if (unlikely(!audit_dummy_context()))
  384. __audit_mq_notify(mqdes, notification);
  385. }
  386. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  387. {
  388. if (unlikely(!audit_dummy_context()))
  389. __audit_mq_getsetattr(mqdes, mqstat);
  390. }
  391. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  392. const struct cred *new,
  393. const struct cred *old)
  394. {
  395. if (unlikely(!audit_dummy_context()))
  396. return __audit_log_bprm_fcaps(bprm, new, old);
  397. return 0;
  398. }
  399. static inline void audit_log_capset(const struct cred *new,
  400. const struct cred *old)
  401. {
  402. if (unlikely(!audit_dummy_context()))
  403. __audit_log_capset(new, old);
  404. }
  405. static inline void audit_mmap_fd(int fd, int flags)
  406. {
  407. if (unlikely(!audit_dummy_context()))
  408. __audit_mmap_fd(fd, flags);
  409. }
  410. static inline void audit_log_kern_module(char *name)
  411. {
  412. if (!audit_dummy_context())
  413. __audit_log_kern_module(name);
  414. }
  415. extern int audit_n_rules;
  416. extern int audit_signals;
  417. #else /* CONFIG_AUDITSYSCALL */
  418. static inline int audit_alloc(struct task_struct *task)
  419. {
  420. return 0;
  421. }
  422. static inline void audit_free(struct task_struct *task)
  423. { }
  424. static inline void audit_syscall_entry(int major, unsigned long a0,
  425. unsigned long a1, unsigned long a2,
  426. unsigned long a3)
  427. { }
  428. static inline void audit_syscall_exit(void *pt_regs)
  429. { }
  430. static inline bool audit_dummy_context(void)
  431. {
  432. return true;
  433. }
  434. static inline struct filename *audit_reusename(const __user char *name)
  435. {
  436. return NULL;
  437. }
  438. static inline void audit_getname(struct filename *name)
  439. { }
  440. static inline void __audit_inode(struct filename *name,
  441. const struct dentry *dentry,
  442. unsigned int flags)
  443. { }
  444. static inline void __audit_inode_child(struct inode *parent,
  445. const struct dentry *dentry,
  446. const unsigned char type)
  447. { }
  448. static inline void audit_inode(struct filename *name,
  449. const struct dentry *dentry,
  450. unsigned int parent)
  451. { }
  452. static inline void audit_file(struct file *file)
  453. {
  454. }
  455. static inline void audit_inode_parent_hidden(struct filename *name,
  456. const struct dentry *dentry)
  457. { }
  458. static inline void audit_inode_child(struct inode *parent,
  459. const struct dentry *dentry,
  460. const unsigned char type)
  461. { }
  462. static inline void audit_core_dumps(long signr)
  463. { }
  464. static inline void __audit_seccomp(unsigned long syscall, long signr, int code)
  465. { }
  466. static inline void audit_seccomp(unsigned long syscall, long signr, int code)
  467. { }
  468. static inline int auditsc_get_stamp(struct audit_context *ctx,
  469. struct timespec64 *t, unsigned int *serial)
  470. {
  471. return 0;
  472. }
  473. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  474. {
  475. return INVALID_UID;
  476. }
  477. static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
  478. {
  479. return -1;
  480. }
  481. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  482. { }
  483. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
  484. gid_t gid, umode_t mode)
  485. { }
  486. static inline void audit_bprm(struct linux_binprm *bprm)
  487. { }
  488. static inline int audit_socketcall(int nargs, unsigned long *args)
  489. {
  490. return 0;
  491. }
  492. static inline int audit_socketcall_compat(int nargs, u32 *args)
  493. {
  494. return 0;
  495. }
  496. static inline void audit_fd_pair(int fd1, int fd2)
  497. { }
  498. static inline int audit_sockaddr(int len, void *addr)
  499. {
  500. return 0;
  501. }
  502. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  503. { }
  504. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len,
  505. unsigned int msg_prio,
  506. const struct timespec64 *abs_timeout)
  507. { }
  508. static inline void audit_mq_notify(mqd_t mqdes,
  509. const struct sigevent *notification)
  510. { }
  511. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  512. { }
  513. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  514. const struct cred *new,
  515. const struct cred *old)
  516. {
  517. return 0;
  518. }
  519. static inline void audit_log_capset(const struct cred *new,
  520. const struct cred *old)
  521. { }
  522. static inline void audit_mmap_fd(int fd, int flags)
  523. { }
  524. static inline void audit_log_kern_module(char *name)
  525. {
  526. }
  527. static inline void audit_ptrace(struct task_struct *t)
  528. { }
  529. #define audit_n_rules 0
  530. #define audit_signals 0
  531. #endif /* CONFIG_AUDITSYSCALL */
  532. static inline bool audit_loginuid_set(struct task_struct *tsk)
  533. {
  534. return uid_valid(audit_get_loginuid(tsk));
  535. }
  536. static inline void audit_log_string(struct audit_buffer *ab, const char *buf)
  537. {
  538. audit_log_n_string(ab, buf, strlen(buf));
  539. }
  540. #endif