audit.c 27 KB

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