audit.c 40 KB

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