audit.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 && unlikely(!audit_dummy_context()))
  290. __audit_seccomp(syscall, signr, code);
  291. }
  292. static inline void audit_ptrace(struct task_struct *t)
  293. {
  294. if (unlikely(!audit_dummy_context()))
  295. __audit_ptrace(t);
  296. }
  297. /* Private API (for audit.c only) */
  298. extern unsigned int audit_serial(void);
  299. extern int auditsc_get_stamp(struct audit_context *ctx,
  300. struct timespec64 *t, unsigned int *serial);
  301. extern int audit_set_loginuid(kuid_t loginuid);
  302. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  303. {
  304. return tsk->loginuid;
  305. }
  306. static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
  307. {
  308. return tsk->sessionid;
  309. }
  310. extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
  311. extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
  312. extern void __audit_bprm(struct linux_binprm *bprm);
  313. extern int __audit_socketcall(int nargs, unsigned long *args);
  314. extern int __audit_sockaddr(int len, void *addr);
  315. extern void __audit_fd_pair(int fd1, int fd2);
  316. extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr);
  317. extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout);
  318. extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification);
  319. extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
  320. extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
  321. const struct cred *new,
  322. const struct cred *old);
  323. extern void __audit_log_capset(const struct cred *new, const struct cred *old);
  324. extern void __audit_mmap_fd(int fd, int flags);
  325. extern void __audit_log_kern_module(char *name);
  326. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  327. {
  328. if (unlikely(!audit_dummy_context()))
  329. __audit_ipc_obj(ipcp);
  330. }
  331. static inline void audit_fd_pair(int fd1, int fd2)
  332. {
  333. if (unlikely(!audit_dummy_context()))
  334. __audit_fd_pair(fd1, fd2);
  335. }
  336. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
  337. {
  338. if (unlikely(!audit_dummy_context()))
  339. __audit_ipc_set_perm(qbytes, uid, gid, mode);
  340. }
  341. static inline void audit_bprm(struct linux_binprm *bprm)
  342. {
  343. if (unlikely(!audit_dummy_context()))
  344. __audit_bprm(bprm);
  345. }
  346. static inline int audit_socketcall(int nargs, unsigned long *args)
  347. {
  348. if (unlikely(!audit_dummy_context()))
  349. return __audit_socketcall(nargs, args);
  350. return 0;
  351. }
  352. static inline int audit_socketcall_compat(int nargs, u32 *args)
  353. {
  354. unsigned long a[AUDITSC_ARGS];
  355. int i;
  356. if (audit_dummy_context())
  357. return 0;
  358. for (i = 0; i < nargs; i++)
  359. a[i] = (unsigned long)args[i];
  360. return __audit_socketcall(nargs, a);
  361. }
  362. static inline int audit_sockaddr(int len, void *addr)
  363. {
  364. if (unlikely(!audit_dummy_context()))
  365. return __audit_sockaddr(len, addr);
  366. return 0;
  367. }
  368. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  369. {
  370. if (unlikely(!audit_dummy_context()))
  371. __audit_mq_open(oflag, mode, attr);
  372. }
  373. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout)
  374. {
  375. if (unlikely(!audit_dummy_context()))
  376. __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
  377. }
  378. static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
  379. {
  380. if (unlikely(!audit_dummy_context()))
  381. __audit_mq_notify(mqdes, notification);
  382. }
  383. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  384. {
  385. if (unlikely(!audit_dummy_context()))
  386. __audit_mq_getsetattr(mqdes, mqstat);
  387. }
  388. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  389. const struct cred *new,
  390. const struct cred *old)
  391. {
  392. if (unlikely(!audit_dummy_context()))
  393. return __audit_log_bprm_fcaps(bprm, new, old);
  394. return 0;
  395. }
  396. static inline void audit_log_capset(const struct cred *new,
  397. const struct cred *old)
  398. {
  399. if (unlikely(!audit_dummy_context()))
  400. __audit_log_capset(new, old);
  401. }
  402. static inline void audit_mmap_fd(int fd, int flags)
  403. {
  404. if (unlikely(!audit_dummy_context()))
  405. __audit_mmap_fd(fd, flags);
  406. }
  407. static inline void audit_log_kern_module(char *name)
  408. {
  409. if (!audit_dummy_context())
  410. __audit_log_kern_module(name);
  411. }
  412. extern int audit_n_rules;
  413. extern int audit_signals;
  414. #else /* CONFIG_AUDITSYSCALL */
  415. static inline int audit_alloc(struct task_struct *task)
  416. {
  417. return 0;
  418. }
  419. static inline void audit_free(struct task_struct *task)
  420. { }
  421. static inline void audit_syscall_entry(int major, unsigned long a0,
  422. unsigned long a1, unsigned long a2,
  423. unsigned long a3)
  424. { }
  425. static inline void audit_syscall_exit(void *pt_regs)
  426. { }
  427. static inline bool audit_dummy_context(void)
  428. {
  429. return true;
  430. }
  431. static inline struct filename *audit_reusename(const __user char *name)
  432. {
  433. return NULL;
  434. }
  435. static inline void audit_getname(struct filename *name)
  436. { }
  437. static inline void __audit_inode(struct filename *name,
  438. const struct dentry *dentry,
  439. unsigned int flags)
  440. { }
  441. static inline void __audit_inode_child(struct inode *parent,
  442. const struct dentry *dentry,
  443. const unsigned char type)
  444. { }
  445. static inline void audit_inode(struct filename *name,
  446. const struct dentry *dentry,
  447. unsigned int parent)
  448. { }
  449. static inline void audit_file(struct file *file)
  450. {
  451. }
  452. static inline void audit_inode_parent_hidden(struct filename *name,
  453. const struct dentry *dentry)
  454. { }
  455. static inline void audit_inode_child(struct inode *parent,
  456. const struct dentry *dentry,
  457. const unsigned char type)
  458. { }
  459. static inline void audit_core_dumps(long signr)
  460. { }
  461. static inline void __audit_seccomp(unsigned long syscall, long signr, int code)
  462. { }
  463. static inline void audit_seccomp(unsigned long syscall, long signr, int code)
  464. { }
  465. static inline int auditsc_get_stamp(struct audit_context *ctx,
  466. struct timespec64 *t, unsigned int *serial)
  467. {
  468. return 0;
  469. }
  470. static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
  471. {
  472. return INVALID_UID;
  473. }
  474. static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
  475. {
  476. return -1;
  477. }
  478. static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
  479. { }
  480. static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
  481. gid_t gid, umode_t mode)
  482. { }
  483. static inline void audit_bprm(struct linux_binprm *bprm)
  484. { }
  485. static inline int audit_socketcall(int nargs, unsigned long *args)
  486. {
  487. return 0;
  488. }
  489. static inline int audit_socketcall_compat(int nargs, u32 *args)
  490. {
  491. return 0;
  492. }
  493. static inline void audit_fd_pair(int fd1, int fd2)
  494. { }
  495. static inline int audit_sockaddr(int len, void *addr)
  496. {
  497. return 0;
  498. }
  499. static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
  500. { }
  501. static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len,
  502. unsigned int msg_prio,
  503. const struct timespec *abs_timeout)
  504. { }
  505. static inline void audit_mq_notify(mqd_t mqdes,
  506. const struct sigevent *notification)
  507. { }
  508. static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  509. { }
  510. static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
  511. const struct cred *new,
  512. const struct cred *old)
  513. {
  514. return 0;
  515. }
  516. static inline void audit_log_capset(const struct cred *new,
  517. const struct cred *old)
  518. { }
  519. static inline void audit_mmap_fd(int fd, int flags)
  520. { }
  521. static inline void audit_log_kern_module(char *name)
  522. {
  523. }
  524. static inline void audit_ptrace(struct task_struct *t)
  525. { }
  526. #define audit_n_rules 0
  527. #define audit_signals 0
  528. #endif /* CONFIG_AUDITSYSCALL */
  529. static inline bool audit_loginuid_set(struct task_struct *tsk)
  530. {
  531. return uid_valid(audit_get_loginuid(tsk));
  532. }
  533. static inline void audit_log_string(struct audit_buffer *ab, const char *buf)
  534. {
  535. audit_log_n_string(ab, buf, strlen(buf));
  536. }
  537. #endif