audit.c 40 KB

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