audit.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* audit.c -- Auditing support -*- linux-c -*-
  2. * Gateway between the kernel (e.g., selinux) and the user-space audit daemon.
  3. * System-call specific features have moved to auditsc.c
  4. *
  5. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  6. * All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  23. *
  24. * Goals: 1) Integrate fully with SELinux.
  25. * 2) Minimal run-time overhead:
  26. * a) Minimal when syscall auditing is disabled (audit_enable=0).
  27. * b) Small when syscall auditing is enabled and no audit record
  28. * is generated (defer as much work as possible to record
  29. * generation time):
  30. * i) context is allocated,
  31. * ii) names from getname are stored without a copy, and
  32. * iii) inode information stored from path_lookup.
  33. * 3) Ability to disable syscall auditing at boot time (audit=0).
  34. * 4) Usable by other parts of the kernel (if audit_log* is called,
  35. * then a syscall record will be generated automatically for the
  36. * current syscall).
  37. * 5) Netlink interface to user-space.
  38. * 6) Support low-overhead kernel-based filtering to minimize the
  39. * information that must be passed to user-space.
  40. *
  41. * Example user-space utilities: http://people.redhat.com/faith/audit/
  42. */
  43. #include <linux/init.h>
  44. #include <asm/atomic.h>
  45. #include <asm/types.h>
  46. #include <linux/mm.h>
  47. #include <linux/module.h>
  48. #include <linux/audit.h>
  49. #include <net/sock.h>
  50. #include <linux/skbuff.h>
  51. #include <linux/netlink.h>
  52. /* No auditing will take place until audit_initialized != 0.
  53. * (Initialization happens after skb_init is called.) */
  54. static int audit_initialized;
  55. /* No syscall auditing will take place unless audit_enabled != 0. */
  56. int audit_enabled;
  57. /* Default state when kernel boots without any parameters. */
  58. static int audit_default;
  59. /* If auditing cannot proceed, audit_failure selects what happens. */
  60. static int audit_failure = AUDIT_FAIL_PRINTK;
  61. /* If audit records are to be written to the netlink socket, audit_pid
  62. * contains the (non-zero) pid. */
  63. static int audit_pid;
  64. /* If audit_limit is non-zero, limit the rate of sending audit records
  65. * to that number per second. This prevents DoS attacks, but results in
  66. * audit records being dropped. */
  67. static int audit_rate_limit;
  68. /* Number of outstanding audit_buffers allowed. */
  69. static int audit_backlog_limit = 64;
  70. static atomic_t audit_backlog = ATOMIC_INIT(0);
  71. /* Records can be lost in several ways:
  72. 0) [suppressed in audit_alloc]
  73. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  74. 2) out of memory in audit_log_move [alloc_skb]
  75. 3) suppressed due to audit_rate_limit
  76. 4) suppressed due to audit_backlog_limit
  77. */
  78. static atomic_t audit_lost = ATOMIC_INIT(0);
  79. /* The netlink socket. */
  80. static struct sock *audit_sock;
  81. /* There are two lists of audit buffers. The txlist contains audit
  82. * buffers that cannot be sent immediately to the netlink device because
  83. * we are in an irq context (these are sent later in a tasklet).
  84. *
  85. * The second list is a list of pre-allocated audit buffers (if more
  86. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  87. * being placed on the freelist). */
  88. static DEFINE_SPINLOCK(audit_txlist_lock);
  89. static DEFINE_SPINLOCK(audit_freelist_lock);
  90. static int audit_freelist_count = 0;
  91. static LIST_HEAD(audit_txlist);
  92. static LIST_HEAD(audit_freelist);
  93. /* There are three lists of rules -- one to search at task creation
  94. * time, one to search at syscall entry time, and another to search at
  95. * syscall exit time. */
  96. static LIST_HEAD(audit_tsklist);
  97. static LIST_HEAD(audit_entlist);
  98. static LIST_HEAD(audit_extlist);
  99. /* The netlink socket is only to be read by 1 CPU, which lets us assume
  100. * that list additions and deletions never happen simultaneiously in
  101. * auditsc.c */
  102. static DECLARE_MUTEX(audit_netlink_sem);
  103. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  104. * audit records. Since printk uses a 1024 byte buffer, this buffer
  105. * should be at least that large. */
  106. #define AUDIT_BUFSIZ 1024
  107. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  108. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  109. #define AUDIT_MAXFREE (2*NR_CPUS)
  110. /* The audit_buffer is used when formatting an audit record. The caller
  111. * locks briefly to get the record off the freelist or to allocate the
  112. * buffer, and locks briefly to send the buffer to the netlink layer or
  113. * to place it on a transmit queue. Multiple audit_buffers can be in
  114. * use simultaneously. */
  115. struct audit_buffer {
  116. struct list_head list;
  117. struct sk_buff_head sklist; /* formatted skbs ready to send */
  118. struct audit_context *ctx; /* NULL or associated context */
  119. int len; /* used area of tmp */
  120. char tmp[AUDIT_BUFSIZ];
  121. /* Pointer to header and contents */
  122. struct nlmsghdr *nlh;
  123. int total;
  124. int type;
  125. int pid;
  126. int count; /* Times requeued */
  127. };
  128. void audit_set_type(struct audit_buffer *ab, int type)
  129. {
  130. ab->type = type;
  131. }
  132. struct audit_entry {
  133. struct list_head list;
  134. struct audit_rule rule;
  135. };
  136. static void audit_log_end_irq(struct audit_buffer *ab);
  137. static void audit_log_end_fast(struct audit_buffer *ab);
  138. static void audit_panic(const char *message)
  139. {
  140. switch (audit_failure)
  141. {
  142. case AUDIT_FAIL_SILENT:
  143. break;
  144. case AUDIT_FAIL_PRINTK:
  145. printk(KERN_ERR "audit: %s\n", message);
  146. break;
  147. case AUDIT_FAIL_PANIC:
  148. panic("audit: %s\n", message);
  149. break;
  150. }
  151. }
  152. static inline int audit_rate_check(void)
  153. {
  154. static unsigned long last_check = 0;
  155. static int messages = 0;
  156. static DEFINE_SPINLOCK(lock);
  157. unsigned long flags;
  158. unsigned long now;
  159. unsigned long elapsed;
  160. int retval = 0;
  161. if (!audit_rate_limit) return 1;
  162. spin_lock_irqsave(&lock, flags);
  163. if (++messages < audit_rate_limit) {
  164. retval = 1;
  165. } else {
  166. now = jiffies;
  167. elapsed = now - last_check;
  168. if (elapsed > HZ) {
  169. last_check = now;
  170. messages = 0;
  171. retval = 1;
  172. }
  173. }
  174. spin_unlock_irqrestore(&lock, flags);
  175. return retval;
  176. }
  177. /* Emit at least 1 message per second, even if audit_rate_check is
  178. * throttling. */
  179. void audit_log_lost(const char *message)
  180. {
  181. static unsigned long last_msg = 0;
  182. static DEFINE_SPINLOCK(lock);
  183. unsigned long flags;
  184. unsigned long now;
  185. int print;
  186. atomic_inc(&audit_lost);
  187. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  188. if (!print) {
  189. spin_lock_irqsave(&lock, flags);
  190. now = jiffies;
  191. if (now - last_msg > HZ) {
  192. print = 1;
  193. last_msg = now;
  194. }
  195. spin_unlock_irqrestore(&lock, flags);
  196. }
  197. if (print) {
  198. printk(KERN_WARNING
  199. "audit: audit_lost=%d audit_backlog=%d"
  200. " audit_rate_limit=%d audit_backlog_limit=%d\n",
  201. atomic_read(&audit_lost),
  202. atomic_read(&audit_backlog),
  203. audit_rate_limit,
  204. audit_backlog_limit);
  205. audit_panic(message);
  206. }
  207. }
  208. static int audit_set_rate_limit(int limit)
  209. {
  210. int old = audit_rate_limit;
  211. audit_rate_limit = limit;
  212. audit_log(current->audit_context, "audit_rate_limit=%d old=%d",
  213. audit_rate_limit, old);
  214. return old;
  215. }
  216. static int audit_set_backlog_limit(int limit)
  217. {
  218. int old = audit_backlog_limit;
  219. audit_backlog_limit = limit;
  220. audit_log(current->audit_context, "audit_backlog_limit=%d old=%d",
  221. audit_backlog_limit, old);
  222. return old;
  223. }
  224. static int audit_set_enabled(int state)
  225. {
  226. int old = audit_enabled;
  227. if (state != 0 && state != 1)
  228. return -EINVAL;
  229. audit_enabled = state;
  230. audit_log(current->audit_context, "audit_enabled=%d old=%d",
  231. audit_enabled, old);
  232. return old;
  233. }
  234. static int audit_set_failure(int state)
  235. {
  236. int old = audit_failure;
  237. if (state != AUDIT_FAIL_SILENT
  238. && state != AUDIT_FAIL_PRINTK
  239. && state != AUDIT_FAIL_PANIC)
  240. return -EINVAL;
  241. audit_failure = state;
  242. audit_log(current->audit_context, "audit_failure=%d old=%d",
  243. audit_failure, old);
  244. return old;
  245. }
  246. #ifdef CONFIG_NET
  247. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  248. void *payload, int size)
  249. {
  250. struct sk_buff *skb;
  251. struct nlmsghdr *nlh;
  252. int len = NLMSG_SPACE(size);
  253. void *data;
  254. int flags = multi ? NLM_F_MULTI : 0;
  255. int t = done ? NLMSG_DONE : type;
  256. skb = alloc_skb(len, GFP_KERNEL);
  257. if (!skb)
  258. goto nlmsg_failure;
  259. nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
  260. nlh->nlmsg_flags = flags;
  261. data = NLMSG_DATA(nlh);
  262. memcpy(data, payload, size);
  263. netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
  264. return;
  265. nlmsg_failure: /* Used by NLMSG_PUT */
  266. if (skb)
  267. kfree_skb(skb);
  268. }
  269. /*
  270. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  271. * control messages.
  272. */
  273. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  274. {
  275. int err = 0;
  276. switch (msg_type) {
  277. case AUDIT_GET:
  278. case AUDIT_LIST:
  279. case AUDIT_SET:
  280. case AUDIT_ADD:
  281. case AUDIT_DEL:
  282. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  283. err = -EPERM;
  284. break;
  285. case AUDIT_USER:
  286. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  287. err = -EPERM;
  288. break;
  289. default: /* bad msg */
  290. err = -EINVAL;
  291. }
  292. return err;
  293. }
  294. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  295. {
  296. u32 uid, pid, seq;
  297. void *data;
  298. struct audit_status *status_get, status_set;
  299. int err;
  300. struct audit_buffer *ab;
  301. u16 msg_type = nlh->nlmsg_type;
  302. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  303. if (err)
  304. return err;
  305. pid = NETLINK_CREDS(skb)->pid;
  306. uid = NETLINK_CREDS(skb)->uid;
  307. seq = nlh->nlmsg_seq;
  308. data = NLMSG_DATA(nlh);
  309. switch (msg_type) {
  310. case AUDIT_GET:
  311. status_set.enabled = audit_enabled;
  312. status_set.failure = audit_failure;
  313. status_set.pid = audit_pid;
  314. status_set.rate_limit = audit_rate_limit;
  315. status_set.backlog_limit = audit_backlog_limit;
  316. status_set.lost = atomic_read(&audit_lost);
  317. status_set.backlog = atomic_read(&audit_backlog);
  318. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  319. &status_set, sizeof(status_set));
  320. break;
  321. case AUDIT_SET:
  322. if (nlh->nlmsg_len < sizeof(struct audit_status))
  323. return -EINVAL;
  324. status_get = (struct audit_status *)data;
  325. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  326. err = audit_set_enabled(status_get->enabled);
  327. if (err < 0) return err;
  328. }
  329. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  330. err = audit_set_failure(status_get->failure);
  331. if (err < 0) return err;
  332. }
  333. if (status_get->mask & AUDIT_STATUS_PID) {
  334. int old = audit_pid;
  335. audit_pid = status_get->pid;
  336. audit_log(current->audit_context,
  337. "audit_pid=%d old=%d", audit_pid, old);
  338. }
  339. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  340. audit_set_rate_limit(status_get->rate_limit);
  341. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  342. audit_set_backlog_limit(status_get->backlog_limit);
  343. break;
  344. case AUDIT_USER:
  345. ab = audit_log_start(NULL);
  346. if (!ab)
  347. break; /* audit_panic has been called */
  348. audit_log_format(ab,
  349. "user pid=%d uid=%d length=%d msg='%.1024s'",
  350. pid, uid,
  351. (int)(nlh->nlmsg_len
  352. - ((char *)data - (char *)nlh)),
  353. (char *)data);
  354. ab->type = AUDIT_USER;
  355. ab->pid = pid;
  356. audit_log_end(ab);
  357. break;
  358. case AUDIT_ADD:
  359. case AUDIT_DEL:
  360. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  361. return -EINVAL;
  362. /* fallthrough */
  363. case AUDIT_LIST:
  364. #ifdef CONFIG_AUDITSYSCALL
  365. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  366. uid, seq, data);
  367. #else
  368. err = -EOPNOTSUPP;
  369. #endif
  370. break;
  371. default:
  372. err = -EINVAL;
  373. break;
  374. }
  375. return err < 0 ? err : 0;
  376. }
  377. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  378. * processed by audit_receive_msg. Malformed skbs with wrong length are
  379. * discarded silently. */
  380. static int audit_receive_skb(struct sk_buff *skb)
  381. {
  382. int err;
  383. struct nlmsghdr *nlh;
  384. u32 rlen;
  385. while (skb->len >= NLMSG_SPACE(0)) {
  386. nlh = (struct nlmsghdr *)skb->data;
  387. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  388. return 0;
  389. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  390. if (rlen > skb->len)
  391. rlen = skb->len;
  392. if ((err = audit_receive_msg(skb, nlh))) {
  393. netlink_ack(skb, nlh, err);
  394. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  395. netlink_ack(skb, nlh, 0);
  396. skb_pull(skb, rlen);
  397. }
  398. return 0;
  399. }
  400. /* Receive messages from netlink socket. */
  401. static void audit_receive(struct sock *sk, int length)
  402. {
  403. struct sk_buff *skb;
  404. if (down_trylock(&audit_netlink_sem))
  405. return;
  406. /* FIXME: this must not cause starvation */
  407. while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
  408. if (audit_receive_skb(skb) && skb->len)
  409. skb_queue_head(&sk->sk_receive_queue, skb);
  410. else
  411. kfree_skb(skb);
  412. }
  413. up(&audit_netlink_sem);
  414. }
  415. /* Move data from tmp buffer into an skb. This is an extra copy, and
  416. * that is unfortunate. However, the copy will only occur when a record
  417. * is being written to user space, which is already a high-overhead
  418. * operation. (Elimination of the copy is possible, for example, by
  419. * writing directly into a pre-allocated skb, at the cost of wasting
  420. * memory. */
  421. static void audit_log_move(struct audit_buffer *ab)
  422. {
  423. struct sk_buff *skb;
  424. char *start;
  425. int extra = ab->nlh ? 0 : NLMSG_SPACE(0);
  426. /* possible resubmission */
  427. if (ab->len == 0)
  428. return;
  429. skb = skb_peek(&ab->sklist);
  430. if (!skb || skb_tailroom(skb) <= ab->len + extra) {
  431. skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);
  432. if (!skb) {
  433. ab->len = 0; /* Lose information in ab->tmp */
  434. audit_log_lost("out of memory in audit_log_move");
  435. return;
  436. }
  437. __skb_queue_tail(&ab->sklist, skb);
  438. if (!ab->nlh)
  439. ab->nlh = (struct nlmsghdr *)skb_put(skb,
  440. NLMSG_SPACE(0));
  441. }
  442. start = skb_put(skb, ab->len);
  443. memcpy(start, ab->tmp, ab->len);
  444. ab->len = 0;
  445. }
  446. /* Iterate over the skbuff in the audit_buffer, sending their contents
  447. * to user space. */
  448. static inline int audit_log_drain(struct audit_buffer *ab)
  449. {
  450. struct sk_buff *skb;
  451. while ((skb = skb_dequeue(&ab->sklist))) {
  452. int retval = 0;
  453. if (audit_pid) {
  454. if (ab->nlh) {
  455. ab->nlh->nlmsg_len = ab->total;
  456. ab->nlh->nlmsg_type = ab->type;
  457. ab->nlh->nlmsg_flags = 0;
  458. ab->nlh->nlmsg_seq = 0;
  459. ab->nlh->nlmsg_pid = ab->pid;
  460. }
  461. skb_get(skb); /* because netlink_* frees */
  462. retval = netlink_unicast(audit_sock, skb, audit_pid,
  463. MSG_DONTWAIT);
  464. }
  465. if (retval == -EAGAIN && ab->count < 5) {
  466. ++ab->count;
  467. skb_queue_tail(&ab->sklist, skb);
  468. audit_log_end_irq(ab);
  469. return 1;
  470. }
  471. if (retval < 0) {
  472. if (retval == -ECONNREFUSED) {
  473. printk(KERN_ERR
  474. "audit: *NO* daemon at audit_pid=%d\n",
  475. audit_pid);
  476. audit_pid = 0;
  477. } else
  478. audit_log_lost("netlink socket too busy");
  479. }
  480. if (!audit_pid) { /* No daemon */
  481. int offset = ab->nlh ? NLMSG_SPACE(0) : 0;
  482. int len = skb->len - offset;
  483. printk(KERN_ERR "%*.*s\n",
  484. len, len, skb->data + offset);
  485. }
  486. kfree_skb(skb);
  487. ab->nlh = NULL;
  488. }
  489. return 0;
  490. }
  491. /* Initialize audit support at boot time. */
  492. static int __init audit_init(void)
  493. {
  494. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  495. audit_default ? "enabled" : "disabled");
  496. audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
  497. if (!audit_sock)
  498. audit_panic("cannot initialize netlink socket");
  499. audit_initialized = 1;
  500. audit_enabled = audit_default;
  501. audit_log(NULL, "initialized");
  502. return 0;
  503. }
  504. #else
  505. /* Without CONFIG_NET, we have no skbuffs. For now, print what we have
  506. * in the buffer. */
  507. static void audit_log_move(struct audit_buffer *ab)
  508. {
  509. printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
  510. ab->len = 0;
  511. }
  512. static inline int audit_log_drain(struct audit_buffer *ab)
  513. {
  514. return 0;
  515. }
  516. /* Initialize audit support at boot time. */
  517. int __init audit_init(void)
  518. {
  519. printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
  520. audit_sock = NULL;
  521. audit_pid = 0;
  522. audit_initialized = 1;
  523. audit_enabled = audit_default;
  524. audit_log(NULL, "initialized");
  525. return 0;
  526. }
  527. #endif
  528. __initcall(audit_init);
  529. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  530. static int __init audit_enable(char *str)
  531. {
  532. audit_default = !!simple_strtol(str, NULL, 0);
  533. printk(KERN_INFO "audit: %s%s\n",
  534. audit_default ? "enabled" : "disabled",
  535. audit_initialized ? "" : " (after initialization)");
  536. if (audit_initialized)
  537. audit_enabled = audit_default;
  538. return 0;
  539. }
  540. __setup("audit=", audit_enable);
  541. /* Obtain an audit buffer. This routine does locking to obtain the
  542. * audit buffer, but then no locking is required for calls to
  543. * audit_log_*format. If the tsk is a task that is currently in a
  544. * syscall, then the syscall is marked as auditable and an audit record
  545. * will be written at syscall exit. If there is no associated task, tsk
  546. * should be NULL. */
  547. struct audit_buffer *audit_log_start(struct audit_context *ctx)
  548. {
  549. struct audit_buffer *ab = NULL;
  550. unsigned long flags;
  551. struct timespec t;
  552. int serial = 0;
  553. if (!audit_initialized)
  554. return NULL;
  555. if (audit_backlog_limit
  556. && atomic_read(&audit_backlog) > audit_backlog_limit) {
  557. if (audit_rate_check())
  558. printk(KERN_WARNING
  559. "audit: audit_backlog=%d > "
  560. "audit_backlog_limit=%d\n",
  561. atomic_read(&audit_backlog),
  562. audit_backlog_limit);
  563. audit_log_lost("backlog limit exceeded");
  564. return NULL;
  565. }
  566. spin_lock_irqsave(&audit_freelist_lock, flags);
  567. if (!list_empty(&audit_freelist)) {
  568. ab = list_entry(audit_freelist.next,
  569. struct audit_buffer, list);
  570. list_del(&ab->list);
  571. --audit_freelist_count;
  572. }
  573. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  574. if (!ab)
  575. ab = kmalloc(sizeof(*ab), GFP_ATOMIC);
  576. if (!ab) {
  577. audit_log_lost("out of memory in audit_log_start");
  578. return NULL;
  579. }
  580. atomic_inc(&audit_backlog);
  581. skb_queue_head_init(&ab->sklist);
  582. ab->ctx = ctx;
  583. ab->len = 0;
  584. ab->nlh = NULL;
  585. ab->total = 0;
  586. ab->type = AUDIT_KERNEL;
  587. ab->pid = 0;
  588. ab->count = 0;
  589. #ifdef CONFIG_AUDITSYSCALL
  590. if (ab->ctx)
  591. audit_get_stamp(ab->ctx, &t, &serial);
  592. else
  593. #endif
  594. t = CURRENT_TIME;
  595. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  596. t.tv_sec, t.tv_nsec/1000000, serial);
  597. return ab;
  598. }
  599. /* Format an audit message into the audit buffer. If there isn't enough
  600. * room in the audit buffer, more room will be allocated and vsnprint
  601. * will be called a second time. Currently, we assume that a printk
  602. * can't format message larger than 1024 bytes, so we don't either. */
  603. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  604. va_list args)
  605. {
  606. int len, avail;
  607. if (!ab)
  608. return;
  609. avail = sizeof(ab->tmp) - ab->len;
  610. if (avail <= 0) {
  611. audit_log_move(ab);
  612. avail = sizeof(ab->tmp) - ab->len;
  613. }
  614. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  615. if (len >= avail) {
  616. /* The printk buffer is 1024 bytes long, so if we get
  617. * here and AUDIT_BUFSIZ is at least 1024, then we can
  618. * log everything that printk could have logged. */
  619. audit_log_move(ab);
  620. avail = sizeof(ab->tmp) - ab->len;
  621. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  622. }
  623. ab->len += (len < avail) ? len : avail;
  624. ab->total += (len < avail) ? len : avail;
  625. }
  626. /* Format a message into the audit buffer. All the work is done in
  627. * audit_log_vformat. */
  628. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  629. {
  630. va_list args;
  631. if (!ab)
  632. return;
  633. va_start(args, fmt);
  634. audit_log_vformat(ab, fmt, args);
  635. va_end(args);
  636. }
  637. /* This is a helper-function to print the d_path without using a static
  638. * buffer or allocating another buffer in addition to the one in
  639. * audit_buffer. */
  640. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  641. struct dentry *dentry, struct vfsmount *vfsmnt)
  642. {
  643. char *p;
  644. int len, avail;
  645. if (prefix) audit_log_format(ab, " %s", prefix);
  646. if (ab->len > 128)
  647. audit_log_move(ab);
  648. avail = sizeof(ab->tmp) - ab->len;
  649. p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
  650. if (IS_ERR(p)) {
  651. /* FIXME: can we save some information here? */
  652. audit_log_format(ab, "<toolong>");
  653. } else {
  654. /* path isn't at start of buffer */
  655. len = (ab->tmp + sizeof(ab->tmp) - 1) - p;
  656. memmove(ab->tmp + ab->len, p, len);
  657. ab->len += len;
  658. ab->total += len;
  659. }
  660. }
  661. /* Remove queued messages from the audit_txlist and send them to userspace. */
  662. static void audit_tasklet_handler(unsigned long arg)
  663. {
  664. LIST_HEAD(list);
  665. struct audit_buffer *ab;
  666. unsigned long flags;
  667. spin_lock_irqsave(&audit_txlist_lock, flags);
  668. list_splice_init(&audit_txlist, &list);
  669. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  670. while (!list_empty(&list)) {
  671. ab = list_entry(list.next, struct audit_buffer, list);
  672. list_del(&ab->list);
  673. audit_log_end_fast(ab);
  674. }
  675. }
  676. static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
  677. /* The netlink_* functions cannot be called inside an irq context, so
  678. * the audit buffer is places on a queue and a tasklet is scheduled to
  679. * remove them from the queue outside the irq context. May be called in
  680. * any context. */
  681. static void audit_log_end_irq(struct audit_buffer *ab)
  682. {
  683. unsigned long flags;
  684. if (!ab)
  685. return;
  686. spin_lock_irqsave(&audit_txlist_lock, flags);
  687. list_add_tail(&ab->list, &audit_txlist);
  688. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  689. tasklet_schedule(&audit_tasklet);
  690. }
  691. /* Send the message in the audit buffer directly to user space. May not
  692. * be called in an irq context. */
  693. static void audit_log_end_fast(struct audit_buffer *ab)
  694. {
  695. unsigned long flags;
  696. BUG_ON(in_irq());
  697. if (!ab)
  698. return;
  699. if (!audit_rate_check()) {
  700. audit_log_lost("rate limit exceeded");
  701. } else {
  702. audit_log_move(ab);
  703. if (audit_log_drain(ab))
  704. return;
  705. }
  706. atomic_dec(&audit_backlog);
  707. spin_lock_irqsave(&audit_freelist_lock, flags);
  708. if (++audit_freelist_count > AUDIT_MAXFREE)
  709. kfree(ab);
  710. else
  711. list_add(&ab->list, &audit_freelist);
  712. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  713. }
  714. /* Send or queue the message in the audit buffer, depending on the
  715. * current context. (A convenience function that may be called in any
  716. * context.) */
  717. void audit_log_end(struct audit_buffer *ab)
  718. {
  719. if (in_irq())
  720. audit_log_end_irq(ab);
  721. else
  722. audit_log_end_fast(ab);
  723. }
  724. /* Log an audit record. This is a convenience function that calls
  725. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  726. * called in any context. */
  727. void audit_log(struct audit_context *ctx, const char *fmt, ...)
  728. {
  729. struct audit_buffer *ab;
  730. va_list args;
  731. ab = audit_log_start(ctx);
  732. if (ab) {
  733. va_start(args, fmt);
  734. audit_log_vformat(ab, fmt, args);
  735. va_end(args);
  736. audit_log_end(ab);
  737. }
  738. }