audit.c 27 KB

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