audit.c 32 KB

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