audit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  310. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  311. err = -EPERM;
  312. break;
  313. default: /* bad msg */
  314. err = -EINVAL;
  315. }
  316. return err;
  317. }
  318. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  319. {
  320. u32 uid, pid, seq;
  321. void *data;
  322. struct audit_status *status_get, status_set;
  323. int err;
  324. struct audit_buffer *ab;
  325. u16 msg_type = nlh->nlmsg_type;
  326. uid_t loginuid; /* loginuid of sender */
  327. struct audit_sig_info sig_data;
  328. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  329. if (err)
  330. return err;
  331. /* As soon as there's any sign of userspace auditd, start kauditd to talk to it */
  332. if (!kauditd_task)
  333. kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
  334. if (IS_ERR(kauditd_task)) {
  335. err = PTR_ERR(kauditd_task);
  336. kauditd_task = NULL;
  337. return err;
  338. }
  339. pid = NETLINK_CREDS(skb)->pid;
  340. uid = NETLINK_CREDS(skb)->uid;
  341. loginuid = NETLINK_CB(skb).loginuid;
  342. seq = nlh->nlmsg_seq;
  343. data = NLMSG_DATA(nlh);
  344. switch (msg_type) {
  345. case AUDIT_GET:
  346. status_set.enabled = audit_enabled;
  347. status_set.failure = audit_failure;
  348. status_set.pid = audit_pid;
  349. status_set.rate_limit = audit_rate_limit;
  350. status_set.backlog_limit = audit_backlog_limit;
  351. status_set.lost = atomic_read(&audit_lost);
  352. status_set.backlog = skb_queue_len(&audit_skb_queue);
  353. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  354. &status_set, sizeof(status_set));
  355. break;
  356. case AUDIT_SET:
  357. if (nlh->nlmsg_len < sizeof(struct audit_status))
  358. return -EINVAL;
  359. status_get = (struct audit_status *)data;
  360. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  361. err = audit_set_enabled(status_get->enabled, loginuid);
  362. if (err < 0) return err;
  363. }
  364. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  365. err = audit_set_failure(status_get->failure, loginuid);
  366. if (err < 0) return err;
  367. }
  368. if (status_get->mask & AUDIT_STATUS_PID) {
  369. int old = audit_pid;
  370. audit_pid = status_get->pid;
  371. audit_log(NULL, AUDIT_CONFIG_CHANGE,
  372. "audit_pid=%d old=%d by auid %u",
  373. audit_pid, old, loginuid);
  374. }
  375. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  376. audit_set_rate_limit(status_get->rate_limit, loginuid);
  377. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  378. audit_set_backlog_limit(status_get->backlog_limit,
  379. loginuid);
  380. break;
  381. case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
  382. ab = audit_log_start(NULL, msg_type);
  383. if (!ab)
  384. break; /* audit_panic has been called */
  385. audit_log_format(ab,
  386. "user pid=%d uid=%d length=%d loginuid=%u"
  387. " msg='%.1024s'",
  388. pid, uid,
  389. (int)(nlh->nlmsg_len
  390. - ((char *)data - (char *)nlh)),
  391. loginuid, (char *)data);
  392. audit_set_pid(ab, pid);
  393. audit_log_end(ab);
  394. break;
  395. case AUDIT_ADD:
  396. case AUDIT_DEL:
  397. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  398. return -EINVAL;
  399. /* fallthrough */
  400. case AUDIT_LIST:
  401. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  402. uid, seq, data, loginuid);
  403. break;
  404. case AUDIT_SIGNAL_INFO:
  405. sig_data.uid = audit_sig_uid;
  406. sig_data.pid = audit_sig_pid;
  407. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  408. 0, 0, &sig_data, sizeof(sig_data));
  409. break;
  410. default:
  411. err = -EINVAL;
  412. break;
  413. }
  414. return err < 0 ? err : 0;
  415. }
  416. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  417. * processed by audit_receive_msg. Malformed skbs with wrong length are
  418. * discarded silently. */
  419. static void audit_receive_skb(struct sk_buff *skb)
  420. {
  421. int err;
  422. struct nlmsghdr *nlh;
  423. u32 rlen;
  424. while (skb->len >= NLMSG_SPACE(0)) {
  425. nlh = (struct nlmsghdr *)skb->data;
  426. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  427. return;
  428. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  429. if (rlen > skb->len)
  430. rlen = skb->len;
  431. if ((err = audit_receive_msg(skb, nlh))) {
  432. netlink_ack(skb, nlh, err);
  433. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  434. netlink_ack(skb, nlh, 0);
  435. skb_pull(skb, rlen);
  436. }
  437. }
  438. /* Receive messages from netlink socket. */
  439. static void audit_receive(struct sock *sk, int length)
  440. {
  441. struct sk_buff *skb;
  442. unsigned int qlen;
  443. down(&audit_netlink_sem);
  444. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  445. skb = skb_dequeue(&sk->sk_receive_queue);
  446. audit_receive_skb(skb);
  447. kfree_skb(skb);
  448. }
  449. up(&audit_netlink_sem);
  450. }
  451. /* Initialize audit support at boot time. */
  452. static int __init audit_init(void)
  453. {
  454. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  455. audit_default ? "enabled" : "disabled");
  456. audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
  457. if (!audit_sock)
  458. audit_panic("cannot initialize netlink socket");
  459. audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
  460. skb_queue_head_init(&audit_skb_queue);
  461. audit_initialized = 1;
  462. audit_enabled = audit_default;
  463. audit_log(NULL, AUDIT_KERNEL, "initialized");
  464. return 0;
  465. }
  466. __initcall(audit_init);
  467. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  468. static int __init audit_enable(char *str)
  469. {
  470. audit_default = !!simple_strtol(str, NULL, 0);
  471. printk(KERN_INFO "audit: %s%s\n",
  472. audit_default ? "enabled" : "disabled",
  473. audit_initialized ? "" : " (after initialization)");
  474. if (audit_initialized)
  475. audit_enabled = audit_default;
  476. return 0;
  477. }
  478. __setup("audit=", audit_enable);
  479. static void audit_buffer_free(struct audit_buffer *ab)
  480. {
  481. unsigned long flags;
  482. if (!ab)
  483. return;
  484. if (ab->skb)
  485. kfree_skb(ab->skb);
  486. spin_lock_irqsave(&audit_freelist_lock, flags);
  487. if (++audit_freelist_count > AUDIT_MAXFREE)
  488. kfree(ab);
  489. else
  490. list_add(&ab->list, &audit_freelist);
  491. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  492. }
  493. static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
  494. int gfp_mask, int type)
  495. {
  496. unsigned long flags;
  497. struct audit_buffer *ab = NULL;
  498. struct nlmsghdr *nlh;
  499. spin_lock_irqsave(&audit_freelist_lock, flags);
  500. if (!list_empty(&audit_freelist)) {
  501. ab = list_entry(audit_freelist.next,
  502. struct audit_buffer, list);
  503. list_del(&ab->list);
  504. --audit_freelist_count;
  505. }
  506. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  507. if (!ab) {
  508. ab = kmalloc(sizeof(*ab), gfp_mask);
  509. if (!ab)
  510. goto err;
  511. }
  512. ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
  513. if (!ab->skb)
  514. goto err;
  515. ab->ctx = ctx;
  516. nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
  517. nlh->nlmsg_type = type;
  518. nlh->nlmsg_flags = 0;
  519. nlh->nlmsg_pid = 0;
  520. nlh->nlmsg_seq = 0;
  521. return ab;
  522. err:
  523. audit_buffer_free(ab);
  524. return NULL;
  525. }
  526. /* Obtain an audit buffer. This routine does locking to obtain the
  527. * audit buffer, but then no locking is required for calls to
  528. * audit_log_*format. If the tsk is a task that is currently in a
  529. * syscall, then the syscall is marked as auditable and an audit record
  530. * will be written at syscall exit. If there is no associated task, tsk
  531. * should be NULL. */
  532. struct audit_buffer *audit_log_start(struct audit_context *ctx, int type)
  533. {
  534. struct audit_buffer *ab = NULL;
  535. struct timespec t;
  536. unsigned int serial;
  537. if (!audit_initialized)
  538. return NULL;
  539. ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type);
  540. if (!ab) {
  541. audit_log_lost("out of memory in audit_log_start");
  542. return NULL;
  543. }
  544. if (!audit_get_stamp(ab->ctx, &t, &serial)) {
  545. t = CURRENT_TIME;
  546. serial = 0;
  547. }
  548. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  549. t.tv_sec, t.tv_nsec/1000000, serial);
  550. return ab;
  551. }
  552. /**
  553. * audit_expand - expand skb in the audit buffer
  554. * @ab: audit_buffer
  555. *
  556. * Returns 0 (no space) on failed expansion, or available space if
  557. * successful.
  558. */
  559. static inline int audit_expand(struct audit_buffer *ab, int extra)
  560. {
  561. struct sk_buff *skb = ab->skb;
  562. int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
  563. GFP_ATOMIC);
  564. if (ret < 0) {
  565. audit_log_lost("out of memory in audit_expand");
  566. return 0;
  567. }
  568. return skb_tailroom(skb);
  569. }
  570. /* Format an audit message into the audit buffer. If there isn't enough
  571. * room in the audit buffer, more room will be allocated and vsnprint
  572. * will be called a second time. Currently, we assume that a printk
  573. * can't format message larger than 1024 bytes, so we don't either. */
  574. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  575. va_list args)
  576. {
  577. int len, avail;
  578. struct sk_buff *skb;
  579. va_list args2;
  580. if (!ab)
  581. return;
  582. BUG_ON(!ab->skb);
  583. skb = ab->skb;
  584. avail = skb_tailroom(skb);
  585. if (avail == 0) {
  586. avail = audit_expand(ab, AUDIT_BUFSIZ);
  587. if (!avail)
  588. goto out;
  589. }
  590. va_copy(args2, args);
  591. len = vsnprintf(skb->tail, avail, fmt, args);
  592. if (len >= avail) {
  593. /* The printk buffer is 1024 bytes long, so if we get
  594. * here and AUDIT_BUFSIZ is at least 1024, then we can
  595. * log everything that printk could have logged. */
  596. avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
  597. if (!avail)
  598. goto out;
  599. len = vsnprintf(skb->tail, avail, fmt, args2);
  600. }
  601. if (len > 0)
  602. skb_put(skb, len);
  603. out:
  604. return;
  605. }
  606. /* Format a message into the audit buffer. All the work is done in
  607. * audit_log_vformat. */
  608. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  609. {
  610. va_list args;
  611. if (!ab)
  612. return;
  613. va_start(args, fmt);
  614. audit_log_vformat(ab, fmt, args);
  615. va_end(args);
  616. }
  617. /* This function will take the passed buf and convert it into a string of
  618. * ascii hex digits. The new string is placed onto the skb. */
  619. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
  620. size_t len)
  621. {
  622. int i, avail, new_len;
  623. unsigned char *ptr;
  624. struct sk_buff *skb;
  625. static const unsigned char *hex = "0123456789ABCDEF";
  626. BUG_ON(!ab->skb);
  627. skb = ab->skb;
  628. avail = skb_tailroom(skb);
  629. new_len = len<<1;
  630. if (new_len >= avail) {
  631. /* Round the buffer request up to the next multiple */
  632. new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
  633. avail = audit_expand(ab, new_len);
  634. if (!avail)
  635. return;
  636. }
  637. ptr = skb->tail;
  638. for (i=0; i<len; i++) {
  639. *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
  640. *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
  641. }
  642. *ptr = 0;
  643. skb_put(skb, len << 1); /* new string is twice the old string */
  644. }
  645. /* This code will escape a string that is passed to it if the string
  646. * contains a control character, unprintable character, double quote mark,
  647. * or a space. Unescaped strings will start and end with a double quote mark.
  648. * Strings that are escaped are printed in hex (2 digits per char). */
  649. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  650. {
  651. const unsigned char *p = string;
  652. while (*p) {
  653. if (*p == '"' || *p < 0x21 || *p > 0x7f) {
  654. audit_log_hex(ab, string, strlen(string));
  655. return;
  656. }
  657. p++;
  658. }
  659. audit_log_format(ab, "\"%s\"", string);
  660. }
  661. /* This is a helper-function to print the escaped d_path */
  662. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  663. struct dentry *dentry, struct vfsmount *vfsmnt)
  664. {
  665. char *p, *path;
  666. if (prefix)
  667. audit_log_format(ab, " %s", prefix);
  668. /* We will allow 11 spaces for ' (deleted)' to be appended */
  669. path = kmalloc(PATH_MAX+11, GFP_KERNEL);
  670. if (!path) {
  671. audit_log_format(ab, "<no memory>");
  672. return;
  673. }
  674. p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
  675. if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
  676. /* FIXME: can we save some information here? */
  677. audit_log_format(ab, "<too long>");
  678. } else
  679. audit_log_untrustedstring(ab, p);
  680. kfree(path);
  681. }
  682. /* The netlink_* functions cannot be called inside an irq context, so
  683. * the audit buffer is places on a queue and a tasklet is scheduled to
  684. * remove them from the queue outside the irq context. May be called in
  685. * any context. */
  686. void audit_log_end(struct audit_buffer *ab)
  687. {
  688. if (!ab)
  689. return;
  690. if (!audit_rate_check()) {
  691. audit_log_lost("rate limit exceeded");
  692. } else {
  693. if (audit_pid) {
  694. struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
  695. nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
  696. skb_queue_tail(&audit_skb_queue, ab->skb);
  697. ab->skb = NULL;
  698. wake_up_interruptible(&kauditd_wait);
  699. } else {
  700. printk("%s\n", ab->skb->data + NLMSG_SPACE(0));
  701. }
  702. }
  703. audit_buffer_free(ab);
  704. }
  705. /* Log an audit record. This is a convenience function that calls
  706. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  707. * called in any context. */
  708. void audit_log(struct audit_context *ctx, int type, const char *fmt, ...)
  709. {
  710. struct audit_buffer *ab;
  711. va_list args;
  712. ab = audit_log_start(ctx, type);
  713. if (ab) {
  714. va_start(args, fmt);
  715. audit_log_vformat(ab, fmt, args);
  716. va_end(args);
  717. audit_log_end(ab);
  718. }
  719. }