audit.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* audit.c -- Auditing support
  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/sgrubb/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, uid_t loginuid)
  209. {
  210. int old = audit_rate_limit;
  211. audit_rate_limit = limit;
  212. audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
  213. audit_rate_limit, old, loginuid);
  214. return old;
  215. }
  216. static int audit_set_backlog_limit(int limit, uid_t loginuid)
  217. {
  218. int old = audit_backlog_limit;
  219. audit_backlog_limit = limit;
  220. audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
  221. audit_backlog_limit, old, loginuid);
  222. return old;
  223. }
  224. static int audit_set_enabled(int state, uid_t loginuid)
  225. {
  226. int old = audit_enabled;
  227. if (state != 0 && state != 1)
  228. return -EINVAL;
  229. audit_enabled = state;
  230. audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
  231. audit_enabled, old, loginuid);
  232. return old;
  233. }
  234. static int audit_set_failure(int state, uid_t loginuid)
  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(NULL, "audit_failure=%d old=%d by auid %u",
  243. audit_failure, old, loginuid);
  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. uid_t loginuid; /* loginuid of sender */
  303. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  304. if (err)
  305. return err;
  306. pid = NETLINK_CREDS(skb)->pid;
  307. uid = NETLINK_CREDS(skb)->uid;
  308. loginuid = NETLINK_CB(skb).loginuid;
  309. seq = nlh->nlmsg_seq;
  310. data = NLMSG_DATA(nlh);
  311. switch (msg_type) {
  312. case AUDIT_GET:
  313. status_set.enabled = audit_enabled;
  314. status_set.failure = audit_failure;
  315. status_set.pid = audit_pid;
  316. status_set.rate_limit = audit_rate_limit;
  317. status_set.backlog_limit = audit_backlog_limit;
  318. status_set.lost = atomic_read(&audit_lost);
  319. status_set.backlog = atomic_read(&audit_backlog);
  320. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  321. &status_set, sizeof(status_set));
  322. break;
  323. case AUDIT_SET:
  324. if (nlh->nlmsg_len < sizeof(struct audit_status))
  325. return -EINVAL;
  326. status_get = (struct audit_status *)data;
  327. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  328. err = audit_set_enabled(status_get->enabled, loginuid);
  329. if (err < 0) return err;
  330. }
  331. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  332. err = audit_set_failure(status_get->failure, loginuid);
  333. if (err < 0) return err;
  334. }
  335. if (status_get->mask & AUDIT_STATUS_PID) {
  336. int old = audit_pid;
  337. audit_pid = status_get->pid;
  338. audit_log(NULL, "audit_pid=%d old=%d by auid %u",
  339. audit_pid, old, loginuid);
  340. }
  341. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  342. audit_set_rate_limit(status_get->rate_limit, loginuid);
  343. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  344. audit_set_backlog_limit(status_get->backlog_limit,
  345. loginuid);
  346. break;
  347. case AUDIT_USER:
  348. ab = audit_log_start(NULL);
  349. if (!ab)
  350. break; /* audit_panic has been called */
  351. audit_log_format(ab,
  352. "user pid=%d uid=%d length=%d loginuid=%u"
  353. " msg='%.1024s'",
  354. pid, uid,
  355. (int)(nlh->nlmsg_len
  356. - ((char *)data - (char *)nlh)),
  357. loginuid, (char *)data);
  358. ab->type = AUDIT_USER;
  359. ab->pid = pid;
  360. audit_log_end(ab);
  361. break;
  362. case AUDIT_ADD:
  363. case AUDIT_DEL:
  364. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  365. return -EINVAL;
  366. /* fallthrough */
  367. case AUDIT_LIST:
  368. #ifdef CONFIG_AUDITSYSCALL
  369. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  370. uid, seq, data, loginuid);
  371. #else
  372. err = -EOPNOTSUPP;
  373. #endif
  374. break;
  375. default:
  376. err = -EINVAL;
  377. break;
  378. }
  379. return err < 0 ? err : 0;
  380. }
  381. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  382. * processed by audit_receive_msg. Malformed skbs with wrong length are
  383. * discarded silently. */
  384. static int audit_receive_skb(struct sk_buff *skb)
  385. {
  386. int err;
  387. struct nlmsghdr *nlh;
  388. u32 rlen;
  389. while (skb->len >= NLMSG_SPACE(0)) {
  390. nlh = (struct nlmsghdr *)skb->data;
  391. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  392. return 0;
  393. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  394. if (rlen > skb->len)
  395. rlen = skb->len;
  396. if ((err = audit_receive_msg(skb, nlh))) {
  397. netlink_ack(skb, nlh, err);
  398. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  399. netlink_ack(skb, nlh, 0);
  400. skb_pull(skb, rlen);
  401. }
  402. return 0;
  403. }
  404. /* Receive messages from netlink socket. */
  405. static void audit_receive(struct sock *sk, int length)
  406. {
  407. struct sk_buff *skb;
  408. if (down_trylock(&audit_netlink_sem))
  409. return;
  410. /* FIXME: this must not cause starvation */
  411. while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
  412. if (audit_receive_skb(skb) && skb->len)
  413. skb_queue_head(&sk->sk_receive_queue, skb);
  414. else
  415. kfree_skb(skb);
  416. }
  417. up(&audit_netlink_sem);
  418. }
  419. /* Move data from tmp buffer into an skb. This is an extra copy, and
  420. * that is unfortunate. However, the copy will only occur when a record
  421. * is being written to user space, which is already a high-overhead
  422. * operation. (Elimination of the copy is possible, for example, by
  423. * writing directly into a pre-allocated skb, at the cost of wasting
  424. * memory. */
  425. static void audit_log_move(struct audit_buffer *ab)
  426. {
  427. struct sk_buff *skb;
  428. char *start;
  429. int extra = ab->nlh ? 0 : NLMSG_SPACE(0);
  430. /* possible resubmission */
  431. if (ab->len == 0)
  432. return;
  433. skb = skb_peek(&ab->sklist);
  434. if (!skb || skb_tailroom(skb) <= ab->len + extra) {
  435. skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);
  436. if (!skb) {
  437. ab->len = 0; /* Lose information in ab->tmp */
  438. audit_log_lost("out of memory in audit_log_move");
  439. return;
  440. }
  441. __skb_queue_tail(&ab->sklist, skb);
  442. if (!ab->nlh)
  443. ab->nlh = (struct nlmsghdr *)skb_put(skb,
  444. NLMSG_SPACE(0));
  445. }
  446. start = skb_put(skb, ab->len);
  447. memcpy(start, ab->tmp, ab->len);
  448. ab->len = 0;
  449. }
  450. /* Iterate over the skbuff in the audit_buffer, sending their contents
  451. * to user space. */
  452. static inline int audit_log_drain(struct audit_buffer *ab)
  453. {
  454. struct sk_buff *skb;
  455. while ((skb = skb_dequeue(&ab->sklist))) {
  456. int retval = 0;
  457. if (audit_pid) {
  458. if (ab->nlh) {
  459. ab->nlh->nlmsg_len = ab->total;
  460. ab->nlh->nlmsg_type = ab->type;
  461. ab->nlh->nlmsg_flags = 0;
  462. ab->nlh->nlmsg_seq = 0;
  463. ab->nlh->nlmsg_pid = ab->pid;
  464. }
  465. skb_get(skb); /* because netlink_* frees */
  466. retval = netlink_unicast(audit_sock, skb, audit_pid,
  467. MSG_DONTWAIT);
  468. }
  469. if (retval == -EAGAIN && ab->count < 5) {
  470. ++ab->count;
  471. skb_queue_tail(&ab->sklist, skb);
  472. audit_log_end_irq(ab);
  473. return 1;
  474. }
  475. if (retval < 0) {
  476. if (retval == -ECONNREFUSED) {
  477. printk(KERN_ERR
  478. "audit: *NO* daemon at audit_pid=%d\n",
  479. audit_pid);
  480. audit_pid = 0;
  481. } else
  482. audit_log_lost("netlink socket too busy");
  483. }
  484. if (!audit_pid) { /* No daemon */
  485. int offset = ab->nlh ? NLMSG_SPACE(0) : 0;
  486. int len = skb->len - offset;
  487. skb->data[offset + len] = '\0';
  488. printk(KERN_ERR "%s\n", skb->data + offset);
  489. }
  490. kfree_skb(skb);
  491. ab->nlh = NULL;
  492. }
  493. return 0;
  494. }
  495. /* Initialize audit support at boot time. */
  496. static int __init audit_init(void)
  497. {
  498. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  499. audit_default ? "enabled" : "disabled");
  500. audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
  501. if (!audit_sock)
  502. audit_panic("cannot initialize netlink socket");
  503. audit_initialized = 1;
  504. audit_enabled = audit_default;
  505. audit_log(NULL, "initialized");
  506. return 0;
  507. }
  508. #else
  509. /* Without CONFIG_NET, we have no skbuffs. For now, print what we have
  510. * in the buffer. */
  511. static void audit_log_move(struct audit_buffer *ab)
  512. {
  513. printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
  514. ab->len = 0;
  515. }
  516. static inline int audit_log_drain(struct audit_buffer *ab)
  517. {
  518. return 0;
  519. }
  520. /* Initialize audit support at boot time. */
  521. int __init audit_init(void)
  522. {
  523. printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
  524. audit_sock = NULL;
  525. audit_pid = 0;
  526. audit_initialized = 1;
  527. audit_enabled = audit_default;
  528. audit_log(NULL, "initialized");
  529. return 0;
  530. }
  531. #endif
  532. __initcall(audit_init);
  533. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  534. static int __init audit_enable(char *str)
  535. {
  536. audit_default = !!simple_strtol(str, NULL, 0);
  537. printk(KERN_INFO "audit: %s%s\n",
  538. audit_default ? "enabled" : "disabled",
  539. audit_initialized ? "" : " (after initialization)");
  540. if (audit_initialized)
  541. audit_enabled = audit_default;
  542. return 0;
  543. }
  544. __setup("audit=", audit_enable);
  545. /* Obtain an audit buffer. This routine does locking to obtain the
  546. * audit buffer, but then no locking is required for calls to
  547. * audit_log_*format. If the tsk is a task that is currently in a
  548. * syscall, then the syscall is marked as auditable and an audit record
  549. * will be written at syscall exit. If there is no associated task, tsk
  550. * should be NULL. */
  551. struct audit_buffer *audit_log_start(struct audit_context *ctx)
  552. {
  553. struct audit_buffer *ab = NULL;
  554. unsigned long flags;
  555. struct timespec t;
  556. unsigned int serial;
  557. if (!audit_initialized)
  558. return NULL;
  559. if (audit_backlog_limit
  560. && atomic_read(&audit_backlog) > audit_backlog_limit) {
  561. if (audit_rate_check())
  562. printk(KERN_WARNING
  563. "audit: audit_backlog=%d > "
  564. "audit_backlog_limit=%d\n",
  565. atomic_read(&audit_backlog),
  566. audit_backlog_limit);
  567. audit_log_lost("backlog limit exceeded");
  568. return NULL;
  569. }
  570. spin_lock_irqsave(&audit_freelist_lock, flags);
  571. if (!list_empty(&audit_freelist)) {
  572. ab = list_entry(audit_freelist.next,
  573. struct audit_buffer, list);
  574. list_del(&ab->list);
  575. --audit_freelist_count;
  576. }
  577. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  578. if (!ab)
  579. ab = kmalloc(sizeof(*ab), GFP_ATOMIC);
  580. if (!ab) {
  581. audit_log_lost("out of memory in audit_log_start");
  582. return NULL;
  583. }
  584. atomic_inc(&audit_backlog);
  585. skb_queue_head_init(&ab->sklist);
  586. ab->ctx = ctx;
  587. ab->len = 0;
  588. ab->nlh = NULL;
  589. ab->total = 0;
  590. ab->type = AUDIT_KERNEL;
  591. ab->pid = 0;
  592. ab->count = 0;
  593. #ifdef CONFIG_AUDITSYSCALL
  594. if (ab->ctx)
  595. audit_get_stamp(ab->ctx, &t, &serial);
  596. else
  597. #endif
  598. {
  599. t = CURRENT_TIME;
  600. serial = 0;
  601. }
  602. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  603. t.tv_sec, t.tv_nsec/1000000, serial);
  604. return ab;
  605. }
  606. /* Format an audit message into the audit buffer. If there isn't enough
  607. * room in the audit buffer, more room will be allocated and vsnprint
  608. * will be called a second time. Currently, we assume that a printk
  609. * can't format message larger than 1024 bytes, so we don't either. */
  610. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  611. va_list args)
  612. {
  613. int len, avail;
  614. if (!ab)
  615. return;
  616. avail = sizeof(ab->tmp) - ab->len;
  617. if (avail <= 0) {
  618. audit_log_move(ab);
  619. avail = sizeof(ab->tmp) - ab->len;
  620. }
  621. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  622. if (len >= avail) {
  623. /* The printk buffer is 1024 bytes long, so if we get
  624. * here and AUDIT_BUFSIZ is at least 1024, then we can
  625. * log everything that printk could have logged. */
  626. audit_log_move(ab);
  627. avail = sizeof(ab->tmp) - ab->len;
  628. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  629. }
  630. ab->len += (len < avail) ? len : avail;
  631. ab->total += (len < avail) ? len : avail;
  632. }
  633. /* Format a message into the audit buffer. All the work is done in
  634. * audit_log_vformat. */
  635. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  636. {
  637. va_list args;
  638. if (!ab)
  639. return;
  640. va_start(args, fmt);
  641. audit_log_vformat(ab, fmt, args);
  642. va_end(args);
  643. }
  644. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
  645. {
  646. int i;
  647. for (i=0; i<len; i++)
  648. audit_log_format(ab, "%02x", buf[i]);
  649. }
  650. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  651. {
  652. const unsigned char *p = string;
  653. while (*p) {
  654. if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
  655. audit_log_hex(ab, string, strlen(string));
  656. return;
  657. }
  658. p++;
  659. }
  660. audit_log_format(ab, "\"%s\"", string);
  661. }
  662. /* This is a helper-function to print the d_path without using a static
  663. * buffer or allocating another buffer in addition to the one in
  664. * audit_buffer. */
  665. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  666. struct dentry *dentry, struct vfsmount *vfsmnt)
  667. {
  668. char *p;
  669. int len, avail;
  670. if (prefix) audit_log_format(ab, " %s", prefix);
  671. if (ab->len > 128)
  672. audit_log_move(ab);
  673. avail = sizeof(ab->tmp) - ab->len;
  674. p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
  675. if (IS_ERR(p)) {
  676. /* FIXME: can we save some information here? */
  677. audit_log_format(ab, "<toolong>");
  678. } else {
  679. /* path isn't at start of buffer */
  680. len = (ab->tmp + sizeof(ab->tmp) - 1) - p;
  681. memmove(ab->tmp + ab->len, p, len);
  682. ab->len += len;
  683. ab->total += len;
  684. }
  685. }
  686. /* Remove queued messages from the audit_txlist and send them to userspace. */
  687. static void audit_tasklet_handler(unsigned long arg)
  688. {
  689. LIST_HEAD(list);
  690. struct audit_buffer *ab;
  691. unsigned long flags;
  692. spin_lock_irqsave(&audit_txlist_lock, flags);
  693. list_splice_init(&audit_txlist, &list);
  694. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  695. while (!list_empty(&list)) {
  696. ab = list_entry(list.next, struct audit_buffer, list);
  697. list_del(&ab->list);
  698. audit_log_end_fast(ab);
  699. }
  700. }
  701. static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
  702. /* The netlink_* functions cannot be called inside an irq context, so
  703. * the audit buffer is places on a queue and a tasklet is scheduled to
  704. * remove them from the queue outside the irq context. May be called in
  705. * any context. */
  706. static void audit_log_end_irq(struct audit_buffer *ab)
  707. {
  708. unsigned long flags;
  709. if (!ab)
  710. return;
  711. spin_lock_irqsave(&audit_txlist_lock, flags);
  712. list_add_tail(&ab->list, &audit_txlist);
  713. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  714. tasklet_schedule(&audit_tasklet);
  715. }
  716. /* Send the message in the audit buffer directly to user space. May not
  717. * be called in an irq context. */
  718. static void audit_log_end_fast(struct audit_buffer *ab)
  719. {
  720. unsigned long flags;
  721. BUG_ON(in_irq());
  722. if (!ab)
  723. return;
  724. if (!audit_rate_check()) {
  725. audit_log_lost("rate limit exceeded");
  726. } else {
  727. audit_log_move(ab);
  728. if (audit_log_drain(ab))
  729. return;
  730. }
  731. atomic_dec(&audit_backlog);
  732. spin_lock_irqsave(&audit_freelist_lock, flags);
  733. if (++audit_freelist_count > AUDIT_MAXFREE)
  734. kfree(ab);
  735. else
  736. list_add(&ab->list, &audit_freelist);
  737. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  738. }
  739. /* Send or queue the message in the audit buffer, depending on the
  740. * current context. (A convenience function that may be called in any
  741. * context.) */
  742. void audit_log_end(struct audit_buffer *ab)
  743. {
  744. if (in_irq())
  745. audit_log_end_irq(ab);
  746. else
  747. audit_log_end_fast(ab);
  748. }
  749. /* Log an audit record. This is a convenience function that calls
  750. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  751. * called in any context. */
  752. void audit_log(struct audit_context *ctx, const char *fmt, ...)
  753. {
  754. struct audit_buffer *ab;
  755. va_list args;
  756. ab = audit_log_start(ctx);
  757. if (ab) {
  758. va_start(args, fmt);
  759. audit_log_vformat(ab, fmt, args);
  760. va_end(args);
  761. audit_log_end(ab);
  762. }
  763. }