audit.c 40 KB

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