audit.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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/types.h>
  45. #include <asm/atomic.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. static 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. try_to_freeze();
  258. schedule();
  259. }
  260. __set_current_state(TASK_RUNNING);
  261. remove_wait_queue(&kauditd_wait, &wait);
  262. }
  263. }
  264. }
  265. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  266. void *payload, int size)
  267. {
  268. struct sk_buff *skb;
  269. struct nlmsghdr *nlh;
  270. int len = NLMSG_SPACE(size);
  271. void *data;
  272. int flags = multi ? NLM_F_MULTI : 0;
  273. int t = done ? NLMSG_DONE : type;
  274. skb = alloc_skb(len, GFP_KERNEL);
  275. if (!skb)
  276. return;
  277. nlh = NLMSG_PUT(skb, pid, seq, t, size);
  278. nlh->nlmsg_flags = flags;
  279. data = NLMSG_DATA(nlh);
  280. memcpy(data, payload, size);
  281. /* Ignore failure. It'll only happen if the sender goes away,
  282. because our timeout is set to infinite. */
  283. netlink_unicast(audit_sock, skb, pid, 0);
  284. return;
  285. nlmsg_failure: /* Used by NLMSG_PUT */
  286. if (skb)
  287. kfree_skb(skb);
  288. }
  289. /*
  290. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  291. * control messages.
  292. */
  293. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  294. {
  295. int err = 0;
  296. switch (msg_type) {
  297. case AUDIT_GET:
  298. case AUDIT_LIST:
  299. case AUDIT_SET:
  300. case AUDIT_ADD:
  301. case AUDIT_DEL:
  302. case AUDIT_SIGNAL_INFO:
  303. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  304. err = -EPERM;
  305. break;
  306. case AUDIT_USER:
  307. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  308. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  309. err = -EPERM;
  310. break;
  311. default: /* bad msg */
  312. err = -EINVAL;
  313. }
  314. return err;
  315. }
  316. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  317. {
  318. u32 uid, pid, seq;
  319. void *data;
  320. struct audit_status *status_get, status_set;
  321. int err;
  322. struct audit_buffer *ab;
  323. u16 msg_type = nlh->nlmsg_type;
  324. uid_t loginuid; /* loginuid of sender */
  325. struct audit_sig_info sig_data;
  326. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  327. if (err)
  328. return err;
  329. /* As soon as there's any sign of userspace auditd, start kauditd to talk to it */
  330. if (!kauditd_task)
  331. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  332. if (IS_ERR(kauditd_task)) {
  333. err = PTR_ERR(kauditd_task);
  334. kauditd_task = NULL;
  335. return err;
  336. }
  337. pid = NETLINK_CREDS(skb)->pid;
  338. uid = NETLINK_CREDS(skb)->uid;
  339. loginuid = NETLINK_CB(skb).loginuid;
  340. seq = nlh->nlmsg_seq;
  341. data = NLMSG_DATA(nlh);
  342. switch (msg_type) {
  343. case AUDIT_GET:
  344. status_set.enabled = audit_enabled;
  345. status_set.failure = audit_failure;
  346. status_set.pid = audit_pid;
  347. status_set.rate_limit = audit_rate_limit;
  348. status_set.backlog_limit = audit_backlog_limit;
  349. status_set.lost = atomic_read(&audit_lost);
  350. status_set.backlog = skb_queue_len(&audit_skb_queue);
  351. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  352. &status_set, sizeof(status_set));
  353. break;
  354. case AUDIT_SET:
  355. if (nlh->nlmsg_len < sizeof(struct audit_status))
  356. return -EINVAL;
  357. status_get = (struct audit_status *)data;
  358. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  359. err = audit_set_enabled(status_get->enabled, loginuid);
  360. if (err < 0) return err;
  361. }
  362. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  363. err = audit_set_failure(status_get->failure, loginuid);
  364. if (err < 0) return err;
  365. }
  366. if (status_get->mask & AUDIT_STATUS_PID) {
  367. int old = audit_pid;
  368. audit_pid = status_get->pid;
  369. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  370. "audit_pid=%d old=%d by auid=%u",
  371. audit_pid, old, loginuid);
  372. }
  373. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  374. audit_set_rate_limit(status_get->rate_limit, loginuid);
  375. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  376. audit_set_backlog_limit(status_get->backlog_limit,
  377. loginuid);
  378. break;
  379. case AUDIT_USER:
  380. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  381. if (!audit_enabled && msg_type != AUDIT_USER_AVC)
  382. return 0;
  383. err = audit_filter_user(&NETLINK_CB(skb), msg_type);
  384. if (err == 1) {
  385. err = 0;
  386. ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
  387. if (ab) {
  388. audit_log_format(ab,
  389. "user pid=%d uid=%u auid=%u msg='%.1024s'",
  390. pid, uid, loginuid, (char *)data);
  391. audit_set_pid(ab, pid);
  392. audit_log_end(ab);
  393. }
  394. }
  395. break;
  396. case AUDIT_ADD:
  397. case AUDIT_DEL:
  398. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  399. return -EINVAL;
  400. /* fallthrough */
  401. case AUDIT_LIST:
  402. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  403. uid, seq, data, loginuid);
  404. break;
  405. case AUDIT_SIGNAL_INFO:
  406. sig_data.uid = audit_sig_uid;
  407. sig_data.pid = audit_sig_pid;
  408. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  409. 0, 0, &sig_data, sizeof(sig_data));
  410. break;
  411. default:
  412. err = -EINVAL;
  413. break;
  414. }
  415. return err < 0 ? err : 0;
  416. }
  417. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  418. * processed by audit_receive_msg. Malformed skbs with wrong length are
  419. * discarded silently. */
  420. static void audit_receive_skb(struct sk_buff *skb)
  421. {
  422. int err;
  423. struct nlmsghdr *nlh;
  424. u32 rlen;
  425. while (skb->len >= NLMSG_SPACE(0)) {
  426. nlh = (struct nlmsghdr *)skb->data;
  427. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  428. return;
  429. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  430. if (rlen > skb->len)
  431. rlen = skb->len;
  432. if ((err = audit_receive_msg(skb, nlh))) {
  433. netlink_ack(skb, nlh, err);
  434. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  435. netlink_ack(skb, nlh, 0);
  436. skb_pull(skb, rlen);
  437. }
  438. }
  439. /* Receive messages from netlink socket. */
  440. static void audit_receive(struct sock *sk, int length)
  441. {
  442. struct sk_buff *skb;
  443. unsigned int qlen;
  444. down(&audit_netlink_sem);
  445. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  446. skb = skb_dequeue(&sk->sk_receive_queue);
  447. audit_receive_skb(skb);
  448. kfree_skb(skb);
  449. }
  450. up(&audit_netlink_sem);
  451. }
  452. /* Initialize audit support at boot time. */
  453. static int __init audit_init(void)
  454. {
  455. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  456. audit_default ? "enabled" : "disabled");
  457. audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
  458. THIS_MODULE);
  459. if (!audit_sock)
  460. audit_panic("cannot initialize netlink socket");
  461. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  462. skb_queue_head_init(&audit_skb_queue);
  463. audit_initialized = 1;
  464. audit_enabled = audit_default;
  465. audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
  466. return 0;
  467. }
  468. __initcall(audit_init);
  469. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  470. static int __init audit_enable(char *str)
  471. {
  472. audit_default = !!simple_strtol(str, NULL, 0);
  473. printk(KERN_INFO "audit: %s%s\n",
  474. audit_default ? "enabled" : "disabled",
  475. audit_initialized ? "" : " (after initialization)");
  476. if (audit_initialized)
  477. audit_enabled = audit_default;
  478. return 0;
  479. }
  480. __setup("audit=", audit_enable);
  481. static void audit_buffer_free(struct audit_buffer *ab)
  482. {
  483. unsigned long flags;
  484. if (!ab)
  485. return;
  486. if (ab->skb)
  487. kfree_skb(ab->skb);
  488. spin_lock_irqsave(&audit_freelist_lock, flags);
  489. if (++audit_freelist_count > AUDIT_MAXFREE)
  490. kfree(ab);
  491. else
  492. list_add(&ab->list, &audit_freelist);
  493. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  494. }
  495. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  496. gfp_t gfp_mask, int type)
  497. {
  498. unsigned long flags;
  499. struct audit_buffer *ab = NULL;
  500. struct nlmsghdr *nlh;
  501. spin_lock_irqsave(&audit_freelist_lock, flags);
  502. if (!list_empty(&audit_freelist)) {
  503. ab = list_entry(audit_freelist.next,
  504. struct audit_buffer, list);
  505. list_del(&ab->list);
  506. --audit_freelist_count;
  507. }
  508. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  509. if (!ab) {
  510. ab = kmalloc(sizeof(*ab), gfp_mask);
  511. if (!ab)
  512. goto err;
  513. }
  514. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  515. if (!ab->skb)
  516. goto err;
  517. ab->ctx = ctx;
  518. ab->gfp_mask = gfp_mask;
  519. nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
  520. nlh->nlmsg_type = type;
  521. nlh->nlmsg_flags = 0;
  522. nlh->nlmsg_pid = 0;
  523. nlh->nlmsg_seq = 0;
  524. return ab;
  525. err:
  526. audit_buffer_free(ab);
  527. return NULL;
  528. }
  529. /* Compute a serial number for the audit record. Audit records are
  530. * written to user-space as soon as they are generated, so a complete
  531. * audit record may be written in several pieces. The timestamp of the
  532. * record and this serial number are used by the user-space tools to
  533. * determine which pieces belong to the same audit record. The
  534. * (timestamp,serial) tuple is unique for each syscall and is live from
  535. * syscall entry to syscall exit.
  536. *
  537. * NOTE: Another possibility is to store the formatted records off the
  538. * audit context (for those records that have a context), and emit them
  539. * all at syscall exit. However, this could delay the reporting of
  540. * significant errors until syscall exit (or never, if the system
  541. * halts). */
  542. unsigned int audit_serial(void)
  543. {
  544. static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
  545. static unsigned int serial = 0;
  546. unsigned long flags;
  547. unsigned int ret;
  548. spin_lock_irqsave(&serial_lock, flags);
  549. do {
  550. ret = ++serial;
  551. } while (unlikely(!ret));
  552. spin_unlock_irqrestore(&serial_lock, flags);
  553. return ret;
  554. }
  555. static inline void audit_get_stamp(struct audit_context *ctx,
  556. struct timespec *t, unsigned int *serial)
  557. {
  558. if (ctx)
  559. auditsc_get_stamp(ctx, t, serial);
  560. else {
  561. *t = CURRENT_TIME;
  562. *serial = audit_serial();
  563. }
  564. }
  565. /* Obtain an audit buffer. This routine does locking to obtain the
  566. * audit buffer, but then no locking is required for calls to
  567. * audit_log_*format. If the tsk is a task that is currently in a
  568. * syscall, then the syscall is marked as auditable and an audit record
  569. * will be written at syscall exit. If there is no associated task, tsk
  570. * should be NULL. */
  571. struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
  572. int type)
  573. {
  574. struct audit_buffer *ab = NULL;
  575. struct timespec t;
  576. unsigned int serial;
  577. int reserve;
  578. unsigned long timeout_start = jiffies;
  579. if (!audit_initialized)
  580. return NULL;
  581. if (gfp_mask & __GFP_WAIT)
  582. reserve = 0;
  583. else
  584. reserve = 5; /* Allow atomic callers to go up to five
  585. entries over the normal backlog limit */
  586. while (audit_backlog_limit
  587. && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
  588. if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
  589. && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
  590. /* Wait for auditd to drain the queue a little */
  591. DECLARE_WAITQUEUE(wait, current);
  592. set_current_state(TASK_INTERRUPTIBLE);
  593. add_wait_queue(&audit_backlog_wait, &wait);
  594. if (audit_backlog_limit &&
  595. skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
  596. schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
  597. __set_current_state(TASK_RUNNING);
  598. remove_wait_queue(&audit_backlog_wait, &wait);
  599. continue;
  600. }
  601. if (audit_rate_check())
  602. printk(KERN_WARNING
  603. "audit: audit_backlog=%d > "
  604. "audit_backlog_limit=%d\n",
  605. skb_queue_len(&audit_skb_queue),
  606. audit_backlog_limit);
  607. audit_log_lost("backlog limit exceeded");
  608. audit_backlog_wait_time = audit_backlog_wait_overflow;
  609. wake_up(&audit_backlog_wait);
  610. return NULL;
  611. }
  612. ab = audit_buffer_alloc(ctx, gfp_mask, type);
  613. if (!ab) {
  614. audit_log_lost("out of memory in audit_log_start");
  615. return NULL;
  616. }
  617. audit_get_stamp(ab->ctx, &t, &serial);
  618. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  619. t.tv_sec, t.tv_nsec/1000000, serial);
  620. return ab;
  621. }
  622. /**
  623. * audit_expand - expand skb in the audit buffer
  624. * @ab: audit_buffer
  625. *
  626. * Returns 0 (no space) on failed expansion, or available space if
  627. * successful.
  628. */
  629. static inline int audit_expand(struct audit_buffer *ab, int extra)
  630. {
  631. struct sk_buff *skb = ab->skb;
  632. int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
  633. ab->gfp_mask);
  634. if (ret < 0) {
  635. audit_log_lost("out of memory in audit_expand");
  636. return 0;
  637. }
  638. return skb_tailroom(skb);
  639. }
  640. /* Format an audit message into the audit buffer. If there isn't enough
  641. * room in the audit buffer, more room will be allocated and vsnprint
  642. * will be called a second time. Currently, we assume that a printk
  643. * can't format message larger than 1024 bytes, so we don't either. */
  644. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  645. va_list args)
  646. {
  647. int len, avail;
  648. struct sk_buff *skb;
  649. va_list args2;
  650. if (!ab)
  651. return;
  652. BUG_ON(!ab->skb);
  653. skb = ab->skb;
  654. avail = skb_tailroom(skb);
  655. if (avail == 0) {
  656. avail = audit_expand(ab, AUDIT_BUFSIZ);
  657. if (!avail)
  658. goto out;
  659. }
  660. va_copy(args2, args);
  661. len = vsnprintf(skb->tail, avail, fmt, args);
  662. if (len >= avail) {
  663. /* The printk buffer is 1024 bytes long, so if we get
  664. * here and AUDIT_BUFSIZ is at least 1024, then we can
  665. * log everything that printk could have logged. */
  666. avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  667. if (!avail)
  668. goto out;
  669. len = vsnprintf(skb->tail, avail, fmt, args2);
  670. }
  671. if (len > 0)
  672. skb_put(skb, len);
  673. out:
  674. return;
  675. }
  676. /* Format a message into the audit buffer. All the work is done in
  677. * audit_log_vformat. */
  678. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  679. {
  680. va_list args;
  681. if (!ab)
  682. return;
  683. va_start(args, fmt);
  684. audit_log_vformat(ab, fmt, args);
  685. va_end(args);
  686. }
  687. /* This function will take the passed buf and convert it into a string of
  688. * ascii hex digits. The new string is placed onto the skb. */
  689. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
  690. size_t len)
  691. {
  692. int i, avail, new_len;
  693. unsigned char *ptr;
  694. struct sk_buff *skb;
  695. static const unsigned char *hex = "0123456789ABCDEF";
  696. BUG_ON(!ab->skb);
  697. skb = ab->skb;
  698. avail = skb_tailroom(skb);
  699. new_len = len<<1;
  700. if (new_len >= avail) {
  701. /* Round the buffer request up to the next multiple */
  702. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  703. avail = audit_expand(ab, new_len);
  704. if (!avail)
  705. return;
  706. }
  707. ptr = skb->tail;
  708. for (i=0; i<len; i++) {
  709. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  710. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  711. }
  712. *ptr = 0;
  713. skb_put(skb, len << 1); /* new string is twice the old string */
  714. }
  715. /* This code will escape a string that is passed to it if the string
  716. * contains a control character, unprintable character, double quote mark,
  717. * or a space. Unescaped strings will start and end with a double quote mark.
  718. * Strings that are escaped are printed in hex (2 digits per char). */
  719. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  720. {
  721. const unsigned char *p = string;
  722. while (*p) {
  723. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  724. audit_log_hex(ab, string, strlen(string));
  725. return;
  726. }
  727. p++;
  728. }
  729. audit_log_format(ab, "\"%s\"", string);
  730. }
  731. /* This is a helper-function to print the escaped d_path */
  732. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  733. struct dentry *dentry, struct vfsmount *vfsmnt)
  734. {
  735. char *p, *path;
  736. if (prefix)
  737. audit_log_format(ab, " %s", prefix);
  738. /* We will allow 11 spaces for ' (deleted)' to be appended */
  739. path = kmalloc(PATH_MAX+11, ab->gfp_mask);
  740. if (!path) {
  741. audit_log_format(ab, "<no memory>");
  742. return;
  743. }
  744. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  745. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  746. /* FIXME: can we save some information here? */
  747. audit_log_format(ab, "<too long>");
  748. } else
  749. audit_log_untrustedstring(ab, p);
  750. kfree(path);
  751. }
  752. /* The netlink_* functions cannot be called inside an irq context, so
  753. * the audit buffer is places on a queue and a tasklet is scheduled to
  754. * remove them from the queue outside the irq context. May be called in
  755. * any context. */
  756. void audit_log_end(struct audit_buffer *ab)
  757. {
  758. if (!ab)
  759. return;
  760. if (!audit_rate_check()) {
  761. audit_log_lost("rate limit exceeded");
  762. } else {
  763. if (audit_pid) {
  764. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  765. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  766. skb_queue_tail(&audit_skb_queue, ab->skb);
  767. ab->skb = NULL;
  768. wake_up_interruptible(&kauditd_wait);
  769. } else {
  770. printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
  771. }
  772. }
  773. audit_buffer_free(ab);
  774. }
  775. /* Log an audit record. This is a convenience function that calls
  776. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  777. * called in any context. */
  778. void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
  779. const char *fmt, ...)
  780. {
  781. struct audit_buffer *ab;
  782. va_list args;
  783. ab = audit_log_start(ctx, gfp_mask, type);
  784. if (ab) {
  785. va_start(args, fmt);
  786. audit_log_vformat(ab, fmt, args);
  787. va_end(args);
  788. audit_log_end(ab);
  789. }
  790. }