audit.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. /* The identity of the user shutting down the audit system. */
  73. uid_t audit_sig_uid = -1;
  74. pid_t audit_sig_pid = -1;
  75. /* Records can be lost in several ways:
  76. 0) [suppressed in audit_alloc]
  77. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  78. 2) out of memory in audit_log_move [alloc_skb]
  79. 3) suppressed due to audit_rate_limit
  80. 4) suppressed due to audit_backlog_limit
  81. */
  82. static atomic_t audit_lost = ATOMIC_INIT(0);
  83. /* The netlink socket. */
  84. static struct sock *audit_sock;
  85. /* The audit_freelist is a list of pre-allocated audit buffers (if more
  86. * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
  87. * being placed on the freelist). */
  88. static DEFINE_SPINLOCK(audit_freelist_lock);
  89. static int audit_freelist_count = 0;
  90. static LIST_HEAD(audit_freelist);
  91. static struct sk_buff_head audit_skb_queue;
  92. static struct task_struct *kauditd_task;
  93. static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
  94. /* The netlink socket is only to be read by 1 CPU, which lets us assume
  95. * that list additions and deletions never happen simultaneously in
  96. * auditsc.c */
  97. static DECLARE_MUTEX(audit_netlink_sem);
  98. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  99. * audit records. Since printk uses a 1024 byte buffer, this buffer
  100. * should be at least that large. */
  101. #define AUDIT_BUFSIZ 1024
  102. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  103. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  104. #define AUDIT_MAXFREE (2*NR_CPUS)
  105. /* The audit_buffer is used when formatting an audit record. The caller
  106. * locks briefly to get the record off the freelist or to allocate the
  107. * buffer, and locks briefly to send the buffer to the netlink layer or
  108. * to place it on a transmit queue. Multiple audit_buffers can be in
  109. * use simultaneously. */
  110. struct audit_buffer {
  111. struct list_head list;
  112. struct sk_buff *skb; /* formatted skb ready to send */
  113. struct audit_context *ctx; /* NULL or associated context */
  114. };
  115. static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
  116. {
  117. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  118. nlh->nlmsg_pid = pid;
  119. }
  120. struct audit_entry {
  121. struct list_head list;
  122. struct audit_rule rule;
  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, 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, 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, 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, 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. if (skb) {
  240. if (audit_pid) {
  241. int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
  242. if (err < 0) {
  243. BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
  244. printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
  245. audit_pid = 0;
  246. }
  247. } else {
  248. printk(KERN_ERR "%s\n", skb->data + NLMSG_SPACE(0));
  249. kfree_skb(skb);
  250. }
  251. } else {
  252. DECLARE_WAITQUEUE(wait, current);
  253. set_current_state(TASK_INTERRUPTIBLE);
  254. add_wait_queue(&kauditd_wait, &wait);
  255. if (!skb_queue_len(&audit_skb_queue))
  256. schedule();
  257. __set_current_state(TASK_RUNNING);
  258. remove_wait_queue(&kauditd_wait, &wait);
  259. }
  260. }
  261. }
  262. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  263. void *payload, int size)
  264. {
  265. struct sk_buff *skb;
  266. struct nlmsghdr *nlh;
  267. int len = NLMSG_SPACE(size);
  268. void *data;
  269. int flags = multi ? NLM_F_MULTI : 0;
  270. int t = done ? NLMSG_DONE : type;
  271. skb = alloc_skb(len, GFP_KERNEL);
  272. if (!skb)
  273. return;
  274. nlh = NLMSG_PUT(skb, pid, seq, t, size);
  275. nlh->nlmsg_flags = flags;
  276. data = NLMSG_DATA(nlh);
  277. memcpy(data, payload, size);
  278. /* Ignore failure. It'll only happen if the sender goes away,
  279. because our timeout is set to infinite. */
  280. netlink_unicast(audit_sock, skb, pid, 0);
  281. return;
  282. nlmsg_failure: /* Used by NLMSG_PUT */
  283. if (skb)
  284. kfree_skb(skb);
  285. }
  286. /*
  287. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  288. * control messages.
  289. */
  290. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  291. {
  292. int err = 0;
  293. switch (msg_type) {
  294. case AUDIT_GET:
  295. case AUDIT_LIST:
  296. case AUDIT_SET:
  297. case AUDIT_ADD:
  298. case AUDIT_DEL:
  299. case AUDIT_SIGNAL_INFO:
  300. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  301. err = -EPERM;
  302. break;
  303. case AUDIT_USER:
  304. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  305. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  306. err = -EPERM;
  307. break;
  308. default: /* bad msg */
  309. err = -EINVAL;
  310. }
  311. return err;
  312. }
  313. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  314. {
  315. u32 uid, pid, seq;
  316. void *data;
  317. struct audit_status *status_get, status_set;
  318. int err;
  319. struct audit_buffer *ab;
  320. u16 msg_type = nlh->nlmsg_type;
  321. uid_t loginuid; /* loginuid of sender */
  322. struct audit_sig_info sig_data;
  323. struct task_struct *tsk;
  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, 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. read_lock(&tasklist_lock);
  380. tsk = find_task_by_pid(pid);
  381. if (tsk)
  382. get_task_struct(tsk);
  383. read_unlock(&tasklist_lock);
  384. if (!tsk)
  385. return -ESRCH;
  386. if (audit_filter_user(tsk, msg_type)) {
  387. ab = audit_log_start(NULL, msg_type);
  388. if (ab) {
  389. audit_log_format(ab,
  390. "user pid=%d uid=%u auid=%u msg='%.1024s'",
  391. pid, uid, loginuid, (char *)data);
  392. audit_set_pid(ab, pid);
  393. audit_log_end(ab);
  394. }
  395. }
  396. put_task_struct(tsk);
  397. break;
  398. case AUDIT_ADD:
  399. case AUDIT_DEL:
  400. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  401. return -EINVAL;
  402. /* fallthrough */
  403. case AUDIT_LIST:
  404. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  405. uid, seq, data, loginuid);
  406. break;
  407. case AUDIT_SIGNAL_INFO:
  408. sig_data.uid = audit_sig_uid;
  409. sig_data.pid = audit_sig_pid;
  410. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  411. 0, 0, &sig_data, sizeof(sig_data));
  412. break;
  413. default:
  414. err = -EINVAL;
  415. break;
  416. }
  417. return err < 0 ? err : 0;
  418. }
  419. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  420. * processed by audit_receive_msg. Malformed skbs with wrong length are
  421. * discarded silently. */
  422. static void audit_receive_skb(struct sk_buff *skb)
  423. {
  424. int err;
  425. struct nlmsghdr *nlh;
  426. u32 rlen;
  427. while (skb->len >= NLMSG_SPACE(0)) {
  428. nlh = (struct nlmsghdr *)skb->data;
  429. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  430. return;
  431. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  432. if (rlen > skb->len)
  433. rlen = skb->len;
  434. if ((err = audit_receive_msg(skb, nlh))) {
  435. netlink_ack(skb, nlh, err);
  436. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  437. netlink_ack(skb, nlh, 0);
  438. skb_pull(skb, rlen);
  439. }
  440. }
  441. /* Receive messages from netlink socket. */
  442. static void audit_receive(struct sock *sk, int length)
  443. {
  444. struct sk_buff *skb;
  445. unsigned int qlen;
  446. down(&audit_netlink_sem);
  447. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  448. skb = skb_dequeue(&sk->sk_receive_queue);
  449. audit_receive_skb(skb);
  450. kfree_skb(skb);
  451. }
  452. up(&audit_netlink_sem);
  453. }
  454. /* Initialize audit support at boot time. */
  455. static int __init audit_init(void)
  456. {
  457. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  458. audit_default ? "enabled" : "disabled");
  459. audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
  460. if (!audit_sock)
  461. audit_panic("cannot initialize netlink socket");
  462. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  463. skb_queue_head_init(&audit_skb_queue);
  464. audit_initialized = 1;
  465. audit_enabled = audit_default;
  466. audit_log(NULL, AUDIT_KERNEL, "initialized");
  467. return 0;
  468. }
  469. __initcall(audit_init);
  470. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  471. static int __init audit_enable(char *str)
  472. {
  473. audit_default = !!simple_strtol(str, NULL, 0);
  474. printk(KERN_INFO "audit: %s%s\n",
  475. audit_default ? "enabled" : "disabled",
  476. audit_initialized ? "" : " (after initialization)");
  477. if (audit_initialized)
  478. audit_enabled = audit_default;
  479. return 0;
  480. }
  481. __setup("audit=", audit_enable);
  482. static void audit_buffer_free(struct audit_buffer *ab)
  483. {
  484. unsigned long flags;
  485. if (!ab)
  486. return;
  487. if (ab->skb)
  488. kfree_skb(ab->skb);
  489. spin_lock_irqsave(&audit_freelist_lock, flags);
  490. if (++audit_freelist_count > AUDIT_MAXFREE)
  491. kfree(ab);
  492. else
  493. list_add(&ab->list, &audit_freelist);
  494. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  495. }
  496. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  497. int gfp_mask, int type)
  498. {
  499. unsigned long flags;
  500. struct audit_buffer *ab = NULL;
  501. struct nlmsghdr *nlh;
  502. spin_lock_irqsave(&audit_freelist_lock, flags);
  503. if (!list_empty(&audit_freelist)) {
  504. ab = list_entry(audit_freelist.next,
  505. struct audit_buffer, list);
  506. list_del(&ab->list);
  507. --audit_freelist_count;
  508. }
  509. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  510. if (!ab) {
  511. ab = kmalloc(sizeof(*ab), gfp_mask);
  512. if (!ab)
  513. goto err;
  514. }
  515. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  516. if (!ab->skb)
  517. goto err;
  518. ab->ctx = ctx;
  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. * Atomic values are only guaranteed to be 24-bit, so we count down.
  538. *
  539. * NOTE: Another possibility is to store the formatted records off the
  540. * audit context (for those records that have a context), and emit them
  541. * all at syscall exit. However, this could delay the reporting of
  542. * significant errors until syscall exit (or never, if the system
  543. * halts). */
  544. unsigned int audit_serial(void)
  545. {
  546. static atomic_t serial = ATOMIC_INIT(0xffffff);
  547. unsigned int a, b;
  548. do {
  549. a = atomic_read(&serial);
  550. if (atomic_dec_and_test(&serial))
  551. atomic_set(&serial, 0xffffff);
  552. b = atomic_read(&serial);
  553. } while (b != a - 1);
  554. return 0xffffff - b;
  555. }
  556. static inline void audit_get_stamp(struct audit_context *ctx,
  557. struct timespec *t, unsigned int *serial)
  558. {
  559. if (ctx)
  560. auditsc_get_stamp(ctx, t, serial);
  561. else {
  562. *t = CURRENT_TIME;
  563. *serial = audit_serial();
  564. }
  565. }
  566. /* Obtain an audit buffer. This routine does locking to obtain the
  567. * audit buffer, but then no locking is required for calls to
  568. * audit_log_*format. If the tsk is a task that is currently in a
  569. * syscall, then the syscall is marked as auditable and an audit record
  570. * will be written at syscall exit. If there is no associated task, tsk
  571. * should be NULL. */
  572. struct audit_buffer *audit_log_start(struct audit_context *ctx, int type)
  573. {
  574. struct audit_buffer *ab = NULL;
  575. struct timespec t;
  576. unsigned int serial;
  577. if (!audit_initialized)
  578. return NULL;
  579. if (audit_backlog_limit
  580. && skb_queue_len(&audit_skb_queue) > audit_backlog_limit) {
  581. if (audit_rate_check())
  582. printk(KERN_WARNING
  583. "audit: audit_backlog=%d > "
  584. "audit_backlog_limit=%d\n",
  585. skb_queue_len(&audit_skb_queue),
  586. audit_backlog_limit);
  587. audit_log_lost("backlog limit exceeded");
  588. return NULL;
  589. }
  590. ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type);
  591. if (!ab) {
  592. audit_log_lost("out of memory in audit_log_start");
  593. return NULL;
  594. }
  595. audit_get_stamp(ab->ctx, &t, &serial);
  596. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  597. t.tv_sec, t.tv_nsec/1000000, serial);
  598. return ab;
  599. }
  600. /**
  601. * audit_expand - expand skb in the audit buffer
  602. * @ab: audit_buffer
  603. *
  604. * Returns 0 (no space) on failed expansion, or available space if
  605. * successful.
  606. */
  607. static inline int audit_expand(struct audit_buffer *ab, int extra)
  608. {
  609. struct sk_buff *skb = ab->skb;
  610. int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
  611. GFP_ATOMIC);
  612. if (ret < 0) {
  613. audit_log_lost("out of memory in audit_expand");
  614. return 0;
  615. }
  616. return skb_tailroom(skb);
  617. }
  618. /* Format an audit message into the audit buffer. If there isn't enough
  619. * room in the audit buffer, more room will be allocated and vsnprint
  620. * will be called a second time. Currently, we assume that a printk
  621. * can't format message larger than 1024 bytes, so we don't either. */
  622. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  623. va_list args)
  624. {
  625. int len, avail;
  626. struct sk_buff *skb;
  627. va_list args2;
  628. if (!ab)
  629. return;
  630. BUG_ON(!ab->skb);
  631. skb = ab->skb;
  632. avail = skb_tailroom(skb);
  633. if (avail == 0) {
  634. avail = audit_expand(ab, AUDIT_BUFSIZ);
  635. if (!avail)
  636. goto out;
  637. }
  638. va_copy(args2, args);
  639. len = vsnprintf(skb->tail, avail, fmt, args);
  640. if (len >= avail) {
  641. /* The printk buffer is 1024 bytes long, so if we get
  642. * here and AUDIT_BUFSIZ is at least 1024, then we can
  643. * log everything that printk could have logged. */
  644. avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  645. if (!avail)
  646. goto out;
  647. len = vsnprintf(skb->tail, avail, fmt, args2);
  648. }
  649. if (len > 0)
  650. skb_put(skb, len);
  651. out:
  652. return;
  653. }
  654. /* Format a message into the audit buffer. All the work is done in
  655. * audit_log_vformat. */
  656. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  657. {
  658. va_list args;
  659. if (!ab)
  660. return;
  661. va_start(args, fmt);
  662. audit_log_vformat(ab, fmt, args);
  663. va_end(args);
  664. }
  665. /* This function will take the passed buf and convert it into a string of
  666. * ascii hex digits. The new string is placed onto the skb. */
  667. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
  668. size_t len)
  669. {
  670. int i, avail, new_len;
  671. unsigned char *ptr;
  672. struct sk_buff *skb;
  673. static const unsigned char *hex = "0123456789ABCDEF";
  674. BUG_ON(!ab->skb);
  675. skb = ab->skb;
  676. avail = skb_tailroom(skb);
  677. new_len = len<<1;
  678. if (new_len >= avail) {
  679. /* Round the buffer request up to the next multiple */
  680. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  681. avail = audit_expand(ab, new_len);
  682. if (!avail)
  683. return;
  684. }
  685. ptr = skb->tail;
  686. for (i=0; i<len; i++) {
  687. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  688. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  689. }
  690. *ptr = 0;
  691. skb_put(skb, len << 1); /* new string is twice the old string */
  692. }
  693. /* This code will escape a string that is passed to it if the string
  694. * contains a control character, unprintable character, double quote mark,
  695. * or a space. Unescaped strings will start and end with a double quote mark.
  696. * Strings that are escaped are printed in hex (2 digits per char). */
  697. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  698. {
  699. const unsigned char *p = string;
  700. while (*p) {
  701. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  702. audit_log_hex(ab, string, strlen(string));
  703. return;
  704. }
  705. p++;
  706. }
  707. audit_log_format(ab, "\"%s\"", string);
  708. }
  709. /* This is a helper-function to print the escaped d_path */
  710. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  711. struct dentry *dentry, struct vfsmount *vfsmnt)
  712. {
  713. char *p, *path;
  714. if (prefix)
  715. audit_log_format(ab, " %s", prefix);
  716. /* We will allow 11 spaces for ' (deleted)' to be appended */
  717. path = kmalloc(PATH_MAX+11, GFP_KERNEL);
  718. if (!path) {
  719. audit_log_format(ab, "<no memory>");
  720. return;
  721. }
  722. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  723. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  724. /* FIXME: can we save some information here? */
  725. audit_log_format(ab, "<too long>");
  726. } else
  727. audit_log_untrustedstring(ab, p);
  728. kfree(path);
  729. }
  730. /* The netlink_* functions cannot be called inside an irq context, so
  731. * the audit buffer is places on a queue and a tasklet is scheduled to
  732. * remove them from the queue outside the irq context. May be called in
  733. * any context. */
  734. void audit_log_end(struct audit_buffer *ab)
  735. {
  736. if (!ab)
  737. return;
  738. if (!audit_rate_check()) {
  739. audit_log_lost("rate limit exceeded");
  740. } else {
  741. if (audit_pid) {
  742. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  743. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  744. skb_queue_tail(&audit_skb_queue, ab->skb);
  745. ab->skb = NULL;
  746. wake_up_interruptible(&kauditd_wait);
  747. } else {
  748. printk("%s\n", ab->skb->data + NLMSG_SPACE(0));
  749. }
  750. }
  751. audit_buffer_free(ab);
  752. }
  753. /* Log an audit record. This is a convenience function that calls
  754. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  755. * called in any context. */
  756. void audit_log(struct audit_context *ctx, int type, const char *fmt, ...)
  757. {
  758. struct audit_buffer *ab;
  759. va_list args;
  760. ab = audit_log_start(ctx, type);
  761. if (ab) {
  762. va_start(args, fmt);
  763. audit_log_vformat(ab, fmt, args);
  764. va_end(args);
  765. audit_log_end(ab);
  766. }
  767. }