audit.c 25 KB

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