audit.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. 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. /* The identity of the user shutting down the audit system. */
  72. uid_t audit_sig_uid = -1;
  73. pid_t audit_sig_pid = -1;
  74. /* Records can be lost in several ways:
  75. 0) [suppressed in audit_alloc]
  76. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  77. 2) out of memory in audit_log_move [alloc_skb]
  78. 3) suppressed due to audit_rate_limit
  79. 4) suppressed due to audit_backlog_limit
  80. */
  81. static atomic_t audit_lost = ATOMIC_INIT(0);
  82. /* The netlink socket. */
  83. static struct sock *audit_sock;
  84. /* There are two lists of audit buffers. The txlist contains audit
  85. * buffers that cannot be sent immediately to the netlink device because
  86. * we are in an irq context (these are sent later in a tasklet).
  87. *
  88. * The second list is a list of pre-allocated audit buffers (if more
  89. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  90. * being placed on the freelist). */
  91. static DEFINE_SPINLOCK(audit_txlist_lock);
  92. static DEFINE_SPINLOCK(audit_freelist_lock);
  93. static int audit_freelist_count = 0;
  94. static LIST_HEAD(audit_txlist);
  95. static LIST_HEAD(audit_freelist);
  96. /* There are three lists of rules -- one to search at task creation
  97. * time, one to search at syscall entry time, and another to search at
  98. * syscall exit time. */
  99. static LIST_HEAD(audit_tsklist);
  100. static LIST_HEAD(audit_entlist);
  101. static LIST_HEAD(audit_extlist);
  102. /* The netlink socket is only to be read by 1 CPU, which lets us assume
  103. * that list additions and deletions never happen simultaneiously in
  104. * auditsc.c */
  105. static DECLARE_MUTEX(audit_netlink_sem);
  106. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  107. * audit records. Since printk uses a 1024 byte buffer, this buffer
  108. * should be at least that large. */
  109. #define AUDIT_BUFSIZ 1024
  110. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  111. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  112. #define AUDIT_MAXFREE (2*NR_CPUS)
  113. /* The audit_buffer is used when formatting an audit record. The caller
  114. * locks briefly to get the record off the freelist or to allocate the
  115. * buffer, and locks briefly to send the buffer to the netlink layer or
  116. * to place it on a transmit queue. Multiple audit_buffers can be in
  117. * use simultaneously. */
  118. struct audit_buffer {
  119. struct list_head list;
  120. struct sk_buff *skb; /* formatted skb ready to send */
  121. struct audit_context *ctx; /* NULL or associated context */
  122. };
  123. void audit_set_type(struct audit_buffer *ab, int type)
  124. {
  125. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  126. nlh->nlmsg_type = type;
  127. }
  128. static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
  129. {
  130. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  131. nlh->nlmsg_pid = pid;
  132. }
  133. struct audit_entry {
  134. struct list_head list;
  135. struct audit_rule rule;
  136. };
  137. static void audit_log_end_irq(struct audit_buffer *ab);
  138. static void audit_log_end_fast(struct audit_buffer *ab);
  139. static void audit_panic(const char *message)
  140. {
  141. switch (audit_failure)
  142. {
  143. case AUDIT_FAIL_SILENT:
  144. break;
  145. case AUDIT_FAIL_PRINTK:
  146. printk(KERN_ERR "audit: %s\n", message);
  147. break;
  148. case AUDIT_FAIL_PANIC:
  149. panic("audit: %s\n", message);
  150. break;
  151. }
  152. }
  153. static inline int audit_rate_check(void)
  154. {
  155. static unsigned long last_check = 0;
  156. static int messages = 0;
  157. static DEFINE_SPINLOCK(lock);
  158. unsigned long flags;
  159. unsigned long now;
  160. unsigned long elapsed;
  161. int retval = 0;
  162. if (!audit_rate_limit) return 1;
  163. spin_lock_irqsave(&lock, flags);
  164. if (++messages < audit_rate_limit) {
  165. retval = 1;
  166. } else {
  167. now = jiffies;
  168. elapsed = now - last_check;
  169. if (elapsed > HZ) {
  170. last_check = now;
  171. messages = 0;
  172. retval = 1;
  173. }
  174. }
  175. spin_unlock_irqrestore(&lock, flags);
  176. return retval;
  177. }
  178. /* Emit at least 1 message per second, even if audit_rate_check is
  179. * throttling. */
  180. void audit_log_lost(const char *message)
  181. {
  182. static unsigned long last_msg = 0;
  183. static DEFINE_SPINLOCK(lock);
  184. unsigned long flags;
  185. unsigned long now;
  186. int print;
  187. atomic_inc(&audit_lost);
  188. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  189. if (!print) {
  190. spin_lock_irqsave(&lock, flags);
  191. now = jiffies;
  192. if (now - last_msg > HZ) {
  193. print = 1;
  194. last_msg = now;
  195. }
  196. spin_unlock_irqrestore(&lock, flags);
  197. }
  198. if (print) {
  199. printk(KERN_WARNING
  200. "audit: audit_lost=%d audit_backlog=%d"
  201. " audit_rate_limit=%d audit_backlog_limit=%d\n",
  202. atomic_read(&audit_lost),
  203. atomic_read(&audit_backlog),
  204. audit_rate_limit,
  205. audit_backlog_limit);
  206. audit_panic(message);
  207. }
  208. }
  209. static int audit_set_rate_limit(int limit, uid_t loginuid)
  210. {
  211. int old = audit_rate_limit;
  212. audit_rate_limit = limit;
  213. audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
  214. audit_rate_limit, old, loginuid);
  215. return old;
  216. }
  217. static int audit_set_backlog_limit(int limit, uid_t loginuid)
  218. {
  219. int old = audit_backlog_limit;
  220. audit_backlog_limit = limit;
  221. audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
  222. audit_backlog_limit, old, loginuid);
  223. return old;
  224. }
  225. static int audit_set_enabled(int state, uid_t loginuid)
  226. {
  227. int old = audit_enabled;
  228. if (state != 0 && state != 1)
  229. return -EINVAL;
  230. audit_enabled = state;
  231. audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
  232. audit_enabled, old, loginuid);
  233. return old;
  234. }
  235. static int audit_set_failure(int state, uid_t loginuid)
  236. {
  237. int old = audit_failure;
  238. if (state != AUDIT_FAIL_SILENT
  239. && state != AUDIT_FAIL_PRINTK
  240. && state != AUDIT_FAIL_PANIC)
  241. return -EINVAL;
  242. audit_failure = state;
  243. audit_log(NULL, "audit_failure=%d old=%d by auid %u",
  244. audit_failure, old, loginuid);
  245. return old;
  246. }
  247. #ifdef CONFIG_NET
  248. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  249. void *payload, int size)
  250. {
  251. struct sk_buff *skb;
  252. struct nlmsghdr *nlh;
  253. int len = NLMSG_SPACE(size);
  254. void *data;
  255. int flags = multi ? NLM_F_MULTI : 0;
  256. int t = done ? NLMSG_DONE : type;
  257. skb = alloc_skb(len, GFP_KERNEL);
  258. if (!skb)
  259. goto nlmsg_failure;
  260. nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
  261. nlh->nlmsg_flags = flags;
  262. data = NLMSG_DATA(nlh);
  263. memcpy(data, payload, size);
  264. netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
  265. return;
  266. nlmsg_failure: /* Used by NLMSG_PUT */
  267. if (skb)
  268. kfree_skb(skb);
  269. }
  270. /*
  271. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  272. * control messages.
  273. */
  274. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  275. {
  276. int err = 0;
  277. switch (msg_type) {
  278. case AUDIT_GET:
  279. case AUDIT_LIST:
  280. case AUDIT_SET:
  281. case AUDIT_ADD:
  282. case AUDIT_DEL:
  283. case AUDIT_SIGNAL_INFO:
  284. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  285. err = -EPERM;
  286. break;
  287. case AUDIT_USER:
  288. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  289. err = -EPERM;
  290. break;
  291. default: /* bad msg */
  292. err = -EINVAL;
  293. }
  294. return err;
  295. }
  296. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  297. {
  298. u32 uid, pid, seq;
  299. void *data;
  300. struct audit_status *status_get, status_set;
  301. int err;
  302. struct audit_buffer *ab;
  303. u16 msg_type = nlh->nlmsg_type;
  304. uid_t loginuid; /* loginuid of sender */
  305. struct audit_sig_info sig_data;
  306. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  307. if (err)
  308. return err;
  309. pid = NETLINK_CREDS(skb)->pid;
  310. uid = NETLINK_CREDS(skb)->uid;
  311. loginuid = NETLINK_CB(skb).loginuid;
  312. seq = nlh->nlmsg_seq;
  313. data = NLMSG_DATA(nlh);
  314. switch (msg_type) {
  315. case AUDIT_GET:
  316. status_set.enabled = audit_enabled;
  317. status_set.failure = audit_failure;
  318. status_set.pid = audit_pid;
  319. status_set.rate_limit = audit_rate_limit;
  320. status_set.backlog_limit = audit_backlog_limit;
  321. status_set.lost = atomic_read(&audit_lost);
  322. status_set.backlog = atomic_read(&audit_backlog);
  323. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  324. &status_set, sizeof(status_set));
  325. break;
  326. case AUDIT_SET:
  327. if (nlh->nlmsg_len < sizeof(struct audit_status))
  328. return -EINVAL;
  329. status_get = (struct audit_status *)data;
  330. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  331. err = audit_set_enabled(status_get->enabled, loginuid);
  332. if (err < 0) return err;
  333. }
  334. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  335. err = audit_set_failure(status_get->failure, loginuid);
  336. if (err < 0) return err;
  337. }
  338. if (status_get->mask & AUDIT_STATUS_PID) {
  339. int old = audit_pid;
  340. audit_pid = status_get->pid;
  341. audit_log(NULL, "audit_pid=%d old=%d by auid %u",
  342. audit_pid, old, loginuid);
  343. }
  344. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  345. audit_set_rate_limit(status_get->rate_limit, loginuid);
  346. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  347. audit_set_backlog_limit(status_get->backlog_limit,
  348. loginuid);
  349. break;
  350. case AUDIT_USER:
  351. ab = audit_log_start(NULL);
  352. if (!ab)
  353. break; /* audit_panic has been called */
  354. audit_log_format(ab,
  355. "user pid=%d uid=%d length=%d loginuid=%u"
  356. " msg='%.1024s'",
  357. pid, uid,
  358. (int)(nlh->nlmsg_len
  359. - ((char *)data - (char *)nlh)),
  360. loginuid, (char *)data);
  361. audit_set_type(ab, AUDIT_USER);
  362. audit_set_pid(ab, pid);
  363. audit_log_end(ab);
  364. break;
  365. case AUDIT_ADD:
  366. case AUDIT_DEL:
  367. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  368. return -EINVAL;
  369. /* fallthrough */
  370. case AUDIT_LIST:
  371. #ifdef CONFIG_AUDITSYSCALL
  372. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  373. uid, seq, data, loginuid);
  374. #else
  375. err = -EOPNOTSUPP;
  376. #endif
  377. break;
  378. case AUDIT_SIGNAL_INFO:
  379. sig_data.uid = audit_sig_uid;
  380. sig_data.pid = audit_sig_pid;
  381. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  382. 0, 0, &sig_data, sizeof(sig_data));
  383. break;
  384. default:
  385. err = -EINVAL;
  386. break;
  387. }
  388. return err < 0 ? err : 0;
  389. }
  390. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  391. * processed by audit_receive_msg. Malformed skbs with wrong length are
  392. * discarded silently. */
  393. static void audit_receive_skb(struct sk_buff *skb)
  394. {
  395. int err;
  396. struct nlmsghdr *nlh;
  397. u32 rlen;
  398. while (skb->len >= NLMSG_SPACE(0)) {
  399. nlh = (struct nlmsghdr *)skb->data;
  400. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  401. return;
  402. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  403. if (rlen > skb->len)
  404. rlen = skb->len;
  405. if ((err = audit_receive_msg(skb, nlh))) {
  406. netlink_ack(skb, nlh, err);
  407. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  408. netlink_ack(skb, nlh, 0);
  409. skb_pull(skb, rlen);
  410. }
  411. }
  412. /* Receive messages from netlink socket. */
  413. static void audit_receive(struct sock *sk, int length)
  414. {
  415. struct sk_buff *skb;
  416. unsigned int qlen;
  417. down(&audit_netlink_sem);
  418. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  419. skb = skb_dequeue(&sk->sk_receive_queue);
  420. audit_receive_skb(skb);
  421. kfree_skb(skb);
  422. }
  423. up(&audit_netlink_sem);
  424. }
  425. /* Grab skbuff from the audit_buffer and send to user space. */
  426. static inline int audit_log_drain(struct audit_buffer *ab)
  427. {
  428. struct sk_buff *skb = ab->skb;
  429. if (skb) {
  430. int retval = 0;
  431. if (audit_pid) {
  432. struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
  433. nlh->nlmsg_len = skb->len - sizeof(*nlh);
  434. skb_get(skb); /* because netlink_* frees */
  435. retval = netlink_unicast(audit_sock, skb, audit_pid,
  436. MSG_DONTWAIT);
  437. }
  438. if (retval == -EAGAIN &&
  439. (atomic_read(&audit_backlog)) < audit_backlog_limit) {
  440. audit_log_end_irq(ab);
  441. return 1;
  442. }
  443. if (retval < 0) {
  444. if (retval == -ECONNREFUSED) {
  445. printk(KERN_ERR
  446. "audit: *NO* daemon at audit_pid=%d\n",
  447. audit_pid);
  448. audit_pid = 0;
  449. } else
  450. audit_log_lost("netlink socket too busy");
  451. }
  452. if (!audit_pid) { /* No daemon */
  453. int offset = NLMSG_SPACE(0);
  454. int len = skb->len - offset;
  455. skb->data[offset + len] = '\0';
  456. printk(KERN_ERR "%s\n", skb->data + offset);
  457. }
  458. }
  459. return 0;
  460. }
  461. /* Initialize audit support at boot time. */
  462. static int __init audit_init(void)
  463. {
  464. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  465. audit_default ? "enabled" : "disabled");
  466. audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
  467. if (!audit_sock)
  468. audit_panic("cannot initialize netlink socket");
  469. audit_initialized = 1;
  470. audit_enabled = audit_default;
  471. audit_log(NULL, "initialized");
  472. return 0;
  473. }
  474. #else
  475. /* Without CONFIG_NET, we have no skbuffs. For now, print what we have
  476. * in the buffer. */
  477. static void audit_log_move(struct audit_buffer *ab)
  478. {
  479. printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
  480. ab->len = 0;
  481. }
  482. static inline int audit_log_drain(struct audit_buffer *ab)
  483. {
  484. return 0;
  485. }
  486. /* Initialize audit support at boot time. */
  487. int __init audit_init(void)
  488. {
  489. printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
  490. audit_sock = NULL;
  491. audit_pid = 0;
  492. audit_initialized = 1;
  493. audit_enabled = audit_default;
  494. audit_log(NULL, "initialized");
  495. return 0;
  496. }
  497. #endif
  498. __initcall(audit_init);
  499. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  500. static int __init audit_enable(char *str)
  501. {
  502. audit_default = !!simple_strtol(str, NULL, 0);
  503. printk(KERN_INFO "audit: %s%s\n",
  504. audit_default ? "enabled" : "disabled",
  505. audit_initialized ? "" : " (after initialization)");
  506. if (audit_initialized)
  507. audit_enabled = audit_default;
  508. return 0;
  509. }
  510. __setup("audit=", audit_enable);
  511. static void audit_buffer_free(struct audit_buffer *ab)
  512. {
  513. unsigned long flags;
  514. if (!ab)
  515. return;
  516. if (ab->skb)
  517. kfree_skb(ab->skb);
  518. atomic_dec(&audit_backlog);
  519. spin_lock_irqsave(&audit_freelist_lock, flags);
  520. if (++audit_freelist_count > AUDIT_MAXFREE)
  521. kfree(ab);
  522. else
  523. list_add(&ab->list, &audit_freelist);
  524. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  525. }
  526. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  527. int gfp_mask)
  528. {
  529. unsigned long flags;
  530. struct audit_buffer *ab = NULL;
  531. struct nlmsghdr *nlh;
  532. spin_lock_irqsave(&audit_freelist_lock, flags);
  533. if (!list_empty(&audit_freelist)) {
  534. ab = list_entry(audit_freelist.next,
  535. struct audit_buffer, list);
  536. list_del(&ab->list);
  537. --audit_freelist_count;
  538. }
  539. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  540. if (!ab) {
  541. ab = kmalloc(sizeof(*ab), gfp_mask);
  542. if (!ab)
  543. goto err;
  544. }
  545. atomic_inc(&audit_backlog);
  546. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  547. if (!ab->skb)
  548. goto err;
  549. ab->ctx = ctx;
  550. nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
  551. nlh->nlmsg_type = AUDIT_KERNEL;
  552. nlh->nlmsg_flags = 0;
  553. nlh->nlmsg_pid = 0;
  554. nlh->nlmsg_seq = 0;
  555. return ab;
  556. err:
  557. audit_buffer_free(ab);
  558. return NULL;
  559. }
  560. /* Obtain an audit buffer. This routine does locking to obtain the
  561. * audit buffer, but then no locking is required for calls to
  562. * audit_log_*format. If the tsk is a task that is currently in a
  563. * syscall, then the syscall is marked as auditable and an audit record
  564. * will be written at syscall exit. If there is no associated task, tsk
  565. * should be NULL. */
  566. struct audit_buffer *audit_log_start(struct audit_context *ctx)
  567. {
  568. struct audit_buffer *ab = NULL;
  569. struct timespec t;
  570. unsigned int serial;
  571. if (!audit_initialized)
  572. return NULL;
  573. if (audit_backlog_limit
  574. && atomic_read(&audit_backlog) > audit_backlog_limit) {
  575. if (audit_rate_check())
  576. printk(KERN_WARNING
  577. "audit: audit_backlog=%d > "
  578. "audit_backlog_limit=%d\n",
  579. atomic_read(&audit_backlog),
  580. audit_backlog_limit);
  581. audit_log_lost("backlog limit exceeded");
  582. return NULL;
  583. }
  584. ab = audit_buffer_alloc(ctx, GFP_ATOMIC);
  585. if (!ab) {
  586. audit_log_lost("out of memory in audit_log_start");
  587. return NULL;
  588. }
  589. #ifdef CONFIG_AUDITSYSCALL
  590. if (ab->ctx)
  591. audit_get_stamp(ab->ctx, &t, &serial);
  592. else
  593. #endif
  594. {
  595. t = CURRENT_TIME;
  596. serial = 0;
  597. }
  598. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  599. t.tv_sec, t.tv_nsec/1000000, serial);
  600. return ab;
  601. }
  602. /**
  603. * audit_expand - expand skb in the audit buffer
  604. * @ab: audit_buffer
  605. *
  606. * Returns 0 (no space) on failed expansion, or available space if
  607. * successful.
  608. */
  609. static inline int audit_expand(struct audit_buffer *ab)
  610. {
  611. struct sk_buff *skb = ab->skb;
  612. int ret = pskb_expand_head(skb, skb_headroom(skb), AUDIT_BUFSIZ,
  613. GFP_ATOMIC);
  614. if (ret < 0) {
  615. audit_log_lost("out of memory in audit_expand");
  616. return 0;
  617. }
  618. return skb_tailroom(skb);
  619. }
  620. /* Format an audit message into the audit buffer. If there isn't enough
  621. * room in the audit buffer, more room will be allocated and vsnprint
  622. * will be called a second time. Currently, we assume that a printk
  623. * can't format message larger than 1024 bytes, so we don't either. */
  624. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  625. va_list args)
  626. {
  627. int len, avail;
  628. struct sk_buff *skb;
  629. if (!ab)
  630. return;
  631. BUG_ON(!ab->skb);
  632. skb = ab->skb;
  633. avail = skb_tailroom(skb);
  634. if (avail == 0) {
  635. avail = audit_expand(ab);
  636. if (!avail)
  637. goto out;
  638. }
  639. len = vsnprintf(skb->tail, avail, fmt, args);
  640. if (len >= avail) {
  641. /* The printk buffer is 1024 bytes long, so if we get
  642. * here and AUDIT_BUFSIZ is at least 1024, then we can
  643. * log everything that printk could have logged. */
  644. avail = audit_expand(ab);
  645. if (!avail)
  646. goto out;
  647. len = vsnprintf(skb->tail, avail, fmt, args);
  648. }
  649. skb_put(skb, (len < avail) ? len : avail);
  650. out:
  651. return;
  652. }
  653. /* Format a message into the audit buffer. All the work is done in
  654. * audit_log_vformat. */
  655. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  656. {
  657. va_list args;
  658. if (!ab)
  659. return;
  660. va_start(args, fmt);
  661. audit_log_vformat(ab, fmt, args);
  662. va_end(args);
  663. }
  664. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
  665. {
  666. int i;
  667. for (i=0; i<len; i++)
  668. audit_log_format(ab, "%02x", buf[i]);
  669. }
  670. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  671. {
  672. const unsigned char *p = string;
  673. while (*p) {
  674. if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
  675. audit_log_hex(ab, string, strlen(string));
  676. return;
  677. }
  678. p++;
  679. }
  680. audit_log_format(ab, "\"%s\"", string);
  681. }
  682. /* This is a helper-function to print the d_path without using a static
  683. * buffer or allocating another buffer in addition to the one in
  684. * audit_buffer. */
  685. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  686. struct dentry *dentry, struct vfsmount *vfsmnt)
  687. {
  688. char *p;
  689. struct sk_buff *skb = ab->skb;
  690. int len, avail;
  691. if (prefix)
  692. audit_log_format(ab, " %s", prefix);
  693. avail = skb_tailroom(skb);
  694. p = d_path(dentry, vfsmnt, skb->tail, avail);
  695. if (IS_ERR(p)) {
  696. /* FIXME: can we save some information here? */
  697. audit_log_format(ab, "<toolong>");
  698. } else {
  699. /* path isn't at start of buffer */
  700. len = ((char *)skb->tail + avail - 1) - p;
  701. memmove(skb->tail, p, len);
  702. skb_put(skb, len);
  703. }
  704. }
  705. /* Remove queued messages from the audit_txlist and send them to userspace. */
  706. static void audit_tasklet_handler(unsigned long arg)
  707. {
  708. LIST_HEAD(list);
  709. struct audit_buffer *ab;
  710. unsigned long flags;
  711. spin_lock_irqsave(&audit_txlist_lock, flags);
  712. list_splice_init(&audit_txlist, &list);
  713. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  714. while (!list_empty(&list)) {
  715. ab = list_entry(list.next, struct audit_buffer, list);
  716. list_del(&ab->list);
  717. audit_log_end_fast(ab);
  718. }
  719. }
  720. static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
  721. /* The netlink_* functions cannot be called inside an irq context, so
  722. * the audit buffer is places on a queue and a tasklet is scheduled to
  723. * remove them from the queue outside the irq context. May be called in
  724. * any context. */
  725. static void audit_log_end_irq(struct audit_buffer *ab)
  726. {
  727. unsigned long flags;
  728. if (!ab)
  729. return;
  730. spin_lock_irqsave(&audit_txlist_lock, flags);
  731. list_add_tail(&ab->list, &audit_txlist);
  732. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  733. tasklet_schedule(&audit_tasklet);
  734. }
  735. /* Send the message in the audit buffer directly to user space. May not
  736. * be called in an irq context. */
  737. static void audit_log_end_fast(struct audit_buffer *ab)
  738. {
  739. BUG_ON(in_irq());
  740. if (!ab)
  741. return;
  742. if (!audit_rate_check()) {
  743. audit_log_lost("rate limit exceeded");
  744. } else {
  745. if (audit_log_drain(ab))
  746. return;
  747. }
  748. audit_buffer_free(ab);
  749. }
  750. /* Send or queue the message in the audit buffer, depending on the
  751. * current context. (A convenience function that may be called in any
  752. * context.) */
  753. void audit_log_end(struct audit_buffer *ab)
  754. {
  755. if (in_irq())
  756. audit_log_end_irq(ab);
  757. else
  758. audit_log_end_fast(ab);
  759. }
  760. /* Log an audit record. This is a convenience function that calls
  761. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  762. * called in any context. */
  763. void audit_log(struct audit_context *ctx, const char *fmt, ...)
  764. {
  765. struct audit_buffer *ab;
  766. va_list args;
  767. ab = audit_log_start(ctx);
  768. if (ab) {
  769. va_start(args, fmt);
  770. audit_log_vformat(ab, fmt, args);
  771. va_end(args);
  772. audit_log_end(ab);
  773. }
  774. }