audit.c 30 KB

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