audit.c 39 KB

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