audit.c 36 KB

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