audit.c 36 KB

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