audit.c 36 KB

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