audit.c 41 KB

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