audit.c 24 KB

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