audit.c 40 KB

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