audit.c 36 KB

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