audit.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. };
  127. void audit_set_type(struct audit_buffer *ab, int type)
  128. {
  129. ab->type = type;
  130. }
  131. struct audit_entry {
  132. struct list_head list;
  133. struct audit_rule rule;
  134. };
  135. static void audit_log_end_irq(struct audit_buffer *ab);
  136. static void audit_log_end_fast(struct audit_buffer *ab);
  137. static void audit_panic(const char *message)
  138. {
  139. switch (audit_failure)
  140. {
  141. case AUDIT_FAIL_SILENT:
  142. break;
  143. case AUDIT_FAIL_PRINTK:
  144. printk(KERN_ERR "audit: %s\n", message);
  145. break;
  146. case AUDIT_FAIL_PANIC:
  147. panic("audit: %s\n", message);
  148. break;
  149. }
  150. }
  151. static inline int audit_rate_check(void)
  152. {
  153. static unsigned long last_check = 0;
  154. static int messages = 0;
  155. static DEFINE_SPINLOCK(lock);
  156. unsigned long flags;
  157. unsigned long now;
  158. unsigned long elapsed;
  159. int retval = 0;
  160. if (!audit_rate_limit) return 1;
  161. spin_lock_irqsave(&lock, flags);
  162. if (++messages < audit_rate_limit) {
  163. retval = 1;
  164. } else {
  165. now = jiffies;
  166. elapsed = now - last_check;
  167. if (elapsed > HZ) {
  168. last_check = now;
  169. messages = 0;
  170. retval = 1;
  171. }
  172. }
  173. spin_unlock_irqrestore(&lock, flags);
  174. return retval;
  175. }
  176. /* Emit at least 1 message per second, even if audit_rate_check is
  177. * throttling. */
  178. void audit_log_lost(const char *message)
  179. {
  180. static unsigned long last_msg = 0;
  181. static DEFINE_SPINLOCK(lock);
  182. unsigned long flags;
  183. unsigned long now;
  184. int print;
  185. atomic_inc(&audit_lost);
  186. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  187. if (!print) {
  188. spin_lock_irqsave(&lock, flags);
  189. now = jiffies;
  190. if (now - last_msg > HZ) {
  191. print = 1;
  192. last_msg = now;
  193. }
  194. spin_unlock_irqrestore(&lock, flags);
  195. }
  196. if (print) {
  197. printk(KERN_WARNING
  198. "audit: audit_lost=%d audit_backlog=%d"
  199. " audit_rate_limit=%d audit_backlog_limit=%d\n",
  200. atomic_read(&audit_lost),
  201. atomic_read(&audit_backlog),
  202. audit_rate_limit,
  203. audit_backlog_limit);
  204. audit_panic(message);
  205. }
  206. }
  207. static int audit_set_rate_limit(int limit, uid_t loginuid)
  208. {
  209. int old = audit_rate_limit;
  210. audit_rate_limit = limit;
  211. audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
  212. audit_rate_limit, old, loginuid);
  213. return old;
  214. }
  215. static int audit_set_backlog_limit(int limit, uid_t loginuid)
  216. {
  217. int old = audit_backlog_limit;
  218. audit_backlog_limit = limit;
  219. audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
  220. audit_backlog_limit, old, loginuid);
  221. return old;
  222. }
  223. static int audit_set_enabled(int state, uid_t loginuid)
  224. {
  225. int old = audit_enabled;
  226. if (state != 0 && state != 1)
  227. return -EINVAL;
  228. audit_enabled = state;
  229. audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
  230. audit_enabled, old, loginuid);
  231. return old;
  232. }
  233. static int audit_set_failure(int state, uid_t loginuid)
  234. {
  235. int old = audit_failure;
  236. if (state != AUDIT_FAIL_SILENT
  237. && state != AUDIT_FAIL_PRINTK
  238. && state != AUDIT_FAIL_PANIC)
  239. return -EINVAL;
  240. audit_failure = state;
  241. audit_log(NULL, "audit_failure=%d old=%d by auid %u",
  242. audit_failure, old, loginuid);
  243. return old;
  244. }
  245. #ifdef CONFIG_NET
  246. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  247. void *payload, int size)
  248. {
  249. struct sk_buff *skb;
  250. struct nlmsghdr *nlh;
  251. int len = NLMSG_SPACE(size);
  252. void *data;
  253. int flags = multi ? NLM_F_MULTI : 0;
  254. int t = done ? NLMSG_DONE : type;
  255. skb = alloc_skb(len, GFP_KERNEL);
  256. if (!skb)
  257. goto nlmsg_failure;
  258. nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
  259. nlh->nlmsg_flags = flags;
  260. data = NLMSG_DATA(nlh);
  261. memcpy(data, payload, size);
  262. netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
  263. return;
  264. nlmsg_failure: /* Used by NLMSG_PUT */
  265. if (skb)
  266. kfree_skb(skb);
  267. }
  268. /*
  269. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  270. * control messages.
  271. */
  272. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  273. {
  274. int err = 0;
  275. switch (msg_type) {
  276. case AUDIT_GET:
  277. case AUDIT_LIST:
  278. case AUDIT_SET:
  279. case AUDIT_ADD:
  280. case AUDIT_DEL:
  281. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  282. err = -EPERM;
  283. break;
  284. case AUDIT_USER:
  285. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  286. err = -EPERM;
  287. break;
  288. default: /* bad msg */
  289. err = -EINVAL;
  290. }
  291. return err;
  292. }
  293. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  294. {
  295. u32 uid, pid, seq;
  296. void *data;
  297. struct audit_status *status_get, status_set;
  298. int err;
  299. struct audit_buffer *ab;
  300. u16 msg_type = nlh->nlmsg_type;
  301. uid_t loginuid; /* loginuid of sender */
  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. loginuid = NETLINK_CB(skb).loginuid;
  308. seq = nlh->nlmsg_seq;
  309. data = NLMSG_DATA(nlh);
  310. switch (msg_type) {
  311. case AUDIT_GET:
  312. status_set.enabled = audit_enabled;
  313. status_set.failure = audit_failure;
  314. status_set.pid = audit_pid;
  315. status_set.rate_limit = audit_rate_limit;
  316. status_set.backlog_limit = audit_backlog_limit;
  317. status_set.lost = atomic_read(&audit_lost);
  318. status_set.backlog = atomic_read(&audit_backlog);
  319. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  320. &status_set, sizeof(status_set));
  321. break;
  322. case AUDIT_SET:
  323. if (nlh->nlmsg_len < sizeof(struct audit_status))
  324. return -EINVAL;
  325. status_get = (struct audit_status *)data;
  326. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  327. err = audit_set_enabled(status_get->enabled, loginuid);
  328. if (err < 0) return err;
  329. }
  330. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  331. err = audit_set_failure(status_get->failure, loginuid);
  332. if (err < 0) return err;
  333. }
  334. if (status_get->mask & AUDIT_STATUS_PID) {
  335. int old = audit_pid;
  336. audit_pid = status_get->pid;
  337. audit_log(NULL, "audit_pid=%d old=%d by auid %u",
  338. audit_pid, old, loginuid);
  339. }
  340. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  341. audit_set_rate_limit(status_get->rate_limit, loginuid);
  342. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  343. audit_set_backlog_limit(status_get->backlog_limit,
  344. loginuid);
  345. break;
  346. case AUDIT_USER:
  347. ab = audit_log_start(NULL);
  348. if (!ab)
  349. break; /* audit_panic has been called */
  350. audit_log_format(ab,
  351. "user pid=%d uid=%d length=%d loginuid=%u"
  352. " msg='%.1024s'",
  353. pid, uid,
  354. (int)(nlh->nlmsg_len
  355. - ((char *)data - (char *)nlh)),
  356. loginuid, (char *)data);
  357. ab->type = AUDIT_USER;
  358. ab->pid = pid;
  359. audit_log_end(ab);
  360. break;
  361. case AUDIT_ADD:
  362. case AUDIT_DEL:
  363. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  364. return -EINVAL;
  365. /* fallthrough */
  366. case AUDIT_LIST:
  367. #ifdef CONFIG_AUDITSYSCALL
  368. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  369. uid, seq, data, loginuid);
  370. #else
  371. err = -EOPNOTSUPP;
  372. #endif
  373. break;
  374. default:
  375. err = -EINVAL;
  376. break;
  377. }
  378. return err < 0 ? err : 0;
  379. }
  380. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  381. * processed by audit_receive_msg. Malformed skbs with wrong length are
  382. * discarded silently. */
  383. static void audit_receive_skb(struct sk_buff *skb)
  384. {
  385. int err;
  386. struct nlmsghdr *nlh;
  387. u32 rlen;
  388. while (skb->len >= NLMSG_SPACE(0)) {
  389. nlh = (struct nlmsghdr *)skb->data;
  390. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  391. return;
  392. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  393. if (rlen > skb->len)
  394. rlen = skb->len;
  395. if ((err = audit_receive_msg(skb, nlh))) {
  396. netlink_ack(skb, nlh, err);
  397. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  398. netlink_ack(skb, nlh, 0);
  399. skb_pull(skb, rlen);
  400. }
  401. }
  402. /* Receive messages from netlink socket. */
  403. static void audit_receive(struct sock *sk, int length)
  404. {
  405. struct sk_buff *skb;
  406. unsigned int qlen;
  407. down(&audit_netlink_sem);
  408. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  409. skb = skb_dequeue(&sk->sk_receive_queue);
  410. audit_receive_skb(skb);
  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_tail(&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 &&
  466. (atomic_read(&audit_backlog)) < audit_backlog_limit) {
  467. skb_queue_head(&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. skb->data[offset + len] = '\0';
  484. printk(KERN_ERR "%s\n", 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. unsigned int serial;
  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. #ifdef CONFIG_AUDITSYSCALL
  589. if (ab->ctx)
  590. audit_get_stamp(ab->ctx, &t, &serial);
  591. else
  592. #endif
  593. {
  594. t = CURRENT_TIME;
  595. serial = 0;
  596. }
  597. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  598. t.tv_sec, t.tv_nsec/1000000, serial);
  599. return ab;
  600. }
  601. /* Format an audit message into the audit buffer. If there isn't enough
  602. * room in the audit buffer, more room will be allocated and vsnprint
  603. * will be called a second time. Currently, we assume that a printk
  604. * can't format message larger than 1024 bytes, so we don't either. */
  605. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  606. va_list args)
  607. {
  608. int len, avail;
  609. if (!ab)
  610. return;
  611. avail = sizeof(ab->tmp) - ab->len;
  612. if (avail <= 0) {
  613. audit_log_move(ab);
  614. avail = sizeof(ab->tmp) - ab->len;
  615. }
  616. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  617. if (len >= avail) {
  618. /* The printk buffer is 1024 bytes long, so if we get
  619. * here and AUDIT_BUFSIZ is at least 1024, then we can
  620. * log everything that printk could have logged. */
  621. audit_log_move(ab);
  622. avail = sizeof(ab->tmp) - ab->len;
  623. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  624. }
  625. ab->len += (len < avail) ? len : avail;
  626. ab->total += (len < avail) ? len : avail;
  627. }
  628. /* Format a message into the audit buffer. All the work is done in
  629. * audit_log_vformat. */
  630. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  631. {
  632. va_list args;
  633. if (!ab)
  634. return;
  635. va_start(args, fmt);
  636. audit_log_vformat(ab, fmt, args);
  637. va_end(args);
  638. }
  639. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
  640. {
  641. int i;
  642. for (i=0; i<len; i++)
  643. audit_log_format(ab, "%02x", buf[i]);
  644. }
  645. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  646. {
  647. const unsigned char *p = string;
  648. while (*p) {
  649. if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
  650. audit_log_hex(ab, string, strlen(string));
  651. return;
  652. }
  653. p++;
  654. }
  655. audit_log_format(ab, "\"%s\"", string);
  656. }
  657. /* This is a helper-function to print the d_path without using a static
  658. * buffer or allocating another buffer in addition to the one in
  659. * audit_buffer. */
  660. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  661. struct dentry *dentry, struct vfsmount *vfsmnt)
  662. {
  663. char *p;
  664. int len, avail;
  665. if (prefix) audit_log_format(ab, " %s", prefix);
  666. if (ab->len > 128)
  667. audit_log_move(ab);
  668. avail = sizeof(ab->tmp) - ab->len;
  669. p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
  670. if (IS_ERR(p)) {
  671. /* FIXME: can we save some information here? */
  672. audit_log_format(ab, "<toolong>");
  673. } else {
  674. /* path isn't at start of buffer */
  675. len = (ab->tmp + sizeof(ab->tmp) - 1) - p;
  676. memmove(ab->tmp + ab->len, p, len);
  677. ab->len += len;
  678. ab->total += len;
  679. }
  680. }
  681. /* Remove queued messages from the audit_txlist and send them to userspace. */
  682. static void audit_tasklet_handler(unsigned long arg)
  683. {
  684. LIST_HEAD(list);
  685. struct audit_buffer *ab;
  686. unsigned long flags;
  687. spin_lock_irqsave(&audit_txlist_lock, flags);
  688. list_splice_init(&audit_txlist, &list);
  689. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  690. while (!list_empty(&list)) {
  691. ab = list_entry(list.next, struct audit_buffer, list);
  692. list_del(&ab->list);
  693. audit_log_end_fast(ab);
  694. }
  695. }
  696. static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
  697. /* The netlink_* functions cannot be called inside an irq context, so
  698. * the audit buffer is places on a queue and a tasklet is scheduled to
  699. * remove them from the queue outside the irq context. May be called in
  700. * any context. */
  701. static void audit_log_end_irq(struct audit_buffer *ab)
  702. {
  703. unsigned long flags;
  704. if (!ab)
  705. return;
  706. spin_lock_irqsave(&audit_txlist_lock, flags);
  707. list_add_tail(&ab->list, &audit_txlist);
  708. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  709. tasklet_schedule(&audit_tasklet);
  710. }
  711. /* Send the message in the audit buffer directly to user space. May not
  712. * be called in an irq context. */
  713. static void audit_log_end_fast(struct audit_buffer *ab)
  714. {
  715. unsigned long flags;
  716. BUG_ON(in_irq());
  717. if (!ab)
  718. return;
  719. if (!audit_rate_check()) {
  720. audit_log_lost("rate limit exceeded");
  721. } else {
  722. audit_log_move(ab);
  723. if (audit_log_drain(ab))
  724. return;
  725. }
  726. atomic_dec(&audit_backlog);
  727. spin_lock_irqsave(&audit_freelist_lock, flags);
  728. if (++audit_freelist_count > AUDIT_MAXFREE)
  729. kfree(ab);
  730. else
  731. list_add(&ab->list, &audit_freelist);
  732. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  733. }
  734. /* Send or queue the message in the audit buffer, depending on the
  735. * current context. (A convenience function that may be called in any
  736. * context.) */
  737. void audit_log_end(struct audit_buffer *ab)
  738. {
  739. if (in_irq())
  740. audit_log_end_irq(ab);
  741. else
  742. audit_log_end_fast(ab);
  743. }
  744. /* Log an audit record. This is a convenience function that calls
  745. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  746. * called in any context. */
  747. void audit_log(struct audit_context *ctx, const char *fmt, ...)
  748. {
  749. struct audit_buffer *ab;
  750. va_list args;
  751. ab = audit_log_start(ctx);
  752. if (ab) {
  753. va_start(args, fmt);
  754. audit_log_vformat(ab, fmt, args);
  755. va_end(args);
  756. audit_log_end(ab);
  757. }
  758. }