audit.c 28 KB

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