audit.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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/audit.h>
  49. #include <net/sock.h>
  50. #include <linux/skbuff.h>
  51. #include <linux/netlink.h>
  52. /* No auditing will take place until audit_initialized != 0.
  53. * (Initialization happens after skb_init is called.) */
  54. static int audit_initialized;
  55. /* No syscall auditing will take place unless audit_enabled != 0. */
  56. int audit_enabled;
  57. /* Default state when kernel boots without any parameters. */
  58. static int audit_default;
  59. /* If auditing cannot proceed, audit_failure selects what happens. */
  60. static int audit_failure = AUDIT_FAIL_PRINTK;
  61. /* If audit records are to be written to the netlink socket, audit_pid
  62. * contains the (non-zero) pid. */
  63. int audit_pid;
  64. /* If audit_limit is non-zero, limit the rate of sending audit records
  65. * to that number per second. This prevents DoS attacks, but results in
  66. * audit records being dropped. */
  67. static int audit_rate_limit;
  68. /* Number of outstanding audit_buffers allowed. */
  69. static int audit_backlog_limit = 64;
  70. static atomic_t audit_backlog = ATOMIC_INIT(0);
  71. /* The identity of the user shutting down the audit system. */
  72. uid_t audit_sig_uid = -1;
  73. pid_t audit_sig_pid = -1;
  74. /* Records can be lost in several ways:
  75. 0) [suppressed in audit_alloc]
  76. 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
  77. 2) out of memory in audit_log_move [alloc_skb]
  78. 3) suppressed due to audit_rate_limit
  79. 4) suppressed due to audit_backlog_limit
  80. */
  81. static atomic_t audit_lost = ATOMIC_INIT(0);
  82. /* The netlink socket. */
  83. static struct sock *audit_sock;
  84. /* There are two lists of audit buffers. The txlist contains audit
  85. * buffers that cannot be sent immediately to the netlink device because
  86. * we are in an irq context (these are sent later in a tasklet).
  87. *
  88. * The second list 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_txlist_lock);
  92. static DEFINE_SPINLOCK(audit_freelist_lock);
  93. static int audit_freelist_count = 0;
  94. static LIST_HEAD(audit_txlist);
  95. static LIST_HEAD(audit_freelist);
  96. /* There are three lists of rules -- one to search at task creation
  97. * time, one to search at syscall entry time, and another to search at
  98. * syscall exit time. */
  99. static LIST_HEAD(audit_tsklist);
  100. static LIST_HEAD(audit_entlist);
  101. static LIST_HEAD(audit_extlist);
  102. /* The netlink socket is only to be read by 1 CPU, which lets us assume
  103. * that list additions and deletions never happen simultaneiously in
  104. * auditsc.c */
  105. static DECLARE_MUTEX(audit_netlink_sem);
  106. /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  107. * audit records. Since printk uses a 1024 byte buffer, this buffer
  108. * should be at least that large. */
  109. #define AUDIT_BUFSIZ 1024
  110. /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
  111. * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
  112. #define AUDIT_MAXFREE (2*NR_CPUS)
  113. /* The audit_buffer is used when formatting an audit record. The caller
  114. * locks briefly to get the record off the freelist or to allocate the
  115. * buffer, and locks briefly to send the buffer to the netlink layer or
  116. * to place it on a transmit queue. Multiple audit_buffers can be in
  117. * use simultaneously. */
  118. struct audit_buffer {
  119. struct list_head list;
  120. struct sk_buff_head sklist; /* formatted skbs ready to send */
  121. struct audit_context *ctx; /* NULL or associated context */
  122. int len; /* used area of tmp */
  123. char tmp[AUDIT_BUFSIZ];
  124. /* Pointer to header and contents */
  125. struct nlmsghdr *nlh;
  126. int total;
  127. int type;
  128. int pid;
  129. };
  130. void audit_set_type(struct audit_buffer *ab, int type)
  131. {
  132. ab->type = type;
  133. }
  134. struct audit_entry {
  135. struct list_head list;
  136. struct audit_rule rule;
  137. };
  138. static void audit_log_end_irq(struct audit_buffer *ab);
  139. static void audit_log_end_fast(struct audit_buffer *ab);
  140. static void audit_panic(const char *message)
  141. {
  142. switch (audit_failure)
  143. {
  144. case AUDIT_FAIL_SILENT:
  145. break;
  146. case AUDIT_FAIL_PRINTK:
  147. printk(KERN_ERR "audit: %s\n", message);
  148. break;
  149. case AUDIT_FAIL_PANIC:
  150. panic("audit: %s\n", message);
  151. break;
  152. }
  153. }
  154. static inline int audit_rate_check(void)
  155. {
  156. static unsigned long last_check = 0;
  157. static int messages = 0;
  158. static DEFINE_SPINLOCK(lock);
  159. unsigned long flags;
  160. unsigned long now;
  161. unsigned long elapsed;
  162. int retval = 0;
  163. if (!audit_rate_limit) return 1;
  164. spin_lock_irqsave(&lock, flags);
  165. if (++messages < audit_rate_limit) {
  166. retval = 1;
  167. } else {
  168. now = jiffies;
  169. elapsed = now - last_check;
  170. if (elapsed > HZ) {
  171. last_check = now;
  172. messages = 0;
  173. retval = 1;
  174. }
  175. }
  176. spin_unlock_irqrestore(&lock, flags);
  177. return retval;
  178. }
  179. /* Emit at least 1 message per second, even if audit_rate_check is
  180. * throttling. */
  181. void audit_log_lost(const char *message)
  182. {
  183. static unsigned long last_msg = 0;
  184. static DEFINE_SPINLOCK(lock);
  185. unsigned long flags;
  186. unsigned long now;
  187. int print;
  188. atomic_inc(&audit_lost);
  189. print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
  190. if (!print) {
  191. spin_lock_irqsave(&lock, flags);
  192. now = jiffies;
  193. if (now - last_msg > HZ) {
  194. print = 1;
  195. last_msg = now;
  196. }
  197. spin_unlock_irqrestore(&lock, flags);
  198. }
  199. if (print) {
  200. printk(KERN_WARNING
  201. "audit: audit_lost=%d audit_backlog=%d"
  202. " audit_rate_limit=%d audit_backlog_limit=%d\n",
  203. atomic_read(&audit_lost),
  204. atomic_read(&audit_backlog),
  205. audit_rate_limit,
  206. audit_backlog_limit);
  207. audit_panic(message);
  208. }
  209. }
  210. static int audit_set_rate_limit(int limit, uid_t loginuid)
  211. {
  212. int old = audit_rate_limit;
  213. audit_rate_limit = limit;
  214. audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
  215. audit_rate_limit, old, loginuid);
  216. return old;
  217. }
  218. static int audit_set_backlog_limit(int limit, uid_t loginuid)
  219. {
  220. int old = audit_backlog_limit;
  221. audit_backlog_limit = limit;
  222. audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
  223. audit_backlog_limit, old, loginuid);
  224. return old;
  225. }
  226. static int audit_set_enabled(int state, uid_t loginuid)
  227. {
  228. int old = audit_enabled;
  229. if (state != 0 && state != 1)
  230. return -EINVAL;
  231. audit_enabled = state;
  232. audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
  233. audit_enabled, old, loginuid);
  234. return old;
  235. }
  236. static int audit_set_failure(int state, uid_t loginuid)
  237. {
  238. int old = audit_failure;
  239. if (state != AUDIT_FAIL_SILENT
  240. && state != AUDIT_FAIL_PRINTK
  241. && state != AUDIT_FAIL_PANIC)
  242. return -EINVAL;
  243. audit_failure = state;
  244. audit_log(NULL, "audit_failure=%d old=%d by auid %u",
  245. audit_failure, old, loginuid);
  246. return old;
  247. }
  248. #ifdef CONFIG_NET
  249. void audit_send_reply(int pid, int seq, int type, int done, int multi,
  250. void *payload, int size)
  251. {
  252. struct sk_buff *skb;
  253. struct nlmsghdr *nlh;
  254. int len = NLMSG_SPACE(size);
  255. void *data;
  256. int flags = multi ? NLM_F_MULTI : 0;
  257. int t = done ? NLMSG_DONE : type;
  258. skb = alloc_skb(len, GFP_KERNEL);
  259. if (!skb)
  260. goto nlmsg_failure;
  261. nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
  262. nlh->nlmsg_flags = flags;
  263. data = NLMSG_DATA(nlh);
  264. memcpy(data, payload, size);
  265. netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
  266. return;
  267. nlmsg_failure: /* Used by NLMSG_PUT */
  268. if (skb)
  269. kfree_skb(skb);
  270. }
  271. /*
  272. * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
  273. * control messages.
  274. */
  275. static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
  276. {
  277. int err = 0;
  278. switch (msg_type) {
  279. case AUDIT_GET:
  280. case AUDIT_LIST:
  281. case AUDIT_SET:
  282. case AUDIT_ADD:
  283. case AUDIT_DEL:
  284. case AUDIT_SIGNAL_INFO:
  285. if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
  286. err = -EPERM;
  287. break;
  288. case AUDIT_USER:
  289. if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
  290. err = -EPERM;
  291. break;
  292. default: /* bad msg */
  293. err = -EINVAL;
  294. }
  295. return err;
  296. }
  297. static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  298. {
  299. u32 uid, pid, seq;
  300. void *data;
  301. struct audit_status *status_get, status_set;
  302. int err;
  303. struct audit_buffer *ab;
  304. u16 msg_type = nlh->nlmsg_type;
  305. uid_t loginuid; /* loginuid of sender */
  306. struct audit_sig_info sig_data;
  307. err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
  308. if (err)
  309. return err;
  310. pid = NETLINK_CREDS(skb)->pid;
  311. uid = NETLINK_CREDS(skb)->uid;
  312. loginuid = NETLINK_CB(skb).loginuid;
  313. seq = nlh->nlmsg_seq;
  314. data = NLMSG_DATA(nlh);
  315. switch (msg_type) {
  316. case AUDIT_GET:
  317. status_set.enabled = audit_enabled;
  318. status_set.failure = audit_failure;
  319. status_set.pid = audit_pid;
  320. status_set.rate_limit = audit_rate_limit;
  321. status_set.backlog_limit = audit_backlog_limit;
  322. status_set.lost = atomic_read(&audit_lost);
  323. status_set.backlog = atomic_read(&audit_backlog);
  324. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
  325. &status_set, sizeof(status_set));
  326. break;
  327. case AUDIT_SET:
  328. if (nlh->nlmsg_len < sizeof(struct audit_status))
  329. return -EINVAL;
  330. status_get = (struct audit_status *)data;
  331. if (status_get->mask & AUDIT_STATUS_ENABLED) {
  332. err = audit_set_enabled(status_get->enabled, loginuid);
  333. if (err < 0) return err;
  334. }
  335. if (status_get->mask & AUDIT_STATUS_FAILURE) {
  336. err = audit_set_failure(status_get->failure, loginuid);
  337. if (err < 0) return err;
  338. }
  339. if (status_get->mask & AUDIT_STATUS_PID) {
  340. int old = audit_pid;
  341. audit_pid = status_get->pid;
  342. audit_log(NULL, "audit_pid=%d old=%d by auid %u",
  343. audit_pid, old, loginuid);
  344. }
  345. if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
  346. audit_set_rate_limit(status_get->rate_limit, loginuid);
  347. if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
  348. audit_set_backlog_limit(status_get->backlog_limit,
  349. loginuid);
  350. break;
  351. case AUDIT_USER:
  352. ab = audit_log_start(NULL);
  353. if (!ab)
  354. break; /* audit_panic has been called */
  355. audit_log_format(ab,
  356. "user pid=%d uid=%d length=%d loginuid=%u"
  357. " msg='%.1024s'",
  358. pid, uid,
  359. (int)(nlh->nlmsg_len
  360. - ((char *)data - (char *)nlh)),
  361. loginuid, (char *)data);
  362. ab->type = AUDIT_USER;
  363. ab->pid = pid;
  364. audit_log_end(ab);
  365. break;
  366. case AUDIT_ADD:
  367. case AUDIT_DEL:
  368. if (nlh->nlmsg_len < sizeof(struct audit_rule))
  369. return -EINVAL;
  370. /* fallthrough */
  371. case AUDIT_LIST:
  372. #ifdef CONFIG_AUDITSYSCALL
  373. err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
  374. uid, seq, data, loginuid);
  375. #else
  376. err = -EOPNOTSUPP;
  377. #endif
  378. break;
  379. case AUDIT_SIGNAL_INFO:
  380. sig_data.uid = audit_sig_uid;
  381. sig_data.pid = audit_sig_pid;
  382. audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
  383. 0, 0, &sig_data, sizeof(sig_data));
  384. break;
  385. default:
  386. err = -EINVAL;
  387. break;
  388. }
  389. return err < 0 ? err : 0;
  390. }
  391. /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
  392. * processed by audit_receive_msg. Malformed skbs with wrong length are
  393. * discarded silently. */
  394. static void audit_receive_skb(struct sk_buff *skb)
  395. {
  396. int err;
  397. struct nlmsghdr *nlh;
  398. u32 rlen;
  399. while (skb->len >= NLMSG_SPACE(0)) {
  400. nlh = (struct nlmsghdr *)skb->data;
  401. if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
  402. return;
  403. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  404. if (rlen > skb->len)
  405. rlen = skb->len;
  406. if ((err = audit_receive_msg(skb, nlh))) {
  407. netlink_ack(skb, nlh, err);
  408. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  409. netlink_ack(skb, nlh, 0);
  410. skb_pull(skb, rlen);
  411. }
  412. }
  413. /* Receive messages from netlink socket. */
  414. static void audit_receive(struct sock *sk, int length)
  415. {
  416. struct sk_buff *skb;
  417. unsigned int qlen;
  418. down(&audit_netlink_sem);
  419. for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
  420. skb = skb_dequeue(&sk->sk_receive_queue);
  421. audit_receive_skb(skb);
  422. kfree_skb(skb);
  423. }
  424. up(&audit_netlink_sem);
  425. }
  426. /* Move data from tmp buffer into an skb. This is an extra copy, and
  427. * that is unfortunate. However, the copy will only occur when a record
  428. * is being written to user space, which is already a high-overhead
  429. * operation. (Elimination of the copy is possible, for example, by
  430. * writing directly into a pre-allocated skb, at the cost of wasting
  431. * memory. */
  432. static void audit_log_move(struct audit_buffer *ab)
  433. {
  434. struct sk_buff *skb;
  435. char *start;
  436. int extra = ab->nlh ? 0 : NLMSG_SPACE(0);
  437. /* possible resubmission */
  438. if (ab->len == 0)
  439. return;
  440. skb = skb_peek_tail(&ab->sklist);
  441. if (!skb || skb_tailroom(skb) <= ab->len + extra) {
  442. skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);
  443. if (!skb) {
  444. ab->len = 0; /* Lose information in ab->tmp */
  445. audit_log_lost("out of memory in audit_log_move");
  446. return;
  447. }
  448. __skb_queue_tail(&ab->sklist, skb);
  449. if (!ab->nlh)
  450. ab->nlh = (struct nlmsghdr *)skb_put(skb,
  451. NLMSG_SPACE(0));
  452. }
  453. start = skb_put(skb, ab->len);
  454. memcpy(start, ab->tmp, ab->len);
  455. ab->len = 0;
  456. }
  457. /* Iterate over the skbuff in the audit_buffer, sending their contents
  458. * to user space. */
  459. static inline int audit_log_drain(struct audit_buffer *ab)
  460. {
  461. struct sk_buff *skb;
  462. while ((skb = skb_dequeue(&ab->sklist))) {
  463. int retval = 0;
  464. if (audit_pid) {
  465. if (ab->nlh) {
  466. ab->nlh->nlmsg_len = ab->total;
  467. ab->nlh->nlmsg_type = ab->type;
  468. ab->nlh->nlmsg_flags = 0;
  469. ab->nlh->nlmsg_seq = 0;
  470. ab->nlh->nlmsg_pid = ab->pid;
  471. }
  472. skb_get(skb); /* because netlink_* frees */
  473. retval = netlink_unicast(audit_sock, skb, audit_pid,
  474. MSG_DONTWAIT);
  475. }
  476. if (retval == -EAGAIN &&
  477. (atomic_read(&audit_backlog)) < audit_backlog_limit) {
  478. skb_queue_head(&ab->sklist, skb);
  479. audit_log_end_irq(ab);
  480. return 1;
  481. }
  482. if (retval < 0) {
  483. if (retval == -ECONNREFUSED) {
  484. printk(KERN_ERR
  485. "audit: *NO* daemon at audit_pid=%d\n",
  486. audit_pid);
  487. audit_pid = 0;
  488. } else
  489. audit_log_lost("netlink socket too busy");
  490. }
  491. if (!audit_pid) { /* No daemon */
  492. int offset = ab->nlh ? NLMSG_SPACE(0) : 0;
  493. int len = skb->len - offset;
  494. skb->data[offset + len] = '\0';
  495. printk(KERN_ERR "%s\n", skb->data + offset);
  496. }
  497. kfree_skb(skb);
  498. ab->nlh = NULL;
  499. }
  500. return 0;
  501. }
  502. /* Initialize audit support at boot time. */
  503. static int __init audit_init(void)
  504. {
  505. printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
  506. audit_default ? "enabled" : "disabled");
  507. audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
  508. if (!audit_sock)
  509. audit_panic("cannot initialize netlink socket");
  510. audit_initialized = 1;
  511. audit_enabled = audit_default;
  512. audit_log(NULL, "initialized");
  513. return 0;
  514. }
  515. #else
  516. /* Without CONFIG_NET, we have no skbuffs. For now, print what we have
  517. * in the buffer. */
  518. static void audit_log_move(struct audit_buffer *ab)
  519. {
  520. printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
  521. ab->len = 0;
  522. }
  523. static inline int audit_log_drain(struct audit_buffer *ab)
  524. {
  525. return 0;
  526. }
  527. /* Initialize audit support at boot time. */
  528. int __init audit_init(void)
  529. {
  530. printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
  531. audit_sock = NULL;
  532. audit_pid = 0;
  533. audit_initialized = 1;
  534. audit_enabled = audit_default;
  535. audit_log(NULL, "initialized");
  536. return 0;
  537. }
  538. #endif
  539. __initcall(audit_init);
  540. /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
  541. static int __init audit_enable(char *str)
  542. {
  543. audit_default = !!simple_strtol(str, NULL, 0);
  544. printk(KERN_INFO "audit: %s%s\n",
  545. audit_default ? "enabled" : "disabled",
  546. audit_initialized ? "" : " (after initialization)");
  547. if (audit_initialized)
  548. audit_enabled = audit_default;
  549. return 0;
  550. }
  551. __setup("audit=", audit_enable);
  552. static void audit_buffer_free(struct audit_buffer *ab)
  553. {
  554. unsigned long flags;
  555. atomic_dec(&audit_backlog);
  556. spin_lock_irqsave(&audit_freelist_lock, flags);
  557. if (++audit_freelist_count > AUDIT_MAXFREE)
  558. kfree(ab);
  559. else
  560. list_add(&ab->list, &audit_freelist);
  561. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  562. }
  563. static struct audit_buffer * audit_buffer_alloc(int gfp_mask)
  564. {
  565. unsigned long flags;
  566. struct audit_buffer *ab = NULL;
  567. spin_lock_irqsave(&audit_freelist_lock, flags);
  568. if (!list_empty(&audit_freelist)) {
  569. ab = list_entry(audit_freelist.next,
  570. struct audit_buffer, list);
  571. list_del(&ab->list);
  572. --audit_freelist_count;
  573. }
  574. spin_unlock_irqrestore(&audit_freelist_lock, flags);
  575. if (!ab) {
  576. ab = kmalloc(sizeof(*ab), GFP_ATOMIC);
  577. if (!ab)
  578. goto out;
  579. }
  580. atomic_inc(&audit_backlog);
  581. out:
  582. return ab;
  583. }
  584. /* Obtain an audit buffer. This routine does locking to obtain the
  585. * audit buffer, but then no locking is required for calls to
  586. * audit_log_*format. If the tsk is a task that is currently in a
  587. * syscall, then the syscall is marked as auditable and an audit record
  588. * will be written at syscall exit. If there is no associated task, tsk
  589. * should be NULL. */
  590. struct audit_buffer *audit_log_start(struct audit_context *ctx)
  591. {
  592. struct audit_buffer *ab = NULL;
  593. struct timespec t;
  594. unsigned int serial;
  595. if (!audit_initialized)
  596. return NULL;
  597. if (audit_backlog_limit
  598. && atomic_read(&audit_backlog) > audit_backlog_limit) {
  599. if (audit_rate_check())
  600. printk(KERN_WARNING
  601. "audit: audit_backlog=%d > "
  602. "audit_backlog_limit=%d\n",
  603. atomic_read(&audit_backlog),
  604. audit_backlog_limit);
  605. audit_log_lost("backlog limit exceeded");
  606. return NULL;
  607. }
  608. ab = audit_buffer_alloc(GFP_ATOMIC);
  609. if (!ab) {
  610. audit_log_lost("out of memory in audit_log_start");
  611. return NULL;
  612. }
  613. skb_queue_head_init(&ab->sklist);
  614. ab->ctx = ctx;
  615. ab->len = 0;
  616. ab->nlh = NULL;
  617. ab->total = 0;
  618. ab->type = AUDIT_KERNEL;
  619. ab->pid = 0;
  620. #ifdef CONFIG_AUDITSYSCALL
  621. if (ab->ctx)
  622. audit_get_stamp(ab->ctx, &t, &serial);
  623. else
  624. #endif
  625. {
  626. t = CURRENT_TIME;
  627. serial = 0;
  628. }
  629. audit_log_format(ab, "audit(%lu.%03lu:%u): ",
  630. t.tv_sec, t.tv_nsec/1000000, serial);
  631. return ab;
  632. }
  633. /* Format an audit message into the audit buffer. If there isn't enough
  634. * room in the audit buffer, more room will be allocated and vsnprint
  635. * will be called a second time. Currently, we assume that a printk
  636. * can't format message larger than 1024 bytes, so we don't either. */
  637. static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
  638. va_list args)
  639. {
  640. int len, avail;
  641. if (!ab)
  642. return;
  643. avail = sizeof(ab->tmp) - ab->len;
  644. if (avail <= 0) {
  645. audit_log_move(ab);
  646. avail = sizeof(ab->tmp) - ab->len;
  647. }
  648. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  649. if (len >= avail) {
  650. /* The printk buffer is 1024 bytes long, so if we get
  651. * here and AUDIT_BUFSIZ is at least 1024, then we can
  652. * log everything that printk could have logged. */
  653. audit_log_move(ab);
  654. avail = sizeof(ab->tmp) - ab->len;
  655. len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
  656. }
  657. ab->len += (len < avail) ? len : avail;
  658. ab->total += (len < avail) ? len : avail;
  659. }
  660. /* Format a message into the audit buffer. All the work is done in
  661. * audit_log_vformat. */
  662. void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  663. {
  664. va_list args;
  665. if (!ab)
  666. return;
  667. va_start(args, fmt);
  668. audit_log_vformat(ab, fmt, args);
  669. va_end(args);
  670. }
  671. void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
  672. {
  673. int i;
  674. for (i=0; i<len; i++)
  675. audit_log_format(ab, "%02x", buf[i]);
  676. }
  677. void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
  678. {
  679. const unsigned char *p = string;
  680. while (*p) {
  681. if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
  682. audit_log_hex(ab, string, strlen(string));
  683. return;
  684. }
  685. p++;
  686. }
  687. audit_log_format(ab, "\"%s\"", string);
  688. }
  689. /* This is a helper-function to print the d_path without using a static
  690. * buffer or allocating another buffer in addition to the one in
  691. * audit_buffer. */
  692. void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
  693. struct dentry *dentry, struct vfsmount *vfsmnt)
  694. {
  695. char *p;
  696. int len, avail;
  697. if (prefix) audit_log_format(ab, " %s", prefix);
  698. if (ab->len > 128)
  699. audit_log_move(ab);
  700. avail = sizeof(ab->tmp) - ab->len;
  701. p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
  702. if (IS_ERR(p)) {
  703. /* FIXME: can we save some information here? */
  704. audit_log_format(ab, "<toolong>");
  705. } else {
  706. /* path isn't at start of buffer */
  707. len = (ab->tmp + sizeof(ab->tmp) - 1) - p;
  708. memmove(ab->tmp + ab->len, p, len);
  709. ab->len += len;
  710. ab->total += len;
  711. }
  712. }
  713. /* Remove queued messages from the audit_txlist and send them to userspace. */
  714. static void audit_tasklet_handler(unsigned long arg)
  715. {
  716. LIST_HEAD(list);
  717. struct audit_buffer *ab;
  718. unsigned long flags;
  719. spin_lock_irqsave(&audit_txlist_lock, flags);
  720. list_splice_init(&audit_txlist, &list);
  721. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  722. while (!list_empty(&list)) {
  723. ab = list_entry(list.next, struct audit_buffer, list);
  724. list_del(&ab->list);
  725. audit_log_end_fast(ab);
  726. }
  727. }
  728. static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
  729. /* The netlink_* functions cannot be called inside an irq context, so
  730. * the audit buffer is places on a queue and a tasklet is scheduled to
  731. * remove them from the queue outside the irq context. May be called in
  732. * any context. */
  733. static void audit_log_end_irq(struct audit_buffer *ab)
  734. {
  735. unsigned long flags;
  736. if (!ab)
  737. return;
  738. spin_lock_irqsave(&audit_txlist_lock, flags);
  739. list_add_tail(&ab->list, &audit_txlist);
  740. spin_unlock_irqrestore(&audit_txlist_lock, flags);
  741. tasklet_schedule(&audit_tasklet);
  742. }
  743. /* Send the message in the audit buffer directly to user space. May not
  744. * be called in an irq context. */
  745. static void audit_log_end_fast(struct audit_buffer *ab)
  746. {
  747. BUG_ON(in_irq());
  748. if (!ab)
  749. return;
  750. if (!audit_rate_check()) {
  751. audit_log_lost("rate limit exceeded");
  752. } else {
  753. audit_log_move(ab);
  754. if (audit_log_drain(ab))
  755. return;
  756. }
  757. audit_buffer_free(ab);
  758. }
  759. /* Send or queue the message in the audit buffer, depending on the
  760. * current context. (A convenience function that may be called in any
  761. * context.) */
  762. void audit_log_end(struct audit_buffer *ab)
  763. {
  764. if (in_irq())
  765. audit_log_end_irq(ab);
  766. else
  767. audit_log_end_fast(ab);
  768. }
  769. /* Log an audit record. This is a convenience function that calls
  770. * audit_log_start, audit_log_vformat, and audit_log_end. It may be
  771. * called in any context. */
  772. void audit_log(struct audit_context *ctx, const char *fmt, ...)
  773. {
  774. struct audit_buffer *ab;
  775. va_list args;
  776. ab = audit_log_start(ctx);
  777. if (ab) {
  778. va_start(args, fmt);
  779. audit_log_vformat(ab, fmt, args);
  780. va_end(args);
  781. audit_log_end(ab);
  782. }
  783. }